Query
Kind: global class
- Query
- .only([...attributes]) ↩︎
- .with([...attributes]) ↩︎
- .without([...attributes]) ↩︎
- .$(scope) ↩︎
- .at(where) ↩︎
- .limit(max) ↩︎
- .asc(...columns) ↩︎
- .desc(...columns) ↩︎
- .group(...columns) ↩︎
- .raw() ↩︎
- .update(data, [where]) ↩︎
- .remove([where]) ↩︎
- .select() ↩︎
- .all([where]) ↩︎
- .sum(column) ↩︎
- .min(column) ↩︎
- .max(column) ↩︎
- .count([column]) ↩︎
- .use(connection) ↩︎
- .bind(thisArg) ↩︎
- .run() ⇒
Promise - .then(resolve, reject) ⇒
Promise - .finally(handler) ⇒
Promise - .tap(handler) ⇒
Promise - .setOptions([options]) ↩︎
query.only([...attributes]) ↩︎
Select only the provided list of attributes. This overrides any scope
Kind: instance method of Query
Chainable
| Param | Type | Description |
|---|---|---|
| [...attributes] | string |
Attributes to select |
Example
User.at({ id: 42 }).only('name', 'age')
.then(function (user) {
// SELECT `User`.`id`, `User`.`name`, `User`.`age` FROM `user` `User`
// WHERE `User`.`id` = 42
// LIMIT 1
});
query.with([...attributes]) ↩︎
Append the provided attributes
Kind: instance method of Query
Chainable
| Param | Type | Description |
|---|---|---|
| [...attributes] | string |
Attributes to append |
Example
User.at({ id: 42 }).only('name').with('age')
.then(function (user) {
// SELECT `User`.`id`, `User`.`name`, `User`.`age` FROM `user` `User`
// WHERE `User`.`id` = 42
// LIMIT 1
});
query.without([...attributes]) ↩︎
Remove the provided attributes
Kind: instance method of Query
Chainable
| Param | Type | Description |
|---|---|---|
| [...attributes] | string |
Attributes to remove |
Example
User.at({ id: 42 }).only('id', 'name', 'age').without('name')
.then(function (user) {
// SELECT `User`.`id`, `User`.`age` FROM `user` `User`
// WHERE `User`.`id` = 42
// LIMIT 1
});
query.$(scope) ↩︎
Set the scope of the query, so it selects only attributes within the scope
Kind: instance method of Query
Chainable
| Param | Type | Description |
|---|---|---|
| scope | string |
The scope to choose |
Example
var User = ziti.define('User', {
id: ziti.Int().primaryKey().autoIncrement().$([ 'account', 'profile' ]),
username: ziti.String().$('profile'),
age: ziti.Int(),
sex: ziti.Enum('M', 'F').$('profile'),
email: ziti.String().$('account')
});
User.at({ age: { $ge: 18 } }).$('profile')
.then(function (user) {
// SELECT `User`.`id`, `User`.`username`, `User`.`sex` FROM `user` `User`
// WHERE `User`.`age` >= 18
// LIMIT 1
})
User.at({ age: { $ge: 18 } }).$('account')
.then(function (user) {
// SELECT `User`.`id`, `User`.`email` FROM `user` `User`
// WHERE `User`.`age` >= 18
// LIMIT 1
})
query.at(where) ↩︎
Specify the WHERE expression to apply
Kind: instance method of Query
Chainable
| Param | Type | Description |
|---|---|---|
| where | Object |
Query expression for the WHERE condition. |
Example
User.at({ id: 42 })
.then(function (user) {
// SELECT `User`.`id`, `User`.`name` FROM `user` `User`
// WHERE `User`.`id` = 42
// LIMIT 1
});
query.limit(max) ↩︎
Limit the number of rows to select.
Kind: instance method of Query
Chainable
| Param | Type | Description |
|---|---|---|
| max | Number |
The limit to set, if null it does not set any limit |
Example
User.at({ age: { $ge: 18 } }).all().limit(10)
.then(function (users) {
// SELECT `User`.`id`, `User`.`name` FROM `user` `User`
// WHERE `User`.`age` >= 18
// LIMIT 10
});
query.asc(...columns) ↩︎
Specify columns to sort in ascending order
Kind: instance method of Query
Chainable
| Param | Type | Description |
|---|---|---|
| ...columns | string |
Columns to sort |
query.desc(...columns) ↩︎
Specify columns to sort in descending order
Kind: instance method of Query
Chainable
| Param | Type | Description |
|---|---|---|
| ...columns | string |
Columns to sort |
query.group(...columns) ↩︎
Speficy how to group the result-set by one or more columns
Kind: instance method of Query
Chainable
| Param | Type | Description |
|---|---|---|
| ...columns | string |
Columns to group |
query.raw() ↩︎
When retrieving data, it returns raw data instead of Model instance(s)
Kind: instance method of Query
Chainable
query.update(data, [where]) ↩︎
Set the query as update
Kind: instance method of Query
Chainable
| Param | Type | Description |
|---|---|---|
| data | Object |
The data to update |
| [where] | Object |
Query expression to find the row. |
Example
User.at({ age: { $ge: 18 } }).update({ status: 'major' })
.then(function (user) {
// UPDATE `user` `User` SET `User`.`status` = 'major'
// WHERE `User`.`age` >= 18
});
// OR
User.update({ name: 'alex' }, { age: { $ge: 18 } })
.then(function (user) {
// UPDATE `user` `User` SET `User`.`status` = 'major'
// WHERE `User`.`age` >= 18
});
query.remove([where]) ↩︎
Set the query as delete
Kind: instance method of Query
Chainable
| Param | Type | Description |
|---|---|---|
| [where] | Object |
Query expression to find the row. |
Example
User.at({ id: 42 }).remove()
.then(function (user) {
// DELETE FROM `User` USING `user` `User`
// WHERE `User`.`id` = 42
});
// OR
User.remove({ id: 42 })
.then(function (user) {
// DELETE FROM `User` USING `user` `User`
// WHERE `User`.`id` = 42
});
query.select() ↩︎
Set the query as select one. Note that this is the default query, so it's not necessary to call it.
Kind: instance method of Query
Chainable
Params: ...string [attributes] - Attributes to select (overrides #only)
Example
User.at({ id: 42 }).select()
.then(function (user) {
// SELECT `User`.`id`, `User`.`name` FROM `user` `User`
// WHERE `User`.`id` = 42
// LIMIT 1
});
// OR
User.at({ id: 42 })
.then(function (user) {
// SELECT `User`.`id`, `User`.`name` FROM `user` `User`
// WHERE `User`.`id` = 42
// LIMIT 1
});
query.all([where]) ↩︎
Set the query as select all
Kind: instance method of Query
Chainable
| Param | Type | Description |
|---|---|---|
| [where] | Object |
Query expression to find the row. |
Example
User.at({ age: { $ge: 18 } }).all().limit(10)
.then(function (users) {
// SELECT `User`.`id`, `User`.`name` FROM `user` `User`
// WHERE `User`.`age` >= 18
// LIMIT 10
});
// OR
User.all({ age: { $ge: 18 } }).limit(10)
.then(function (users) {
// SELECT `User`.`id`, `User`.`name` FROM `user` `User`
// WHERE `User`.`age` >= 18
// LIMIT 10
});
query.sum(column) ↩︎
Get the sum of the numeric values of the column
Kind: instance method of Query
Chainable
| Param | Type | Description |
|---|---|---|
| column | string |
The numeric column |
Example
User.sum('age').at({ username: { $not: null } })
.then(function (sum) {
// SELECT SUM(`User.`age`) FROM `user` `User`
// WHERE `User`.`username` NOT NULL
});
query.min(column) ↩︎
Get the minimum numeric values of the column
Kind: instance method of Query
Chainable
| Param | Type | Description |
|---|---|---|
| column | string |
The numeric column |
Example
User.min('age').at({ username: { $not: null } })
.then(function (min) {
// SELECT MIN(`User.`age`) FROM `user` `User`
// WHERE `User`.`username` NOT NULL
});
query.max(column) ↩︎
Get the maximum numeric values of the column
Kind: instance method of Query
Chainable
| Param | Type | Description |
|---|---|---|
| column | string |
The numeric column |
Example
User.max('age').at({ username: { $not: null } })
.then(function (max) {
// SELECT MAX(`User.`age`) FROM `user` `User`
// WHERE `User`.`username` NOT NULL
});
query.count([column]) ↩︎
Count the number of rows. If a column is provided, it counts only the non-NULL values.
Kind: instance method of Query
Chainable
| Param | Type | Description |
|---|---|---|
| [column] | string |
The column |
Example
User.count().at({ username: { $not: null } })
.then(function (count) {
// SELECT COUNT(*) FROM `user` `User`
// WHERE `User`.`username` NOT NULL
});
query.use(connection) ↩︎
Use this connection instead
Kind: instance method of Query
Chainable
| Param | Type | Description |
|---|---|---|
| connection | PoolConnection |
Use this connection |
Example
ziti.withConnection(function (connection) {
User.at({ id: 42 }).use(connection).run();
});
query.bind(thisArg) ↩︎
Bind the query to thisArg. This is useful to bind a callback.
Kind: instance method of Query
Chainable
| Param | Type |
|---|---|
| thisArg | * |
Example
User.at({ id: 42 }).bind(this).then(this.callback);
query.run() ⇒ Promise
Run the query and returns a promise than the query has been run
Kind: instance method of Query
Example
User.at({ id: 42 }).run().then(function (user) {
console.log(user.raw());
});
query.then(resolve, reject) ⇒ Promise
Shortcut for query.run().then(resolve, reject)
Kind: instance method of Query
See: Bluebird#then
| Param | Type | Description |
|---|---|---|
| resolve | function |
Promise fulfilledHandler |
| reject | function |
Promise rejectedHandler |
query.finally(handler) ⇒ Promise
Shortcut for query.run().finally(handler)
Kind: instance method of Query
See: Bluebird#finally
| Param | Type | Description |
|---|---|---|
| handler | function |
Promise handler |
query.tap(handler) ⇒ Promise
Shortcut for query.run().tap(handler)
Kind: instance method of Query
See: Bluebird#tap
| Param | Type | Description |
|---|---|---|
| handler | function |
Promise handler |
query.setOptions([options]) ↩︎
Set options using an object instead, mainly for retro compatibility
Kind: instance method of Query
Chainable
| Param | Type | Description |
|---|---|---|
| [options] | Object |
|
| [options.attributes] | Array.<string> |
Select only the provided list of attributes. |
| [options.limit] | Number |
Limit the number of rows to select |
| [options.using] | PoolConnection |
Use this connection |