mirror of
https://github.com/mmumshad/ansible-playable.git
synced 2025-03-09 23:38:54 +00:00
Initial Commit
This commit is contained in:
commit
c92f737237
273 changed files with 16964 additions and 0 deletions
31
server/components/utils/dbutility.js
Normal file
31
server/components/utils/dbutility.js
Normal file
|
@ -0,0 +1,31 @@
|
|||
var _ = require('underscore');
|
||||
|
||||
exports.updateDocument = function(doc, SchemaTarget, data) {
|
||||
for (var field in SchemaTarget.schema.paths) {
|
||||
if ((field !== '_id') && (field !== '__v')) {
|
||||
var newValue = getObjValue(field, data);
|
||||
if (newValue !== undefined) {
|
||||
setObjValue(field, doc, newValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
return doc;
|
||||
};
|
||||
|
||||
function getObjValue(field, data) {
|
||||
return _.reduce(field.split("."), function(obj, f) {
|
||||
if(obj) return obj[f];
|
||||
}, data);
|
||||
}
|
||||
|
||||
function setObjValue(field, data, value) {
|
||||
var fieldArr = field.split('.');
|
||||
return _.reduce(fieldArr, function(o, f, i) {
|
||||
if(i == fieldArr.length-1) {
|
||||
o[f] = value;
|
||||
} else {
|
||||
if(!o[f]) o[f] = {};
|
||||
}
|
||||
return o[f];
|
||||
}, data);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue