-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.js
More file actions
403 lines (373 loc) · 13 KB
/
graph.js
File metadata and controls
403 lines (373 loc) · 13 KB
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import { Graph as AncientGraph } from 'ancient-graph';
/**
* This method allows you to use MeteorGraph class to its inheritance chain.
*
* @param {Class} ParentClassGraph
* @return {Class} MeteorGraph
*/
function factoryMeteorGraph(ParentClassGraph) {
/**
* Class to inherit. Class with methods for control links in graph.
* Adapted for Meteor Minimongo.
*
* @class
* @description `import { Graph } from 'meteor/ancient-graph';`
*/
class MeteorGraph extends ParentClassGraph {
/**
* Construct new graph and checks for required adaptation methods.
* @param {Mongo.Collection} collection
* @param {Object.<string, string>} fields - matching of fields in the link with fields in document
* @param {Object} [config] - Additional config.
* @throws {Error} if the adapter methods is not complete
*/
constructor(collection, fields, config) {
super(...arguments);
}
/**
* Should insert new link into graph.
* Return a synchronous result. This can be useful in your application. But for writing generic code, it is recommended to only use the callback result.
*
* @param {Link} link
* @param {Graph~insertCallback} [callback]
* @return {string} [id]
*/
insert(link, callback) {
var _modifier = {};
for (var f in link) {
if (this.fields[this.config._aliases[f]]) {
_modifier[this.fields[this.config._aliases[f]]] = link[f];
}
}
return this.collection.insert(_modifier, callback);
}
/**
* Optional callback. If present, called with an error object as the first argument and, if no error, the unique id of inserted link as the second.
*
* @callback Graph~insertCallback
* @param {Error} [error]
* @param {string} [id]
*/
/**
* Should update to new state of modifier object link by unique id or by link query object.
* If the database allows, it is recommended to return a synchronous result. This can be useful in your application. But for writing generic code, it is recommended to only use the callback result.
*
* @param {string|LinkSelector} selector
* @param {LinkModifier} modifier
* @param {Graph~updateCallback} [callback]
* @return {number} [count]
*/
update(selector, modifier, callback) {
var _selector = this.query(selector);
var _modifier = {};
for (var m in modifier) {
if (this.fields[this.config._aliases[m]]) {
if (typeof(modifier[m]) == 'undefined') {
if (!_modifier.$unset) _modifier.$unset = {};
_modifier.$unset[this.fields[this.config._aliases[m]]] = '';
} else {
if (typeof(modifier[m]) == 'object') {
if (Object.prototype.toString.call(modifier[m]) === '[object Array]') {
if (!_modifier.$set) _modifier.$set = {};
_modifier.$set[this.fields[this.config._aliases[m]]] = modifier[m];
} else {
for (var key in modifier[m]) {
if (key == 'add') {
if (!_modifier.$addToSet) _modifier.$addToSet = {};
if (typeof(modifier[m][key]) == 'object') {
_modifier.$addToSet[this.fields[this.config._aliases[m]]] = { $each: modifier[m][key] };
} else {
_modifier.$addToSet[this.fields[this.config._aliases[m]]] = { $each: [modifier[m][key]] };
}
}
if (key == 'push') {
if (!_modifier.$push) _modifier.$push = {};
if (typeof(modifier[m][key]) == 'object') {
_modifier.$push[this.fields[this.config._aliases[m]]] = { $each: modifier[m][key] };
} else {
_modifier.$push[this.fields[this.config._aliases[m]]] = { $each: [modifier[m][key]] };
}
}
if (key == 'remove') {
if (!_modifier.$pullAll) _modifier.$pullAll = {};
if (typeof(modifier[m][key]) == 'object') {
_modifier.$pullAll[this.fields[this.config._aliases[m]]] = modifier[m][key];
} else {
_modifier.$pullAll[this.fields[this.config._aliases[m]]] = [modifier[m][key]];
}
}
}
}
} else {
if (!_modifier.$set) _modifier.$set = {};
_modifier.$set[this.fields[this.config._aliases[m]]] = modifier[m];
}
}
}
}
this.collection.update(_selector, _modifier, { multi: true }, callback);
}
/**
* Optional callback. If present, called with an error object as the first argument and, if no error, the number of affected documents as the second.
*
* @callback Graph~updateCallback
* @param {Error} [error]
* @param {number} [count]
*/
/**
* Should remove link by unique id or by link query object.
*
* @param {string|LinkSelector} selector
* @param {Graph~removeCallback} [callback]
*/
remove(selector, callback) {
var _selector = this.query(selector);
this.collection.remove(_selector, callback);
}
/**
* Optional callback. If present, called with an error object as the first argument.
*
* @callback Graph~removeCallback
* @param {Error} [error]
* @param {number} [count]
*/
/**
* Generate adapter for database query for links search by unique id or by link query object.
*
* @param {string|LinkSelector} selector
* @return {*} query
*/
query(selector) {
var type = typeof(selector);
if (type == 'string' || type == 'number') {
return { [this.fields[this.config.aliases['id']]]: selector };
} else if (type == 'object') {
var _selector = {};
for (var m in selector) {
if (this.fields[this.config._aliases[m]]) {
if (typeof(selector[m]) == 'undefined') {
_selector[this.fields[this.config._aliases[m]]] = { $exists: false };
} else {
if (Object.prototype.toString.call(selector[m]) === '[object Array]') {
_selector[this.fields[this.config._aliases[m]]] = { $in: selector[m] };
} else {
_selector[this.fields[this.config._aliases[m]]] = selector[m];
}
}
}
}
return _selector;
}
}
/**
* Generate adapted for database options object.
*
* @param {Object} [options]
* @return {*} options - a options suitable for the database
*/
options(options) {
var _options = {};
if (options) {
if (options.sort) {
_options.sort = options.sort;
for (var s in _options.sort) {
if (this.fields[this.config._aliases[s]]) {
_options.sort[this.fields[this.config._aliases[s]]] = options.sort[s]?1:-1;
}
}
}
if (typeof(options.skip) == 'number') {
_options.skip = options.skip;
}
if (typeof(options.limit) == 'number') {
_options.limit = options.limit;
}
}
return _options;
}
/**
* Generate Link from document by fields.
*
* @param {Object} document
* @return {Link} link
*/
_generateLink(document) {
var link = {};
for (var f in this.fields) {
if (document.hasOwnProperty(this.fields[f])) {
link[this.config.aliases[f]] = document[this.fields[f]];
}
}
return link;
}
/**
* Get one first matching link.
*
* @param {string|LinkSelector} selector
* @param {SelectOptions} [options]
* @param {Graph~getCallback} [callback]
* @return {Link} link - result link object
*/
get(selector, options, callback) {
var _selector = this.query(selector);
var _options = this.options(options);
var result = this.collection.findOne(_selector, _options);
if (callback) callback(undefined, result?this._generateLink(result):undefined);
return result;
}
/**
* Optional callback. If present, called with an error object as the first argument and, if no error, the result link object.
*
* @callback Graph~getCallback
* @param {Error} [error]
* @param {Link} [link]
*/
/**
* Fetch native database documents.
*
* @param {string|linkSelector} selector
* @param {SelectOptions} [options]
* @return {Object[]} documents - result documents
*/
_find(selector, options) {
var _selector = this.query(selector);
var _options = this.options(options);
return this.collection.find(_selector);
}
/**
* Find and all matching links as an Array.
*
* @param {string|LinkSelector} selector
* @param {SelectOptions} [options]
* @param {Graph~fetchCallback} [callback]
* @return {Link[]} links - result links objects in array
*/
fetch(selector, options, callback) {
var documents = this._find(selector, options).fetch();
var links = [];
for (var d in documents) {
links.push(this._generateLink(documents[d]));
}
if (callback) callback(undefined, links);
return links;
}
/**
* Optional callback. If present, called with an error object as the first argument and, if no error, the result links objects in array.
*
* @callback Graph~fetchCallback
* @param {Error} [error]
* @param {Link[]} [links]
*/
/**
* Should call callback once for each matching document, sequentially and synchronously.
*
* @param {string|LinkSelector} selector
* @param {SelectOptions} [options]
* @param {Graph~eachCallback} [callback]
*/
each(selector, options, callback) {
this._find(selector, options).each(function(document) {
callback(this._generateLink(document));
});
}
/**
* @callback Graph~eachCallback
* @param {Link} [link]
*/
/**
* Should map callback over all matching documents. Returns an Array.
*
* @param {string|LinkSelector} selector
* @param {SelectOptions} [options]
* @param {Graph~mapCallback} [callback]
* @return {Array} results
*/
map(selector, options, callback) {
return this._find(selector, options).map(function(document) {
return callback(this._generateLink(document));
});
}
/**
* @callback Graph~mapCallback
* @param {Link} [link]
* @return {*} result
*/
/**
* Count all matching documents.
*
* @param {string|LinkSelector} selector
* @param {SelectOptions} [options]
* @param {Graph~countCallback} [callback]
* @return {number} [count]
*/
count(selector, options, callback) {
var count = this._find(selector, options).count();
if (callback) callback(undefined, count);
return count;
}
/**
* @callback Graph~countCallback
* @param {Error} [error]
* @param {number} [count]
*/
/**
* Should subscribe to the events: link, unlink, insert, update, remove.
*
* @param {string} event - name
* @param {Graph-onCallback} callback
*/
on(event, callback) {
var graph = this;
if (event == 'insert') {
this.collection.after.insert(function(userId, document) {
callback(undefined, graph._generateLink(document), { userId: userId });
});
}
if (event == 'update') {
this.collection.after.update(function(userId, doc, fieldNames, modifier, options) {
var hasFields = false;
for (var f in graph.fields) {
if (includes(fieldNames, graph.fields[f])) {
hasFields = true;
break;
}
}
if (hasFields) {
callback(graph._generateLink(this.previous), graph._generateLink(doc), { userId: userId });
}
});
}
if (event == 'remove') {
this.collection.after.remove(function(userId, document) {
callback(graph._generateLink(document), undefined, { userId: userId });
});
}
if (event == 'link') {
this.on('insert', callback);
this.on('update', callback);
}
if (event == 'unlink') {
this.on('update', callback);
this.on('remove', callback);
}
}
/**
* @callback Graph~onCallback
* @param {Link} [oldLink] - can be undefined on link and insert events
* @param {Link} [newLink] - can be undefined on unlink and remove events
* @param {Object} [context] - additional app information, such as context.userId
*/
};
return MeteorGraph;
}
function includes(a, obj) {
var i = a.length;
while (i--) {
if (a[i] === obj) {
return true;
}
}
return false;
}
var MeteorGraph = factoryMeteorGraph(AncientGraph);
export { factoryMeteorGraph, MeteorGraph as Graph };