How can I get a list of records by PK in a single query using bookshelf.js?
The end query should be equivalent to:
SELECT * FROM `user` WHERE `id` IN (1,3,5,6,7);
This solution works:
AdModel
.where('search_id', 'IN', [1, 2, 3])
.fetchAll();
You can also use the Knex QueryBuilder to get the same result
AdModel
.query(qb => {
qb.whereIn('search_id', [1, 2, 3]);
})
.fetchAll();
List (single) query and fetching relations (withRelated)
BookshelfModel
.where('field_id', 'IN', [1, 2, 3])
.fetchAll({withRelated: ["tableA", "tableB"]});
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