-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
58 lines (53 loc) · 1.52 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
var _ = require('underscore');
/**
* gets the value of objects based on text (dot syntax)
*
* @param {String} field - dictionary key
* @param {Object} data - dictionary object
**/
var getObjValue = function(field, data) {
return _.reduce(field.split("."), function(obj, f) {
if(obj) return obj[f];
}, data);
};
/**
* sets the value of objects based on text (dot syntax)
*
* @param {String} field - key for data object
* @param {Object} data - mongoose data object
* @param {Object|String|Number} value - value for key
**/
var setObjValue = function(data, field, 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);
};
/**
* A Mongoose plugin method to update documents-
* only updates the fields that are contained within the object passed in the parameter
*
* @param {Object} data - object containing fields with updated values
* @param {Function} done - callback object after save is complete
**/
var updateDocument = function(data, done) {
var doc = this;
for(var field in doc.constructor.schema.paths) {
if((field !== '_id') && (field !== '__v')) {
var new_value = getObjValue(field, data);
if(new_value !== undefined) {
setObjValue(doc, field, new_value);
}
}
};
if(done) return doc.save(done);
else return doc;
};
module.exports = function(schema) {
schema.methods.leanUpdate = updateDocument;
};