BookshelfJS has the following example for using 'query':
model
.query({where: {other_id: '5'}, orWhere: {key: 'value'}})
.fetch()
.then(function(model) {
...
});
Is it okay to do the following:
var whereObj = {
'key1':'value1',
'key2':'value2'
};
model
.query({where: whereObj, orWhere: {key: 'value'}})
.fetch()
.then(function(model) {
...
});
For more complex queries, you can use this:
.query(function(qb) {
qb.select('*');
qb.where(function () {
this.where('attr1', 1);
this.where('attr2','in' , [1,2,3]);
});
qb.orWhere(function () {
this.where('attr1', 2);
this.where('attr2','in' , [4,5,6]);
});
})
Check out the bookshelf-eloquent extension which exposes many of the Knex.js functions directly on the bookshelf model. Your code would be simplified to something like this:
let result = await Model.where({other_id: 5}).orWhere('key', 'value').fetch();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With