This repository has been archived by the owner on Mar 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
118 lines (103 loc) · 3.21 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
'use strict';
var _ = require('lodash');
module.exports = (function(collection, opts){
var Model = collection.prototype;
///////////////////
// Configuration //
///////////////////
/** Merge Default Options **/
var options = {
permitted: []
};
if(opts) _.assign(options, opts);
if(_.isString(options.permitted) && options.permitted.indexOf(' ') >= 0)
options.permitted = options.permitted.split(' ');
////////////////////
// Helper Methods //
////////////////////
/**
* Filter data with permitted params and apply overrides
*
* @param source {Object} Source object
* @param [override] {Object} Overrides to apply after filtering
*
* @returns {Object} Filtered object with overrides
*/
var params = function(source, override){
var filtered = _.pick(source, options.permitted);
if(_.isPlainObject(override)) _.assign(filtered, override);
return filtered;
};
//////////////////////
// Instance Methods //
//////////////////////
_.assign(Model.attributes, {
only: function(params){
if(arguments.length > 1) params = arguments;
return _.pick(this.toObject(), params);
},
except: function(params){
if(arguments.length > 1) params = arguments;
return _.omit(this.toObject(), params);
},
/**
* Filter assign to document
*
* @param source {Object} Source object
* @param [override] {Object} Overrides to apply after filtering
*
* @returns {this}
*/
assign: function(source, override){
var filtered = params(source, override);
return _.assign(this, filtered);
},
/**
* Filter assign to a copy of the document
*
* @param source {Object} Source object
* @param [override] {Object} Overrides to apply after filtering
*
* @returns {Document<this>} Copy of the document properties were assigned to
*/
merge: function(source, override){
var filtered = params(source, override);
return _.merge(this, filtered);
},
/**
* Perform an update with permitted params
*
* @param source {Object} Source object
* @param [override] {Object} Overrides to apply after filtering
* @param [done] {Function} Model.save callback
*
* @returns {Promise} The promise returned by Model.save
*/
safeUpdate: function(source, override, done){
return this.assign(source, override).save(_.isFunction(override) ? override : done);
}
});
////////////////////
// Static Methods //
////////////////////
_.assign(Model, {
only: _.pick,
except: _.omit,
/**
* Perform a create with permitted params
*
* @param docs {Array|Object} Source object(s)
* @param [override] {Object} Overrides to apply after filtering
* @param [done] {Function} Model.create callback
*
* @returns {Promise} Model.create promise
*/
safeCreate: function(docs, override, done){
if(!_.isArray(docs)) docs = [docs];
var filtered = _.map(docs, function(source){
return params(source, override);
});
return this.create(filtered, _.isFunction(override) ? override : done);
}
});
});