new Provider(opts, schema)
Constructor for the data provider base class. This class cannot be used directly.
Options
connStr{String}, connection string.
Parameters:
| Name | Type | Argument | Description |
|---|---|---|---|
opts |
object | Additional options. The only required option is |
|
schema |
object |
<optional> |
Defines the database schema such as fields, indexes, types and structure. |
- Source:
Methods
-
<protected> _do()
-
- Source:
-
<virtual> _get()
-
- Source:
-
<protected> _getId()
-
- Source:
-
<protected> _getIdKey()
-
- Source:
-
<virtual> _insert()
-
- Source:
-
<virtual> _select()
-
- Source:
-
<protected> _setId()
-
- Source:
-
<virtual> _update()
-
- Source:
-
<virtual> _upsert()
-
- Source:
-
delete(context, item|identity, callback) → {null}
-
Removes the specified item from the data collection. If an item with the specified identity is not found an error is returned.
Parameters:
Name Type Argument Description contextObject <optional>
provides context for the current execution. Usually this is information about the user making the call. Although this parameter is optional, it might be required by some interceptors, such as authorization.
item|identityObject | String | Number the item or the identity of an item to be deleted.
callbackProvider~actionCallback The callback that handles the result of this action.
- Source:
Returns:
- Type
- null
-
dispose(callback) → {null}
-
Releases its database connection, clears current state and any cached data.
This is utility method intended mainly for data provider implementers.
Parameters:
Name Type Description callbackfunction Optional callback that will be called when the provider is disposed.
- Source:
Returns:
- Type
- null
-
get(context, item|identity, callback) → {null}
-
Retrieves the specified item from the data collection. If an item with the specified identity is not found an error is returned.
Parameters:
Name Type Argument Description contextObject <optional>
provides context for the current execution. Usually this is information about the user making the call. Although this parameter is optional, it might be required by some interceptors, such as authorization.
item|identityObject | String | Number the item or the identity of an item to be retrieved.
callbackProvider~actionCallback The callback that handles the result of this action.
- Source:
Returns:
- Type
- null
-
handleError(err, callback) → {null}
-
Creates new error object with the specified error message or error code.
This is utility method intended mainly for data provider implementers.
Parameters:
Name Type Argument Description errstring | Error This argument should be either error message, EntreeJS error code or error object.
callbackfunction <optional>
Error handler. If omitted the error will be thrown immediately.
- Source:
Returns:
- Type
- null
-
init(callback) → {null}
-
This is utility method intended mainly for data provider implementers.
Parameters:
Name Type Description callbackfunction Optional callback that will be called when the provider is initialized.
- Source:
Returns:
- Type
- null
-
insert(context, items, callback) → {null}
-
Inserts new item in the data collecton. If an item with the same identity is already present in the data collection, the operation is aborted and an error is returned.
Parameters:
Name Type Argument Description contextobject <optional>
Provides context for the current execution. Usually this is information about the user making the call. Although this parameter is optional, it might be required by some interceptors, such as authorization.
itemsobject | Array.<object> An item or an array of items to be inserted in the data store.
callbackProvider~actionCallback The callback that handles the result of this action.
- Source:
Returns:
- Type
- null
-
select(context, query, options, callback) → {null|Cursor}
-
Selects items in the data collection and returns a Cursor to the selected items.
This method can return a Cursor either directly or with a callback. In most cases a cursor is created and returned synchronously, but some interceptors like module:cache require a callback. If you plan to use asynchronous interceptors you have to use the callback form.
Select uses MongoDB query syntax. All Entree data providers should support the full specification.
Options
skip{Number}, the number of items to skip.limit{Number}, limit the number of returned items.sort{Object}, set to sort the documents coming back from the query.
Parameters:
Name Type Argument Description contextobject <optional>
Provides context for the current execution.
queryobject <optional>
Specifies selection criteria. To return all items in a collection, omit this parameter or pass an empty object.
optionsobject <optional>
Additional options for the cursor.
callbackfunction <optional>
Optional callback function that will be called when the Cursor is constructed.
- Source:
Returns:
- The method returns null if a callback is provided otherwise it returns a Cursor.
- Type
- null | Cursor
Example
// Select at most 10 teenagers starting from the 21th that matches the criteria: entree.users.select({ age: { $gte: 13, $lte: 19 }}) .skip(20) .limit(10) .sort({ age: 1 }) .each(function (err, user)) { if (err) { return console.log(err); } console.log(JSON.stringify(user)); }); // Do the same but with a callback - the recommended way if interceptors are used on the select method: entree.users.select({ age: { $gte: 13, $lte: 19 }}, function (err, cursor) { if (err) { return console.log(err); } cursor .skip(20) .limit(10) .sort({ age: 1 }) .each(function (err, user) { if (err) { return console.log(err); } console.log(JSON.stringify(user)); }); }); // Again the same but limitations set as options: entree.users.select({ age: { $gte: 13, $lte: 19 }}, { skip: 20, limit: 10, sort: { age: 1 }}, function (err, cursor) { if (err) { return console.log(err); } cursor.each(function (err, user) { if (err) { return console.log(err); } onsole.log(JSON.stringify(user)); }); }); // Get the total number of all users in the collection: entree.users.select(function (err, currsor) { cursor.count(err, count) { if (err) { return console.log(err); } console.log(count); }); }); -
selectOne(context, query, options, callback) → {null}
-
This method is the same as
select, but instead of cursor it returns the first item that matches the select and sort criteria. If there is no match, null is returned.Parameters:
Name Type Argument Description contextobject <optional>
Provides context for the current execution.
queryobject <optional>
Specifies selection criteria.
optionsobject <optional>
Additional options for the cursor.
callbackProvider~actionCallback The callback that handles the result of this action.
- Source:
Returns:
- Type
- null
-
update(context, item, callback) → {null}
-
Updates the specified item with the provided fields. If an item with the specified identity could not be found an error is returned.
Parameters:
Name Type Argument Description contextObject <optional>
provides context for the current execution. Usually this is information about the user making the call. Although this parameter is optional, it might be required by some interceptors, such as authorization.
itemObject the item to be updated.
callbackProvider~actionCallback The callback that handles the result of this action.
- Source:
Returns:
- Type
- null
-
upsert(context, item, callback) → {null}
-
Inserts new item in the data collection if the item is not present, otherwise updates it.
Parameters:
Name Type Argument Description contextObject <optional>
provides context for the current execution. Usually this is information about the user making the call. Although this parameter is optional, it might be required by some interceptors, such as authorization.
itemObject the item to be added or updated.
callbackProvider~actionCallback The callback that handles the result of this action.
- Source:
Returns:
- Type
- null
-
use(fn) → {null}
-
Adds an interception module to this provider instance. See Interceptors tutorial for more details.
Parameters:
Name Type Description fnfunction The function that handles the interception.
- Source:
Returns:
- Type
- null
Type Definitions
-
actionCallback(err, res)
-
This callback will be called after executing the method. The first parameter will contain an error object if an error occurred during execution, while the second parameter will contain the result from the execution if it was successful.
For insert, upsert and update operations the result should be an object containing the same set of fields that were originally passed plus any fields that were automatically updated, such as timestamps or identity fields.
NOTE: the returned result on insert, update and delete may differ slightly for the different providers, but if no error is returned the operation is considered successful.
Parameters:
Name Type Description errobject the error object upon unsuccessful execution.
resobject the result object upon successful execution.
- Source: