Advertisement
Vadorequest

dbHelper

Oct 6th, 2013
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * dbHelper
  3.  *
  4.  * @module      :: dbHelper
  5.  * @description :: Contains helper about database.
  6.  */
  7.  
  8. module.exports = {
  9.  
  10.     /**
  11.      * Merge attributes between an instance of a database model and an object of values. Useful before update a model or create one with dynamic fields.
  12.      * @param       model {Object} Instance of a Database Model.
  13.      * @param       data {Object} Data to
  14.      * @param       keepUnusedFields {Array} Array of string which are the key to delete before try to add. Useful for be sure to don't update some fields if they are not set.
  15.      * @returns     {model} Returns the model with fields updated.
  16.      */
  17.     merge: function(model, data, removeFields){
  18.         removeFields = removeFields ? removeFields : false;
  19.  
  20.         // If we have fields to remove.
  21.         if(removeFields && _.isArray(removeFields)){
  22.             // Clean the object.
  23.             for (var field in model){
  24.                 if(_.contains(removeFields, field)){
  25.                     delete model[field];
  26.                 }
  27.             }
  28.         }
  29.  
  30.         // Update values.
  31.         for (var field in data){
  32.             model[field] = data[field];
  33.         }
  34.  
  35.         return model;
  36.     }
  37. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement