express-restify-mongoose 6.1.1 → 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 -86
- 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/resource_filter.js
CHANGED
|
@@ -1,192 +1,192 @@
|
|
|
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
|
|
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
|