NodeQuery(config)

Class for connection management

Parameters

  • config :

Instance members

#constructor(config)

Constructor

constructor(config)

Constructor

Parameters

  • object config :

    connection paramaters

#getQuery

Return an existing query builder instance

getQuery

Return an existing query builder instance

Returns

QueryBuilder :

The Query Builder object

#init(driverType, connObject, [connLib])

Create a query builder object

init(driverType, connObject, [connLib])

Create a query builder object

Parameters

  • String driverType :

    The name of the database type, eg. mysql or pg

  • Object connObject :

    A connection object from the database library you are connecting with

  • [String] connLib :

    The name of the db connection library you are using, eg. mysql or mysql2. Optional if the same as driverType

Returns

QueryBuilder :

The Query Builder object

QueryBuilder(Driver, Adapter)

Main object that builds SQL queries.

Parameters

  • Driver Driver :

    The syntax driver for the database

  • Adapter Adapter :

    The database module adapter for running queries

Instance members

#delete(table, [where], [callback])

Run the generated delete query

delete(table, [where], [callback])

Run the generated delete query

Parameters

  • String table :

    The table to insert into

  • [Object] where :

    Where clause for delete statement

  • [Function] callback :

    Callback for handling response from the database

Returns

void or Promise :

If no callback is passed, a promise is returned

#end

Closes the database connection for the current adapter

end

Closes the database connection for the current adapter

Returns

void
#from(tableName)

Specify the database table to select from

from(tableName)

Specify the database table to select from

Parameters

  • String tableName :

    The table to use for the current query

Returns

QueryBuilder :

The Query Builder object, for chaining

Examples

query.from('tableName');
query.from('tableName t'); // Select the table with an alias
#get([table], [limit], [offset], [callback])

Get the results of the compiled query

get([table], [limit], [offset], [callback])

Get the results of the compiled query

Parameters

  • [String] table :

    The table to select from

  • [Number] limit :

    A limit for the query

  • [Number] offset :

    An offset for the query

  • [Function] callback :

    A callback for receiving the result

Returns

void or Promise :

If no callback is passed, a promise is returned

Examples

query.get('table_name').then(promiseCallback); // Get all the rows in the table
query.get('table_name', 5, callback); // Get 5 rows from the table
query.get(callback); // Get the results of a query generated with other methods
#getCompiledDelete(table, [reset])

Return generated delete query SQL

getCompiledDelete(table, [reset])

Return generated delete query SQL

Parameters

  • String table :

    the name of the table to delete from

  • [Boolean] reset (default true) :

    Whether to reset the query builder so another query can be built

Returns

String :

The compiled sql statement

#getCompiledInsert(table, [reset])

Return generated insert query SQL

getCompiledInsert(table, [reset])

Return generated insert query SQL

Parameters

  • String table :

    the name of the table to insert into

  • [Boolean] reset (default true) :

    Whether to reset the query builder so another query can be built

Returns

String :

The compiled sql statement

#getCompiledSelect([table], [reset])

Return generated select query SQL

getCompiledSelect([table], [reset])

Return generated select query SQL

Parameters

  • [String] table :

    the name of the table to retrieve from

  • [Boolean] reset (default true) :

    Whether to reset the query builder so another query can be built

Returns

String :

The compiled sql statement

#getCompiledUpdate(table, [reset])

Return generated update query SQL

getCompiledUpdate(table, [reset])

Return generated update query SQL

Parameters

  • String table :

    the name of the table to update

  • [Boolean] reset (default true) :

    Whether to reset the query builder so another query can be built

Returns

String :

The compiled sql statement

#groupBy(field)

Group the results by the selected field(s)

groupBy(field)

Group the results by the selected field(s)

Parameters

  • String or Array field :

    The name of the field to group by

Returns

QueryBuilder :

The Query Builder object, for chaining

#groupEnd

Ends a logical grouping started with one of the groupStart methods

groupEnd

Ends a logical grouping started with one of the groupStart methods

Returns

QueryBuilder :

The Query Builder object, for chaining

#groupStart

Adds an open paren to the current query for logical grouping

groupStart

Adds an open paren to the current query for logical grouping

Returns

QueryBuilder :

The Query Builder object, for chaining

#having(key, [val])

Add a 'having' clause

having(key, [val])

Add a 'having' clause

