Skip to main content

Query Builder

Just like any other ORM, ObjectionJS also provides support for query builder. We have added a CustomQueryBuilder which provides some high utility methods.

info

All methods listed below support method chaining with all objectionjs methods

Methods

In addition to what knexjs provides, we have added a few important query methods which you can use.

paginate

Use this method to load paginated data.

const query = User.query();
await query.paginate(1, 15); // loads the page number 1 of size 15

/** Output =>
{
pagination: {
currentPage: 1,
totalPages: 10,
perPage: 15,
total: 150,
},
data: [
... instance of user models
],
}
*/

exists

Use this method to check if any model exists for any condition

const query = User.query().where("name", "vinayak");
await query.exists(); // will return true if any user exists with name of 'vinayak'

chunk

Use this method to load data in small chunks if you have any large amount of data, for example, loading 1K rows at a time in case of a table having 100K rows.

const query = User.query();

const cb = (users: User[]) => {
// perform your action here.
};

await query.chunk(cb, 1000);