# ci-node-query 5.0.0 | Documentation

### ci-node-query

`5.0.0`
Need help reading this?

### NodeQuery

Class for connection management

new NodeQuery(config: object)
Parameters
config `(object)` connection parameters
Instance Members
▸ getQuery()

Return an existing query builder instance

getQuery(): QueryBuilder
Returns
`QueryBuilder`: The Query Builder object

### QueryBuilder

Main object that builds SQL queries.

new QueryBuilder(Driver: Driver, Adapter: Adapter)

Extends QueryBuilderBase

Parameters
Driver `(Driver)` The syntax driver for the database
Adapter `(Adapter)` The database module adapter for running queries
Instance Members
▸ queryFile(file, separator)

Run a set of queries from a file

queryFile(file: string, separator: string): Promise
Parameters
file `(string)` The path to the sql file
separator `(string = `';'`)` The character separating each query
Returns
`Promise`: The result of all the queries
▸ query(sql, params?)

Run an arbitrary sql query. Run as a prepared statement.

query(sql: string, params: Array?): Promise
Parameters
sql `(string)` The sql to execute
params `(Array?)` The query parameters
Returns
`Promise`: Promise with result of query
▸ resetQuery()

Reset the object state for a new query

resetQuery(): void
Returns
`void`:
▸ truncate(table)

Empties the selected database table

truncate(table: string): (void | Promise)
Parameters
table `(string)` the name of the table to truncate
Returns
`(void | Promise)`: Returns a promise if no callback is supplied
▸ end()

Closes the database connection for the current adapter

end(): void
Returns
`void`:
▸ select(fields)

Specify rows to select in the query

select(fields: (String | Array)): QueryBuilder
Parameters
fields `((String | Array))` The fields to select from the current table
Returns
`QueryBuilder`: The Query Builder object, for chaining
Example

    query.select('foo, bar'); // Select multiple fields with a string

    query.select(['foo', 'bar']); // Select multiple fileds with an array

▸ from(tableName)

Specify the database table to select from

from(tableName: String): QueryBuilder
Parameters
tableName `(String)` The table to use for the current query
Returns
`QueryBuilder`: The Query Builder object, for chaining
Example

    query.from('tableName');

    query.from('tableName t'); // Select the table with an alias

▸ like(field, val, pos)

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

like(field: String, val: String, pos: String): QueryBuilder
Parameters
field `(String)` The name of the field to compare to
val `(String)` The value to compare to
pos `(String = `both`)` The placement of the wildcard character(s): before, after, or both
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: String, val: String, pos: String): QueryBuilder
Parameters
field `(String)` The name of the field to compare to
val `(String)` The value to compare to
pos `(String = `both`)` The placement of the wildcard character(s): before, after, or both
Returns
`QueryBuilder`: The Query Builder object, for chaining
▸ orLike(field, val, pos)

Add an 'or like' clause to the query

orLike(field: String, val: String, pos: String): QueryBuilder
Parameters
field `(String)` The name of the field to compare to
val `(String)` The value to compare to
pos `(String = `both`)` The placement of the wildcard character(s): before, after, or both
Returns
`QueryBuilder`: The Query Builder object, for chaining
▸ orNotLike(field, val, pos)

Add an 'or not like' clause to the query

orNotLike(field: String, val: String, pos: String): QueryBuilder
Parameters
field `(String)` The name of the field to compare to
val `(String)` The value to compare to
pos `(String = `both`)` The placement of the wildcard character(s): before, after, or both
Returns
`QueryBuilder`: The Query Builder object, for chaining
▸ having(key, val = null)

Add a 'having' clause

having(key: (String | Object), val: (String | Number)?): QueryBuilder
Parameters
key `((String | Object))` The name of the field and the comparision operator, or an object
val `((String | Number)? = `null`)` The value to compare if the value of key is a string
Returns
`QueryBuilder`: The Query Builder object, for chaining
▸ orHaving(key, val = null)

Add an 'or having' clause

orHaving(key: (String | Object), val: (String | Number)?): QueryBuilder
Parameters
key `((String | Object))` The name of the field and the comparision operator, or an object
val `((String | Number)? = `null`)` The value to compare if the value of key is a string
Returns
`QueryBuilder`: The Query Builder object, for chaining
▸ where(key, val?)

