Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * dbHelper
- *
- * @module :: dbHelper
- * @description :: Contains helper about database.
- */
- module.exports = {
- /**
- * 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.
- * @param model {Object} Instance of a Database Model.
- * @param data {Object} Data to
- * @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.
- * @returns {model} Returns the model with fields updated.
- */
- merge: function(model, data, removeFields){
- removeFields = removeFields ? removeFields : false;
- // If we have fields to remove.
- if(removeFields && _.isArray(removeFields)){
- // Clean the object.
- for (var field in model){
- if(_.contains(removeFields, field)){
- delete model[field];
- }
- }
- }
- // Update values.
- for (var field in data){
- model[field] = data[field];
- }
- return model;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement