-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.js
62 lines (54 loc) · 1.78 KB
/
update.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
var query = require('mongo-query');
module.exports.wrapClass = wrapClass;
module.exports.wrapObject = wrapObject;
function wrapClass(type, updateMethod, initDeferred) {
wrapObject(type.prototype, updateMethod);
return type;
}
function wrapObject(type, updateMethod, initDeferred) {
type.$set = op($set);
type.$unset = op(op($unset));
type.$inc = op($inc);
type.$pop = op($pop);
type.$push = op($push);
type.$pull = op($pull);
type.$addToSet = op($addToSet);
type.$pushAll = op($pushAll);
type.$pullAll = op($pullAll);
type.$getChanges = getChanges;
type.$defer = defer;
type.$undefer = undefer;
type.$update = update;
Object.defineProperty(type, '$changes', {value: [], enumerable: false, configurable: true, writable: true});
Object.defineProperty(type, '$deferred', {value: initDeferred, enumerable: false, configurable: true, writable: true});
return type;
function op(_op) {
return function (key, value) {
var qq = pair(_op, (typeof (key) == 'object') ? key : pair(key, value));
this.$changes.push(query(this, qq));
process.nextTick(doUpdate.bind(this));
};
}
function undefer() {
this.$deferred = false;
process.nextTick(doUpdate.bind(this));
}
function doUpdate() {
if (this.$changes.length && !this.$deferred) this.$update();
}
function defer() { this.$deferred = arguments.length ? arguments[0] : true; }
function update() {
var changes = this.$changes;
this.$changes = [];
if (!changes.length) return;
if (typeof(updateMethod) == 'string') return this[updateMethod](getUpdateQueryFromChanges(changes));
return method(this, getUpdateQueryFromChanges(changes));
}
}
function getUpdateQueryFromChanges(changes) {
}
function pair(key, value) {
var ret = {};
ret[key] = value;
return ret;
}