-
-
Notifications
You must be signed in to change notification settings - Fork 92
/
insert.js
71 lines (63 loc) · 2 KB
/
insert.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
import { EJSON } from 'meteor/ejson'
import { Mongo } from 'meteor/mongo'
import { CollectionHooks } from './collection-hooks'
CollectionHooks.defineWrapper('insert', async function (userId, _super, instance, hooks, getTransform, args, suppressHooks) {
const ctx = { context: this, _super, args }
let doc = args[0]
let callback
if (typeof args[args.length - 1] === 'function') {
callback = args[args.length - 1]
}
const async = typeof callback === 'function'
let abort
let ret
// before
if (!suppressHooks) {
try {
for (const o of hooks.before) {
const r = await o.hook.call({ transform: getTransform(doc), ...ctx }, userId, doc)
if (r === false) {
abort = true
break
}
}
if (abort) return
} catch (e) {
if (async) return callback.call(this, e)
throw e
}
}
const after = async (id, err) => {
if (id) {
// In some cases (namely Meteor.users on Meteor 1.4+), the _id property
// is a raw mongo _id object. We need to extract the _id from this object
if (typeof id === 'object' && id.ops) {
// If _str then collection is using Mongo.ObjectID as ids
if (doc._id._str) {
id = new Mongo.ObjectID(doc._id._str.toString())
} else {
id = id.ops && id.ops[0] && id.ops[0]._id
}
}
doc = EJSON.clone(doc)
doc._id = id
}
if (!suppressHooks) {
const lctx = { transform: getTransform(doc), _id: id, err, ...ctx }
for (const o of hooks.after) {
await o.hook.call(lctx, userId, doc)
}
}
return id
}
if (async) {
const wrappedCallback = async function (err, obj, ...args) {
await after((obj && obj[0] && obj[0]._id) || obj, err)
return callback.call(this, err, obj, ...args)
}
return _super.call(this, doc, wrappedCallback)
} else {
ret = await _super.call(this, doc, callback)
return (await after((ret && ret.insertedId) || (ret && ret[0] && ret[0]._id) || ret))
}
})