monastery 1.34.0 → 1.36.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 +1 -1
- package/changelog.md +29 -0
- package/docs/readme.md +6 -4
- package/docs/schema/index.md +3 -3
- package/lib/index.js +36 -34
- package/lib/model-crud.js +203 -115
- package/lib/model-validate.js +37 -62
- package/lib/model.js +3 -4
- package/lib/monk-monkey-patches.js +73 -0
- package/lib/util.js +20 -18
- package/package.json +1 -1
- package/test/blacklisting.js +215 -203
- package/test/crud.js +60 -2
- package/test/mock/blacklisting.js +122 -0
- package/test/monk.js +1 -1
package/test/blacklisting.js
CHANGED
|
@@ -1,104 +1,25 @@
|
|
|
1
|
+
let bird = require('./mock/blacklisting').bird
|
|
2
|
+
let user = require('./mock/blacklisting').user
|
|
3
|
+
let util = require('../lib/util')
|
|
4
|
+
|
|
1
5
|
module.exports = function(monastery, opendb) {
|
|
2
6
|
|
|
3
|
-
test('find blacklisting', async () => {
|
|
7
|
+
test('find blacklisting basic', async () => {
|
|
4
8
|
// Setup
|
|
5
9
|
let db = (await opendb(null)).db
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
name: { type: 'string' },
|
|
9
|
-
}
|
|
10
|
-
})
|
|
11
|
-
let user = db.model('user', {
|
|
12
|
-
fields: {
|
|
13
|
-
list: [{ type: 'number' }],
|
|
14
|
-
dog: { type: 'string' },
|
|
15
|
-
pet: { type: 'string' },
|
|
16
|
-
pets: [{
|
|
17
|
-
name: { type: 'string'},
|
|
18
|
-
age: { type: 'number'}
|
|
19
|
-
}],
|
|
20
|
-
animals: {
|
|
21
|
-
cat: { type: 'string' },
|
|
22
|
-
dog: { type: 'string' }
|
|
23
|
-
},
|
|
24
|
-
hiddenPets: [{
|
|
25
|
-
name: { type: 'string'}
|
|
26
|
-
}],
|
|
27
|
-
hiddenList: [{ type: 'number'}],
|
|
28
|
-
deep: {
|
|
29
|
-
deep2: {
|
|
30
|
-
deep3: {
|
|
31
|
-
deep4: { type: 'string' }
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
},
|
|
35
|
-
deeper: {
|
|
36
|
-
deeper2: {
|
|
37
|
-
deeper3: {
|
|
38
|
-
deeper4: { type: 'string' }
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
},
|
|
42
|
-
deepModel: {
|
|
43
|
-
myBird: { model: 'bird' }
|
|
44
|
-
},
|
|
45
|
-
hiddenDeepModel: {
|
|
46
|
-
myBird: { model: 'bird' }
|
|
47
|
-
}
|
|
48
|
-
},
|
|
49
|
-
findBL: [
|
|
50
|
-
'dog',
|
|
51
|
-
'animals.cat',
|
|
52
|
-
'pets.age',
|
|
53
|
-
'hiddenPets',
|
|
54
|
-
'hiddenList',
|
|
55
|
-
'deep.deep2.deep3',
|
|
56
|
-
'deeper',
|
|
57
|
-
'hiddenDeepModel'
|
|
58
|
-
],
|
|
59
|
-
})
|
|
60
|
-
let bird1 = await bird.insert({ data: { name: 'ponyo' }})
|
|
61
|
-
let user1 = await user.insert({ data: {
|
|
62
|
-
list: [44, 54],
|
|
63
|
-
dog: 'Bruce',
|
|
64
|
-
pet: 'Freddy',
|
|
65
|
-
pets: [{ name: 'Pluto', age: 5 }, { name: 'Milo', age: 4 }],
|
|
66
|
-
animals: {
|
|
67
|
-
cat: 'Ginger',
|
|
68
|
-
dog: 'Max'
|
|
69
|
-
},
|
|
70
|
-
hiddenPets: [{
|
|
71
|
-
name: 'secretPet'
|
|
72
|
-
}],
|
|
73
|
-
hiddenList: [12, 23],
|
|
74
|
-
deep: {
|
|
75
|
-
deep2: {
|
|
76
|
-
deep3: {
|
|
77
|
-
deep4: 'hideme'
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
},
|
|
81
|
-
deeper: {
|
|
82
|
-
deeper2: {
|
|
83
|
-
deeper3: {
|
|
84
|
-
deeper4: 'hideme'
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
},
|
|
88
|
-
deepModel: {
|
|
89
|
-
myBird: bird1._id
|
|
90
|
-
},
|
|
91
|
-
hiddenDeepModel: {
|
|
92
|
-
myBird: bird1._id
|
|
93
|
-
}
|
|
94
|
-
}})
|
|
10
|
+
db.model('bird', bird.schema())
|
|
11
|
+
db.model('user', user.schema())
|
|
95
12
|
|
|
96
|
-
|
|
97
|
-
let
|
|
13
|
+
let bird1 = await db.bird.insert({ data: bird.mock() })
|
|
14
|
+
let user1 = await db.user.insert({ data: user.mock(bird1) })
|
|
15
|
+
|
|
16
|
+
// initial blacklist
|
|
17
|
+
let find1 = await db.user.findOne({
|
|
98
18
|
query: user1._id
|
|
99
19
|
})
|
|
100
20
|
expect(find1).toEqual({
|
|
101
21
|
_id: user1._id,
|
|
22
|
+
bird: bird1._id,
|
|
102
23
|
list: [44, 54],
|
|
103
24
|
pet: 'Freddy',
|
|
104
25
|
pets: [{ name: 'Pluto' }, { name: 'Milo' }],
|
|
@@ -107,117 +28,100 @@ module.exports = function(monastery, opendb) {
|
|
|
107
28
|
deepModel: { myBird: bird1._id }
|
|
108
29
|
})
|
|
109
30
|
|
|
110
|
-
//
|
|
111
|
-
let find2 = await user.findOne({
|
|
31
|
+
// augmented blacklist
|
|
32
|
+
let find2 = await db.user.findOne({
|
|
112
33
|
query: user1._id,
|
|
113
34
|
blacklist: ['pet', 'pet', 'deep', 'deepModel', '-dog', '-animals.cat']
|
|
114
35
|
})
|
|
115
|
-
|
|
36
|
+
let customBlacklist
|
|
37
|
+
expect(find2).toEqual((customBlacklist = {
|
|
116
38
|
_id: user1._id,
|
|
39
|
+
bird: bird1._id,
|
|
117
40
|
dog: 'Bruce',
|
|
118
41
|
list: [44, 54],
|
|
119
42
|
pets: [{ name: 'Pluto' }, { name: 'Milo' }],
|
|
120
43
|
animals: { dog: 'Max', cat: 'Ginger' }
|
|
44
|
+
}))
|
|
45
|
+
|
|
46
|
+
// blacklist string
|
|
47
|
+
let find3 = await db.user.findOne({
|
|
48
|
+
query: user1._id,
|
|
49
|
+
blacklist: 'pet pet deep deepModel -dog -animals.cat'
|
|
121
50
|
})
|
|
51
|
+
expect(find3).toEqual(customBlacklist)
|
|
52
|
+
|
|
53
|
+
// blacklist removal
|
|
54
|
+
let find4 = await db.user.findOne({ query: user1._id, blacklist: false })
|
|
55
|
+
expect(find4).toEqual(user1)
|
|
122
56
|
|
|
123
57
|
db.close()
|
|
124
58
|
})
|
|
125
59
|
|
|
126
60
|
test('find blacklisting population', async () => {
|
|
127
|
-
// inprogresss
|
|
128
61
|
// Setup
|
|
129
|
-
let db =
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
})
|
|
133
|
-
let bird = db.model('bird', {
|
|
134
|
-
fields: {
|
|
135
|
-
color: { type: 'string', default: 'red' },
|
|
136
|
-
height: { type: 'number' },
|
|
137
|
-
name: { type: 'string' },
|
|
138
|
-
sub: {
|
|
139
|
-
color: { type: 'string', default: 'red' },
|
|
140
|
-
},
|
|
141
|
-
subs: [{
|
|
142
|
-
color: { type: 'string', default: 'red'},
|
|
143
|
-
}],
|
|
144
|
-
wing: {
|
|
145
|
-
size: { type: 'number' },
|
|
146
|
-
sizes: {
|
|
147
|
-
one: { type: 'number' },
|
|
148
|
-
two: { type: 'number' },
|
|
149
|
-
}
|
|
150
|
-
},
|
|
151
|
-
},
|
|
152
|
-
findBL: ['wing']
|
|
153
|
-
})
|
|
154
|
-
let user = db.model('user', {
|
|
62
|
+
let db = (await opendb(null)).db
|
|
63
|
+
db.model('bird', bird.schema())
|
|
64
|
+
db.model('user', {
|
|
155
65
|
fields: {
|
|
156
66
|
dog: { type: 'string' },
|
|
157
|
-
|
|
158
|
-
bird2: { model: 'bird' },
|
|
159
|
-
bird3: { model: 'bird' },
|
|
160
|
-
bird4: { model: 'bird' },
|
|
161
|
-
bird5: { model: 'bird' },
|
|
67
|
+
bird: { model: 'bird' },
|
|
162
68
|
},
|
|
163
|
-
findBL: [
|
|
164
|
-
'bird1.name', // bird1.name & bird1.wing blacklisted
|
|
165
|
-
'-bird2', 'bird2.name', // bird2.name blacklisted
|
|
166
|
-
'bird3.name', '-bird3', 'bird3.height', // bird3.height blacklisted
|
|
167
|
-
'-bird4.wing.sizes.one', '-bird4.wing.size', // ignored
|
|
168
|
-
// bird4.wing.sizes.two blacklisted (expand in future verion)
|
|
169
|
-
'-bird5.wing.sizes.one', // bird5.wing.sizes.one ignored, wing blacklisted
|
|
170
|
-
// bird5.wing.sizes.two, wing.size blacklisted (expand in future verion)
|
|
171
|
-
]
|
|
172
69
|
})
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
height: 40,
|
|
177
|
-
sub: {},
|
|
178
|
-
wing: { size: 1, sizes: { one: 1, two: 1 }}
|
|
179
|
-
}
|
|
180
|
-
})
|
|
181
|
-
let userData = {
|
|
70
|
+
|
|
71
|
+
let bird1 = await db.bird.insert({ data: bird.mock() })
|
|
72
|
+
let user1 = await db.user.insert({ data: {
|
|
182
73
|
dog: 'Bruce',
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
74
|
+
bird: bird1._id,
|
|
75
|
+
}})
|
|
76
|
+
|
|
77
|
+
let bird1Base = {
|
|
78
|
+
_id: bird1._id,
|
|
79
|
+
color: 'red',
|
|
80
|
+
sub: { color: 'red' }
|
|
188
81
|
}
|
|
189
|
-
let user1 = await user.insert({ data: userData })
|
|
190
|
-
let bird1Base = { _id: bird1._id, color: 'red', sub: { color: 'red' }}
|
|
191
82
|
|
|
192
|
-
//
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
//
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
83
|
+
// 'bird1.name', // bird1.name & bird1.wing blacklisted
|
|
84
|
+
// '-bird2', 'bird2.name', // bird2.name blacklisted
|
|
85
|
+
// 'bird3.name', '-bird3', 'bird3.height', // bird3.height blacklisted
|
|
86
|
+
// '-bird4.wing.sizes.one', '-bird4.wing.size', // ignored
|
|
87
|
+
// bird4.wing.sizes.two blacklisted (expand in future verion)
|
|
88
|
+
// '-bird5.wing.sizes.one', // bird5.wing.sizes.one ignored, wing blacklisted
|
|
89
|
+
// bird5.wing.sizes.two, wing.size blacklisted (expand in future verion)
|
|
90
|
+
|
|
91
|
+
// test 1
|
|
92
|
+
db.user.findBL = ['bird.name']
|
|
93
|
+
expect(await db.user.findOne({ query: user1._id, populate: ['bird'] })).toEqual({
|
|
94
|
+
...user1,
|
|
95
|
+
bird: { ...bird1Base, height: 12 },
|
|
96
|
+
})
|
|
97
|
+
// test 2
|
|
98
|
+
db.user.findBL = ['-bird', 'bird.name']
|
|
99
|
+
expect(await db.user.findOne({ query: user1._id, populate: ['bird'] })).toEqual({
|
|
100
|
+
...user1,
|
|
101
|
+
bird: { ...bird1Base, height: 12, wing: { size: 1, sizes: { one: 1, two: 1 }} },
|
|
102
|
+
})
|
|
103
|
+
// test 3
|
|
104
|
+
db.user.findBL = ['bird.name', '-bird', 'bird.height']
|
|
105
|
+
expect(await db.user.findOne({ query: user1._id, populate: ['bird'] })).toEqual({
|
|
106
|
+
...user1,
|
|
107
|
+
bird: { ...bird1Base, name: 'Ponyo', wing: { size: 1, sizes: { one: 1, two: 1 }} },
|
|
108
|
+
})
|
|
109
|
+
// test 4
|
|
110
|
+
db.user.findBL = ['-bird.wing.sizes.one', '-bird.wing.size']
|
|
111
|
+
expect(await db.user.findOne({ query: user1._id, populate: ['bird'] })).toEqual({
|
|
112
|
+
...user1,
|
|
113
|
+
bird: { ...bird1Base, name: 'Ponyo', height: 12 },
|
|
114
|
+
})
|
|
115
|
+
// test 5
|
|
116
|
+
db.user.findBL = ['-bird.wing.sizes.one']
|
|
117
|
+
expect(await db.user.findOne({ query: user1._id, populate: ['bird'] })).toEqual({
|
|
118
|
+
...user1,
|
|
119
|
+
bird: { ...bird1Base, name: 'Ponyo', height: 12 },
|
|
120
|
+
})
|
|
121
|
+
// blacklist removal
|
|
122
|
+
expect(await db.user.findOne({ query: user1._id, blacklist: false, populate: ['bird'] })).toEqual({
|
|
123
|
+
...user1,
|
|
124
|
+
bird: { ...bird1Base, height: 12, name: 'Ponyo', wing: { size: 1, sizes: { one: 1, two: 1 }} },
|
|
221
125
|
})
|
|
222
126
|
|
|
223
127
|
db.close()
|
|
@@ -243,13 +147,13 @@ module.exports = function(monastery, opendb) {
|
|
|
243
147
|
},
|
|
244
148
|
})
|
|
245
149
|
// default
|
|
246
|
-
expect(db.user.
|
|
150
|
+
expect(db.user._getProjectionFromBlacklist('find')).toEqual({
|
|
247
151
|
'bird1.wing': 0,
|
|
248
152
|
'bird1.age': 0,
|
|
249
153
|
'password': 0,
|
|
250
154
|
})
|
|
251
155
|
// blacklist /w invalid field (which goes through)
|
|
252
|
-
expect(db.user.
|
|
156
|
+
expect(db.user._getProjectionFromBlacklist('find', ['name', 'invalidfield'])).toEqual({
|
|
253
157
|
'bird1.wing': 0,
|
|
254
158
|
'bird1.age': 0,
|
|
255
159
|
'invalidfield': 0,
|
|
@@ -257,38 +161,38 @@ module.exports = function(monastery, opendb) {
|
|
|
257
161
|
'password': 0,
|
|
258
162
|
})
|
|
259
163
|
// whitelist
|
|
260
|
-
expect(db.user.
|
|
164
|
+
expect(db.user._getProjectionFromBlacklist('find', ['-password', '-bird1.age'])).toEqual({
|
|
261
165
|
'bird1.wing': 0,
|
|
262
166
|
})
|
|
263
167
|
// whitelist parent
|
|
264
|
-
expect(db.user.
|
|
168
|
+
expect(db.user._getProjectionFromBlacklist('find', ['-bird1'])).toEqual({
|
|
265
169
|
'password': 0,
|
|
266
170
|
})
|
|
267
171
|
// whitelist parent, then blacklist child
|
|
268
|
-
expect(db.user.
|
|
172
|
+
expect(db.user._getProjectionFromBlacklist('find', ['-bird1', 'bird1.name'])).toEqual({
|
|
269
173
|
'password': 0,
|
|
270
174
|
'bird1.name': 0,
|
|
271
175
|
})
|
|
272
176
|
// the model's blacklists are applied after deep model's
|
|
273
177
|
db.user.findBL = ['-bird1.age']
|
|
274
|
-
expect(db.user.
|
|
178
|
+
expect(db.user._getProjectionFromBlacklist('find')).toEqual({
|
|
275
179
|
'bird1.wing': 0,
|
|
276
180
|
})
|
|
277
181
|
// custom blacklists are applied after the model's, which are after deep model's
|
|
278
182
|
db.user.findBL = ['-bird1.age']
|
|
279
|
-
expect(db.user.
|
|
183
|
+
expect(db.user._getProjectionFromBlacklist('find', ['bird1'])).toEqual({
|
|
280
184
|
'bird1': 0,
|
|
281
185
|
})
|
|
282
186
|
// blacklisted parent with a blacklisted child
|
|
283
|
-
expect(db.user.
|
|
187
|
+
expect(db.user._getProjectionFromBlacklist('find', ['bird1', 'bird1.wing'])).toEqual({
|
|
284
188
|
'bird1': 0,
|
|
285
189
|
})
|
|
286
190
|
// A mess of things
|
|
287
|
-
expect(db.user.
|
|
191
|
+
expect(db.user._getProjectionFromBlacklist('find', ['-bird1', 'bird1.wing', '-bird1.wing','bird1.wing.size'])).toEqual({
|
|
288
192
|
'bird1.wing.size': 0,
|
|
289
193
|
})
|
|
290
194
|
// blacklisted parent with a whitelisted child (expect blacklist expansion in future version?)
|
|
291
|
-
// expect(db.user.
|
|
195
|
+
// expect(db.user._getProjectionFromBlacklist('find', ['bird1', '-bird1.wing'])).toEqual({
|
|
292
196
|
// 'bird1.age': 0,
|
|
293
197
|
// 'bird1.name': 0,
|
|
294
198
|
// })
|
|
@@ -296,7 +200,7 @@ module.exports = function(monastery, opendb) {
|
|
|
296
200
|
db.close()
|
|
297
201
|
})
|
|
298
202
|
|
|
299
|
-
test('find project', async () => {
|
|
203
|
+
test('find project basic', async () => {
|
|
300
204
|
// Test mongodb native project option
|
|
301
205
|
// Setup
|
|
302
206
|
let db = (await opendb(null)).db
|
|
@@ -370,14 +274,14 @@ module.exports = function(monastery, opendb) {
|
|
|
370
274
|
color: { type: 'string', default: 'red' },
|
|
371
275
|
}
|
|
372
276
|
},
|
|
373
|
-
findBL: ['age']
|
|
277
|
+
findBL: ['age'],
|
|
374
278
|
})
|
|
375
279
|
let user = db.model('user', {
|
|
376
280
|
fields: {
|
|
377
281
|
dog: { type: 'string' },
|
|
378
282
|
bird: { model: 'bird' },
|
|
379
283
|
bird2: { model: 'bird' },
|
|
380
|
-
bird3: { model: 'bird' }
|
|
284
|
+
bird3: { model: 'bird' },
|
|
381
285
|
},
|
|
382
286
|
findBL: [
|
|
383
287
|
// allll these should be ignored.....?/////
|
|
@@ -390,38 +294,47 @@ module.exports = function(monastery, opendb) {
|
|
|
390
294
|
name: 'ponyo',
|
|
391
295
|
age: 3,
|
|
392
296
|
height: 40,
|
|
393
|
-
sub: {}
|
|
297
|
+
sub: {},
|
|
394
298
|
}})
|
|
395
299
|
let user1 = await user.insert({ data: {
|
|
396
300
|
dog: 'Bruce',
|
|
397
301
|
bird: bird1._id,
|
|
398
302
|
bird2: bird1._id,
|
|
399
|
-
bird3: bird1._id
|
|
303
|
+
bird3: bird1._id,
|
|
400
304
|
}})
|
|
401
305
|
|
|
402
|
-
//
|
|
306
|
+
// project
|
|
403
307
|
let find1 = await user.findOne({
|
|
404
308
|
query: user1._id,
|
|
405
309
|
populate: ['bird', 'bird2'],
|
|
406
|
-
project: ['bird.age', 'bird2']
|
|
310
|
+
project: ['bird.age', 'bird2'],
|
|
407
311
|
})
|
|
408
312
|
expect(find1).toEqual({
|
|
409
313
|
_id: user1._id,
|
|
410
314
|
bird: { age: 3 },
|
|
411
|
-
bird2: { _id: bird1._id, age: 3, name: 'ponyo', height: 40, color: 'red', sub: { color: 'red' }}
|
|
315
|
+
bird2: { _id: bird1._id, age: 3, name: 'ponyo', height: 40, color: 'red', sub: { color: 'red' }},
|
|
412
316
|
})
|
|
413
317
|
|
|
414
|
-
//
|
|
318
|
+
// project (different project details)
|
|
415
319
|
let find2 = await user.findOne({
|
|
416
320
|
query: user1._id,
|
|
417
321
|
populate: ['bird', 'bird2'],
|
|
418
|
-
project: ['bird', 'bird2.height']
|
|
322
|
+
project: ['bird', 'bird2.height'],
|
|
419
323
|
})
|
|
420
|
-
|
|
324
|
+
let customProject
|
|
325
|
+
expect(find2).toEqual((customProject={
|
|
421
326
|
_id: user1._id,
|
|
422
327
|
bird: { _id: bird1._id, age: 3, name: 'ponyo', height: 40, color: 'red', sub: { color: 'red' }},
|
|
423
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',
|
|
424
336
|
})
|
|
337
|
+
expect(find3).toEqual(customProject)
|
|
425
338
|
|
|
426
339
|
db.close()
|
|
427
340
|
})
|
|
@@ -506,7 +419,8 @@ module.exports = function(monastery, opendb) {
|
|
|
506
419
|
'-deep' // blacklist a parent
|
|
507
420
|
],
|
|
508
421
|
})
|
|
509
|
-
|
|
422
|
+
let customBlacklist
|
|
423
|
+
expect(user2).toEqual((customBlacklist = {
|
|
510
424
|
list: [44, 54],
|
|
511
425
|
dog: 'Bruce',
|
|
512
426
|
pet: 'Freddy',
|
|
@@ -522,7 +436,105 @@ module.exports = function(monastery, opendb) {
|
|
|
522
436
|
}
|
|
523
437
|
}
|
|
524
438
|
}
|
|
439
|
+
}))
|
|
440
|
+
|
|
441
|
+
// Blacklist string
|
|
442
|
+
let user3 = await user.validate(doc1, {
|
|
443
|
+
blacklist: '-dog -animals.dog pets.name -hiddenList -deep'
|
|
525
444
|
})
|
|
445
|
+
expect(user3).toEqual(customBlacklist)
|
|
446
|
+
|
|
447
|
+
// Blacklist removal
|
|
448
|
+
let user4 = await user.validate(doc1, { blacklist: false })
|
|
449
|
+
expect(user4).toEqual(doc1)
|
|
450
|
+
|
|
451
|
+
// Project whitelist
|
|
452
|
+
let user5 = await user.validate(doc1, {
|
|
453
|
+
project: [
|
|
454
|
+
'dog',
|
|
455
|
+
'pets.name',
|
|
456
|
+
'deep'
|
|
457
|
+
],
|
|
458
|
+
})
|
|
459
|
+
expect(user5).toEqual({
|
|
460
|
+
dog: 'Bruce',
|
|
461
|
+
pets: [ {name: 'Pluto'}, {name: 'Milo'} ],
|
|
462
|
+
deep: {
|
|
463
|
+
deep2: {
|
|
464
|
+
deep3: {
|
|
465
|
+
deep4: 'hideme'
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
})
|
|
470
|
+
db.close()
|
|
471
|
+
})
|
|
472
|
+
|
|
473
|
+
test('findOneAndUpdate blacklisting general', async () => {
|
|
474
|
+
// todo: test all findOneAndUpdate options
|
|
475
|
+
// todo: test find & update hooks
|
|
476
|
+
let db = (await opendb(null)).db
|
|
477
|
+
db.model('bird', bird.schema())
|
|
478
|
+
db.model('user', user.schema())
|
|
479
|
+
|
|
480
|
+
let bird1 = await db.bird.insert({ data: bird.mock() })
|
|
481
|
+
let user1 = await db.user.insert({ data: user.mock(bird1) })
|
|
482
|
+
|
|
483
|
+
// augmented blacklist
|
|
484
|
+
let find2 = await db.user.findOneAndUpdate({
|
|
485
|
+
query: user1._id,
|
|
486
|
+
data: { dog: 'Bruce2', pet: 'Freddy2' }, // pet shouldn't update
|
|
487
|
+
blacklist: ['pet', 'deep', 'deepModel', '-dog', '-animals.cat'],
|
|
488
|
+
})
|
|
489
|
+
expect(find2).toEqual({
|
|
490
|
+
_id: user1._id,
|
|
491
|
+
bird: bird1._id,
|
|
492
|
+
dog: 'Bruce2',
|
|
493
|
+
list: [44, 54],
|
|
494
|
+
pets: [{ name: 'Pluto' }, { name: 'Milo' }],
|
|
495
|
+
animals: { dog: 'Max', cat: 'Ginger' },
|
|
496
|
+
})
|
|
497
|
+
expect(await db.user.findOne({ query: user1._id, project: ['pet'] })).toEqual({
|
|
498
|
+
_id: user1._id,
|
|
499
|
+
pet: 'Freddy',
|
|
500
|
+
})
|
|
501
|
+
|
|
502
|
+
db.close()
|
|
503
|
+
})
|
|
504
|
+
|
|
505
|
+
test('findOneAndUpdate blacklisting populate', async () => {
|
|
506
|
+
let db = (await opendb(null)).db
|
|
507
|
+
db.model('bird', bird.schema())
|
|
508
|
+
db.model('user', user.schema())
|
|
509
|
+
|
|
510
|
+
let bird1 = await db.bird.insert({ data: bird.mock() })
|
|
511
|
+
let user1 = await db.user.insert({ data: user.mock(bird1) })
|
|
512
|
+
|
|
513
|
+
// augmented blacklist
|
|
514
|
+
let find2 = await db.user.findOneAndUpdate({
|
|
515
|
+
query: user1._id,
|
|
516
|
+
data: { dog: 'Bruce2', pet: 'Freddy2' }, // pet shouldn't update
|
|
517
|
+
blacklist: [
|
|
518
|
+
'pet', 'deep', 'deepModel', '-dog', '-animals.cat',
|
|
519
|
+
'bird.name', '-bird', 'bird.height' // <- populated model
|
|
520
|
+
],
|
|
521
|
+
populate: ['bird'],
|
|
522
|
+
})
|
|
523
|
+
expect(find2).toEqual({
|
|
524
|
+
_id: user1._id,
|
|
525
|
+
bird: {
|
|
526
|
+
...util.omit(bird1, ['height']),
|
|
527
|
+
},
|
|
528
|
+
dog: 'Bruce2',
|
|
529
|
+
list: [44, 54],
|
|
530
|
+
pets: [{ name: 'Pluto' }, { name: 'Milo' }],
|
|
531
|
+
animals: { dog: 'Max', cat: 'Ginger' },
|
|
532
|
+
})
|
|
533
|
+
expect(await db.user.findOne({ query: user1._id, project: ['pet'] })).toEqual({
|
|
534
|
+
_id: user1._id,
|
|
535
|
+
pet: 'Freddy',
|
|
536
|
+
})
|
|
537
|
+
|
|
526
538
|
db.close()
|
|
527
539
|
})
|
|
528
540
|
|
package/test/crud.js
CHANGED
|
@@ -145,7 +145,7 @@ module.exports = function(monastery, opendb) {
|
|
|
145
145
|
db2.close()
|
|
146
146
|
})
|
|
147
147
|
|
|
148
|
-
test('update
|
|
148
|
+
test('update general', async () => {
|
|
149
149
|
let db = (await opendb(null)).db
|
|
150
150
|
let user = db.model('user', {
|
|
151
151
|
fields: {
|
|
@@ -423,14 +423,72 @@ module.exports = function(monastery, opendb) {
|
|
|
423
423
|
db.close()
|
|
424
424
|
})
|
|
425
425
|
|
|
426
|
-
test('
|
|
426
|
+
test('findOneAndUpdate general', async () => {
|
|
427
|
+
// todo: test all findOneAndUpdate options
|
|
428
|
+
// todo: test find & update hooks
|
|
427
429
|
let db = (await opendb(null)).db
|
|
430
|
+
let dog = db.model('dog', {
|
|
431
|
+
fields: {
|
|
432
|
+
name: { type: 'string', default: 'Scruff' },
|
|
433
|
+
}
|
|
434
|
+
})
|
|
428
435
|
let user = db.model('user', {
|
|
436
|
+
fields: {
|
|
437
|
+
name: { type: 'string', default: 'Martin' },
|
|
438
|
+
dog: { model: 'dog' },
|
|
439
|
+
}
|
|
440
|
+
})
|
|
441
|
+
|
|
442
|
+
// Returns omitted field after update, i.e. dog
|
|
443
|
+
let dog1 = await dog.insert({ data: {} })
|
|
444
|
+
let user1 = await user.insert({ data: { dog: dog1._id }})
|
|
445
|
+
expect(await user.findOneAndUpdate({ query: { _id: user1._id }, data: { name: 'Martin2' }})).toEqual({
|
|
446
|
+
_id: user1._id,
|
|
447
|
+
name: 'Martin2',
|
|
448
|
+
dog: dog1._id,
|
|
449
|
+
})
|
|
450
|
+
|
|
451
|
+
// Returns omitted field requiring population after update, i.e. dog
|
|
452
|
+
expect(await user.findOneAndUpdate({
|
|
453
|
+
query: { _id: user1._id },
|
|
454
|
+
data: { name: 'Martin2' },
|
|
455
|
+
populate: ['dog'],
|
|
456
|
+
})).toEqual({
|
|
457
|
+
_id: user1._id,
|
|
458
|
+
name: 'Martin2',
|
|
459
|
+
dog: {
|
|
460
|
+
_id: dog1._id,
|
|
461
|
+
name: 'Scruff'
|
|
462
|
+
},
|
|
463
|
+
})
|
|
464
|
+
|
|
465
|
+
// Error finding document to update
|
|
466
|
+
expect(await user.findOneAndUpdate({
|
|
467
|
+
query: { _id: db.id() },
|
|
468
|
+
data: { name: 'Martin2' },
|
|
469
|
+
})).toEqual(null)
|
|
470
|
+
|
|
471
|
+
// Error finding document to update (populate)
|
|
472
|
+
expect(await user.findOneAndUpdate({
|
|
473
|
+
query: { _id: db.id() },
|
|
474
|
+
data: { name: 'Martin2' },
|
|
475
|
+
populate: ['dog'],
|
|
476
|
+
})).toEqual(null)
|
|
477
|
+
|
|
478
|
+
db.close()
|
|
479
|
+
})
|
|
480
|
+
|
|
481
|
+
test('remove general', async () => {
|
|
482
|
+
let db = (await opendb(null)).db
|
|
483
|
+
let user = db.model('userRemove', {
|
|
429
484
|
fields: {
|
|
430
485
|
name: { type: 'string' },
|
|
431
486
|
},
|
|
432
487
|
})
|
|
433
488
|
|
|
489
|
+
// Clear (incase of failed a test)
|
|
490
|
+
user._remove({}, { multi: true })
|
|
491
|
+
|
|
434
492
|
// Insert multiple
|
|
435
493
|
let inserted2 = await user.insert({ data: [{ name: 'Martin' }, { name: 'Martin' }, { name: 'Martin' }]})
|
|
436
494
|
expect(inserted2).toEqual([
|