/
Instance.js
294 lines (232 loc) · 8.68 KB
/
Instance.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/// <reference path="../nodelib/node.js"/>
/// <reference path="utils/transforms.js"/>
/// <reference path="utils/validation.js"/>
var ObjectID = require('mongodb').ObjectID;
var _ = require('lodash');
var validate = require('./utils/validation');
var transform = require('./utils/transforms');
(require.modules || {}).Instance = module.exports = Instance;
function Instance(collection, doc, options, isNew) {
/// <signature>
/// <summary>Creates a new wrapper around a database document</summary>
/// <param name="collection" type="Object">The database collection from which the document originated</param>
/// <param name="doc" type="Object">The document from the database which is to be wrapped</param>
/// <param name="options" type="Object">Additional options used to determine how the instance is handled</param>
/// </signature>
/// <signature>
/// <summary>Creates a new wrapper around a database document</summary>
/// <param name="collection" type="Object">The database collection from which the document originated</param>
/// <param name="doc" type="Object">The document from the database which is to be wrapped</param>
/// <param name="options" type="Object">Additional options used to determine how the instance is handled</param>
/// <param name="isNew" type="Boolean>Should be true if this instance is not present in the database</param>
/// </signature>
"use strict";
var $ = this;
options = options || {};
_.defaults(options, {
rename: {},
methods: {},
virtuals: {},
schema: {},
transforms: {}
});
if (!isNew) transform.down(options.transforms, doc);
var oldDoc = _.cloneDeep(doc);
var newDoc = _.cloneDeep(doc);
var k;
Object.defineProperty($, 'id', {
get: function () {
/// <value type="Object">The document's unique database identifier field</value>
return oldDoc._id;
},
enumerable: true
});
var schema = {};
for (k in doc) {
schema[k] = false;
}
for (k in options.schema) {
schema[k] = options.schema[k];
}
for (k in schema) {
var targetProperty = k;
if (options.rename && options.rename[k])
targetProperty = options.rename[k];
if (targetProperty === '_id' || targetProperty === "id") continue;
(function (sourceProperty) {
if (sourceProperty === '_id' || sourceProperty === 'id')
Object.defineProperty($, targetProperty, {
get: function () {
return newDoc['_id'];
},
enumerable: true
});
else
Object.defineProperty($, targetProperty, {
get: function () {
/// <value type="Object">Get the most recent value for this field</value>
return newDoc[sourceProperty] === undefined ? null : newDoc[sourceProperty];
},
set: function (value) {
/// <value type="Object">Set the value of this field. Changes may be committed by calling save() on this instance.</value>
var validation = validate(options.schema[sourceProperty], value, sourceProperty);
if (!validation.passed) throw validation.toError();
newDoc[sourceProperty] = value;
},
enumerable: true
});
})(k);
}
for (k in options.methods) {
if (k === '_id' || k === "id") continue;
(function (sourceProperty) {
Object.defineProperty($, sourceProperty, {
value: function () {
return options.methods[sourceProperty].apply($, arguments);
}
});
})(k);
}
for (k in options.virtuals) {
if (k === '_id' || k === "id") continue;
(function (sourceProperty) {
Object.defineProperty($, sourceProperty, {
get: function () {
return options.virtuals[sourceProperty].call($);
}
});
})(k);
}
Object.defineProperty($, 'save', {
value: function (changes, cb) {
/// <signature>
/// <summary>Saves changes made to the current instance to the database without waiting for a response</summary>
/// </signature>
/// <signature>
/// <summary>Saves changes made to the current instance to the database without waiting for a response</summary>
/// <param name="changes" type="Object">MongoDB changes query to be used instead of differential patching</param>
/// </signature>
/// <signature>
/// <summary>Saves changes made to the current instance to the database</summary>
/// <param name="cb" type="Function">A function which is called when the save has been completed</param>
/// </signature>
/// <signature>
/// <summary>Saves changes made to the current instance to the database</summary>
/// <param name="changes" type="Object">MongoDB changes query to be used instead of differential patching</param>
/// <param name="cb" type="Function">A function which is called when the save has been completed</param>
/// </signature>
if (!cb && typeof changes === 'function') {
cb = changes;
changes = null;
}
if (!cb) cb = function () { };
var createObject = function (callback) {
var createDoc = _.cloneDeep(newDoc);
transform.up(options.transforms, createDoc);
collection.insert(createDoc, { w: 1 }, function (err, inserted) {
if (err) return callback(err);
inserted = inserted[0];
transform.down(options.transforms, inserted);
oldDoc = newDoc = inserted;
isNew = false;
return callback(null, $);
});
};
if (!changes) {
var transformedOld = _.cloneDeep(oldDoc);
var transformedNew = _.cloneDeep(newDoc);
transform.up(options.transforms, transformedOld);
transform.up(options.transforms, transformedNew);
changes = diffPatch(transformedOld, transformedNew);
}
if (!!isNew || Object.keys(changes).length > 0) {
var runOptions = function () {
var conditions = { _id: $.id };
transform.up(options.transforms, conditions);
collection.update(conditions, changes, { w: 1 }, function (err, updateCount) {
if (err) return cb(err);
if (updateCount > 0) {
collection.findOne(conditions, function (err, updated) {
if (err) return cb(err);
transform.down(options.transforms, updated);
oldDoc = newDoc = updated;
return cb(null, $);
});
} else return cb(null, $);
});
};
if (oldDoc._id && !isNew) return runOptions();
else createObject(function (err) {
if (err) return cb(err);
if (Object.keys(changes).length > 0) runOptions(cb);
else return cb(null, $);
});
}
else return cb(null, $);
},
enumerable: false
});
Object.defineProperty($, 'update', {
value: function (callback) {
/// <signature>
/// <summary>Updates this object from the database, bringing it up to date</summary>
/// </signature>
/// <signature>
/// <summary>Updates this object from the database, bringing it up to date</summary>
/// <param name="callback" type="Function">A function to be called once the update is complete</param>
/// </signature>
var conditions = { _id: $.id };
transform.up(options.transforms, conditions);
collection.findOne(conditions, function (err, updated) {
if (err) return cb(err);
transform.down(options.transforms, updated);
oldDoc = newDoc = updated;
if(callback) return callback(null, $);
});
}
});
Object.defineProperty($, 'remove', {
value: function (callback) {
/// <summary>Removes this object from the database collection</summary>
/// <param name="callback" type="Function">A function to be called when the object has been removed</param>
if (oldDoc._id) {
var conditions = { _id: $.id };
transform.up(options.transforms, conditions);
collection.remove(conditions, callback);
} else {
callback(null, 0); // No objects removed from the database
}
},
enumerable: false
});
}
var diffPatch = Instance.diff = function (oldDoc, newDoc, path) {
/// <signature>
/// <summary>Creates a differential update query for use by MongoDB</summary>
/// <param name="oldDoc" type="Object">The original document prior to any changes</param>
/// <param name="newDoc" type="Object">The document containing the changes made to the original document</param>
/// </signature>
"use strict";
var changes = {};
for (var k in newDoc) {
if (Array.isArray(newDoc[k]) && Array.isArray(oldDoc[k])) {
var different = newDoc.length !== oldDoc.length;
for (var i = 0; i < newDoc[k].length && !different; i++) {
if (oldDoc[k][i] !== newDoc[k][i]) different = true;
}
if (!different) continue;
changes.$set = changes.$set || {};
changes.$set[(path ? (path + '.') : '') + k] = newDoc[k];
}
else if (_.isPlainObject(newDoc[k]) && _.isPlainObject(oldDoc[k])) {
// Make recursive diff update
_.merge(changes, diffPatch(oldDoc[k], newDoc[k], (path ? (path + '.') : '') + k));
}
else {
if (oldDoc[k] === newDoc[k]) continue;
changes.$set = changes.$set || {};
changes.$set[(path ? (path + '.') : '') + k] = newDoc[k];
}
}
return changes;
};