monastery 2.2.3 → 3.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/.eslintrc.json +10 -1
- package/changelog.md +3 -1
- package/docs/_config.yml +2 -2
- package/docs/assets/imgs/monastery.jpg +0 -0
- package/docs/definition/index.md +1 -2
- package/docs/manager/index.md +19 -11
- package/docs/manager/model.md +1 -1
- package/docs/manager/models.md +2 -3
- package/docs/model/count.md +25 -0
- package/docs/model/find.md +5 -9
- package/docs/model/findOne.md +1 -1
- package/docs/model/findOneAndUpdate.md +1 -1
- package/docs/model/index.md +5 -30
- package/docs/model/insert.md +4 -6
- package/docs/model/rawMethods.md +290 -0
- package/docs/model/remove.md +4 -6
- package/docs/model/update.md +4 -6
- package/docs/readme.md +69 -48
- package/lib/collection.js +324 -0
- package/lib/index.js +207 -67
- package/lib/model-crud.js +605 -619
- package/lib/model-validate.js +227 -245
- package/lib/model.js +70 -91
- package/lib/rules.js +36 -35
- package/lib/util.js +69 -15
- package/package.json +12 -11
- package/plugins/images/index.js +11 -11
- package/test/blacklisting.js +506 -537
- package/test/collection.js +445 -0
- package/test/crud.js +810 -730
- package/test/index.test.js +26 -0
- package/test/manager.js +77 -0
- package/test/mock/blacklisting.js +23 -23
- package/test/model.js +611 -572
- package/test/plugin-images.js +880 -965
- package/test/populate.js +249 -262
- package/test/util.js +126 -45
- package/test/validate.js +1074 -1121
- package/test/virtuals.js +222 -227
- package/lib/monk-monkey-patches.js +0 -90
- package/test/monk.js +0 -53
- package/test/test.js +0 -38
package/test/blacklisting.js
CHANGED
|
@@ -1,557 +1,526 @@
|
|
|
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
|
-
// Setup
|
|
133
|
-
db.model('bird', {
|
|
134
|
-
fields: {
|
|
135
|
-
age: { type: 'number' },
|
|
136
|
-
name: { type: 'string' },
|
|
137
|
-
wing: {
|
|
138
|
-
size: { type: 'number' },
|
|
139
|
-
},
|
|
140
|
-
},
|
|
141
|
-
findBL: ['age', 'wing']
|
|
142
|
-
})
|
|
143
|
-
db.model('user', {
|
|
144
|
-
fields: {
|
|
145
|
-
name: { type: 'string' },
|
|
146
|
-
bird1: { model: 'bird' },
|
|
1
|
+
const monastery = require('../lib/index.js')
|
|
2
|
+
const util = require('../lib/util.js')
|
|
3
|
+
const bird = require('./mock/blacklisting.js').bird
|
|
4
|
+
const user = require('./mock/blacklisting.js').user
|
|
5
|
+
|
|
6
|
+
let db
|
|
7
|
+
beforeAll(async () => { db = monastery('127.0.0.1/monastery', { timestamps: false }) })
|
|
8
|
+
afterAll(async () => { db.close() })
|
|
9
|
+
|
|
10
|
+
test('find blacklisting basic', async () => {
|
|
11
|
+
db.model('bird', bird.schema())
|
|
12
|
+
db.model('user', user.schema())
|
|
13
|
+
|
|
14
|
+
let bird1 = await db.bird.insert({ data: bird.mock() })
|
|
15
|
+
let user1 = await db.user.insert({ data: user.mock(bird1) })
|
|
16
|
+
|
|
17
|
+
// initial blacklist
|
|
18
|
+
let find1 = await db.user.findOne({
|
|
19
|
+
query: user1._id,
|
|
20
|
+
})
|
|
21
|
+
expect(find1).toEqual({
|
|
22
|
+
_id: user1._id,
|
|
23
|
+
bird: bird1._id,
|
|
24
|
+
list: [44, 54],
|
|
25
|
+
pet: 'Freddy',
|
|
26
|
+
pets: [{ name: 'Pluto' }, { name: 'Milo' }],
|
|
27
|
+
animals: { dog: 'Max' },
|
|
28
|
+
deep: { deep2: {} },
|
|
29
|
+
deepModel: { myBird: bird1._id },
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
// augmented blacklist
|
|
33
|
+
let find2 = await db.user.findOne({
|
|
34
|
+
query: user1._id,
|
|
35
|
+
blacklist: ['pet', 'pet', 'deep', 'deepModel', '-dog', '-animals.cat'],
|
|
36
|
+
})
|
|
37
|
+
let customBlacklist
|
|
38
|
+
expect(find2).toEqual((customBlacklist = {
|
|
39
|
+
_id: user1._id,
|
|
40
|
+
bird: bird1._id,
|
|
41
|
+
dog: 'Bruce',
|
|
42
|
+
list: [44, 54],
|
|
43
|
+
pets: [{ name: 'Pluto' }, { name: 'Milo' }],
|
|
44
|
+
animals: { dog: 'Max', cat: 'Ginger' },
|
|
45
|
+
}))
|
|
46
|
+
|
|
47
|
+
// blacklist string
|
|
48
|
+
let find3 = await db.user.findOne({
|
|
49
|
+
query: user1._id,
|
|
50
|
+
blacklist: 'pet pet deep deepModel -dog -animals.cat',
|
|
51
|
+
})
|
|
52
|
+
expect(find3).toEqual(customBlacklist)
|
|
53
|
+
|
|
54
|
+
// blacklist removal
|
|
55
|
+
let find4 = await db.user.findOne({ query: user1._id, blacklist: false })
|
|
56
|
+
expect(find4).toEqual(user1)
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
test('find blacklisting population', async () => {
|
|
60
|
+
db.model('bird', bird.schema())
|
|
61
|
+
db.model('user', {
|
|
62
|
+
fields: {
|
|
63
|
+
dog: { type: 'string' },
|
|
64
|
+
bird: { model: 'bird' },
|
|
65
|
+
},
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
let bird1 = await db.bird.insert({ data: bird.mock() })
|
|
69
|
+
let user1 = await db.user.insert({ data: {
|
|
70
|
+
dog: 'Bruce',
|
|
71
|
+
bird: bird1._id,
|
|
72
|
+
}})
|
|
73
|
+
|
|
74
|
+
let bird1Base = {
|
|
75
|
+
_id: bird1._id,
|
|
76
|
+
color: 'red',
|
|
77
|
+
sub: { color: 'red' },
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// 'bird1.name', // bird1.name & bird1.wing blacklisted
|
|
81
|
+
// '-bird2', 'bird2.name', // bird2.name blacklisted
|
|
82
|
+
// 'bird3.name', '-bird3', 'bird3.height', // bird3.height blacklisted
|
|
83
|
+
// '-bird4.wing.sizes.one', '-bird4.wing.size', // ignored
|
|
84
|
+
// bird4.wing.sizes.two blacklisted (expand in future verion)
|
|
85
|
+
// '-bird5.wing.sizes.one', // bird5.wing.sizes.one ignored, wing blacklisted
|
|
86
|
+
// bird5.wing.sizes.two, wing.size blacklisted (expand in future verion)
|
|
87
|
+
|
|
88
|
+
// test 1
|
|
89
|
+
db.user.findBL = ['bird.name']
|
|
90
|
+
expect(await db.user.findOne({ query: user1._id, populate: ['bird'] })).toEqual({
|
|
91
|
+
...user1,
|
|
92
|
+
bird: { ...bird1Base, height: 12 },
|
|
93
|
+
})
|
|
94
|
+
// test 2
|
|
95
|
+
db.user.findBL = ['-bird', 'bird.name']
|
|
96
|
+
expect(await db.user.findOne({ query: user1._id, populate: ['bird'] })).toEqual({
|
|
97
|
+
...user1,
|
|
98
|
+
bird: { ...bird1Base, height: 12, wing: { size: 1, sizes: { one: 1, two: 1 }} },
|
|
99
|
+
})
|
|
100
|
+
// test 3
|
|
101
|
+
db.user.findBL = ['bird.name', '-bird', 'bird.height']
|
|
102
|
+
expect(await db.user.findOne({ query: user1._id, populate: ['bird'] })).toEqual({
|
|
103
|
+
...user1,
|
|
104
|
+
bird: { ...bird1Base, name: 'Ponyo', wing: { size: 1, sizes: { one: 1, two: 1 }} },
|
|
105
|
+
})
|
|
106
|
+
// test 4
|
|
107
|
+
db.user.findBL = ['-bird.wing.sizes.one', '-bird.wing.size']
|
|
108
|
+
expect(await db.user.findOne({ query: user1._id, populate: ['bird'] })).toEqual({
|
|
109
|
+
...user1,
|
|
110
|
+
bird: { ...bird1Base, name: 'Ponyo', height: 12 },
|
|
111
|
+
})
|
|
112
|
+
// test 5
|
|
113
|
+
db.user.findBL = ['-bird.wing.sizes.one']
|
|
114
|
+
expect(await db.user.findOne({ query: user1._id, populate: ['bird'] })).toEqual({
|
|
115
|
+
...user1,
|
|
116
|
+
bird: { ...bird1Base, name: 'Ponyo', height: 12 },
|
|
117
|
+
})
|
|
118
|
+
// blacklist removal
|
|
119
|
+
expect(await db.user.findOne({ query: user1._id, blacklist: false, populate: ['bird'] })).toEqual({
|
|
120
|
+
...user1,
|
|
121
|
+
bird: { ...bird1Base, height: 12, name: 'Ponyo', wing: { size: 1, sizes: { one: 1, two: 1 }} },
|
|
122
|
+
})
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
test('find blacklisting getProjection', async () => {
|
|
126
|
+
db.model('bird', {
|
|
127
|
+
fields: {
|
|
128
|
+
age: { type: 'number' },
|
|
129
|
+
name: { type: 'string' },
|
|
130
|
+
wing: {
|
|
131
|
+
size: { type: 'number' },
|
|
147
132
|
},
|
|
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
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
133
|
+
},
|
|
134
|
+
findBL: ['age', 'wing'],
|
|
135
|
+
})
|
|
136
|
+
db.model('user', {
|
|
137
|
+
fields: {
|
|
138
|
+
name: { type: 'string' },
|
|
139
|
+
bird1: { model: 'bird' },
|
|
140
|
+
},
|
|
141
|
+
})
|
|
142
|
+
// default
|
|
143
|
+
expect(db.user._getProjectionFromBlacklist('find')).toEqual({
|
|
144
|
+
'bird1.wing': 0,
|
|
145
|
+
'bird1.age': 0,
|
|
146
|
+
'password': 0,
|
|
147
|
+
})
|
|
148
|
+
// blacklist /w invalid field (which goes through)
|
|
149
|
+
expect(db.user._getProjectionFromBlacklist('find', ['name', 'invalidfield'])).toEqual({
|
|
150
|
+
'bird1.wing': 0,
|
|
151
|
+
'bird1.age': 0,
|
|
152
|
+
'invalidfield': 0,
|
|
153
|
+
'name': 0,
|
|
154
|
+
'password': 0,
|
|
155
|
+
})
|
|
156
|
+
// whitelist
|
|
157
|
+
expect(db.user._getProjectionFromBlacklist('find', ['-password', '-bird1.age'])).toEqual({
|
|
158
|
+
'bird1.wing': 0,
|
|
159
|
+
})
|
|
160
|
+
// whitelist parent
|
|
161
|
+
expect(db.user._getProjectionFromBlacklist('find', ['-bird1'])).toEqual({
|
|
162
|
+
'password': 0,
|
|
163
|
+
})
|
|
164
|
+
// whitelist parent, then blacklist child
|
|
165
|
+
expect(db.user._getProjectionFromBlacklist('find', ['-bird1', 'bird1.name'])).toEqual({
|
|
166
|
+
'password': 0,
|
|
167
|
+
'bird1.name': 0,
|
|
168
|
+
})
|
|
169
|
+
// the model's blacklists are applied after deep model's
|
|
170
|
+
db.user.findBL = ['-bird1.age']
|
|
171
|
+
expect(db.user._getProjectionFromBlacklist('find')).toEqual({
|
|
172
|
+
'bird1.wing': 0,
|
|
173
|
+
})
|
|
174
|
+
// custom blacklists are applied after the model's, which are after deep model's
|
|
175
|
+
db.user.findBL = ['-bird1.age']
|
|
176
|
+
expect(db.user._getProjectionFromBlacklist('find', ['bird1'])).toEqual({
|
|
177
|
+
'bird1': 0,
|
|
178
|
+
})
|
|
179
|
+
// blacklisted parent with a blacklisted child
|
|
180
|
+
expect(db.user._getProjectionFromBlacklist('find', ['bird1', 'bird1.wing'])).toEqual({
|
|
181
|
+
'bird1': 0,
|
|
182
|
+
})
|
|
183
|
+
// A mess of things
|
|
184
|
+
expect(db.user._getProjectionFromBlacklist('find', ['-bird1', 'bird1.wing', '-bird1.wing','bird1.wing.size'])).toEqual({
|
|
185
|
+
'bird1.wing.size': 0,
|
|
186
|
+
})
|
|
187
|
+
// blacklisted parent with a whitelisted child (expect blacklist expansion in future version?)
|
|
188
|
+
// expect(db.user._getProjectionFromBlacklist('find', ['bird1', '-bird1.wing'])).toEqual({
|
|
189
|
+
// 'bird1.age': 0,
|
|
190
|
+
// 'bird1.name': 0,
|
|
191
|
+
// })
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
test('find project basic', async () => {
|
|
195
|
+
let user = db.model('user', {
|
|
196
|
+
fields: {
|
|
197
|
+
name: { type: 'string' },
|
|
198
|
+
color: { type: 'string', default: 'red' },
|
|
199
|
+
animal: {
|
|
209
200
|
name: { type: 'string' },
|
|
210
201
|
color: { type: 'string', default: 'red' },
|
|
211
|
-
animal: {
|
|
212
|
-
name: { type: 'string' },
|
|
213
|
-
color: { type: 'string', default: 'red' },
|
|
214
|
-
},
|
|
215
|
-
animals: [{
|
|
216
|
-
name: { type: 'string' },
|
|
217
|
-
color: { type: 'string', default: 'red' }
|
|
218
|
-
}]
|
|
219
|
-
}
|
|
220
|
-
})
|
|
221
|
-
let user1 = await user.insert({ data: {
|
|
222
|
-
name: 'Bruce',
|
|
223
|
-
animal: {
|
|
224
|
-
name: 'max'
|
|
225
202
|
},
|
|
226
|
-
animals: [
|
|
227
|
-
{ name: 'ponyo' },
|
|
228
|
-
{ name: 'freddy' }
|
|
229
|
-
]
|
|
230
|
-
}})
|
|
231
|
-
|
|
232
|
-
// Test inclusion projections
|
|
233
|
-
let find1 = await user.findOne({
|
|
234
|
-
query: user1._id,
|
|
235
|
-
project: ['animal.name', 'animals.name']
|
|
236
|
-
})
|
|
237
|
-
expect(find1).toEqual({
|
|
238
|
-
_id: user1._id,
|
|
239
|
-
animal: { name: 'max' },
|
|
240
|
-
animals: [
|
|
241
|
-
{ name: 'ponyo' },
|
|
242
|
-
{ name: 'freddy' }
|
|
243
|
-
]
|
|
244
|
-
})
|
|
245
|
-
|
|
246
|
-
// Test exclusion projections
|
|
247
|
-
let find2 = await user.findOne({
|
|
248
|
-
query: user1._id,
|
|
249
|
-
project: ['-animal.name', '-animals.color', '-name', '-color']
|
|
250
|
-
})
|
|
251
|
-
expect(find2).toEqual({
|
|
252
|
-
_id: user1._id,
|
|
253
|
-
animal: { color: 'red' },
|
|
254
|
-
animals: [
|
|
255
|
-
{ name: 'ponyo' },
|
|
256
|
-
{ name: 'freddy' }
|
|
257
|
-
]
|
|
258
|
-
})
|
|
259
|
-
|
|
260
|
-
db.close()
|
|
261
|
-
})
|
|
262
|
-
|
|
263
|
-
test('find project population', async () => {
|
|
264
|
-
// Test mongodb native project option
|
|
265
|
-
// Setup
|
|
266
|
-
let db = (await opendb(null)).db
|
|
267
|
-
let bird = db.model('bird', {
|
|
268
|
-
fields: {
|
|
203
|
+
animals: [{
|
|
269
204
|
name: { type: 'string' },
|
|
270
|
-
age: { type: 'number' },
|
|
271
|
-
height: { type: 'number' },
|
|
272
205
|
color: { type: 'string', default: 'red' },
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
expect(find2).toEqual((customProject={
|
|
326
|
-
_id: user1._id,
|
|
327
|
-
bird: { _id: bird1._id, age: 3, name: 'ponyo', height: 40, color: 'red', sub: { color: 'red' }},
|
|
328
|
-
bird2: { height: 40 },
|
|
329
|
-
}))
|
|
330
|
-
|
|
331
|
-
// project string
|
|
332
|
-
let find3 = await user.findOne({
|
|
333
|
-
query: user1._id,
|
|
334
|
-
populate: ['bird', 'bird2'],
|
|
335
|
-
project: 'bird bird2.height',
|
|
336
|
-
})
|
|
337
|
-
expect(find3).toEqual(customProject)
|
|
338
|
-
|
|
339
|
-
db.close()
|
|
340
|
-
})
|
|
341
|
-
|
|
342
|
-
test('insert blacklisting (validate)', async () => {
|
|
343
|
-
// Setup
|
|
344
|
-
let db = (await opendb(null)).db
|
|
345
|
-
let user = db.model('user', {
|
|
346
|
-
fields: {
|
|
347
|
-
list: [{ type: 'number' }],
|
|
348
|
-
dog: { type: 'string' },
|
|
349
|
-
pet: { type: 'string' },
|
|
350
|
-
pets: [{ name: { type: 'string'}, age: { type: 'number'} }],
|
|
351
|
-
animals: {
|
|
352
|
-
cat: { type: 'string' },
|
|
353
|
-
dog: { type: 'string' }
|
|
354
|
-
},
|
|
355
|
-
hiddenPets: [{
|
|
356
|
-
name: { type: 'string'}
|
|
357
|
-
}],
|
|
358
|
-
hiddenList: [{ type: 'number'}],
|
|
359
|
-
deep: {
|
|
360
|
-
deep2: {
|
|
361
|
-
deep3: {
|
|
362
|
-
deep4: { type: 'string' }
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
}
|
|
206
|
+
}],
|
|
207
|
+
},
|
|
208
|
+
})
|
|
209
|
+
let user1 = await user.insert({ data: {
|
|
210
|
+
name: 'Bruce',
|
|
211
|
+
animal: {
|
|
212
|
+
name: 'max',
|
|
213
|
+
},
|
|
214
|
+
animals: [
|
|
215
|
+
{ name: 'ponyo' },
|
|
216
|
+
{ name: 'freddy' },
|
|
217
|
+
],
|
|
218
|
+
}})
|
|
219
|
+
|
|
220
|
+
// pass: test inclusion of projections via native mongodb project option
|
|
221
|
+
let find1 = await user.findOne({
|
|
222
|
+
query: user1._id,
|
|
223
|
+
project: ['animal.name', 'animals.name'],
|
|
224
|
+
})
|
|
225
|
+
expect(find1).toEqual({
|
|
226
|
+
_id: user1._id,
|
|
227
|
+
animal: { name: 'max' },
|
|
228
|
+
animals: [
|
|
229
|
+
{ name: 'ponyo' },
|
|
230
|
+
{ name: 'freddy' },
|
|
231
|
+
],
|
|
232
|
+
})
|
|
233
|
+
|
|
234
|
+
// pass: test exclusion of projections via native mongodb project option
|
|
235
|
+
let find2 = await user.findOne({
|
|
236
|
+
query: user1._id,
|
|
237
|
+
project: ['-animal.name', '-animals.color', '-name', '-color'],
|
|
238
|
+
})
|
|
239
|
+
expect(find2).toEqual({
|
|
240
|
+
_id: user1._id,
|
|
241
|
+
animal: { color: 'red' },
|
|
242
|
+
animals: [
|
|
243
|
+
{ name: 'ponyo' },
|
|
244
|
+
{ name: 'freddy' },
|
|
245
|
+
],
|
|
246
|
+
})
|
|
247
|
+
})
|
|
248
|
+
|
|
249
|
+
test('find project population', async () => {
|
|
250
|
+
let bird = db.model('bird', {
|
|
251
|
+
fields: {
|
|
252
|
+
name: { type: 'string' },
|
|
253
|
+
age: { type: 'number' },
|
|
254
|
+
height: { type: 'number' },
|
|
255
|
+
color: { type: 'string', default: 'red' },
|
|
256
|
+
sub: {
|
|
257
|
+
color: { type: 'string', default: 'red' },
|
|
366
258
|
},
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
259
|
+
},
|
|
260
|
+
findBL: ['age'],
|
|
261
|
+
})
|
|
262
|
+
let user = db.model('user', {
|
|
263
|
+
fields: {
|
|
264
|
+
dog: { type: 'string' },
|
|
265
|
+
bird: { model: 'bird' },
|
|
266
|
+
bird2: { model: 'bird' },
|
|
267
|
+
bird3: { model: 'bird' },
|
|
268
|
+
},
|
|
269
|
+
findBL: [
|
|
270
|
+
// allll these should be ignored.....?/////
|
|
271
|
+
'bird.name', // bird.name & bird.age blacklisted
|
|
272
|
+
'-bird2', 'bird2.name', // bird2.name blacklisted
|
|
273
|
+
'bird3.name', '-bird3', 'bird3.height', // bird3.height blacklisted
|
|
274
|
+
],
|
|
275
|
+
})
|
|
276
|
+
let bird1 = await bird.insert({ data: {
|
|
277
|
+
name: 'ponyo',
|
|
278
|
+
age: 3,
|
|
279
|
+
height: 40,
|
|
280
|
+
sub: {},
|
|
281
|
+
}})
|
|
282
|
+
let user1 = await user.insert({ data: {
|
|
283
|
+
dog: 'Bruce',
|
|
284
|
+
bird: bird1._id,
|
|
285
|
+
bird2: bird1._id,
|
|
286
|
+
bird3: bird1._id,
|
|
287
|
+
}})
|
|
288
|
+
|
|
289
|
+
// project
|
|
290
|
+
let find1 = await user.findOne({
|
|
291
|
+
query: user1._id,
|
|
292
|
+
populate: ['bird', 'bird2'],
|
|
293
|
+
project: ['bird.age', 'bird2'],
|
|
294
|
+
})
|
|
295
|
+
expect(find1).toEqual({
|
|
296
|
+
_id: user1._id,
|
|
297
|
+
bird: { age: 3 },
|
|
298
|
+
bird2: { _id: bird1._id, age: 3, name: 'ponyo', height: 40, color: 'red', sub: { color: 'red' }},
|
|
299
|
+
})
|
|
300
|
+
|
|
301
|
+
// project (different project details)
|
|
302
|
+
let find2 = await user.findOne({
|
|
303
|
+
query: user1._id,
|
|
304
|
+
populate: ['bird', 'bird2'],
|
|
305
|
+
project: ['bird', 'bird2.height'],
|
|
306
|
+
})
|
|
307
|
+
let customProject
|
|
308
|
+
expect(find2).toEqual((customProject={
|
|
309
|
+
_id: user1._id,
|
|
310
|
+
bird: { _id: bird1._id, age: 3, name: 'ponyo', height: 40, color: 'red', sub: { color: 'red' }},
|
|
311
|
+
bird2: { height: 40 },
|
|
312
|
+
}))
|
|
313
|
+
|
|
314
|
+
// project string
|
|
315
|
+
let find3 = await user.findOne({
|
|
316
|
+
query: user1._id,
|
|
317
|
+
populate: ['bird', 'bird2'],
|
|
318
|
+
project: 'bird bird2.height',
|
|
319
|
+
})
|
|
320
|
+
expect(find3).toEqual(customProject)
|
|
321
|
+
})
|
|
322
|
+
|
|
323
|
+
test('insert blacklisting (validate)', async () => {
|
|
324
|
+
let user = db.model('user', {
|
|
325
|
+
fields: {
|
|
326
|
+
list: [{ type: 'number' }],
|
|
327
|
+
dog: { type: 'string' },
|
|
328
|
+
pet: { type: 'string' },
|
|
329
|
+
pets: [{ name: { type: 'string'}, age: { type: 'number'} }],
|
|
387
330
|
animals: {
|
|
388
|
-
cat: '
|
|
389
|
-
dog: '
|
|
331
|
+
cat: { type: 'string' },
|
|
332
|
+
dog: { type: 'string' },
|
|
390
333
|
},
|
|
391
334
|
hiddenPets: [{
|
|
392
|
-
name: '
|
|
335
|
+
name: { type: 'string'},
|
|
393
336
|
}],
|
|
394
|
-
hiddenList: [
|
|
337
|
+
hiddenList: [{ type: 'number'}],
|
|
395
338
|
deep: {
|
|
396
339
|
deep2: {
|
|
397
340
|
deep3: {
|
|
398
|
-
deep4: '
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
// Default insert validation
|
|
405
|
-
let user1 = await user.validate(doc1)
|
|
406
|
-
expect(user1).toEqual({
|
|
407
|
-
list: [44, 54],
|
|
408
|
-
pet: 'Freddy',
|
|
409
|
-
pets: [{ name: 'Pluto' }, { name: 'Milo' }],
|
|
410
|
-
animals: {
|
|
411
|
-
dog: 'Max'
|
|
341
|
+
deep4: { type: 'string' },
|
|
342
|
+
},
|
|
343
|
+
},
|
|
412
344
|
},
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
345
|
+
},
|
|
346
|
+
insertBL: [
|
|
347
|
+
// '_id', // default
|
|
348
|
+
'dog',
|
|
349
|
+
'animals.cat',
|
|
350
|
+
'pets.age',
|
|
351
|
+
'hiddenPets',
|
|
352
|
+
'hiddenList',
|
|
353
|
+
'deep.deep2.deep3',
|
|
354
|
+
],
|
|
355
|
+
updateBL: [
|
|
356
|
+
// '_id' // default
|
|
357
|
+
],
|
|
358
|
+
})
|
|
359
|
+
let doc1Id = db.id()
|
|
360
|
+
let doc1 = {
|
|
361
|
+
_id: doc1Id,
|
|
362
|
+
list: [44, 54],
|
|
363
|
+
dog: 'Bruce',
|
|
364
|
+
pet: 'Freddy',
|
|
365
|
+
pets: [{ name: 'Pluto', age: 5 }, { name: 'Milo', age: 4 }],
|
|
366
|
+
animals: {
|
|
367
|
+
cat: 'Ginger',
|
|
368
|
+
dog: 'Max',
|
|
369
|
+
},
|
|
370
|
+
hiddenPets: [{
|
|
371
|
+
name: 'secretPet',
|
|
372
|
+
}],
|
|
373
|
+
hiddenList: [12, 23],
|
|
374
|
+
deep: {
|
|
375
|
+
deep2: {
|
|
376
|
+
deep3: {
|
|
377
|
+
deep4: 'hideme',
|
|
378
|
+
},
|
|
438
379
|
},
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
_id: undefined,
|
|
484
|
-
})
|
|
485
|
-
|
|
486
|
-
db.close()
|
|
487
|
-
})
|
|
488
|
-
|
|
489
|
-
test('findOneAndUpdate blacklisting general', async () => {
|
|
490
|
-
// todo: test all findOneAndUpdate options
|
|
491
|
-
// todo: test find & update hooks
|
|
492
|
-
let db = (await opendb(null)).db
|
|
493
|
-
db.model('bird', bird.schema())
|
|
494
|
-
db.model('user', user.schema())
|
|
495
|
-
|
|
496
|
-
let bird1 = await db.bird.insert({ data: bird.mock() })
|
|
497
|
-
let user1 = await db.user.insert({ data: user.mock(bird1) })
|
|
498
|
-
|
|
499
|
-
// augmented blacklist
|
|
500
|
-
let find2 = await db.user.findOneAndUpdate({
|
|
501
|
-
query: user1._id,
|
|
502
|
-
data: { dog: 'Bruce2', pet: 'Freddy2' }, // pet shouldn't update
|
|
503
|
-
blacklist: ['pet', 'deep', 'deepModel', '-dog', '-animals.cat'],
|
|
504
|
-
})
|
|
505
|
-
expect(find2).toEqual({
|
|
506
|
-
_id: user1._id,
|
|
507
|
-
bird: bird1._id,
|
|
508
|
-
dog: 'Bruce2',
|
|
509
|
-
list: [44, 54],
|
|
510
|
-
pets: [{ name: 'Pluto' }, { name: 'Milo' }],
|
|
511
|
-
animals: { dog: 'Max', cat: 'Ginger' },
|
|
512
|
-
})
|
|
513
|
-
expect(await db.user.findOne({ query: user1._id, project: ['pet'] })).toEqual({
|
|
514
|
-
_id: user1._id,
|
|
515
|
-
pet: 'Freddy',
|
|
516
|
-
})
|
|
517
|
-
|
|
518
|
-
db.close()
|
|
519
|
-
})
|
|
520
|
-
|
|
521
|
-
test('findOneAndUpdate blacklisting populate', async () => {
|
|
522
|
-
let db = (await opendb(null)).db
|
|
523
|
-
db.model('bird', bird.schema())
|
|
524
|
-
db.model('user', user.schema())
|
|
525
|
-
|
|
526
|
-
let bird1 = await db.bird.insert({ data: bird.mock() })
|
|
527
|
-
let user1 = await db.user.insert({ data: user.mock(bird1) })
|
|
528
|
-
|
|
529
|
-
// augmented blacklist
|
|
530
|
-
let find2 = await db.user.findOneAndUpdate({
|
|
531
|
-
query: user1._id,
|
|
532
|
-
data: { dog: 'Bruce2', pet: 'Freddy2' }, // pet shouldn't update
|
|
533
|
-
blacklist: [
|
|
534
|
-
'pet', 'deep', 'deepModel', '-dog', '-animals.cat',
|
|
535
|
-
'bird.name', '-bird', 'bird.height' // <- populated model
|
|
536
|
-
],
|
|
537
|
-
populate: ['bird'],
|
|
538
|
-
})
|
|
539
|
-
expect(find2).toEqual({
|
|
540
|
-
_id: user1._id,
|
|
541
|
-
bird: {
|
|
542
|
-
...util.omit(bird1, ['height']),
|
|
380
|
+
},
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
// Default insert validation
|
|
384
|
+
let user1 = await user.validate(doc1)
|
|
385
|
+
expect(user1).toEqual({
|
|
386
|
+
list: [44, 54],
|
|
387
|
+
pet: 'Freddy',
|
|
388
|
+
pets: [{ name: 'Pluto' }, { name: 'Milo' }],
|
|
389
|
+
animals: {
|
|
390
|
+
dog: 'Max',
|
|
391
|
+
},
|
|
392
|
+
deep: {
|
|
393
|
+
deep2: {},
|
|
394
|
+
},
|
|
395
|
+
})
|
|
396
|
+
|
|
397
|
+
// Custom insert blacklist (remove and add to the current schema blacklist)
|
|
398
|
+
let user2 = await user.validate(doc1, {
|
|
399
|
+
blacklist: [
|
|
400
|
+
'-_id',
|
|
401
|
+
'-dog',
|
|
402
|
+
'-animals.dog', // wrong property
|
|
403
|
+
'pets.name',
|
|
404
|
+
'-hiddenList',
|
|
405
|
+
'-deep', // blacklist a parent
|
|
406
|
+
],
|
|
407
|
+
})
|
|
408
|
+
let customBlacklist
|
|
409
|
+
expect(user2).toEqual((customBlacklist = {
|
|
410
|
+
_id: doc1Id,
|
|
411
|
+
list: [44, 54],
|
|
412
|
+
dog: 'Bruce',
|
|
413
|
+
pet: 'Freddy',
|
|
414
|
+
pets: [ {}, {} ],
|
|
415
|
+
animals: {
|
|
416
|
+
dog: 'Max',
|
|
417
|
+
},
|
|
418
|
+
hiddenList: [12, 23],
|
|
419
|
+
deep: {
|
|
420
|
+
deep2: {
|
|
421
|
+
deep3: {
|
|
422
|
+
deep4: 'hideme',
|
|
423
|
+
},
|
|
543
424
|
},
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
pets: [{ name: 'Pluto' }, { name: 'Milo' }],
|
|
547
|
-
animals: { dog: 'Max', cat: 'Ginger' },
|
|
548
|
-
})
|
|
549
|
-
expect(await db.user.findOne({ query: user1._id, project: ['pet'] })).toEqual({
|
|
550
|
-
_id: user1._id,
|
|
551
|
-
pet: 'Freddy',
|
|
552
|
-
})
|
|
425
|
+
},
|
|
426
|
+
}))
|
|
553
427
|
|
|
554
|
-
|
|
428
|
+
// Blacklist string
|
|
429
|
+
let user3 = await user.validate(doc1, {
|
|
430
|
+
blacklist: '-_id -dog -animals.dog pets.name -hiddenList -deep',
|
|
431
|
+
})
|
|
432
|
+
expect(user3).toEqual(customBlacklist)
|
|
433
|
+
|
|
434
|
+
// Blacklist removal
|
|
435
|
+
let user4 = await user.validate(doc1, { blacklist: false })
|
|
436
|
+
expect(user4).toEqual(doc1)
|
|
437
|
+
|
|
438
|
+
// Project whitelist
|
|
439
|
+
let user5 = await user.validate(doc1, {
|
|
440
|
+
project: [
|
|
441
|
+
'dog',
|
|
442
|
+
'pets.name',
|
|
443
|
+
'deep',
|
|
444
|
+
],
|
|
445
|
+
})
|
|
446
|
+
expect(user5).toEqual({
|
|
447
|
+
dog: 'Bruce',
|
|
448
|
+
pets: [ {name: 'Pluto'}, {name: 'Milo'} ],
|
|
449
|
+
deep: {
|
|
450
|
+
deep2: {
|
|
451
|
+
deep3: {
|
|
452
|
+
deep4: 'hideme',
|
|
453
|
+
},
|
|
454
|
+
},
|
|
455
|
+
},
|
|
555
456
|
})
|
|
556
457
|
|
|
557
|
-
|
|
458
|
+
// double check that _id.insertOnly is working
|
|
459
|
+
let user6 = await user.validate(doc1, { update: true, blacklist: false })
|
|
460
|
+
expect(user6).toEqual({
|
|
461
|
+
...doc1,
|
|
462
|
+
_id: undefined,
|
|
463
|
+
})
|
|
464
|
+
})
|
|
465
|
+
|
|
466
|
+
test('findOneAndUpdate blacklisting general', async () => {
|
|
467
|
+
// todo: test all findOneAndUpdate options
|
|
468
|
+
// todo: test find & update hooks
|
|
469
|
+
db.model('bird', bird.schema())
|
|
470
|
+
db.model('user', user.schema())
|
|
471
|
+
|
|
472
|
+
let bird1 = await db.bird.insert({ data: bird.mock() })
|
|
473
|
+
let user1 = await db.user.insert({ data: user.mock(bird1) })
|
|
474
|
+
|
|
475
|
+
// augmented blacklist
|
|
476
|
+
let find2 = await db.user.findOneAndUpdate({
|
|
477
|
+
query: user1._id,
|
|
478
|
+
data: { dog: 'Bruce2', pet: 'Freddy2' }, // pet shouldn't update
|
|
479
|
+
blacklist: ['pet', 'deep', 'deepModel', '-dog', '-animals.cat'],
|
|
480
|
+
})
|
|
481
|
+
expect(find2).toEqual({
|
|
482
|
+
_id: user1._id,
|
|
483
|
+
bird: bird1._id,
|
|
484
|
+
dog: 'Bruce2',
|
|
485
|
+
list: [44, 54],
|
|
486
|
+
pets: [{ name: 'Pluto' }, { name: 'Milo' }],
|
|
487
|
+
animals: { dog: 'Max', cat: 'Ginger' },
|
|
488
|
+
})
|
|
489
|
+
expect(await db.user.findOne({ query: user1._id, project: ['pet'] })).toEqual({
|
|
490
|
+
_id: user1._id,
|
|
491
|
+
pet: 'Freddy',
|
|
492
|
+
})
|
|
493
|
+
})
|
|
494
|
+
|
|
495
|
+
test('findOneAndUpdate blacklisting populate', async () => {
|
|
496
|
+
db.model('bird', bird.schema())
|
|
497
|
+
db.model('user', user.schema())
|
|
498
|
+
|
|
499
|
+
let bird1 = await db.bird.insert({ data: bird.mock() })
|
|
500
|
+
let user1 = await db.user.insert({ data: user.mock(bird1) })
|
|
501
|
+
|
|
502
|
+
// augmented blacklist
|
|
503
|
+
let find2 = await db.user.findOneAndUpdate({
|
|
504
|
+
query: user1._id,
|
|
505
|
+
data: { dog: 'Bruce2', pet: 'Freddy2' }, // pet shouldn't update
|
|
506
|
+
blacklist: [
|
|
507
|
+
'pet', 'deep', 'deepModel', '-dog', '-animals.cat',
|
|
508
|
+
'bird.name', '-bird', 'bird.height', // <- populated model
|
|
509
|
+
],
|
|
510
|
+
populate: ['bird'],
|
|
511
|
+
})
|
|
512
|
+
expect(find2).toEqual({
|
|
513
|
+
_id: user1._id,
|
|
514
|
+
bird: {
|
|
515
|
+
...util.omit(bird1, ['height']),
|
|
516
|
+
},
|
|
517
|
+
dog: 'Bruce2',
|
|
518
|
+
list: [44, 54],
|
|
519
|
+
pets: [{ name: 'Pluto' }, { name: 'Milo' }],
|
|
520
|
+
animals: { dog: 'Max', cat: 'Ginger' },
|
|
521
|
+
})
|
|
522
|
+
expect(await db.user.findOne({ query: user1._id, project: ['pet'] })).toEqual({
|
|
523
|
+
_id: user1._id,
|
|
524
|
+
pet: 'Freddy',
|
|
525
|
+
})
|
|
526
|
+
})
|