Parameters

  • String or Object key :

    The name of the field and the comparision operator, or an object

  • [String or Number] val :

    The value to compare if the value of key is a string

Returns

QueryBuilder :

The Query Builder object, for chaining

#insert(table, [data], [callback])

Run the generated insert query

insert(table, [data], [callback])

Run the generated insert query

Parameters

  • String table :

    The table to insert into

  • [Object] data :

    Data to insert, if not already added with the 'set' method

  • [Function] callback :

    Callback for handling response from the database

Returns

void or Promise :

If no callback is passed, a promise is returned

#insertBatch(table, data, [callback])

Insert multiple sets of rows at a time

insertBatch(table, data, [callback])

Insert multiple sets of rows at a time

Parameters

  • String table :

    The table to insert into

  • Array data :

    The array of objects containing data rows to insert

  • [Function] callback :

    Callback for handling database response

Returns

void or Promise :

If no callback is passed, a promise is returned

Examples

query.insertBatch('foo',[{id:1,val:'bar'},{id:2,val:'baz'}], callbackFunction);
query.insertBatch('foo',[{id:1,val:'bar'},{id:2,val:'baz'}])
            .then(promiseCallback);
#join(table, cond, [type])

Add a join clause to the query

join(table, cond, [type])

Add a join clause to the query

Parameters

  • String table :

    The table you are joining

  • String cond :

    The join condition.

  • [String] type (default 'inner') :

    The type of join, which defaults to inner

Returns

QueryBuilder :

The Query Builder object, for chaining

#like(field, val, [pos])

Add a 'like/ and like' clause to the query

like(field, val, [pos])

Add a 'like/ and like' clause to the query

Parameters

  • String field :

    The name of the field to compare to

  • String val :

    The value to compare to

  • [String] pos (default both) :

    The placement of the wildcard character(s): before, after, or both

Returns

QueryBuilder :

The Query Builder object, for chaining

#limit(limit, [offset])

Put a limit on the query

limit(limit, [offset])

Put a limit on the query

Parameters

  • Number limit :

    The maximum number of rows to fetch

  • [Number] offset :

    The row number to start from

Returns

QueryBuilder :

The Query Builder object, for chaining

#notLike(field, val, [pos])

Add a 'not like/ and not like' clause to the query

notLike(field, val, [pos])

Add a 'not like/ and not like' clause to the query

Parameters

  • String field :

    The name of the field to compare to

  • String val :

    The value to compare to

  • [String] pos (default both) :

    The placement of the wildcard character(s): before, after, or both

Returns

QueryBuilder :

The Query Builder object, for chaining

#orGroupStart

Adds an open paren to the current query for logical grouping, prefixed with 'OR'

orGroupStart

Adds an open paren to the current query for logical grouping, prefixed with 'OR'

Returns

QueryBuilder :

The Query Builder object, for chaining

#orHaving(key, [val])

Add an 'or having' clause

orHaving(key, [val])

Add an 'or having' clause

Parameters

  • String or Object key :

    The name of the field and the comparision operator, or an object

  • [String or Number] val :

    The value to compare if the value of key is a string

Returns

QueryBuilder :

The Query Builder object, for chaining

#orLike(field, val, [pos])

Add an 'or like' clause to the query

orLike(field, val, [pos])

Add an 'or like' clause to the query

Parameters

  • String field :

    The name of the field to compare to

  • String val :

    The value to compare to

  • [String] pos (default both) :

    The placement of the wildcard character(s): before, after, or both

Returns

QueryBuilder :

The Query Builder object, for chaining

#orNotGroupStart

Adds an open paren to the current query for logical grouping, prefixed with 'OR NOT'

orNotGroupStart

Adds an open paren to the current query for logical grouping, prefixed with 'OR NOT'

Returns

QueryBuilder :

The Query Builder object, for chaining

#orNotLike(field, val, [pos])

Add an 'or not like' clause to the query

orNotLike(field, val, [pos])

Add an 'or not like' clause to the query

Parameters

  • String field :

    The name of the field to compare to

  • String val :

    The value to compare to

  • [String] pos (default both) :

    The placement of the wildcard character(s): before, after, or both

Returns

QueryBuilder :

The Query Builder object, for chaining

#orWhere(key, [val])

Set a 'or where' clause

orWhere(key, [val])

Set a 'or where' clause

