Class: GenericDao

GenericDao

Generic data-access object for simple CRUD operations.

Constructor

new GenericDao(dataContext, tableName)

Initialize the DAO. Note that the db is expected to be connected before any of this class's methods are used.
Parameters:
Name Type Description
dataContext DataContext A DataContext instance that is used to run queries.
tableName string The name of the table that this DAO operates on.
Source:

Methods

create(resource) → {Promise.<Object>}

Generic create method that validates a model using an InsertValidator and then inserts the model.
Parameters:
Name Type Description
resource Object A model to create.
Source:
Returns:
Same as createIf.
Type
Promise.<Object>

createIf(resource, condition) → {Promise.<Object>}

Create a resource if a condition resolves successfully. Note that prior to invoking the condition the resource is validated using an InsertValidator.
Parameters:
Name Type Description
resource Object A model to create.
condition GenericDao~conditionCallback A function that returns a promise. If the promise is resolved then the model is created. resource is passed to condition.
Source:
Returns:
A promise that is: 1) Resolved with the model if the model is valid and the condition is resolved. The model will be updated with the new ID if possible. 2) Rejected with a ValidationErrorList if the model is invalid. 3) Rejected with condition's promise if condition is rejected.
Type
Promise.<Object>

delete(resource) → {Promise.<Object>}

Generic delete method that validates a model using a DeleteValidator and then deletes it by ID.
Parameters:
Name Type Description
resource Object A model to delete by ID.
Source:
Returns:
A promise that is: 1) Resolved with the model if the model is valid and deleted. 2) Rejected with a ValidationErrorList if the model is invalid. 3) Rejected with a NotFoundError instance if no records are affected by the delete attempt.
Type
Promise.<Object>

isUnique(where, params, onDupe) → {Promise.<bool>}

Helper function to check that something is unique. This is useful before creating or updating a record.
Parameters:
Name Type Description
where Object An optional where condition.
params Object Query parameters for the where condition.
onDupe GenericDao~errorCallback An option function that is called when a duplicate is found. If the resource is not found and this function is defined, the resource is rejected with the result of this function.
Source:
Returns:
A promise that is resolved if the resource is unique (that is, if no records are found). If the resource is not unique then the promise is rejected with a DuplicateError instance.
Type
Promise.<bool>

replace(parentTableName, parentID, resources) → {Promise.<Array.<Object>>}

Replace (remove and recreate) all the resources associated with a parent table.
Parameters:
Name Type Description
parentTableName string The name of the parent table.
parentID any The identifier of the parent resource.
resources Array.<Object> An array of resources which will be created.
Source:
Returns:
The array of resources, each updated with their new identifier and parentID. The parent and the resources are validated, so the returned promise shall be rejected if a validation error occurs.
Type
Promise.<Array.<Object>>

retrieve(where, params) → {Promise.<Array.<Object>>}

Retrieve an array of resources.
Parameters:
Name Type Description
where Object An optional where condition.
params Object Query parameters for the where condition.
Source:
Returns:
A promise that is resolved with the results as an array.
Type
Promise.<Array.<Object>>

retrieveByID(id)

Retrieve a single resource by ID. Specialized version of retrieveSingle.
Parameters:
Name Type Description
id any The unique identifier of the resource.
Source:

retrieveSingle(where, params, onNotFound) → {Promise.<Object>}

Retrieve a single resource as an object.
Parameters:
Name Type Description
where Object An optional where condition.
params Object Query parameters for the where condition.
onNotFound GenericDao~errorCallback An optional function that produces an Error when a resource is not found.
Source:
Returns:
A promise that is resolved with the first matching resource. If there are no matches found, then the promise is rejected with a NotFoundError instance.
Type
Promise.<Object>

update(resource) → {Promise.<Object>}

Generic update method that validates a model using an UpdateValidator and then updates it by ID.
Parameters:
Name Type Description
resource Object A model to update by ID.
Source:
Returns:
Same as updateIf.
Type
Promise.<Object>

updateIf(resource, condition) → {Promise.<Object>}

Update a resource if a condition resolves successfully. Note that prior to invoking the condition the resource is validated using an UpdateValidator.
Parameters:
Name Type Description
resource Object A model to update by ID.
condition GenericDao~conditionCallback A function that returns a promise. If the promise is resolved then the model is created. resource is passed to condition.
Source:
Returns:
A promise that is: 1) Resolved with the model if the model is valid and the condition is resolved. 2) Rejected with a ValidationErrorList if the model is invalid. 3) Rejected with condition's promise if condition is rejected.
Type
Promise.<Object>

Type Definitions

conditionCallback(resource) → {Promise.<bool>}

Parameters:
Name Type Description
resource Object The resource object.
Source:
Returns:
A promise that is resolved if the condition is met, or otherwise rejected.
Type
Promise.<bool>

errorCallback(err) → {Error}

Parameters:
Name Type Description
err Error An Error instance.
Source:
Returns:
A promise that produces a customized Error instance.
Type
Error