OpenVeo server API for plugins

API Docs for: 3.0.0
Show:

File: lib/models/EntityModel.js

'use strict';

/**
 * @module models
 */

var shortid = require('shortid');

/**
 * Defines class EntityModel.
 *
 * The EntityModel provides basic CRUD (**C**reate **R**ead **U**pdate **D**elete) operations on entities.<br/>
 * All entities models must extend EntityModel. EntityModel must not be used directly. Use one of its sub class
 * instead.
 *
 * Each entity as it's own associated Model (sub class of EntityModel).
 *
 * @example
 *
 *     // Example for implementing a new EntityModel named "CustomModel"
 *
 *     // CustomModel.js
 *
 *     var util = require('util');
 *     var api = require('@openveo/api');
 *     var CustomProvider = process.require('CustomProvider.js');
 *
 *     function CustomModel() {
 *
 *       // Initialize the entity model with a dedicated provider
 *       api.EntityModel.call(this, new CustomProvider(api.applicationStorage.getDatabase()));
 *
 *     }
 *
 *     // CustomModel must extends EntityModel
 *     module.exports = CustomModel;
 *     util.inherits(CustomModel, api.EntityModel);
 *
 * @example
 *
 *     // Example for how to use CustomModel defined in previous example
 *
 *     var api = require('@openveo/api');
 *
 *     var CustomModel = process.require('CustomModel.js');
 *     var model = new CustomModel();
 *
 * @class EntityModel
 * @constructor
 * @param {EntityProvider} provider The entity provider
 */
function EntityModel(provider) {
  this.provider = provider;

  if (!this.provider)
    throw new Error('An EntityModel needs a provider');
}

module.exports = EntityModel;

/**
 * Gets a single entity by its id.
 *
 * @method getOne
 * @async
 * @param {String} id The entity id
 * @param {Object} filter A MongoDB filter
 * @param {Function} callback The function to call when it's done
 *   - **Error** The error if an error occurred, null otherwise
 *   - **Object** The entity
 */
EntityModel.prototype.getOne = function(id, filter, callback) {
  this.provider.getOne(id, filter, callback);
};

/**
 * Gets all entities.
 *
 * @method get
 * @async
 * @param {Object} filter A MongoDB filter
 * @param {Function} callback The function to call when it's done
 *   - **Error** The error if an error occurred, null otherwise
 *   - **Array** The list of entities
 */
EntityModel.prototype.get = function(filter, callback) {
  this.provider.get(filter, callback);
};

/**
 * Gets an ordered list of entities by page.
 *
 * @method getPaginatedFilteredEntities
 * @async
 * @param {Object} [filter] MongoDB filter
 * @param {Number} [limit] The maximum number of expected entities
 * @param {Number} [page] The expected page
 * @param {Object} [sort] A sort object
 * @param {Boolean} [populate] true to automatically populate results with additional information
 * @param {Function} callback The function to call when it's done
 *   - **Error** The error if an error occurred, null otherwise
 *   - **Array** The list of entities
 *   - **Object** Pagination information
 */
EntityModel.prototype.getPaginatedFilteredEntities = function(filter, count, page, sort, populate, callback) {
  // TODO change filter format to not directly do a DB call
  this.provider.getPaginatedFilteredEntities(filter, count, page, sort, callback);
};

/**
 * Adds a new entity.
 *
 * @method add
 * @async
 * @param {Object} data Entity data to store into the collection, its structure depends on the type of entity
 * @param {Function} callback The function to call when it's done
 *   - **Error** The error if an error occurred, null otherwise
 *   - **Number** The total amount of items inserted
 *   - **Object** The added entity
 */
EntityModel.prototype.add = function(data, callback) {
  data.id = data.id || shortid.generate();
  this.provider.add(data, function(error, insertCount, documents) {
    if (callback)
      callback(error, insertCount, documents[0]);
  });
};

/**
 * Updates an entity.
 *
 * @method update
 * @async
 * @param {String} id The id of the entity to update
 * @param {Object} data Entity data, its structure depends on the type of entity
 * @param {Function} callback The function to call when it's done
 *   - **Error** The error if an error occurred, null otherwise
 *   - **Number** The number of updated items
 */
EntityModel.prototype.update = function(id, data, callback) {
  this.provider.update(id, data, callback);
};

/**
 * Removes one or several entities.
 *
 * @method remove
 * @async
 * @param {String|Array} ids Id(s) of the document(s) to remove from the collection
 * @param {Function} callback The function to call when it's done
 *   - **Error** The error if an error occurred, null otherwise
 *   - **Number** The number of deleted entities
 */
EntityModel.prototype.remove = function(ids, callback) {
  this.provider.remove(ids, callback);
};