-
-
Notifications
You must be signed in to change notification settings - Fork 92
/
update.js
204 lines (192 loc) · 5.48 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
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
import { EJSON } from 'meteor/ejson'
import { CollectionHooks } from './collection-hooks'
const isEmpty = (a) => !Array.isArray(a) || !a.length
CollectionHooks.defineWrapper(
'update',
async function (
userId,
_super,
instance,
hooks,
getTransform,
args,
suppressHooks
) {
const ctx = { context: this, _super, args }
let [selector, mutator, options, callback] = args
if (typeof options === 'function') {
callback = options
options = {}
}
const async = typeof callback === 'function'
let docs
let docIds
let fields
let abort
const prev = {}
if (!suppressHooks) {
try {
const shouldFetchForBefore = !isEmpty(hooks.before)
const shouldFetchForAfter = !isEmpty(hooks.after)
let shouldFetchForPrevious = false
if (shouldFetchForAfter) {
shouldFetchForPrevious =
Object.values(hooks.after).some(
(o) => o.options.fetchPrevious !== false
) &&
CollectionHooks.extendOptions(
instance.hookOptions,
{},
'after',
'update'
).fetchPrevious !== false
}
fields = CollectionHooks.getFields(args[1])
const fetchFields = {}
if (shouldFetchForPrevious || shouldFetchForBefore) {
const afterHookFetchFields = shouldFetchForPrevious
? Object.values(hooks.after).map(
(o) => (o.options || {}).fetchFields || {}
)
: []
const beforeHookFetchFields = shouldFetchForBefore
? Object.values(hooks.before).map(
(o) => (o.options || {}).fetchFields || {}
)
: []
const afterGlobal = shouldFetchForPrevious
? CollectionHooks.extendOptions(
instance.hookOptions,
{},
'after',
'update'
).fetchFields || {}
: {}
const beforeGlobal = shouldFetchForPrevious
? CollectionHooks.extendOptions(
instance.hookOptions,
{},
'before',
'update'
).fetchFields || {}
: {}
Object.assign(
fetchFields,
afterGlobal,
beforeGlobal,
...afterHookFetchFields,
...beforeHookFetchFields
)
}
const cursor = await CollectionHooks.getDocs.call(
this,
instance,
args[0],
args[2],
fetchFields
)
docs = await cursor.fetch()
docIds = Object.values(docs).map((doc) => doc._id)
// copy originals for convenience for the 'after' pointcut
if (shouldFetchForAfter) {
prev.mutator = EJSON.clone(args[1])
prev.options = EJSON.clone(args[2])
if (shouldFetchForPrevious) {
prev.docs = {}
docs.forEach((doc) => {
prev.docs[doc._id] = EJSON.clone(doc)
})
}
}
// before
for (const o of hooks.before) {
for (const doc of docs) {
const r = await o.hook.call(
{ transform: getTransform(doc), ...ctx },
userId,
doc,
fields,
mutator,
options
)
if (r === false) abort = true
}
}
if (abort) return 0
} catch (e) {
if (async) return callback.call(this, e)
throw e
}
}
const after = async (affected, err) => {
if (!suppressHooks) {
let docs
let fields
if (!isEmpty(hooks.after)) {
fields = CollectionHooks.getFields(args[1])
const fetchFields = {}
const hookFetchFields = Object.values(hooks.after).map(
(o) => (o.options || {}).fetchFields || {}
)
const globalFetchFields = CollectionHooks.extendOptions(
instance.hookOptions,
{},
'after',
'update'
).fetchFields
if (hookFetchFields || globalFetchFields) {
Object.assign(
fetchFields,
globalFetchFields || {},
...hookFetchFields.map((a) => a.fetchFields)
)
}
const cursor = await CollectionHooks.getDocs.call(
this,
instance,
{ _id: { $in: docIds } },
options,
fetchFields,
{ useDirect: true }
)
docs = await cursor.fetch()
}
for (const o of hooks.after) {
for (const doc of docs) {
await o.hook.call(
{
transform: getTransform(doc),
previous: prev.docs && prev.docs[doc._id],
affected,
err,
...ctx
},
userId,
doc,
fields,
prev.mutator,
prev.options
)
}
}
}
}
if (async) {
const wrappedCallback = async function (err, affected, ...args) {
await after(affected, err)
return callback.call(this, err, affected, ...args)
}
return _super.call(this, selector, mutator, options, wrappedCallback)
} else {
const affected = await _super.call(
this,
selector,
mutator,
options,
callback
)
await after(affected)
return affected
}
}
)