/
collection.extension.js
132 lines (108 loc) · 4.15 KB
/
collection.extension.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
import {Meteor} from 'meteor/meteor';
import {Mongo} from 'meteor/mongo';
import {RedisPipe, Events} from '../constants';
import getFields from '../utils/getFields';
import SmartObject from '../utils/SmartObject';
import { _ } from 'meteor/underscore';
import getMutationOptions from './lib/getMutationOptions';
import publish from './lib/publish';
import getRedisClient from '../redis/getRedisClient';
import extendObserveChanges from './extendObserveChanges';
const Originals = {
insert: Mongo.Collection.prototype.insert,
update: Mongo.Collection.prototype.update,
remove: Mongo.Collection.prototype.remove,
find: Mongo.Collection.prototype.find,
};
export default () => {
_.extend(Mongo.Collection.prototype, {
find(...args) {
var cursor = Originals.find.call(this, ...args);
extendObserveChanges(cursor, ...args);
return cursor;
},
/**
* @param data
* @param cb
* @param _config
* @returns {*}
*/
insert(data, cb, _config) {
let {callback, config} = getMutationOptions.call(this, cb, _config);
const result = Originals.insert.call(this, data, callback);
config.pushToRedis && Meteor.defer(() => {
const doc = this.findOne(result);
const client = getRedisClient();
publish(client, this._name, config.channels, {
[RedisPipe.EVENT]: Events.INSERT,
[RedisPipe.FIELDS]: _.keys((new SmartObject(doc)).getDotObject()),
[RedisPipe.DOC]: doc
})
});
return result;
},
/**
* @param selector
* @param modifier
* @param cb
* @param _config
* @returns {*}
*/
update(selector, modifier, cb, _config) {
let {callback, config} = getMutationOptions.call(this, cb, _config);
// TODO: optimization check if it has _id or _id with {$in} so we don't have to redo this.
let docIds = this.find(selector, {
fields: {_id: 1}
}).fetch().map(doc => doc._id);
const result = Originals.update.call(this, selector, modifier, config, callback);
config.pushToRedis && Meteor.defer(() => {
const fields = getFields(modifier);
let fieldsOptions = {};
_.each(fields, field => {
// do not allow operator projection fields
fieldsOptions[field] = 1
});
let docs = this.find({
_id: {
$in: docIds
}
}, {
fields: fieldsOptions
}).fetch();
const client = getRedisClient();
docs.forEach(doc => {
publish(client, this._name, config.channels, {
[RedisPipe.EVENT]: Events.UPDATE,
[RedisPipe.FIELDS]: fields,
[RedisPipe.DOC]: doc
}, doc._id);
})
});
return result;
},
/**
* @param selector
* @param cb
* @param _config
* @returns {*}
*/
remove(selector, cb, _config) {
let {callback, config} = getMutationOptions.call(this, cb, _config);
// TODO: optimization check if it has _id or _id with {$in} so we don't have to redo this.
let docIds = this.find(selector, {
fields: {_id: 1}
}).fetch().map(doc => doc._id);
const result = Originals.remove.call(this, selector, callback);
config.pushToRedis && Meteor.defer(() => {
const client = getRedisClient();
docIds.forEach((docId) => {
publish(client, this._name, config.channels, {
[RedisPipe.EVENT]: Events.REMOVE,
[RedisPipe.DOC]: {_id: docId},
}, docId);
})
});
return result;
}
});
}