Set a 'where' clause

where(key: (String | Object), val: (String | Number)?): QueryBuilder
Parameters
key `((String | Object))` The name of the field and the comparision operator, or an object
val `((String | Number)?)` The value to compare if the value of key is a string
Returns
`QueryBuilder`: The Query Builder object, for chaining
▸ orWhere(key, val?)

Set a 'or where' clause

orWhere(key: (String | Object), val: (String | Number)?): QueryBuilder
Parameters
key `((String | Object))` The name of the field and the comparision operator, or an object
val `((String | Number)?)` The value to compare if the value of key is a string
Returns
`QueryBuilder`: The Query Builder object, for chaining
▸ whereIsNull(field)

Select a field that is Null

whereIsNull(field: String): QueryBuilder
Parameters
field `(String)` The name of the field that has a NULL value
Returns
`QueryBuilder`: The Query Builder object, for chaining
▸ whereIsNotNull(field)

Specify that a field IS NOT NULL

whereIsNotNull(field: String): QueryBuilder
Parameters
field `(String)` The name so the field that is not to be null
Returns
`QueryBuilder`: The Query Builder object, for chaining
▸ orWhereIsNull(field)

Field is null prefixed with 'OR'

orWhereIsNull(field: String): QueryBuilder
Parameters
field `(String)` The name of the field
Returns
`QueryBuilder`: The Query Builder object, for chaining
▸ orWhereIsNotNull(field)

Field is not null prefixed with 'OR'

orWhereIsNotNull(field: String): QueryBuilder
Parameters
field `(String)` The name of the field
Returns
`QueryBuilder`: The Query Builder object, for chaining
▸ whereIn(key, values)

Set a 'where in' clause

whereIn(key: String, values: Array): QueryBuilder
Parameters
key `(String)` the field to search
values `(Array)` the array of items to search in
Returns
`QueryBuilder`: The Query Builder object, for chaining
▸ orWhereIn(key, values)

Set a 'or where in' clause

orWhereIn(key: String, values: Array): QueryBuilder
Parameters
key `(String)` the field to search
values `(Array)` the array of items to search in
Returns
`QueryBuilder`: The Query Builder object, for chaining
▸ whereNotIn(key, values)

Set a 'where not in' clause

whereNotIn(key: String, values: Array): QueryBuilder
Parameters
key `(String)` the field to search
values `(Array)` the array of items to search in
Returns
`QueryBuilder`: The Query Builder object, for chaining
▸ orWhereNotIn(key, values)

Set a 'or where not in' clause

orWhereNotIn(key: String, values: Array): QueryBuilder
Parameters
key `(String)` the field to search
values `(Array)` the array of items to search in
Returns
`QueryBuilder`: The Query Builder object, for chaining
▸ set(key, val?)

Set values for insertion or updating

set(key: (String | Object), val: String?): QueryBuilder
Parameters
key `((String | Object))` The key or object to use
val `(String?)` The value if using a scalar key
Returns
`QueryBuilder`: The Query Builder object, for chaining
Example

    query.set('foo', 'bar'); // Set a key, value pair

    query.set({foo:'bar'}); // Set with an object

▸ join(table, cond, type)

Add a join clause to the query

join(table: String, cond: String, type: String): QueryBuilder
Parameters
table `(String)` The table you are joining
cond `(String)` The join condition.
type `(String = `'inner'`)` The type of join, which defaults to inner
Returns
`QueryBuilder`: The Query Builder object, for chaining
▸ groupBy(field)

Group the results by the selected field(s)

groupBy(field: (String | Array)): QueryBuilder
Parameters
field `((String | Array))` The name of the field to group by
Returns
`QueryBuilder`: The Query Builder object, for chaining
▸ orderBy(field, type)

Order the results by the selected field(s)

orderBy(field: String, type: String): QueryBuilder
Parameters
field `(String)` The field(s) to order by
type `(String = `'ASC'`)` The order direction, ASC or DESC
Returns
`QueryBuilder`: The Query Builder object, for chaining
▸ limit(limit, offset?)

