Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

perform a where in query in bookshelf.js

Tags:

bookshelf.js

I want to perform a WHERE - IN query/operation but normal where gives error.

I want this

select * from `calendar_event_rsvp` where `event_id` in ('1', '2', '3')

But below code leads to

select * from `calendar_event_rsvp` where `event_id in` = '1', '2', '3'

Code

CalendarEventRSVP.forge()
                .where({
                    "event_id": event_ids
                })

How do i do this in bookshelf.js

like image 582
aWebDeveloper Avatar asked Mar 21 '16 12:03

aWebDeveloper


People also ask

What is bookshelf in Node JS?

Bookshelf.js is a JavaScript ORM for Node.js, built on the Knex SQL query builder. It supports both promise based and traditional callback interfaces. Bookshelf provides transaction support, eager/nested-eager relation loading, polymorphic associations, and support for one-to-one, one-to-many, and many-to-many relations.

What is bookshelf JS Orm?

Bookshelf.js tutorial shows how to program databases in JavaScript with the Bookshelf.js ORM. Bookshelf.js is built on top of Knex. Bookshelf.js is a JavaScript ORM for Node.js, built on the Knex SQL query builder.

What is bookshelf?

Bookshelf.js is built on top of Knex. Bookshelf.js is a JavaScript ORM for Node.js, built on the Knex SQL query builder. It supports both promise based and traditional callback interfaces.

How do I get the gender of a user in Bookshelf?

Bookshelf.js supports promise-based interfaces, which in this case means the anonymous function passed into then will only be called if your query was successful. The model is the resulting JavaScript object, which you can use to access attributes associated with User. In our case model.get ('gender') returns the gender of our user.


2 Answers

Try to add the operator:

CalendarEventRSVP.forge()
            .where('event_id', 'in', event_ids)

Or use knex's whereIn:

 CalendarEventRSVP.forge()
            .query({whereIn: {event_id: event_ids}})
like image 79
Jcl Avatar answered Jan 03 '23 17:01

Jcl


try query() function on your model.

CalendarEventRSVP.query(function(qb){
    qb.where('event_id' , 'in' , [1,2,3,4]) ;
})
.fetchAll()
.then();
like image 34
Salar Avatar answered Jan 03 '23 17:01

Salar