@@ -11,8 +11,23 @@ const advices = {}
11
11
12
12
export const CollectionHooks = {
13
13
defaults : {
14
- before : { insert : { } , update : { } , remove : { } , upsert : { } , find : { } , findOne : { } , all : { } } ,
15
- after : { insert : { } , update : { } , remove : { } , find : { } , findOne : { } , all : { } } ,
14
+ before : {
15
+ insert : { } ,
16
+ update : { } ,
17
+ remove : { } ,
18
+ upsert : { } ,
19
+ find : { } ,
20
+ findOne : { } ,
21
+ all : { }
22
+ } ,
23
+ after : {
24
+ insert : { } ,
25
+ update : { } ,
26
+ remove : { } ,
27
+ find : { } ,
28
+ findOne : { } ,
29
+ all : { }
30
+ } ,
16
31
all : { insert : { } , update : { } , remove : { } , find : { } , findOne : { } , all : { } }
17
32
} ,
18
33
directEnv : new Meteor . EnvironmentVariable ( ) ,
@@ -25,7 +40,10 @@ export const CollectionHooks = {
25
40
}
26
41
}
27
42
28
- CollectionHooks . extendCollectionInstance = function extendCollectionInstance ( self , constructor ) {
43
+ CollectionHooks . extendCollectionInstance = function extendCollectionInstance (
44
+ self ,
45
+ constructor
46
+ ) {
29
47
// Offer a public API to allow the user to define aspects
30
48
// Example: collection.before.insert(func);
31
49
[ 'before' , 'after' ] . forEach ( function ( pointcut ) {
@@ -49,7 +67,7 @@ CollectionHooks.extendCollectionInstance = function extendCollectionInstance (se
49
67
// replacing is done by determining the actual index of a given target
50
68
// and replace this with the new one
51
69
const src = self . _hookAspects [ method ] [ pointcut ]
52
- const targetIndex = src . findIndex ( entry => entry === target )
70
+ const targetIndex = src . findIndex ( ( entry ) => entry === target )
53
71
const newTarget = {
54
72
aspect,
55
73
options : CollectionHooks . initOptions ( options , pointcut , method )
@@ -62,7 +80,7 @@ CollectionHooks.extendCollectionInstance = function extendCollectionInstance (se
62
80
// removing a hook is done by determining the actual index of a given target
63
81
// and removing it form the source array
64
82
const src = self . _hookAspects [ method ] [ pointcut ]
65
- const targetIndex = src . findIndex ( entry => entry === target )
83
+ const targetIndex = src . findIndex ( ( entry ) => entry === target )
66
84
self . _hookAspects [ method ] [ pointcut ] . splice ( targetIndex , 1 )
67
85
}
68
86
}
@@ -79,7 +97,8 @@ CollectionHooks.extendCollectionInstance = function extendCollectionInstance (se
79
97
Object . entries ( advices ) . forEach ( function ( [ method , advice ] ) {
80
98
// For client side, it wraps around minimongo LocalCollection
81
99
// For server side, it wraps around mongo Collection._collection (i.e. driver directly)
82
- const collection = Meteor . isClient || method === 'upsert' ? self : self . _collection
100
+ const collection =
101
+ Meteor . isClient || method === 'upsert' ? self : self . _collection
83
102
84
103
// Store a reference to the original mutator method
85
104
const _super = collection [ method ]
@@ -109,7 +128,8 @@ CollectionHooks.extendCollectionInstance = function extendCollectionInstance (se
109
128
if (
110
129
( method === 'update' && this . update . isCalledFromAsync ) ||
111
130
( method === 'remove' && this . remove . isCalledFromAsync ) ||
112
- CollectionHooks . directEnv . get ( ) === true ) {
131
+ CollectionHooks . directEnv . get ( ) === true
132
+ ) {
113
133
return _super . apply ( collection , args )
114
134
}
115
135
@@ -123,7 +143,8 @@ CollectionHooks.extendCollectionInstance = function extendCollectionInstance (se
123
143
// advice = CollectionHooks.getAdvice(method);
124
144
// }
125
145
126
- return advice . call ( this ,
146
+ return advice . call (
147
+ this ,
127
148
CollectionHooks . getUserId ( ) ,
128
149
_super ,
129
150
self ,
@@ -135,11 +156,13 @@ CollectionHooks.extendCollectionInstance = function extendCollectionInstance (se
135
156
}
136
157
: self . _hookAspects [ method ] || { } ,
137
158
function ( doc ) {
138
- return (
139
- typeof self . _transform === 'function'
140
- ? function ( d ) { return self . _transform ( d || doc ) }
141
- : function ( d ) { return d || doc }
142
- )
159
+ return typeof self . _transform === 'function'
160
+ ? function ( d ) {
161
+ return self . _transform ( d || doc )
162
+ }
163
+ : function ( d ) {
164
+ return d || doc
165
+ }
143
166
} ,
144
167
args ,
145
168
false
@@ -162,15 +185,31 @@ CollectionHooks.defineAdvice = (method, advice) => {
162
185
advices [ method ] = advice
163
186
}
164
187
165
- CollectionHooks . getAdvice = method => advices [ method ]
188
+ CollectionHooks . getAdvice = ( method ) => advices [ method ]
166
189
167
190
CollectionHooks . initOptions = ( options , pointcut , method ) =>
168
- CollectionHooks . extendOptions ( CollectionHooks . defaults , options , pointcut , method )
169
-
170
- CollectionHooks . extendOptions = ( source , options , pointcut , method ) =>
171
- ( { ...options , ...source . all . all , ...source [ pointcut ] . all , ...source . all [ method ] , ...source [ pointcut ] [ method ] } )
172
-
173
- CollectionHooks . getDocs = function getDocs ( collection , selector , options , fetchFields = { } , { useDirect = false } = { } ) {
191
+ CollectionHooks . extendOptions (
192
+ CollectionHooks . defaults ,
193
+ options ,
194
+ pointcut ,
195
+ method
196
+ )
197
+
198
+ CollectionHooks . extendOptions = ( source , options , pointcut , method ) => ( {
199
+ ...options ,
200
+ ...source . all . all ,
201
+ ...source [ pointcut ] . all ,
202
+ ...source . all [ method ] ,
203
+ ...source [ pointcut ] [ method ]
204
+ } )
205
+
206
+ CollectionHooks . getDocs = function getDocs (
207
+ collection ,
208
+ selector ,
209
+ options ,
210
+ fetchFields = { } ,
211
+ { useDirect = false } = { }
212
+ ) {
174
213
const findOptions = { transform : null , reactive : false }
175
214
176
215
if ( Object . keys ( fetchFields ) . length > 0 ) {
@@ -203,12 +242,18 @@ CollectionHooks.getDocs = function getDocs (collection, selector, options, fetch
203
242
204
243
// Unlike validators, we iterate over multiple docs, so use
205
244
// find instead of findOne:
206
- return ( useDirect ? collection . direct : collection ) . find ( selector , findOptions )
245
+ return ( useDirect ? collection . direct : collection ) . find (
246
+ selector ,
247
+ findOptions
248
+ )
207
249
}
208
250
209
251
// This function normalizes the selector (converting it to an Object)
210
252
CollectionHooks . normalizeSelector = function ( selector ) {
211
- if ( typeof selector === 'string' || ( selector && selector . constructor === Mongo . ObjectID ) ) {
253
+ if (
254
+ typeof selector === 'string' ||
255
+ ( selector && selector . constructor === Mongo . ObjectID )
256
+ ) {
212
257
return {
213
258
_id : selector
214
259
}
@@ -245,7 +290,7 @@ CollectionHooks.getFields = function getFields (mutator) {
245
290
Object . entries ( mutator ) . forEach ( function ( [ op , params ] ) {
246
291
// ====ADDED START=======================
247
292
if ( operators . includes ( op ) ) {
248
- // ====ADDED END=========================
293
+ // ====ADDED END=========================
249
294
Object . keys ( params ) . forEach ( function ( field ) {
250
295
// treat dotted fields as if they are replacing their
251
296
// top-level part
@@ -268,22 +313,26 @@ CollectionHooks.getFields = function getFields (mutator) {
268
313
return fields
269
314
}
270
315
271
- CollectionHooks . reassignPrototype = function reassignPrototype ( instance , constr ) {
316
+ CollectionHooks . reassignPrototype = function reassignPrototype (
317
+ instance ,
318
+ constr
319
+ ) {
272
320
const hasSetPrototypeOf = typeof Object . setPrototypeOf === 'function'
273
321
constr = constr || Mongo . Collection
274
322
275
323
// __proto__ is not available in < IE11
276
324
// Note: Assigning a prototype dynamically has performance implications
277
325
if ( hasSetPrototypeOf ) {
278
326
Object . setPrototypeOf ( instance , constr . prototype )
279
- } else if ( instance . __proto__ ) { // eslint-disable-line no-proto
327
+ } else if ( instance . __proto__ ) {
328
+ // eslint-disable-line no-proto
280
329
instance . __proto__ = constr . prototype // eslint-disable-line no-proto
281
330
}
282
331
}
283
332
284
333
CollectionHooks . wrapCollection = function wrapCollection ( ns , as ) {
285
334
if ( ! as . _CollectionConstructor ) as . _CollectionConstructor = as . Collection
286
- if ( ! as . _CollectionPrototype ) as . _CollectionPrototype = new as . Collection ( null )
335
+ if ( ! as . _CollectionPrototype ) { as . _CollectionPrototype = new as . Collection ( null ) }
287
336
288
337
const constructor = ns . _NewCollectionContructor || as . _CollectionConstructor
289
338
const proto = as . _CollectionPrototype
@@ -308,17 +357,6 @@ CollectionHooks.wrapCollection = function wrapCollection (ns, as) {
308
357
ns . Collection . apply = Function . prototype . apply
309
358
}
310
359
311
- CollectionHooks . isPromise = ( value ) => {
312
- return value && typeof value . then === 'function'
313
- }
314
-
315
- CollectionHooks . callAfterValueOrPromise = ( value , cb ) => {
316
- if ( CollectionHooks . isPromise ( value ) ) {
317
- return value . then ( ( res ) => cb ( res ) )
318
- }
319
- return cb ( value )
320
- }
321
-
322
360
CollectionHooks . modify = LocalCollection . _modify
323
361
324
362
if ( typeof Mongo !== 'undefined' ) {
0 commit comments