SelectQueryBuilder

Represents a query builder for constructing SELECT queries.

Constructor

new SelectQueryBuilder()

Represents a SelectQueryBuilder.

Extends

  • QueryOptions

Methods

addJoins(query) → {string}

Adds joins to the query.

Parameters:
NameTypeDescription
querystring

The query to add joins to.

Returns:

The updated query with joins added.

Type: 
string

addWhereClause(query) → {string}

Adds a WHERE clause to the query based on the specified conditions.

Parameters:
NameTypeDescription
querystring

The original query string.

Returns:

The modified query string with the WHERE clause added.

Type: 
string

buildAggregateQuery() → {string}

Builds and returns the aggregate query based on the specified aggregates, table, conditions, group by, order by, limit, and offset.

Returns:

The aggregate query.

Type: 
string

buildQuery() → {Object}

Builds the query based on the specified table, aggregates, and values.

Throws:
  • If no table is set.

    Type
    Error
  • If an error occurs while building the query.

    Type
    Error
Returns:

An object containing the built query and the associated values.

Type: 
Object
Example
const queryBuilder = new SelectQueryBuilder();
queryBuilder.setTable('users')
     .setFields('id, name, email')
     .addCondition({ field: 'id', operator: '=', value: 1)
     .addCondition({ conjunction: 'OR', field: 'name', operator: 'LIKE', value: '%John%' });

const { query, values } = queryBuilder.buildQuery();
console.log(query); // SELECT id, name, email FROM users WHERE id = $1 OR name LIKE $2
console.log(values); // [1, '%John%']

buildSelectQuery() → {string}

Builds and returns the SELECT query based on the current state of the SelectQueryBuilder.

Returns:

The generated SELECT query.

Type: 
string