express-restify-mongoose 7.0.2 → 9.0.1
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/README.md +8 -8
- package/dist/buildQuery.js +61 -0
- package/dist/detective.js +41 -0
- package/dist/errorHandler.js +22 -0
- package/dist/express-restify-mongoose.d.ts +45 -0
- package/dist/express-restify-mongoose.js +106 -0
- package/dist/getQuerySchema.js +163 -0
- package/dist/middleware/access.js +34 -0
- package/dist/middleware/ensureContentType.js +25 -0
- package/dist/middleware/filterAndFindById.js +34 -0
- package/dist/middleware/onError.js +31 -0
- package/dist/middleware/outputFn.js +28 -0
- package/dist/middleware/prepareOutput.js +91 -0
- package/dist/middleware/prepareQuery.js +27 -0
- package/dist/moredots.js +26 -0
- package/dist/operations.js +265 -0
- package/dist/resource_filter.js +134 -0
- package/dist/types.js +4 -0
- package/dist/weedout.js +29 -0
- package/package.json +57 -63
- package/CHANGELOG.md +0 -92
- package/index.d.ts +0 -45
- package/src/buildQuery.js +0 -55
- package/src/errorHandler.js +0 -17
- package/src/express-restify-mongoose.js +0 -144
- package/src/middleware/access.js +0 -26
- package/src/middleware/ensureContentType.js +0 -19
- package/src/middleware/filterAndFindById.js +0 -35
- package/src/middleware/onError.js +0 -26
- package/src/middleware/outputFn.js +0 -15
- package/src/middleware/prepareOutput.js +0 -75
- package/src/middleware/prepareQuery.js +0 -123
- package/src/operations.js +0 -274
- package/src/resource_filter.js +0 -192
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const runSeries = require('run-series')
|
|
4
|
-
|
|
5
|
-
module.exports = function (options, excludedMap) {
|
|
6
|
-
const errorHandler = require('../errorHandler')(options)
|
|
7
|
-
|
|
8
|
-
return function (req, res, next) {
|
|
9
|
-
const postMiddleware = (() => {
|
|
10
|
-
switch (req.method.toLowerCase()) {
|
|
11
|
-
case 'get':
|
|
12
|
-
return options.postRead
|
|
13
|
-
case 'post':
|
|
14
|
-
if (req.erm.statusCode === 201) {
|
|
15
|
-
return options.postCreate
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
return options.postUpdate
|
|
19
|
-
case 'put':
|
|
20
|
-
case 'patch':
|
|
21
|
-
return options.postUpdate
|
|
22
|
-
case 'delete':
|
|
23
|
-
return options.postDelete
|
|
24
|
-
}
|
|
25
|
-
})()
|
|
26
|
-
|
|
27
|
-
const callback = (err) => {
|
|
28
|
-
if (err) {
|
|
29
|
-
return errorHandler(req, res, next)(err)
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
// TODO: this will, but should not, filter /count queries
|
|
33
|
-
if (req.erm.result) {
|
|
34
|
-
const opts = {
|
|
35
|
-
access: req.access,
|
|
36
|
-
excludedMap: excludedMap,
|
|
37
|
-
populate: req.erm && req.erm.query ? req.erm.query.populate : null,
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
req.erm.result = options.filter ? options.filter.filterObject(req.erm.result, opts) : req.erm.result
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
if (options.totalCountHeader && typeof req.erm.totalCount === 'number') {
|
|
44
|
-
res.header(typeof options.totalCountHeader === 'string' ? options.totalCountHeader : 'X-Total-Count', req.erm.totalCount)
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const promise = options.outputFn(req, res)
|
|
48
|
-
|
|
49
|
-
if (options.postProcess) {
|
|
50
|
-
if (promise && typeof promise.then === 'function') {
|
|
51
|
-
promise
|
|
52
|
-
.then(() => {
|
|
53
|
-
options.postProcess(req, res)
|
|
54
|
-
})
|
|
55
|
-
.catch(errorHandler(req, res, next))
|
|
56
|
-
} else {
|
|
57
|
-
options.postProcess(req, res)
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
if (!postMiddleware || postMiddleware.length === 0) {
|
|
63
|
-
return callback()
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
runSeries(
|
|
67
|
-
postMiddleware.map((middleware, i) => {
|
|
68
|
-
return (cb) => {
|
|
69
|
-
middleware(req, res, cb)
|
|
70
|
-
}
|
|
71
|
-
}),
|
|
72
|
-
callback
|
|
73
|
-
)
|
|
74
|
-
}
|
|
75
|
-
}
|
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const isCoordinates = require('is-coordinates')
|
|
4
|
-
|
|
5
|
-
module.exports = function (options) {
|
|
6
|
-
const errorHandler = require('../errorHandler')(options)
|
|
7
|
-
|
|
8
|
-
function jsonQueryParser(key, value) {
|
|
9
|
-
if (key === '$regex' && !options.allowRegex) {
|
|
10
|
-
return undefined
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
if (Array.isArray(value) && key[0] !== '$' && key !== 'coordinates' && !isCoordinates(value)) {
|
|
14
|
-
return { $in: value }
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
return value
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
function parseQueryOptions(queryOptions) {
|
|
21
|
-
if (queryOptions.select && typeof queryOptions.select === 'string') {
|
|
22
|
-
const select = queryOptions.select.split(',')
|
|
23
|
-
queryOptions.select = {}
|
|
24
|
-
|
|
25
|
-
for (let i = 0, length = select.length; i < length; i++) {
|
|
26
|
-
if (select[i][0] === '-') {
|
|
27
|
-
queryOptions.select[select[i].substring(1)] = 0
|
|
28
|
-
} else {
|
|
29
|
-
queryOptions.select[select[i]] = 1
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
if (queryOptions.populate) {
|
|
35
|
-
if (typeof queryOptions.populate === 'string') {
|
|
36
|
-
const populate = queryOptions.populate.split(',')
|
|
37
|
-
queryOptions.populate = []
|
|
38
|
-
|
|
39
|
-
for (let i = 0, length = populate.length; i < length; i++) {
|
|
40
|
-
queryOptions.populate.push({
|
|
41
|
-
path: populate[i],
|
|
42
|
-
})
|
|
43
|
-
|
|
44
|
-
for (const key in queryOptions.select) {
|
|
45
|
-
if (key.indexOf(populate[i] + '.') === 0) {
|
|
46
|
-
if (queryOptions.populate[i].select) {
|
|
47
|
-
queryOptions.populate[i].select += ' '
|
|
48
|
-
} else {
|
|
49
|
-
queryOptions.populate[i].select = ''
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
if (queryOptions.select[key] === 0) {
|
|
53
|
-
queryOptions.populate[i].select += '-'
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
queryOptions.populate[i].select += key.substring(populate[i].length + 1)
|
|
57
|
-
delete queryOptions.select[key]
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// If other specific fields are selected, add the populated field
|
|
62
|
-
if (queryOptions.select) {
|
|
63
|
-
if (Object.keys(queryOptions.select).length > 0 && !queryOptions.select[populate[i]]) {
|
|
64
|
-
queryOptions.select[populate[i]] = 1
|
|
65
|
-
} else if (Object.keys(queryOptions.select).length === 0) {
|
|
66
|
-
delete queryOptions.select
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
} else if (!Array.isArray(queryOptions.populate)) {
|
|
71
|
-
queryOptions.populate = [queryOptions.populate]
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// Configure populate query to not use strict populate to maintain
|
|
75
|
-
// behavior from Mongoose previous to v6 (unless already configured).
|
|
76
|
-
queryOptions.populate = queryOptions.populate.map((pop) => {
|
|
77
|
-
if (pop.strictPopulate !== undefined) return pop
|
|
78
|
-
pop.strictPopulate = false
|
|
79
|
-
return pop
|
|
80
|
-
})
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
return queryOptions
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
return function (req, res, next) {
|
|
87
|
-
const whitelist = ['distinct', 'limit', 'populate', 'query', 'select', 'skip', 'sort']
|
|
88
|
-
const query = {}
|
|
89
|
-
|
|
90
|
-
for (const key in req.query) {
|
|
91
|
-
if (whitelist.indexOf(key) === -1) {
|
|
92
|
-
continue
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
if (key === 'query') {
|
|
96
|
-
try {
|
|
97
|
-
query[key] = JSON.parse(req.query[key], jsonQueryParser)
|
|
98
|
-
} catch (e) {
|
|
99
|
-
return errorHandler(req, res, next)(new Error(`invalid_json_${key}`))
|
|
100
|
-
}
|
|
101
|
-
} else if (key === 'populate' || key === 'select' || key === 'sort') {
|
|
102
|
-
try {
|
|
103
|
-
query[key] = JSON.parse(req.query[key])
|
|
104
|
-
} catch (e) {
|
|
105
|
-
query[key] = req.query[key]
|
|
106
|
-
}
|
|
107
|
-
} else if (key === 'limit' || key === 'skip') {
|
|
108
|
-
query[key] = parseInt(req.query[key], 10)
|
|
109
|
-
|
|
110
|
-
if (`${query[key]}` !== `${req.query[key]}`) {
|
|
111
|
-
return errorHandler(req, res, next)(new Error(`invalid_${key}_value`))
|
|
112
|
-
}
|
|
113
|
-
} else {
|
|
114
|
-
query[key] = req.query[key]
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
req.erm = req.erm || {}
|
|
119
|
-
req.erm.query = parseQueryOptions(query)
|
|
120
|
-
|
|
121
|
-
next()
|
|
122
|
-
}
|
|
123
|
-
}
|
package/src/operations.js
DELETED
|
@@ -1,274 +0,0 @@
|
|
|
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
|
-
}
|
package/src/resource_filter.js
DELETED
|
@@ -1,192 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const defaults = require('lodash.defaults')
|
|
4
|
-
const detective = require('mongoose-detective')
|
|
5
|
-
const get = require('lodash.get')
|
|
6
|
-
const has = require('lodash.has')
|
|
7
|
-
const isPlainObject = require('lodash.isplainobject')
|
|
8
|
-
const weedout = require('weedout')
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Represents a filter.
|
|
12
|
-
* @constructor
|
|
13
|
-
* @param {Object} opts - Options
|
|
14
|
-
* @param {Object} opts.model - Mongoose model
|
|
15
|
-
* @param {Object} opts.excludedMap {} - Filtered keys for related models
|
|
16
|
-
* @param {Object} opts.filteredKeys {} - Keys to filter for the current model
|
|
17
|
-
*/
|
|
18
|
-
function Filter(opts) {
|
|
19
|
-
this.model = opts.model
|
|
20
|
-
|
|
21
|
-
this.filteredKeys = isPlainObject(opts.filteredKeys)
|
|
22
|
-
? {
|
|
23
|
-
private: opts.filteredKeys.private || [],
|
|
24
|
-
protected: opts.filteredKeys.protected || [],
|
|
25
|
-
}
|
|
26
|
-
: {
|
|
27
|
-
private: [],
|
|
28
|
-
protected: [],
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
if (this.model && this.model.discriminators && isPlainObject(opts.excludedMap)) {
|
|
32
|
-
for (const modelName in this.model.discriminators) {
|
|
33
|
-
if (opts.excludedMap[modelName]) {
|
|
34
|
-
this.filteredKeys.private = this.filteredKeys.private.concat(opts.excludedMap[modelName].private)
|
|
35
|
-
this.filteredKeys.protected = this.filteredKeys.protected.concat(opts.excludedMap[modelName].protected)
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Gets excluded keys for a given model and access.
|
|
43
|
-
* @memberof Filter
|
|
44
|
-
* @param {Object} - Options.
|
|
45
|
-
* @param {String} opts.access {public} - Access level (private, protected or public).
|
|
46
|
-
* @param {Object} opts.excludedMap {} - Filtered keys for related models
|
|
47
|
-
* @param {Object} opts.filteredKeys {} - Keys to filter for the current model
|
|
48
|
-
* @returns {Array} - Keys to filter.
|
|
49
|
-
*/
|
|
50
|
-
Filter.prototype.getExcluded = function (opts) {
|
|
51
|
-
if (opts.access === 'private') {
|
|
52
|
-
return []
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
let entry = opts.excludedMap && opts.modelName ? opts.excludedMap[opts.modelName] : null
|
|
56
|
-
|
|
57
|
-
if (!entry) {
|
|
58
|
-
entry = isPlainObject(opts.filteredKeys)
|
|
59
|
-
? {
|
|
60
|
-
private: opts.filteredKeys.private || [],
|
|
61
|
-
protected: opts.filteredKeys.protected || [],
|
|
62
|
-
}
|
|
63
|
-
: {
|
|
64
|
-
private: [],
|
|
65
|
-
protected: [],
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
return opts.access === 'protected' ? entry.private : entry.private.concat(entry.protected)
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
Filter.prototype.isExcluded = function (field, opts) {
|
|
73
|
-
if (!field) {
|
|
74
|
-
return false
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
opts = defaults(opts, {
|
|
78
|
-
access: 'public',
|
|
79
|
-
excludedMap: {},
|
|
80
|
-
filteredKeys: this.filteredKeys,
|
|
81
|
-
modelName: this.model.modelName,
|
|
82
|
-
})
|
|
83
|
-
|
|
84
|
-
return this.getExcluded(opts).indexOf(field) >= 0
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Removes excluded keys from a document.
|
|
89
|
-
* @memberof Filter
|
|
90
|
-
* @param {Object} - Source document.
|
|
91
|
-
* @param {Array} - Keys to filter.
|
|
92
|
-
* @returns {Object} - Filtered document.
|
|
93
|
-
*/
|
|
94
|
-
Filter.prototype.filterItem = function (item, excluded) {
|
|
95
|
-
if (Array.isArray(item)) {
|
|
96
|
-
return item.map((i) => this.filterItem(i, excluded))
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
if (item && excluded) {
|
|
100
|
-
if (typeof item.toObject === 'function') {
|
|
101
|
-
item = item.toObject()
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
for (let i = 0, length = excluded.length; i < length; i++) {
|
|
105
|
-
if (excluded[i].indexOf('.') > 0) {
|
|
106
|
-
weedout(item, excluded[i])
|
|
107
|
-
} else {
|
|
108
|
-
delete item[excluded[i]]
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
return item
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Removes excluded keys from a document with populated subdocuments.
|
|
118
|
-
* @memberof Filter
|
|
119
|
-
* @param {Object} - Source document.
|
|
120
|
-
* @param {Object} - Keys to filter.
|
|
121
|
-
* @param {Array} opts.populate - Paths to populated subdocuments.
|
|
122
|
-
* @param {String} opts.access - Access level (private, protected or public).
|
|
123
|
-
* @param {Object} opts.excludedMap {} - Filtered keys for related models
|
|
124
|
-
* @returns {Object} - Filtered document.
|
|
125
|
-
*/
|
|
126
|
-
Filter.prototype.filterPopulatedItem = function (item, opts) {
|
|
127
|
-
if (Array.isArray(item)) {
|
|
128
|
-
return item.map((i) => this.filterPopulatedItem(i, opts))
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
for (let i = 0; i < opts.populate.length; i++) {
|
|
132
|
-
if (!opts.populate[i].path) {
|
|
133
|
-
continue
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
const excluded = this.getExcluded({
|
|
137
|
-
access: opts.access,
|
|
138
|
-
excludedMap: opts.excludedMap,
|
|
139
|
-
modelName: detective(this.model, opts.populate[i].path),
|
|
140
|
-
})
|
|
141
|
-
|
|
142
|
-
if (has(item, opts.populate[i].path)) {
|
|
143
|
-
this.filterItem(get(item, opts.populate[i].path), excluded)
|
|
144
|
-
} else {
|
|
145
|
-
const pathToArray = opts.populate[i].path.split('.').slice(0, -1).join('.')
|
|
146
|
-
|
|
147
|
-
if (has(item, pathToArray)) {
|
|
148
|
-
const array = get(item, pathToArray)
|
|
149
|
-
const pathToObject = opts.populate[i].path.split('.').slice(-1).join('.')
|
|
150
|
-
|
|
151
|
-
if (Array.isArray(array)) {
|
|
152
|
-
this.filterItem(
|
|
153
|
-
array.map((element) => get(element, pathToObject)),
|
|
154
|
-
excluded
|
|
155
|
-
)
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
return item
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* Removes excluded keys from a document.
|
|
166
|
-
* @memberof Filter
|
|
167
|
-
* @access public
|
|
168
|
-
* @param {Object} - Source document.
|
|
169
|
-
* @param {Object} - Options.
|
|
170
|
-
* @param {String} opts.access {public} - Access level (private, protected or public).
|
|
171
|
-
* @param {Object} opts.excludedMap {} - Filtered keys for related models
|
|
172
|
-
* @param {Array} opts.populate - Paths to populated subdocuments.
|
|
173
|
-
* @returns {Object} - Filtered document.
|
|
174
|
-
*/
|
|
175
|
-
Filter.prototype.filterObject = function (resource, opts) {
|
|
176
|
-
opts = defaults(opts || {}, {
|
|
177
|
-
access: 'public',
|
|
178
|
-
excludedMap: {},
|
|
179
|
-
filteredKeys: this.filteredKeys,
|
|
180
|
-
modelName: this.model.modelName,
|
|
181
|
-
})
|
|
182
|
-
|
|
183
|
-
const filtered = this.filterItem(resource, this.getExcluded(opts))
|
|
184
|
-
|
|
185
|
-
if (opts.populate) {
|
|
186
|
-
this.filterPopulatedItem(filtered, opts)
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
return filtered
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
module.exports = Filter
|