-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdenormalized-views.js
547 lines (492 loc) · 20.6 KB
/
denormalized-views.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
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/* eslint-disable no-underscore-dangle */
/**
* Denormalization
*/
import { _ } from 'underscore'
import { check, Match } from 'meteor/check'
import { Meteor } from 'meteor/meteor'
import { Mongo } from 'meteor/mongo'
import { SimpleSchema } from 'meteor/aldeed:simple-schema'
import { debug } from './tools.js'
// ERRORS (export needed for tests)
export const ERROR_IDENTIFIERT_EXISTS = 'identifier already exists'
export const ERROR_SOURCE_AND_TARGET_COLLECTIONS_NEED_TO_BE_DIFFERENT = 'sourceCollection and viewCollection need to refer to different collections'
export const ERROR_SYNC_NEEDS_TO_HAVE_CONTENT = 'sync needs to have properties attached'
export const ERROR_SYNC_ALREADY_EXISTS_FOR_SOURCE_TARGET_COLLECTIONS = 'a sync already exists for the given sourceCollection and viewCollection'
export const ERROR_REFRESH_BY_COLLECTION_CAN_NOT_BE_SET_TO_SOURCE_COLLECTION = 'relatedCollection can NOT be set to sourceCollection or viewCollection. It is meant to be registered to a related collection.'
export const ERROR_REFRESH_BY_COLLECTION_NEEDS_TO_BE_ASSIGNED_TO_AN_EXISTING_ID = 'identifier in refreshByCollection() needs to be a registered syncronisation. It has to be registered before via addView()'
// Storage for ALL system-wide syncronisations
export const SyncronisationStore = []
// ===========================================================
// DENORMALIZED-VIEWS CLASS
// ===========================================================
export const DenormalizedViews = class DenormalizedViews {
// ================================================
// PUBLIC API (to be used from outside)
static addView(options = {}) {
new SimpleSchema({
identifier: { type: String },
sourceCollection: { type: Mongo.Collection },
viewCollection: { type: Mongo.Collection },
filter: { type: Function, blackbox: true, optional: true },
postHook: { type: Function, blackbox: true, optional: true },
pick: { type: [String], optional: true },
postSync: { type: Object, blackbox: true, optional: true },
sync: { type: Object, blackbox: true },
}).validate(options)
const { identifier, sourceCollection, viewCollection, sync } = options
// Validate options
// validate that identifier is NOT yet registered
if (_.contains(_.pluck(SyncronisationStore, 'identifier'), identifier)) {
throw new Meteor.Error(`${ERROR_IDENTIFIERT_EXISTS}: ${identifier}`)
}
// validate that collections are NOT the same
if (sourceCollection===viewCollection) {
throw new Meteor.Error(ERROR_SOURCE_AND_TARGET_COLLECTIONS_NEED_TO_BE_DIFFERENT)
}
if (_.isEmpty(sync)) {
throw new Meteor.Error(ERROR_SYNC_NEEDS_TO_HAVE_CONTENT)
}
if (_.find(SyncronisationStore,
store => (store.sourceCollection===sourceCollection
&& store.viewCollection===viewCollection))) {
throw new Meteor.Error(ERROR_SYNC_ALREADY_EXISTS_FOR_SOURCE_TARGET_COLLECTIONS)
}
// is valid? Register it
debug(`addView from sourceCollection "${sourceCollection._name}" to "${viewCollection._name}"`)
SyncronisationStore.push(options)
// register hooks to sourceCollection
// those hooks wil sync to viewCollection
sourceCollection.after.insert(function (userId, doc) { // eslint-disable-line prefer-arrow-callback
debug(`${sourceCollection._name}.after.insert`)
// Filter?
if (DenormalizedViews._isDocValidToBeProcessed({ doc, userId, syncronisation: options })) {
// fix for insert-hook
// doc._id = doc._id.insertedIds[0]
const processedDoc = DenormalizedViews._processDoc({
doc,
userId,
syncronisation: options,
})
DenormalizedViews._executeDatabaseComand(() => {
debug(`inserting doc with id ${processedDoc._id}`)
viewCollection.insert(processedDoc)
})
DenormalizedViews._callPostHookIfExists({ doc, userId, postHook: options.postHook })
} else {
// filter OUT doc, if it exists
DenormalizedViews._removeDocFromViewCollectionIfExists({ doc, viewCollection: options.viewCollection })
}
})
sourceCollection.after.update(function (userId, doc) { // eslint-disable-line prefer-arrow-callback
debug(`${sourceCollection._name}.after.update`)
// Filter?
if (DenormalizedViews._isDocValidToBeProcessed({ doc, userId, syncronisation: options })) {
const processedDoc = DenormalizedViews._processDoc({
doc,
userId,
syncronisation: options,
})
DenormalizedViews._executeDatabaseComand(() => {
debug(`updating doc with id ${processedDoc._id}`)
viewCollection.update(processedDoc._id, { $set: processedDoc }, {
upsert: true, // important: it might be that doc has passed the filter AFTER an update
// and did NOT exist yet in "view"-collection, p.e. because on "insert"
// it did NOT pass the filter. So let's upsert
})
})
DenormalizedViews._callPostHookIfExists({ doc, userId, postHook: options.postHook })
} else {
// filter OUT doc, if it exists
DenormalizedViews._removeDocFromViewCollectionIfExists({ doc, viewCollection: options.viewCollection })
}
})
sourceCollection.after.remove(function (userId, doc) { // eslint-disable-line prefer-arrow-callback
debug(`${sourceCollection._name}.after.remove`)
DenormalizedViews._executeDatabaseComand(() => {
debug(`removing doc with id ${doc._id}`)
viewCollection.remove(doc._id)
})
DenormalizedViews._callPostHookIfExists({ doc, userId, postHook: options.postHook })
})
}
/**
* Get a reference to an existing view, identified by an identifier.
* You can use this in unit-tests to stub certain behaviour.
*/
static getView(identifier) {
check(identifier, String)
return DenormalizedViews._getExistingSyncronisation({ identifier })
}
static refreshByCollection(options = {}) {
new SimpleSchema({
identifier: { type: String },
relatedCollection: { type: Mongo.Collection },
refreshIds: { type: Function },
}).validate(options)
const { identifier, relatedCollection, refreshIds } = options
// Validate
const existingSyncronisation = DenormalizedViews._getExistingSyncronisation({ identifier })
// validate that we have a valid identifier
if (!existingSyncronisation) {
throw new Meteor.Error(ERROR_REFRESH_BY_COLLECTION_NEEDS_TO_BE_ASSIGNED_TO_AN_EXISTING_ID)
}
// validate that we have a valid collection assigned
if (existingSyncronisation.sourceCollection===relatedCollection
|| existingSyncronisation.viewCollection===relatedCollection) {
throw new Meteor.Error(ERROR_REFRESH_BY_COLLECTION_CAN_NOT_BE_SET_TO_SOURCE_COLLECTION)
}
debug(`setup refreshByCollection for identifier "${identifier}" and relatedCollection "${relatedCollection._name}"`)
relatedCollection.after.insert(function (userId, doc) { // eslint-disable-line prefer-arrow-callback
debug(`relatedCollection ${relatedCollection._name}.after.insert`)
// doc._id = doc._id.insertedIds[0] // fix for insert-hook
const ids = DenormalizedViews._validateAndCallRefreshIds({ doc, refreshIds, userId })
if (ids && ids.length>0) {
DenormalizedViews._updateIds({
identifier,
idsToRefresh: ids,
})
} else {
debug('no _ids received from refreshIds-function. So NO docs will be updated in "view"-collection')
}
})
relatedCollection.after.update(function (userId, doc) { // eslint-disable-line prefer-arrow-callback
debug(`relatedCollection ${relatedCollection._name}.after.update`)
const ids = DenormalizedViews._validateAndCallRefreshIds({
doc,
refreshIds,
userId,
docPrevious: this.previous, // the caller is gonna need that to find the correct ids
})
if (ids && ids.length>0) {
DenormalizedViews._updateIds({
identifier,
idsToRefresh: ids,
})
} else {
debug('no _ids received from refreshIds-function. So NO docs will be updated in "view"-collection')
}
})
// REMOVE hook
// our aim is to always UPDATE the "view"-collection. P.e. if Author changes
// his name or gets deleted than the "view"-collection needs to refresh.
// Of course in this case the App itself would have to make sure that
// before authorId is removed from sourceCollection
relatedCollection.after.remove(function (userId, doc) { // eslint-disable-line prefer-arrow-callback
debug(`relatedCollection ${relatedCollection._name}.after.remove`)
const ids = DenormalizedViews._validateAndCallRefreshIds({ doc, refreshIds, userId })
if (ids && ids.length>0) {
DenormalizedViews._updateIds({
identifier,
idsToRefresh: ids,
})
} else {
debug('no _ids received from refreshIds-function. So NO docs will be updated in "view"-collection')
}
})
}
/**
* Manually refresh a set of Ids in an syncronisation
* for an given identifier.
*
* Use this in your App at places where a manual refresh is needed.
*
* @param {Object} options [description]
* @return {[type]} [description]
*/
static refreshManually(options = {}) {
new SimpleSchema({
identifier: { type: String },
refreshIds: { type: [String] },
}).validate(options)
const { identifier, refreshIds } = options
debug(`refreshManually for identifier ${identifier} and ids:`, refreshIds)
if (refreshIds && refreshIds.length>0) {
DenormalizedViews._updateIds({
identifier,
idsToRefresh: refreshIds,
})
}
}
/**
* Do a TOTAL refresh of the target-collection,
* meaning that ALL elements will get reloaded.
*
* In big collections this can be very time-intense. So be careful with timeouts.
*
* @param {[type]} identifier [description]
* @return {[type]} [description]
*/
static refreshAll(identifier) {
check(identifier, String)
const existingSyncronisation = DenormalizedViews._getExistingSyncronisation({ identifier })
debug(`refreshAll for collection "${existingSyncronisation.sourceCollection._name}"`)
DenormalizedViews._executeDatabaseComand(() => {
existingSyncronisation.viewCollection.remove({})
})
let ids = existingSyncronisation.sourceCollection.find({}, { fields: { _id: 1 } }).fetch()
ids = _.pluck(ids, '_id')
for (const id of ids) {
let doc = existingSyncronisation.sourceCollection.findOne(id) // todo check if we better use data from fetch above
const userId = undefined // TODO: add `Meteor.userId()`. we had problems getting it to work (adding "meteor-base" to packages did not help)
// Filter?
if (DenormalizedViews._isDocValidToBeProcessed({ doc, userId, syncronisation: existingSyncronisation })) {
debug(`refreshAll refreshing doc._id ${id}`)
doc = DenormalizedViews._processDoc({
doc,
syncronisation: existingSyncronisation,
})
DenormalizedViews._executeDatabaseComand(() => {
existingSyncronisation.viewCollection.insert(doc)
})
DenormalizedViews._callPostHookIfExists({ doc, userId: undefined, postHook: existingSyncronisation.postHook })
} else {
debug(`refreshAll: filtered out doc._id ${id}`)
}
}
debug(`${ids.length} docs in cache ${existingSyncronisation.viewCollection._name} were refreshed`)
}
/**
* Check, if the doc of the source-collection valid to be processed.
* If NO filter exists it is always valid.
* If a filter exists, the doc is considered valid, if the filter returns true.
* @return (Boolean) true if doc shall be further processed
* @return (Boolean) false if doc shall be filtered out
*/
static _isDocValidToBeProcessed(options={}) {
const { syncronisation, userId } = options
const { filter } = syncronisation
const doc = options.doc
let returnValue = true
if (!_.isUndefined(filter) && _.isFunction(filter)) {
const filterResult = filter.call(this, doc, userId)
if (!_.isUndefined(filterResult) && _.isBoolean(filterResult) && filterResult===false) {
debug(`doc with _id ${doc._id} was filtered out. NO doc was created in "view"-collection`)
returnValue = false // do NOT process
}
}
return returnValue
}
/**
* Call the `postHook` function, if it exists
*/
static _callPostHookIfExists(options={}) {
const { doc, userId, postHook } = options
if (!_.isUndefined(postHook) && _.isFunction(postHook)) {
postHook.call(this, doc, userId)
}
}
/**
* Process a given doc by a given syncronisation-setting
* and add "sync"- and "postSync" options.
*
* @param {Object} options [description]
* @return {Object} doc that contains the collected data
*/
static _processDoc(options = {}) {
const { syncronisation, userId } = options
const { viewCollection, sync, postSync, pick } = syncronisation
let doc = options.doc
// validate options
// we cannot use SimpleSchema-validation here,
// because we want to support use of superclasses
// for docs in collection.
if (!_.isObject(doc)) {
throw new Meteor.Error('options.doc needs to be an Object')
}
check(syncronisation, Object)
check(userId, Match.Maybe(String))
// Loop each property set in "sync"
// and assign its return-value to the doc
for (const property of Object.getOwnPropertyNames(sync)) {
const propertyFunction = sync[property]
if (!_.isFunction(propertyFunction)) {
throw new Meteor.Error(`sync.${property} needs to be a function`)
}
// call the function
// and assign its result to the object
const result = propertyFunction.call(this, doc, userId)
// if there is a valid result: assign it to doc
if (result || result===[] || result===0) {
doc[property] = result
} else {
delete doc[property]
// we are using a $set operation which will ADD,
// but NOT remove a property. So in this special case
// we need to do an extra write to the db and unset
DenormalizedViews._unsetProperty({ property, _id: doc._id, collection: viewCollection })
}
}
// Loop each property set in "postSync"
// and assign its return-value to the doc
if (postSync) {
for (const property of Object.getOwnPropertyNames(postSync)) {
const propertyFunction = postSync[property]
if (!_.isFunction(propertyFunction)) {
throw new Meteor.Error(`postSync.${property} needs to be a function`)
}
// call the function
// and assign its result to the object
const result = propertyFunction.call(this, doc, userId)
// if there is a valid result: assign it to doc
if (result || result===[] || result===0) {
doc[property] = result
} else {
delete doc[property]
// we are using a $set operation which will ADD,
// but NOT remove a property. So in this special case
// we need to do an extra write to the db and unset
DenormalizedViews._unsetProperty({ property, _id: doc._id, collection: viewCollection })
}
}
}
// pick
if (pick) {
doc = _.pick(doc, _.union(
pick,
Object.getOwnPropertyNames(sync),
Object.getOwnPropertyNames(postSync),
))
}
return doc
}
/**
* Remove doc from view-collection
*/
static _removeDocFromViewCollectionIfExists(options={}) {
const { doc, viewCollection } = options
DenormalizedViews._executeDatabaseComand(() => {
const nrOfUpdates = viewCollection.remove(doc._id)
if (nrOfUpdates>0) {
debug(`Removed doc ${doc._id} from view-collection, because it was filtered out`)
}
})
}
/**
* Refreshes (=updates) the given ids in a syncronisation.
*
* To be used within insert- and update-hooks.
* In remove hooks: use ``_removeIds```
*
* @param {Object} options [description]
* @return {[type]} [description]
*/
static _updateIds(options = {}) {
new SimpleSchema({
identifier: { type: String },
idsToRefresh: { type: [String] },
userId: { type: String, optional: true },
}).validate(options)
const { identifier, idsToRefresh, userId } = options
const existingSyncronisation = DenormalizedViews._getExistingSyncronisation({ identifier })
debug(`refreshing ids in "view"-collection "${existingSyncronisation.viewCollection._name}":`, idsToRefresh)
for (const id of idsToRefresh) {
let doc = existingSyncronisation.sourceCollection.findOne(id)
if (!doc) {
debug(`existing docs in ${existingSyncronisation.sourceCollection._name}`, existingSyncronisation.sourceCollection.find().fetch())
throw new Meteor.Error(`trying to refresh "${id}", but it does NOT exist in collection "${existingSyncronisation.sourceCollection._name}". Are you sure that you passed the correct _ids?`)
}
doc = DenormalizedViews._processDoc({
doc,
userId,
syncronisation: existingSyncronisation,
})
DenormalizedViews._executeDatabaseComand(() => {
existingSyncronisation.viewCollection.update(doc._id, { $set: doc })
})
}
}
/**
* Return an exising syncronisation by a given identifier.
* NOTE: *depreciated* use `getView()` instead.
* @param {Object} options [description]
* @return {[type]} [description]
*/
static _getExistingSyncronisation(options = {}) {
new SimpleSchema({
identifier: { type: String },
}).validate(options)
const { identifier } = options
return _.find(SyncronisationStore, store => store.identifier===identifier)
}
/**
* Helper function which is used in refreshByCollection
* to validate the refreshIds-property.
*
* Validate that we have a function.
* Validate that the function returns either undefined, [String] or []
* @param {Object} options [description]
* @return {[type]} [description]
*/
static _validateAndCallRefreshIds(options = {}) {
new SimpleSchema({
doc: { type: Object, blackbox: true },
docPrevious: { type: Object, blackbox: true, optional: true }, // only on updates!
refreshIds: { type: Function },
userId: { type: String, optional: true },
}).validate(options)
const { doc, docPrevious, userId, refreshIds } = options
// validate that we have a function
if (!_.isFunction(refreshIds)) {
throw new Meteor.Error('refreshByCollection.refreshIds needs to be a function')
}
// validate that the refreshIds-function returns either
// undefined, [String] or []
const ids = refreshIds.call(this, doc, docPrevious, userId)
if (!(Match.test(ids, [String]) || _.isUndefined(ids) || ids===[])) {
throw new Meteor.Error(`refreshByCollection.refreshIds needs to return an array of strings, an empty array or undefined, BUT it returned "${ids}"`)
}
return ids
}
/**
* This is a quick and dirty solution to remove a property.
*
* We should make this better and minimize writes to the collection,
* this will do it for now.
*
* @param {Object} options [description]
* @return {[type]} [description]
*/
static _unsetProperty(options = {}) {
new SimpleSchema({
_id: { type: String },
collection: { type: Mongo.Collection },
property: { type: String },
}).validate(options)
const { _id, collection, property } = options
DenormalizedViews._executeDatabaseComand(() => {
collection.update(_id, JSON.parse(`{ "$unset": { "${property}": 1 } }`))
})
}
/**
* Execute a database command. If ``DeferWriteAccess=true```,
* wrap it into a ``Meteor.defer``, otherwise call it like any
* other command and give it more priority.
*
* @param {[type]} aFunction [description]
* @return {[type]} [description]
*/
static _executeDatabaseComand(aFunction) {
// we run database-updates ONLY on the server,
// in order to relax the client, BUT need collections
// to be known on the client,
// p.e. for aldeed:tabular-support, so this is the
// place to make sure, that we are on server.
// In future we might add a "publishToClient" setting,
// so that we can utilizy latency compensation.
// For now this will do it.
if (Meteor.isServer) {
if (DenormalizedViews.DeferWriteAccess) {
// good for mass-data
Meteor.defer(() => {
aFunction.call()
})
} else {
// high speed
aFunction.call()
}
}
}
}
DenormalizedViews.Debug = false
DenormalizedViews.DeferWriteAccess = false