Database

Introduction


Abstractions to MongoDB operations are provisioned directly on the EntityRepo object.

Overview


Basic methods for CRUD functionality and also aggregation is exposed to the EntityRepo object.

Get

This method gives ability to read data from a particular collection. Definition for the method is given as -

/**
* Find entity of type {name} using {filter}
* @param {String} name Name of Collection/Table
* @param {Object} filter Query filter
* @param {Object} options sorting,populating extra values etc [optional]
* @param {Function} fn Callback
*
*/
function(name, filter, options, fn) {};

Example using this method in a processor -

let VALID_MONGO_FILTER = {};
this.entityRepo.get(COLLECTION_NAME, VALID_MONGO_FILTER, {
one: Boolean, //Determines whether to return a single document or an array of documents.
full: Boolean, //Determines whether to Eager load related properties on an object
sort: Valid Mongo Sort Object, //used to sort the result
limit: Number, //used to limit the number of returned documents
fields: Arrays, //used to determine the fields in the document to return
skip: Number //used to skip the first N number of documents in the result.
}, (error, result) => {});

Create

This method is used to create either a single document or an array of documents in the specified collection. Definition of the method is given as -

/**
* Create an entity
* @param {String} name Name of the collection/table entity is located in
* @param {Object} data Update data
* @param {function} fn Callback
*
*/
function(name, data, fn) {};

Example using this method in a processor -

this.entityRepo.create(
COLLECTION_NAME,
DOCUMENT_TO_CREATE,
(error, result) => {}
);

Update

This method is used to perform updates in a particular collection. Definition of the method is given as -

/**
* Update an entity
* @param {String} name Name of the collection/table entity is located in
* @param {Object} data Update data
* @param {Function} fn Callback
*
*/
function(name, data, fn) {};

Example using this method in a processor -

let update = {
...
}; //Document with a valid _id
let obj = {
$query: Object, //valid mongo query object
$update: Object //Document properties to update in the object
};
this.entityRepo.update(COLLECTION_NAME, update || obj, (error, result) => {});

Delete

This method is used to delete a particular document/ entity in a collection. Definition of the method is given as -

/**
* Delete an entity with the supplied id
* @param {String} name Name of Collection/Table
* @param {String} id Id of object to delete
* @param {Function} fn Callback
*
*/
function(name, id, fn) {};

Example using this method in a processor -

this.entityRepo.delete(COLLECTION_NAME, MONGO_ID, (error, result) => {});

Count

This method is used to return a count of the documents returned by a particular query. Definition of the method is given as -

/**
* Count number of entities that match the filter supplied
* @param {String} name Name of Collection/Table
* @param {Object} filter Query
* @param {Function} fn Callback
*
*/
function(name, filter, fn) {};

Example using this method in a processor is given as -

this.entityRepo.count(COLLECTION_NAME, QUERY_OBJECT, (error, result) => {});

Get Collection Name

This method returns the proper mongo collection name from a mongoose schema name. Definition of this method is given as -

/**
* Normalizes mongoose collection names to actual mongodb collection names
* @param {String} name Name of Collection/Table
* @return {String} Correct collection name.
*/
function(name) {};

Example using this method in a processor is given as -

let collectionName = this.entityRepo.getCollectionName(COLLECTION_NAME); //returns mongo proper collection name

Aggregate

This is an abstraction on MongoDB's aggregation framework. Definition of this method is given as -

/**
* Function that runs aggregation query on persistance object.
* @param {String} name Name of collection/table to run aggregation on
* @param {...Object} rest Other Args including aggregation query and callback
*
*/
function(name, ...rest) {};

Example using this method in a processor is given as -

this.entityRepo.aggregate(
COLLECTION_NAME,
VALID_AGGREGATION_PIPELINE,
(error, result) => {}
);