QueryBuilder

The QueryBuilder class in SchemaJS is designed to help you easily construct complex queries for interacting with tables in your database. It provides methods to build WHERE, AND, and OR conditions, allowing for powerful filtering and querying. The QueryBuilder class allows users to define conditions programmatically, making it easy to dynamically generate queries and control how data is retrieved from the database.

Constructor

new QueryBuilder(dbName?: string, tableName?: string)

If dbName or tableName are not passed, the constructor attempts to use the global context (SJS_CONTEXT) for the current database and table.

Using this class from a custom query or a hook already injects `SJS_CONTEXT`.You do not need to pass `dbName` or `tableName`

where

Adds a WHERE condition to the query. The condition specifies the column key, the filter type, and the value to compare against.

builder.where(key: string, filter_type: string, value: any)

and

Adds an AND condition, grouping multiple conditions under an AND clause. The callback allows the user to add more conditions to the AND clause using a new QueryBuilder instance.

builder.and((b) => {
    b.where("age", "=", 30);
    b.where("country", "=", "USA");
});

or

Adds an OR condition, grouping multiple conditions under an OR clause. The callback allows the user to add more conditions to the OR clause using a new QueryBuilder instance.

builder.or((b) => {
    b.where("status", "=", "inactive");
    b.where("last_login", "<", "2023-01-01");
});

Last updated