Parameters

  • String or Object key :

    The name of the field and the comparision operator, or an object

  • [String or Number] val :

    The value to compare if the value of key is a string

Returns

QueryBuilder :

The Query Builder object, for chaining

#orWhereIn(key, values)

Set a 'or where in' clause

orWhereIn(key, values)

Set a 'or where in' clause

Parameters

  • String key :

    the field to search

  • Array values :

    the array of items to search in

Returns

QueryBuilder :

The Query Builder object, for chaining

#orWhereIsNotNull(field)

Field is not null prefixed with 'OR'

orWhereIsNotNull(field)

Field is not null prefixed with 'OR'

Parameters

  • String field :

    The name of the field

Returns

QueryBuilder :

The Query Builder object, for chaining

#orWhereIsNull(field)

Field is null prefixed with 'OR'

orWhereIsNull(field)

Field is null prefixed with 'OR'

Parameters

  • String field :

    The name of the field

Returns

QueryBuilder :

The Query Builder object, for chaining

#orWhereNotIn(key, values)

Set a 'or where not in' clause

orWhereNotIn(key, values)

Set a 'or where not in' clause

Parameters

  • String key :

    the field to search

  • Array values :

    the array of items to search in

Returns

QueryBuilder :

The Query Builder object, for chaining

#orderBy(field, [type])

Order the results by the selected field(s)

orderBy(field, [type])

Order the results by the selected field(s)

Parameters

  • String field :

    The field(s) to order by

  • [String] type (default 'ASC') :

    The order direction, ASC or DESC

Returns

QueryBuilder :

The Query Builder object, for chaining

#query(sql, [params], [callback])

Manually make an sql query

query(sql, [params], [callback])

Manually make an sql query

Parameters

  • string sql :

    The sql to execute

  • [array] params :

    The query parameters

  • [function] callback :

    Optional callback

Returns

void or Promise :

Returns a promise if no callback is supplied

#resetQuery

Reset the object state for a new query

resetQuery

Reset the object state for a new query

Returns

void
#select(fields)

Specify rows to select in the query

select(fields)

Specify rows to select in the query

Parameters

  • String or Array fields :

    The fields to select from the current table

Returns

QueryBuilder :

The Query Builder object, for chaining

Examples

query.select('foo, bar'); // Select multiple fields with a string
query.select(['foo', 'bar']); // Select multiple fileds with an array
#set(key, [val])

Set values for insertion or updating

set(key, [val])

Set values for insertion or updating

Parameters

  • String or Object key :

    The key or object to use

  • [String] val :

    The value if using a scalar key

Returns

QueryBuilder :

The Query Builder object, for chaining

Examples

query.set('foo', 'bar'); // Set a key, value pair
query.set({foo:'bar'}); // Set with an object
#update(table, [data], [callback])

Run the generated update query

update(table, [data], [callback])

Run the generated update query

Parameters

  • String table :

    The table to insert into

  • [Object] data :

    Data to insert, if not already added with the 'set' method

  • [Function] callback :

    Callback for handling response from the database

Returns

void or Promise :

If no callback is passed, a promise is returned

#where(key, [val])

Set a 'where' clause

where(key, [val])

Set a 'where' clause

Parameters

  • String or Object key :

    The name of the field and the comparision operator, or an object

  • [String or Number] val :

    The value to compare if the value of key is a string

Returns

QueryBuilder :

The Query Builder object, for chaining

#whereIn(key, values)

Set a 'where in' clause

whereIn(key, values)

Set a 'where in' clause

Parameters

  • String key :

    the field to search

  • Array values :

    the array of items to search in

Returns

QueryBuilder :

The Query Builder object, for chaining

#whereIsNotNull(field)

Specify that a field IS NOT NULL

whereIsNotNull(field)

Specify that a field IS NOT NULL

Parameters

  • String field :

    The name so the field that is not to be null

Returns

QueryBuilder :

The Query Builder object, for chaining

#whereIsNull(field)

Select a field that is Null

whereIsNull(field)

Select a field that is Null

Parameters

  • String field :

    The name of the field that has a NULL value

Returns

QueryBuilder :

The Query Builder object, for chaining

#whereNotIn(key, values)

Set a 'where not in' clause

whereNotIn(key, values)

Set a 'where not in' clause

Parameters

  • String key :

    the field to search

  • Array values :

    the array of items to search in

Returns

QueryBuilder :

The Query Builder object, for chaining