new DataAccessObject()
Base class for all persistent objects.
Provides a common API to access any database connector.
This class describes only abstract behavior. Refer to the specific connector for additional details.
`DataAccessObject` mixes `Inclusion` classes methods.
- Source:
Members
remove
Delete object from persistence
Triggers `destroy` hook (async) before and after destroying object
- Source:
updateAttributes
Update set of attributes.
Performs validation before updating.
NOTE: `patchOrCreate` is an alias.
- Source:
(static) remove
Destroy all matching records.
Delete all model instances from data source. Note: destroyAll method does not destroy hooks.
Example:
````js
Product.destroyAll({price: {gt: 99}}, function(err) {
// removed matching products
});
````
- Source:
(static) removeById
Delete the record with the specified ID.
Aliases are `destroyById` and `deleteById`.
- Source:
(static) update
Update multiple instances that match the where clause
Example:
```js
Employee.update({managerId: 'x001'}, {managerId: 'x002'}, function(err) {
...
});
```
- Source:
(static) updateOrCreate
Update or insert a model instance: update exiting record if one is found, such that parameter `data.id` matches `id` of model instance;
otherwise, insert a new record.
NOTE: No setters, validations, or hooks are applied when using upsert.
`updateOrCreate` and `patchOrCreate` are aliases
- Source:
Methods
replaceAttributes(data, optionsopt, cb)
Replace set of attributes.
Performs validation before replacing.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
data |
Object | Data to replace | |
options |
Object |
<optional> |
Options for replace |
cb |
function | Callback function called with (err, instance) |
- Source:
save(cb)
Save instance. If the instance does not have an ID, call `create` instead.
Triggers: validate, save, update or create.
Parameters:
Name | Type | Description |
---|---|---|
cb |
function | Callback function with err and object arguments |
Properties:
Name | Type | Description |
---|---|---|
validate |
Boolean | Default is true. |
throws |
Boolean | Default is false. |
- Source:
setAttribute(name, value)
Set a single attribute.
Equivalent to `setAttributes({name: value})`
Parameters:
Name | Type | Description |
---|---|---|
name |
String | Name of property |
value |
Mixed | Value of property |
- Source:
setAttributes(data)
Update set of attributes.
Parameters:
Name | Type | Description |
---|---|---|
data |
Object | Data to update |
- Source:
updateAttribute(name, value, cb)
Update a single attribute.
Equivalent to `updateAttributes({name: value}, cb)`
Parameters:
Name | Type | Description |
---|---|---|
name |
String | Name of property |
value |
Mixed | Value of property |
cb |
function | Callback function called with (err, instance) |
- Source:
(static) count(whereopt, optionsopt, cb)
Return count of matched records. Optional query parameter allows you to count filtered set of model instances.
Example:
```js
User.count({approved: true}, function(err, count) {
console.log(count); // 2081
});
```
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
where |
Object |
<optional> |
Search conditions (optional) |
options |
Object |
<optional> |
Options |
cb |
function | Callback, called with (err, count) |
- Source:
(static) create(dataopt, optionsopt, cbopt)
Create an instance of Model with given data and save to the attached data source. Callback is optional.
Example:
```js
User.create({first: 'Joe', last: 'Bob'}, function(err, user) {
console.log(user instanceof User); // true
});
```
Note: You must include a callback and use the created model provided in the callback if your code depends on your model being
saved or having an ID.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
data |
Object |
<optional> |
Optional data object |
options |
Object |
<optional> |
Options for create |
cb |
function |
<optional> |
Callback function called with these arguments: - err (null or Error) - instance (null or Model) |
- Source:
(static) createAll(dataArrayopt, optionsopt, cbopt)
Create an instances of Model with given data array and save to the attached data source. Callback is optional.
Example:
```js
User.createAll([{first: 'Joe', last: 'Bob'},{first: 'Tom', last: 'Cat'}], function(err, users) {
console.log(users[0] instanceof User); // true
});
```
Note: You must include a callback and use the created models provided in the callback if your code depends on your model being
saved or having an ID.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
dataArray |
Object |
<optional> |
Optional data object with array of records |
options |
Object |
<optional> |
Options for create |
cb |
function |
<optional> |
Callback function called with these arguments: - err (null or Error) - instance (null or Models) |
- Source:
(static) exists(id, optionsopt, cb)
Check whether a model instance exists in database
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
id |
id | Identifier of object (primary key value) | |
options |
Object |
<optional> |
Options |
cb |
function | Callback function called with (err, exists: Bool) |
- Source:
(static) find(cb) → {Promise}
Find all instances of Model that match the specified query.
Fields used for filter and sort should be declared with `{index: true}` in model definition.
See [Querying models](http://docs.strongloop.com/display/DOC/Querying+models) for more information.
For example, find the second page of ten users over age 21 in descending order exluding the password property.
```js
User.find({
where: {
age: {gt: 21}},
order: 'age DESC',
limit: 10,
skip: 10,
fields: {password: false}
},
console.log
);
```
Parameters:
Name | Type | Description |
---|---|---|
cb |
function | Optional callback function. Call this function with two arguments: `err` (null or Error) and an array of instances. |
Properties:
Name | Type | Description |
---|---|---|
where |
Object | Search criteria in JSON format `{ key: val, key2: {gt: 'val2'}}`. Operations: - gt: > - gte: >= - lt: < - lte: <= - between - inq: IN - nin: NOT IN - neq: != - like: LIKE - nlike: NOT LIKE - ilike: ILIKE - nilike: NOT ILIKE - regexp: REGEXP You can also use `and` and `or` operations. See [Querying models](http://docs.strongloop.com/display/DOC/Querying+models) for more information. |
include |
String | Object | Array | Allows you to load relations of several objects and optimize numbers of requests. Format examples; - `'posts'`: Load posts - `['posts', 'passports']`: Load posts and passports - `{'owner': 'posts'}`: Load owner and owner's posts - `{'owner': ['posts', 'passports']}`: Load owner, owner's posts, and owner's passports - `{'owner': [{posts: 'images'}, 'passports']}`: Load owner, owner's posts, owner's posts' images, and owner's passports See `DataAccessObject.include()`. |
order |
String | Sort order. Format: `'key1 ASC, key2 DESC'` |
limit |
Number | Maximum number of instances to return. |
skip |
Number | Number of instances to skip. |
offset |
Number | Alias for `skip`. |
fields |
Object | Array | String | Included/excluded fields. - `['foo']` or `'foo'` - include only the foo property - `['foo', 'bar']` - include the foo and bar properties. Format: - `{foo: true}` - include only foo - `{bat: false}` - include all properties, exclude bat |
- Source:
Returns:
results If no callback function is provided, a promise (which resolves to an array of instances) is returned
- Type
- Promise
(static) findById(id, filteropt, optionsopt, cb)
Find model instance by ID.
Example:
```js
User.findById(23, function(err, user) {
console.info(user.id); // 23
});
```
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
id |
* | Primary key value | |
filter |
Object |
<optional> |
The filter that contains `include` or `fields`. Other settings such as `where`, `order`, `limit`, or `offset` will be ignored. |
options |
Object |
<optional> |
Options |
cb |
function | Callback called with (err, instance) |
- Source:
(static) findByIds(ids, query, optionsopt, cb)
Find model instances by ids
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
ids |
Array | An array of ids | |
query |
Object | Query filter | |
options |
Object |
<optional> |
Options |
cb |
function | Callback called with (err, instance) |
- Source:
(static) findOne(query, optionsopt, cb)
Find one record, same as `find`, but limited to one result. This function returns an object, not a collection.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
query |
Object | Search conditions. See [find](#dataaccessobjectfindquery-callback) for query format. For example: `{where: {test: 'me'}}`. | |
options |
Object |
<optional> |
Options |
cb |
function | Callback function called with (err, instance) |
- Source:
(static) findOrCreate(query, data, optionsopt, cb)
Find one record that matches specified query criteria. Same as `find`, but limited to one record, and this function returns an
object, not a collection.
If the specified instance is not found, then create it using data provided as second argument.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
query |
Object | Search conditions. See [find](#dataaccessobjectfindquery-callback) for query format. For example: `{where: {test: 'me'}}`. | |
data |
Object | Object to create. | |
options |
Object |
<optional> |
Option for findOrCreate |
cb |
function | Callback called with (err, instance, created) |
- Source:
(static) getConnector() → {Connector}
Get the connector instance for the given model class
- Source:
Returns:
The connector instance
- Type
- Connector
(static) replaceOrCreate(data, optionsopt, cb)
Replace or insert a model instance: replace exiting record if one is found, such that parameter `data.id` matches `id` of model instance;
otherwise, insert a new record.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
data |
Object | The model instance data | |
options |
Object |
<optional> |
Options for replaceOrCreate |
cb |
function | The callback function (optional). |
- Source:
(static) scope(name, query, targetClassopt)
Define a scope for the model class. Scopes enable you to specify commonly-used
queries that you can reference as method calls on a model.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
name |
String | The scope name | |
query |
Object | The query object for DataAccessObject.find() | |
targetClass |
ModelClass |
<optional> |
The model class for the query, default to the declaring model |
- Source: