express-restify-mongoose 6.1.2 → 7.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +92 -92
- package/LICENSE +19 -19
- package/README.md +59 -52
- package/index.d.ts +45 -0
- package/package.json +80 -85
- package/src/buildQuery.js +55 -55
- package/src/errorHandler.js +17 -17
- package/src/express-restify-mongoose.js +144 -144
- package/src/middleware/access.js +26 -26
- package/src/middleware/ensureContentType.js +19 -19
- package/src/middleware/filterAndFindById.js +35 -35
- package/src/middleware/onError.js +26 -26
- package/src/middleware/outputFn.js +15 -15
- package/src/middleware/prepareOutput.js +75 -75
- package/src/middleware/prepareQuery.js +123 -115
- package/src/operations.js +274 -274
- package/src/resource_filter.js +192 -192
package/src/operations.js
CHANGED
|
@@ -1,274 +1,274 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const isPlainObject = require('lodash.isplainobject')
|
|
4
|
-
const http = require('http')
|
|
5
|
-
const moredots = require('moredots')
|
|
6
|
-
|
|
7
|
-
module.exports = function (model, options, excludedMap) {
|
|
8
|
-
const buildQuery = require('./buildQuery')(options)
|
|
9
|
-
const errorHandler = require('./errorHandler')(options)
|
|
10
|
-
|
|
11
|
-
function findById(filteredContext, id) {
|
|
12
|
-
return filteredContext.findOne().and({
|
|
13
|
-
[options.idProperty]: id,
|
|
14
|
-
})
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function isDistinctExcluded(req) {
|
|
18
|
-
return options.filter.isExcluded(req.erm.query['distinct'], {
|
|
19
|
-
access: req.access,
|
|
20
|
-
excludedMap: excludedMap,
|
|
21
|
-
})
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function getItems(req, res, next) {
|
|
25
|
-
const contextModel = (req.erm && req.erm.model) || model
|
|
26
|
-
|
|
27
|
-
if (isDistinctExcluded(req)) {
|
|
28
|
-
req.erm.result = []
|
|
29
|
-
req.erm.statusCode = 200
|
|
30
|
-
return next()
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
options.contextFilter(contextModel, req, (filteredContext) => {
|
|
34
|
-
buildQuery(filteredContext.find(), req.erm.query).then((items) => {
|
|
35
|
-
req.erm.result = items
|
|
36
|
-
req.erm.statusCode = 200
|
|
37
|
-
|
|
38
|
-
if (options.totalCountHeader && !req.erm.query['distinct']) {
|
|
39
|
-
options.contextFilter(contextModel, req, (countFilteredContext) => {
|
|
40
|
-
buildQuery(
|
|
41
|
-
countFilteredContext.countDocuments(),
|
|
42
|
-
Object.assign(req.erm.query, {
|
|
43
|
-
skip: 0,
|
|
44
|
-
limit: 0,
|
|
45
|
-
})
|
|
46
|
-
).then((count) => {
|
|
47
|
-
req.erm.totalCount = count
|
|
48
|
-
next()
|
|
49
|
-
}, errorHandler(req, res, next))
|
|
50
|
-
})
|
|
51
|
-
} else {
|
|
52
|
-
next()
|
|
53
|
-
}
|
|
54
|
-
}, errorHandler(req, res, next))
|
|
55
|
-
})
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function getCount(req, res, next) {
|
|
59
|
-
const contextModel = (req.erm && req.erm.model) || model
|
|
60
|
-
|
|
61
|
-
options.contextFilter(contextModel, req, (filteredContext) => {
|
|
62
|
-
buildQuery(filteredContext.
|
|
63
|
-
req.erm.result = { count: count }
|
|
64
|
-
req.erm.statusCode = 200
|
|
65
|
-
|
|
66
|
-
next()
|
|
67
|
-
}, errorHandler(req, res, next))
|
|
68
|
-
})
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function getShallow(req, res, next) {
|
|
72
|
-
const contextModel = (req.erm && req.erm.model) || model
|
|
73
|
-
|
|
74
|
-
options.contextFilter(contextModel, req, (filteredContext) => {
|
|
75
|
-
buildQuery(findById(filteredContext, req.params.id), req.erm.query).then((item) => {
|
|
76
|
-
if (!item) {
|
|
77
|
-
return errorHandler(req, res, next)(new Error(http.STATUS_CODES[404]))
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
for (const prop in item) {
|
|
81
|
-
item[prop] = typeof item[prop] === 'object' && prop !== '_id' ? true : item[prop]
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
req.erm.result = item
|
|
85
|
-
req.erm.statusCode = 200
|
|
86
|
-
|
|
87
|
-
next()
|
|
88
|
-
}, errorHandler(req, res, next))
|
|
89
|
-
})
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
function deleteItems(req, res, next) {
|
|
93
|
-
const contextModel = (req.erm && req.erm.model) || model
|
|
94
|
-
|
|
95
|
-
options.contextFilter(contextModel, req, (filteredContext) => {
|
|
96
|
-
buildQuery(filteredContext.deleteMany(), req.erm.query).then(() => {
|
|
97
|
-
req.erm.statusCode = 204
|
|
98
|
-
|
|
99
|
-
next()
|
|
100
|
-
}, errorHandler(req, res, next))
|
|
101
|
-
})
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
function getItem(req, res, next) {
|
|
105
|
-
const contextModel = (req.erm && req.erm.model) || model
|
|
106
|
-
|
|
107
|
-
if (isDistinctExcluded(req)) {
|
|
108
|
-
req.erm.result = []
|
|
109
|
-
req.erm.statusCode = 200
|
|
110
|
-
return next()
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
options.contextFilter(contextModel, req, (filteredContext) => {
|
|
114
|
-
buildQuery(findById(filteredContext, req.params.id), req.erm.query).then((item) => {
|
|
115
|
-
if (!item) {
|
|
116
|
-
return errorHandler(req, res, next)(new Error(http.STATUS_CODES[404]))
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
req.erm.result = item
|
|
120
|
-
req.erm.statusCode = 200
|
|
121
|
-
|
|
122
|
-
next()
|
|
123
|
-
}, errorHandler(req, res, next))
|
|
124
|
-
})
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
function deleteItem(req, res, next) {
|
|
128
|
-
const contextModel = (req.erm && req.erm.model) || model
|
|
129
|
-
|
|
130
|
-
if (options.findOneAndRemove) {
|
|
131
|
-
options.contextFilter(contextModel, req, (filteredContext) => {
|
|
132
|
-
findById(filteredContext, req.params.id)
|
|
133
|
-
.findOneAndRemove()
|
|
134
|
-
.then((item) => {
|
|
135
|
-
if (!item) {
|
|
136
|
-
return errorHandler(req, res, next)(new Error(http.STATUS_CODES[404]))
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
req.erm.statusCode = 204
|
|
140
|
-
|
|
141
|
-
next()
|
|
142
|
-
}, errorHandler(req, res, next))
|
|
143
|
-
})
|
|
144
|
-
} else {
|
|
145
|
-
req.erm.document.remove().then(() => {
|
|
146
|
-
req.erm.statusCode = 204
|
|
147
|
-
|
|
148
|
-
next()
|
|
149
|
-
}, errorHandler(req, res, next))
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
function createObject(req, res, next) {
|
|
154
|
-
const contextModel = (req.erm && req.erm.model) || model
|
|
155
|
-
|
|
156
|
-
req.body = options.filter.filterObject(req.body || {}, {
|
|
157
|
-
access: req.access,
|
|
158
|
-
populate: req.erm.query.populate,
|
|
159
|
-
})
|
|
160
|
-
|
|
161
|
-
if (req.body._id === null) {
|
|
162
|
-
delete req.body._id
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
if (contextModel.schema.options.versionKey) {
|
|
166
|
-
delete req.body[contextModel.schema.options.versionKey]
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
contextModel
|
|
170
|
-
.create(req.body)
|
|
171
|
-
.then((item) => contextModel.populate(item, req.erm.query.populate || []))
|
|
172
|
-
.then((item) => {
|
|
173
|
-
req.erm.result = item
|
|
174
|
-
req.erm.statusCode = 201
|
|
175
|
-
|
|
176
|
-
next()
|
|
177
|
-
}, errorHandler(req, res, next))
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
function modifyObject(req, res, next) {
|
|
181
|
-
const contextModel = (req.erm && req.erm.model) || model
|
|
182
|
-
|
|
183
|
-
req.body = options.filter.filterObject(req.body || {}, {
|
|
184
|
-
access: req.access,
|
|
185
|
-
populate: req.erm.query.populate,
|
|
186
|
-
})
|
|
187
|
-
|
|
188
|
-
delete req.body._id
|
|
189
|
-
|
|
190
|
-
if (contextModel.schema.options.versionKey) {
|
|
191
|
-
delete req.body[contextModel.schema.options.versionKey]
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
function depopulate(src) {
|
|
195
|
-
const dst = {}
|
|
196
|
-
|
|
197
|
-
for (const key in src) {
|
|
198
|
-
const path = contextModel.schema.path(key)
|
|
199
|
-
|
|
200
|
-
if (path && path.caster && path.caster.instance === 'ObjectID') {
|
|
201
|
-
if (Array.isArray(src[key])) {
|
|
202
|
-
for (let j = 0; j < src[key].length; ++j) {
|
|
203
|
-
if (typeof src[key][j] === 'object') {
|
|
204
|
-
dst[key] = dst[key] || []
|
|
205
|
-
dst[key].push(src[key][j]._id)
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
} else if (isPlainObject(src[key])) {
|
|
209
|
-
dst[key] = src[key]._id
|
|
210
|
-
}
|
|
211
|
-
} else if (isPlainObject(src[key])) {
|
|
212
|
-
if (path && path.instance === 'ObjectID') {
|
|
213
|
-
dst[key] = src[key]._id
|
|
214
|
-
} else {
|
|
215
|
-
dst[key] = depopulate(src[key])
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
if (typeof dst[key] === 'undefined') {
|
|
220
|
-
dst[key] = src[key]
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
return dst
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
const cleanBody = moredots(depopulate(req.body))
|
|
228
|
-
|
|
229
|
-
if (options.findOneAndUpdate) {
|
|
230
|
-
options.contextFilter(contextModel, req, (filteredContext) => {
|
|
231
|
-
findById(filteredContext, req.params.id)
|
|
232
|
-
.findOneAndUpdate(
|
|
233
|
-
{},
|
|
234
|
-
{
|
|
235
|
-
$set: cleanBody,
|
|
236
|
-
},
|
|
237
|
-
{
|
|
238
|
-
new: true,
|
|
239
|
-
upsert: options.upsert,
|
|
240
|
-
runValidators: options.runValidators,
|
|
241
|
-
}
|
|
242
|
-
)
|
|
243
|
-
.exec()
|
|
244
|
-
.then((item) => contextModel.populate(item, req.erm.query.populate || []))
|
|
245
|
-
.then((item) => {
|
|
246
|
-
if (!item) {
|
|
247
|
-
return errorHandler(req, res, next)(new Error(http.STATUS_CODES[404]))
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
req.erm.result = item
|
|
251
|
-
req.erm.statusCode = 200
|
|
252
|
-
|
|
253
|
-
next()
|
|
254
|
-
}, errorHandler(req, res, next))
|
|
255
|
-
})
|
|
256
|
-
} else {
|
|
257
|
-
for (const key in cleanBody) {
|
|
258
|
-
req.erm.document.set(key, cleanBody[key])
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
req.erm.document
|
|
262
|
-
.save()
|
|
263
|
-
.then((item) => contextModel.populate(item, req.erm.query.populate || []))
|
|
264
|
-
.then((item) => {
|
|
265
|
-
req.erm.result = item
|
|
266
|
-
req.erm.statusCode = 200
|
|
267
|
-
|
|
268
|
-
next()
|
|
269
|
-
}, errorHandler(req, res, next))
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
return { getItems, getCount, getItem, getShallow, createObject, modifyObject, deleteItems, deleteItem }
|
|
274
|
-
}
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const isPlainObject = require('lodash.isplainobject')
|
|
4
|
+
const http = require('http')
|
|
5
|
+
const moredots = require('moredots')
|
|
6
|
+
|
|
7
|
+
module.exports = function (model, options, excludedMap) {
|
|
8
|
+
const buildQuery = require('./buildQuery')(options)
|
|
9
|
+
const errorHandler = require('./errorHandler')(options)
|
|
10
|
+
|
|
11
|
+
function findById(filteredContext, id) {
|
|
12
|
+
return filteredContext.findOne().and({
|
|
13
|
+
[options.idProperty]: id,
|
|
14
|
+
})
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function isDistinctExcluded(req) {
|
|
18
|
+
return options.filter.isExcluded(req.erm.query['distinct'], {
|
|
19
|
+
access: req.access,
|
|
20
|
+
excludedMap: excludedMap,
|
|
21
|
+
})
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function getItems(req, res, next) {
|
|
25
|
+
const contextModel = (req.erm && req.erm.model) || model
|
|
26
|
+
|
|
27
|
+
if (isDistinctExcluded(req)) {
|
|
28
|
+
req.erm.result = []
|
|
29
|
+
req.erm.statusCode = 200
|
|
30
|
+
return next()
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
options.contextFilter(contextModel, req, (filteredContext) => {
|
|
34
|
+
buildQuery(filteredContext.find(), req.erm.query).then((items) => {
|
|
35
|
+
req.erm.result = items
|
|
36
|
+
req.erm.statusCode = 200
|
|
37
|
+
|
|
38
|
+
if (options.totalCountHeader && !req.erm.query['distinct']) {
|
|
39
|
+
options.contextFilter(contextModel, req, (countFilteredContext) => {
|
|
40
|
+
buildQuery(
|
|
41
|
+
countFilteredContext.countDocuments(),
|
|
42
|
+
Object.assign(req.erm.query, {
|
|
43
|
+
skip: 0,
|
|
44
|
+
limit: 0,
|
|
45
|
+
})
|
|
46
|
+
).then((count) => {
|
|
47
|
+
req.erm.totalCount = count
|
|
48
|
+
next()
|
|
49
|
+
}, errorHandler(req, res, next))
|
|
50
|
+
})
|
|
51
|
+
} else {
|
|
52
|
+
next()
|
|
53
|
+
}
|
|
54
|
+
}, errorHandler(req, res, next))
|
|
55
|
+
})
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function getCount(req, res, next) {
|
|
59
|
+
const contextModel = (req.erm && req.erm.model) || model
|
|
60
|
+
|
|
61
|
+
options.contextFilter(contextModel, req, (filteredContext) => {
|
|
62
|
+
buildQuery(filteredContext.countDocuments(), req.erm.query).then((count) => {
|
|
63
|
+
req.erm.result = { count: count }
|
|
64
|
+
req.erm.statusCode = 200
|
|
65
|
+
|
|
66
|
+
next()
|
|
67
|
+
}, errorHandler(req, res, next))
|
|
68
|
+
})
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function getShallow(req, res, next) {
|
|
72
|
+
const contextModel = (req.erm && req.erm.model) || model
|
|
73
|
+
|
|
74
|
+
options.contextFilter(contextModel, req, (filteredContext) => {
|
|
75
|
+
buildQuery(findById(filteredContext, req.params.id), req.erm.query).then((item) => {
|
|
76
|
+
if (!item) {
|
|
77
|
+
return errorHandler(req, res, next)(new Error(http.STATUS_CODES[404]))
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
for (const prop in item) {
|
|
81
|
+
item[prop] = typeof item[prop] === 'object' && prop !== '_id' ? true : item[prop]
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
req.erm.result = item
|
|
85
|
+
req.erm.statusCode = 200
|
|
86
|
+
|
|
87
|
+
next()
|
|
88
|
+
}, errorHandler(req, res, next))
|
|
89
|
+
})
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function deleteItems(req, res, next) {
|
|
93
|
+
const contextModel = (req.erm && req.erm.model) || model
|
|
94
|
+
|
|
95
|
+
options.contextFilter(contextModel, req, (filteredContext) => {
|
|
96
|
+
buildQuery(filteredContext.deleteMany(), req.erm.query).then(() => {
|
|
97
|
+
req.erm.statusCode = 204
|
|
98
|
+
|
|
99
|
+
next()
|
|
100
|
+
}, errorHandler(req, res, next))
|
|
101
|
+
})
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function getItem(req, res, next) {
|
|
105
|
+
const contextModel = (req.erm && req.erm.model) || model
|
|
106
|
+
|
|
107
|
+
if (isDistinctExcluded(req)) {
|
|
108
|
+
req.erm.result = []
|
|
109
|
+
req.erm.statusCode = 200
|
|
110
|
+
return next()
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
options.contextFilter(contextModel, req, (filteredContext) => {
|
|
114
|
+
buildQuery(findById(filteredContext, req.params.id), req.erm.query).then((item) => {
|
|
115
|
+
if (!item) {
|
|
116
|
+
return errorHandler(req, res, next)(new Error(http.STATUS_CODES[404]))
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
req.erm.result = item
|
|
120
|
+
req.erm.statusCode = 200
|
|
121
|
+
|
|
122
|
+
next()
|
|
123
|
+
}, errorHandler(req, res, next))
|
|
124
|
+
})
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function deleteItem(req, res, next) {
|
|
128
|
+
const contextModel = (req.erm && req.erm.model) || model
|
|
129
|
+
|
|
130
|
+
if (options.findOneAndRemove) {
|
|
131
|
+
options.contextFilter(contextModel, req, (filteredContext) => {
|
|
132
|
+
findById(filteredContext, req.params.id)
|
|
133
|
+
.findOneAndRemove()
|
|
134
|
+
.then((item) => {
|
|
135
|
+
if (!item) {
|
|
136
|
+
return errorHandler(req, res, next)(new Error(http.STATUS_CODES[404]))
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
req.erm.statusCode = 204
|
|
140
|
+
|
|
141
|
+
next()
|
|
142
|
+
}, errorHandler(req, res, next))
|
|
143
|
+
})
|
|
144
|
+
} else {
|
|
145
|
+
req.erm.document.remove().then(() => {
|
|
146
|
+
req.erm.statusCode = 204
|
|
147
|
+
|
|
148
|
+
next()
|
|
149
|
+
}, errorHandler(req, res, next))
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function createObject(req, res, next) {
|
|
154
|
+
const contextModel = (req.erm && req.erm.model) || model
|
|
155
|
+
|
|
156
|
+
req.body = options.filter.filterObject(req.body || {}, {
|
|
157
|
+
access: req.access,
|
|
158
|
+
populate: req.erm.query.populate,
|
|
159
|
+
})
|
|
160
|
+
|
|
161
|
+
if (req.body._id === null) {
|
|
162
|
+
delete req.body._id
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (contextModel.schema.options.versionKey) {
|
|
166
|
+
delete req.body[contextModel.schema.options.versionKey]
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
contextModel
|
|
170
|
+
.create(req.body)
|
|
171
|
+
.then((item) => contextModel.populate(item, req.erm.query.populate || []))
|
|
172
|
+
.then((item) => {
|
|
173
|
+
req.erm.result = item
|
|
174
|
+
req.erm.statusCode = 201
|
|
175
|
+
|
|
176
|
+
next()
|
|
177
|
+
}, errorHandler(req, res, next))
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function modifyObject(req, res, next) {
|
|
181
|
+
const contextModel = (req.erm && req.erm.model) || model
|
|
182
|
+
|
|
183
|
+
req.body = options.filter.filterObject(req.body || {}, {
|
|
184
|
+
access: req.access,
|
|
185
|
+
populate: req.erm.query.populate,
|
|
186
|
+
})
|
|
187
|
+
|
|
188
|
+
delete req.body._id
|
|
189
|
+
|
|
190
|
+
if (contextModel.schema.options.versionKey) {
|
|
191
|
+
delete req.body[contextModel.schema.options.versionKey]
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function depopulate(src) {
|
|
195
|
+
const dst = {}
|
|
196
|
+
|
|
197
|
+
for (const key in src) {
|
|
198
|
+
const path = contextModel.schema.path(key)
|
|
199
|
+
|
|
200
|
+
if (path && path.caster && path.caster.instance === 'ObjectID') {
|
|
201
|
+
if (Array.isArray(src[key])) {
|
|
202
|
+
for (let j = 0; j < src[key].length; ++j) {
|
|
203
|
+
if (typeof src[key][j] === 'object') {
|
|
204
|
+
dst[key] = dst[key] || []
|
|
205
|
+
dst[key].push(src[key][j]._id)
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
} else if (isPlainObject(src[key])) {
|
|
209
|
+
dst[key] = src[key]._id
|
|
210
|
+
}
|
|
211
|
+
} else if (isPlainObject(src[key])) {
|
|
212
|
+
if (path && path.instance === 'ObjectID') {
|
|
213
|
+
dst[key] = src[key]._id
|
|
214
|
+
} else {
|
|
215
|
+
dst[key] = depopulate(src[key])
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
if (typeof dst[key] === 'undefined') {
|
|
220
|
+
dst[key] = src[key]
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
return dst
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const cleanBody = moredots(depopulate(req.body))
|
|
228
|
+
|
|
229
|
+
if (options.findOneAndUpdate) {
|
|
230
|
+
options.contextFilter(contextModel, req, (filteredContext) => {
|
|
231
|
+
findById(filteredContext, req.params.id)
|
|
232
|
+
.findOneAndUpdate(
|
|
233
|
+
{},
|
|
234
|
+
{
|
|
235
|
+
$set: cleanBody,
|
|
236
|
+
},
|
|
237
|
+
{
|
|
238
|
+
new: true,
|
|
239
|
+
upsert: options.upsert,
|
|
240
|
+
runValidators: options.runValidators,
|
|
241
|
+
}
|
|
242
|
+
)
|
|
243
|
+
.exec()
|
|
244
|
+
.then((item) => contextModel.populate(item, req.erm.query.populate || []))
|
|
245
|
+
.then((item) => {
|
|
246
|
+
if (!item) {
|
|
247
|
+
return errorHandler(req, res, next)(new Error(http.STATUS_CODES[404]))
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
req.erm.result = item
|
|
251
|
+
req.erm.statusCode = 200
|
|
252
|
+
|
|
253
|
+
next()
|
|
254
|
+
}, errorHandler(req, res, next))
|
|
255
|
+
})
|
|
256
|
+
} else {
|
|
257
|
+
for (const key in cleanBody) {
|
|
258
|
+
req.erm.document.set(key, cleanBody[key])
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
req.erm.document
|
|
262
|
+
.save()
|
|
263
|
+
.then((item) => contextModel.populate(item, req.erm.query.populate || []))
|
|
264
|
+
.then((item) => {
|
|
265
|
+
req.erm.result = item
|
|
266
|
+
req.erm.statusCode = 200
|
|
267
|
+
|
|
268
|
+
next()
|
|
269
|
+
}, errorHandler(req, res, next))
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
return { getItems, getCount, getItem, getShallow, createObject, modifyObject, deleteItems, deleteItem }
|
|
274
|
+
}
|