Put a limit on the query

limit(limit: Number, offset: Number?): QueryBuilder
Parameters
limit `(Number)` The maximum number of rows to fetch
offset `(Number?)` The row number to start from
Returns
`QueryBuilder`: The Query Builder object, for chaining
▸ groupStart()

Adds an open paren to the current query for logical grouping

groupStart(): QueryBuilder
Returns
`QueryBuilder`: The Query Builder object, for chaining
▸ orGroupStart()

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

orGroupStart(): QueryBuilder
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(): QueryBuilder
Returns
`QueryBuilder`: The Query Builder object, for chaining
▸ groupEnd()

Ends a logical grouping started with one of the groupStart methods

groupEnd(): QueryBuilder
Returns
`QueryBuilder`: The Query Builder object, for chaining
▸ get(table?, limit?, offset?)

Get the results of the compiled query

get(table: String?, limit: Number?, offset: Number?): Promise<Result>
Parameters
table `(String?)` The table to select from
limit `(Number?)` A limit for the query
offset `(Number?)` An offset for the query
Returns
`Promise<Result>`: Promise containing the result of the query
Example

    query.get('table_name').then(promiseCallback); // Get all the rows in the table

    query.get('table_name', 5); // Get 5 rows from the table

    query.get(); // Get the results of a query generated with other methods

▸ insert(table, data?)

Run the generated insert query

insert(table: String, data: Object?): Promise<Result>
Parameters
table `(String)` The table to insert into
data `(Object?)` Data to insert, if not already added with the 'set' method
Returns
`Promise<Result>`: Promise containing the result of the query
▸ insertBatch(table, data)

Insert multiple sets of rows at a time

insertBatch(table: String, data: Array): Promise<Result>
Parameters
table `(String)` The table to insert into
data `(Array)` The array of objects containing data rows to insert
Returns
`Promise<Result>`: Promise containing the result of the query
Example

    query.insertBatch('foo',[{id:1,val:'bar'},{id:2,val:'baz'}])
    .then(promiseCallback);

▸ update(table, data?)

Run the generated update query

update(table: String, data: Object?): Promise<Result>
Parameters
table `(String)` The table to insert into
data `(Object?)` Data to insert, if not already added with the 'set' method
Returns
`Promise<Result>`: Promise containing the result of the query
▸ updateBatch(table, data, updateKey)

Creates a batch update sql statement

updateBatch(table: String, data: Object, updateKey: String): Number
Parameters
table `(String)` The table to update
data `(Object)` Batch insert data
updateKey `(String)` The field in the table to compare against for updating
Returns
`Number`: Number of rows updated
▸ delete(table, where?)

Run the generated delete query

delete(table: String, where: Object?): Promise<Result>
Parameters
table `(String)` The table to insert into
where `(Object?)` Where clause for delete statement
Returns
`Promise<Result>`: Promise containing the result of the query
▸ getCompiledSelect(table?, reset)

Return generated select query SQL

getCompiledSelect(table: String?, reset: Boolean): String
Parameters
table `(String?)` the name of the table to retrieve from
reset `(Boolean = `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: String, reset: Boolean): String
Parameters
table `(String)` the name of the table to insert into
reset `(Boolean = `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: String, reset: Boolean): String
Parameters
table `(String)` the name of the table to update
reset `(Boolean = `true`)` Whether to reset the query builder so another query can be built
Returns
`String`: The compiled sql statement
▸ getCompiledDelete(table, reset)

Return generated delete query SQL

getCompiledDelete(table: String, reset: Boolean): String
Parameters
table `(String)` the name of the table to delete from
reset `(Boolean = `true`)` Whether to reset the query builder so another query can be built
Returns
`String`: The compiled sql statement

### Result

Query result object

new Result(rows: Array, columns: Array)
Parameters
rows `(Array = `[]`)` the data rows of the result
columns `(Array = `[]`)` the column names in the result
Instance Members
▸ rowCount()

Get the number of rows returned by the query

rowCount(): Number
Returns
`Number`: the number of rows in the result
▸ columnCount()

Get the number of columns returned by the query

columnCount(): Number
Returns
`Number`: the number of columns in the result