# | Documentation

###

``
Need help reading this?

### limit

Set the limit clause

limit(sql: String, limit: Number, offset: [Number]): String
Parameters
sql `(String)` SQL statement to modify
limit `(Number)` Maximum number of rows to fetch
offset `([Number])` Number of rows to skip
Returns
`String`: Modified SQL statement

### quoteTable

Quote database table name, and set prefix

quoteTable(table: String): String
Parameters
table `(String)` Table name to quote
Returns
`String`: Quoted table name

### quoteIdentifiers

Use the driver's escape character to quote identifiers

quoteIdentifiers(str: (String | Array)): (String | Array)
Parameters
str `((String | Array))` String or array of strings to quote identifiers
Returns
`(String | Array)`: Quoted identifier(s)

### truncate

Generate SQL to truncate the passed table

truncate(table: String): String
Parameters
table `(String)` Table to truncate
Returns
`String`: Truncation SQL

### insertBatch

Generate SQL to insert a group of rows

insertBatch(table: String, data: [Array]): String
Parameters
table `(String)` The table to insert to
data `([Array])` The array of object containing data to insert
Returns
`String`: Query and data to insert

### NodeQuery

Class for connection management

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

Constructor

constructor(config: object)
Parameters
config `(object)` connection parameters
Example

    let nodeQuery = require('ci-node-query')({
    	driver: 'mysql',
    	connection: {
    		host: 'localhost',
    		user: 'root',
    		password: '',
    		database: 'mysql'
    	}
    });

    let nodeQuery = require('ci-node-query')({
    	driver: 'sqlite',
    	connection: ':memory:'
    });

▸ 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
▸ query(sql, [params], [callback])

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

query(sql: string, params: [array], callback: [function]): (void | Promise)
Parameters
sql `(string)` The sql to execute
params `([array])` The query parameters
callback `([function])` Optional callback
Returns
`(void | Promise)`: Returns a promise if no callback is supplied
▸ resetQuery()

Reset the object state for a new query

resetQuery(): void
Returns
`void`:
▸ truncate(table, [callback])

Empties the selected database table

truncate(table: string, callback: [function]): (void | Promise)
Parameters
table `(string)` the name of the table to truncate
callback `([function])` Optional callback
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] (default `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] (default `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] (default `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] (default `both`) )` The placement of the wildcard character(s): before, after, or both
Returns
`QueryBuilder`: The Query Builder object, for chaining
▸ having(key, [val])

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)])` The value to compare if the value of key is a string
Returns
`QueryBuilder`: The Query Builder object, for chaining
▸ orHaving(key, [val])

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)])` 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] (default `'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] (default `'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], [callback])

Get the results of the compiled query

get(table: [String], limit: [Number], offset: [Number], callback: [Function]): (void | Promise)
Parameters
table `([String])` The table to select from
limit `([Number])` A limit for the query
offset `([Number])` An offset for the query
callback `([Function])` A callback for receiving the result
Returns
`(void | Promise)`: If no callback is passed, a promise is returned
Example

    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

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

Run the generated insert query

insert(table: String, data: [Object], callback: [Function]): (void | Promise)
Parameters
table `(String)` The table to insert into
data `([Object])` Data to insert, if not already added with the 'set' method
callback `([Function])` Callback for handling response from the database
Returns
`(void | Promise)`: If no callback is passed, a promise is returned
▸ insertBatch(table, data, [callback])

Insert multiple sets of rows at a time

insertBatch(table: String, data: Array, callback: [Function]): (void | Promise)
Parameters
table `(String)` The table to insert into
data `(Array)` The array of objects containing data rows to insert
callback `([Function])` Callback for handling database response
Returns
`(void | Promise)`: If no callback is passed, a promise is returned
Example

    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);

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

Run the generated update query

update(table: String, data: [Object], callback: [Function]): (void | Promise)
Parameters
table `(String)` The table to insert into
data `([Object])` Data to insert, if not already added with the 'set' method
callback `([Function])` Callback for handling response from the database
Returns
`(void | Promise)`: If no callback is passed, a promise is returned
▸ delete(table, [where], [callback])

Run the generated delete query

delete(table: String, where: [Object], callback: [Function]): (void | Promise)
Parameters
table `(String)` The table to insert into
where `([Object])` Where clause for delete statement
callback `([Function])` Callback for handling response from the database
Returns
`(void | Promise)`: If no callback is passed, a promise is returned
▸ 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] (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: String, reset: [Boolean]): String
Parameters
table `(String)` the name of the table to insert into
reset `([Boolean] (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: String, reset: [Boolean]): String
Parameters
table `(String)` the name of the table to update
reset `([Boolean] (default `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] (default `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