monastery 1.36.0 → 1.36.3

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.
@@ -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
7
  test('find blacklisting basic', async () => {
4
8
  // Setup
5
9
  let db = (await opendb(null)).db
6
- let bird = db.model('bird', {
7
- fields: {
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())
12
+
13
+ let bird1 = await db.bird.insert({ data: bird.mock() })
14
+ let user1 = await db.user.insert({ data: user.mock(bird1) })
95
15
 
96
16
  // initial blacklist
97
- let find1 = await user.findOne({
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' }],
@@ -108,13 +29,14 @@ module.exports = function(monastery, opendb) {
108
29
  })
109
30
 
110
31
  // augmented blacklist
111
- let find2 = await user.findOne({
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
116
37
  expect(find2).toEqual((customBlacklist = {
117
38
  _id: user1._id,
39
+ bird: bird1._id,
118
40
  dog: 'Bruce',
119
41
  list: [44, 54],
120
42
  pets: [{ name: 'Pluto' }, { name: 'Milo' }],
@@ -122,119 +44,84 @@ module.exports = function(monastery, opendb) {
122
44
  }))
123
45
 
124
46
  // blacklist string
125
- let find3 = await user.findOne({
47
+ let find3 = await db.user.findOne({
126
48
  query: user1._id,
127
49
  blacklist: 'pet pet deep deepModel -dog -animals.cat'
128
50
  })
129
51
  expect(find3).toEqual(customBlacklist)
130
52
 
131
53
  // blacklist removal
132
- let find4 = await user.findOne({ query: user1._id, blacklist: false })
54
+ let find4 = await db.user.findOne({ query: user1._id, blacklist: false })
133
55
  expect(find4).toEqual(user1)
134
56
 
135
57
  db.close()
136
58
  })
137
59
 
138
60
  test('find blacklisting population', async () => {
139
- // inprogresss
140
61
  // Setup
141
- let db = monastery('localhost/monastery', {
142
- timestamps: false,
143
- serverSelectionTimeoutMS: 2000,
144
- })
145
- let bird = db.model('bird', {
146
- fields: {
147
- color: { type: 'string', default: 'red' },
148
- height: { type: 'number' },
149
- name: { type: 'string' },
150
- sub: {
151
- color: { type: 'string', default: 'red' },
152
- },
153
- subs: [{
154
- color: { type: 'string', default: 'red'},
155
- }],
156
- wing: {
157
- size: { type: 'number' },
158
- sizes: {
159
- one: { type: 'number' },
160
- two: { type: 'number' },
161
- }
162
- },
163
- },
164
- findBL: ['wing']
165
- })
166
- let user = db.model('user', {
62
+ let db = (await opendb(null)).db
63
+ db.model('bird', bird.schema())
64
+ db.model('user', {
167
65
  fields: {
168
66
  dog: { type: 'string' },
169
- bird1: { model: 'bird' },
170
- bird2: { model: 'bird' },
171
- bird3: { model: 'bird' },
172
- bird4: { model: 'bird' },
173
- bird5: { model: 'bird' },
67
+ bird: { model: 'bird' },
174
68
  },
175
- findBL: [
176
- 'bird1.name', // bird1.name & bird1.wing blacklisted
177
- '-bird2', 'bird2.name', // bird2.name blacklisted
178
- 'bird3.name', '-bird3', 'bird3.height', // bird3.height blacklisted
179
- '-bird4.wing.sizes.one', '-bird4.wing.size', // ignored
180
- // bird4.wing.sizes.two blacklisted (expand in future verion)
181
- '-bird5.wing.sizes.one', // bird5.wing.sizes.one ignored, wing blacklisted
182
- // bird5.wing.sizes.two, wing.size blacklisted (expand in future verion)
183
- ]
184
69
  })
185
- let bird1 = await bird.insert({
186
- data: {
187
- name: 'ponyo',
188
- height: 40,
189
- sub: {},
190
- wing: { size: 1, sizes: { one: 1, two: 1 }}
191
- }
192
- })
193
- let userData = {
70
+
71
+ let bird1 = await db.bird.insert({ data: bird.mock() })
72
+ let user1 = await db.user.insert({ data: {
194
73
  dog: 'Bruce',
195
- bird1: bird1._id,
196
- bird2: bird1._id,
197
- bird3: bird1._id,
198
- bird4: bird1._id,
199
- bird5: bird1._id
74
+ bird: bird1._id,
75
+ }})
76
+
77
+ let bird1Base = {
78
+ _id: bird1._id,
79
+ color: 'red',
80
+ sub: { color: 'red' }
200
81
  }
201
- let user1 = await user.insert({ data: userData })
202
- let bird1Base = { _id: bird1._id, color: 'red', sub: { color: 'red' }}
203
82
 
204
- // Test bird1
205
- expect(await user.findOne({ query: user1._id, populate: ['bird1'] })).toEqual({
206
- ...userData,
207
- _id: user1._id,
208
- bird1: { ...bird1Base, height: 40 },
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 },
209
96
  })
210
- // Test bird2
211
- expect(await user.findOne({ query: user1._id, populate: ['bird2'] })).toEqual({
212
- ...userData,
213
- _id: user1._id,
214
- bird2: { ...bird1Base, height: 40, wing: { size: 1, sizes: { one: 1, two: 1 }} },
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 }} },
215
102
  })
216
- // Test bird3
217
- expect(await user.findOne({ query: user1._id, populate: ['bird3'] })).toEqual({
218
- ...userData,
219
- _id: user1._id,
220
- bird3: { ...bird1Base, name: 'ponyo', wing: { size: 1, sizes: { one: 1, two: 1 }} },
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 }} },
221
108
  })
222
- // Test bird4
223
- expect(await user.findOne({ query: user1._id, populate: ['bird4'] })).toEqual({
224
- ...userData,
225
- _id: user1._id,
226
- bird4: { ...bird1Base, name: 'ponyo', height: 40 },
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 },
227
114
  })
228
- // Test bird5
229
- expect(await user.findOne({ query: user1._id, populate: ['bird5'] })).toEqual({
230
- ...userData,
231
- _id: user1._id,
232
- bird5: { ...bird1Base, name: 'ponyo', height: 40 },
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 },
233
120
  })
234
121
  // blacklist removal
235
- expect(await user.findOne({ query: user1._id, blacklist: false, populate: ['bird1'] })).toEqual({
122
+ expect(await db.user.findOne({ query: user1._id, blacklist: false, populate: ['bird'] })).toEqual({
236
123
  ...user1,
237
- bird1: { ...bird1Base, height: 40, name: 'ponyo', wing: { size: 1, sizes: { one: 1, two: 1 }} },
124
+ bird: { ...bird1Base, height: 12, name: 'Ponyo', wing: { size: 1, sizes: { one: 1, two: 1 }} },
238
125
  })
239
126
 
240
127
  db.close()
@@ -583,4 +470,72 @@ module.exports = function(monastery, opendb) {
583
470
  db.close()
584
471
  })
585
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
+
538
+ db.close()
539
+ })
540
+
586
541
  }
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 basics', async () => {
148
+ test('update general', async () => {
149
149
  let db = (await opendb(null)).db
150
150
  let user = db.model('user', {
151
151
  fields: {
@@ -309,6 +309,77 @@ module.exports = function(monastery, opendb) {
309
309
  db.close()
310
310
  })
311
311
 
312
+ test('update mixing formData', async() => {
313
+ // Mixing data
314
+ let db = (await opendb(null)).db
315
+ let consignment = db.model('consignment', {
316
+ fields: {
317
+ specialInstructions: [{
318
+ text: { type: 'string' },
319
+ createdAt: { type: 'date' },
320
+ updatedByName: { type: 'string' },
321
+ importance: { type: 'string' },
322
+ }],
323
+ },
324
+ })
325
+ let inserted = await consignment.insert({
326
+ data: {
327
+ specialInstructions: [{
328
+ text: 'filler',
329
+ createdAt: 1653601752472,
330
+ updatedByName: 'Paul',
331
+ importance: 'low'
332
+ }]
333
+ }
334
+ })
335
+ let specialInstructions = [
336
+ {
337
+ text: 'POD added by driver',
338
+ createdAt: 1653603212886,
339
+ updatedByName: 'Paul Driver 3',
340
+ importance: 'low'
341
+ }, {
342
+ text: 'filler',
343
+ createdAt: 1653601752472,
344
+ updatedByName: 'Paul',
345
+ importance: 'low'
346
+ }
347
+ ]
348
+ // Key order maintained (not always guaranteed)
349
+ await consignment.update({
350
+ query: inserted._id,
351
+ data: {
352
+ 'specialInstructions[0][text]': 'filler',
353
+ 'specialInstructions[0][createdAt]': 1653601752472,
354
+ 'specialInstructions[0][updatedByName]': 'Paul',
355
+ 'specialInstructions[0][importance]': 'low',
356
+ specialInstructions: specialInstructions.map(o => ({ ...o })),
357
+ }
358
+ })
359
+ await expect(consignment.findOne({ query: inserted._id })).resolves.toEqual({
360
+ _id: expect.any(Object),
361
+ specialInstructions: specialInstructions,
362
+ })
363
+
364
+ // Key order maintained (not always guaranteed)
365
+ await consignment.update({
366
+ query: inserted._id,
367
+ data: {
368
+ specialInstructions: specialInstructions.map(o => ({ ...o })),
369
+ 'specialInstructions[0][text]': 'filler',
370
+ 'specialInstructions[0][createdAt]': 1653601752472,
371
+ 'specialInstructions[0][updatedByName]': 'Paul',
372
+ 'specialInstructions[0][importance]': 'low',
373
+ }
374
+ })
375
+ await expect(consignment.findOne({ query: inserted._id })).resolves.toEqual({
376
+ _id: expect.any(Object),
377
+ specialInstructions: [specialInstructions[1], specialInstructions[1]],
378
+ })
379
+
380
+ db.close()
381
+ })
382
+
312
383
  test('insert with id casting', async () => {
313
384
  let db = (await opendb(null)).db
314
385
  db.model('company', { fields: {
@@ -335,7 +406,7 @@ module.exports = function(monastery, opendb) {
335
406
 
336
407
  test('find default field population', async () => {
337
408
  let db = (await opendb(null)).db
338
- let user = db.model('user', {
409
+ db.model('user', {
339
410
  fields: {
340
411
  name: { type: 'string', default: 'Martin Luther' },
341
412
  addresses: [{ city: { type: 'string' }, country: { type: 'string', default: 'Germany' } }],
@@ -344,7 +415,7 @@ module.exports = function(monastery, opendb) {
344
415
  dogs: [{ model: 'dog' }], // virtual association
345
416
  }
346
417
  })
347
- let dog = db.model('dog', {
418
+ db.model('dog', {
348
419
  fields: {
349
420
  name: { type: 'string', default: 'Scruff' },
350
421
  user: { model: 'user' }
@@ -352,35 +423,35 @@ module.exports = function(monastery, opendb) {
352
423
  })
353
424
 
354
425
  // Default field doesn't override null
355
- let nulldoc1 = await dog.insert({ data: { name: null }})
356
- let nullfind1 = await dog.findOne({ query: nulldoc1._id })
426
+ let nulldoc1 = await db.dog.insert({ data: { name: null }})
427
+ let nullfind1 = await db.dog.findOne({ query: nulldoc1._id })
357
428
  expect(nullfind1).toEqual({ _id: nulldoc1._id, name: null })
358
429
 
359
430
  // Default field doesn't override empty string
360
- let nulldoc2 = await dog.insert({ data: { name: '' }})
361
- let nullfind2 = await dog.findOne({ query: nulldoc2._id })
431
+ let nulldoc2 = await db.dog.insert({ data: { name: '' }})
432
+ let nullfind2 = await db.dog.findOne({ query: nulldoc2._id })
362
433
  expect(nullfind2).toEqual({ _id: nulldoc2._id, name: '' })
363
434
 
364
435
  // Default field overrides undefined
365
- let nulldoc3 = await dog.insert({ data: { name: undefined }})
366
- let nullfind3 = await dog.findOne({ query: nulldoc3._id })
436
+ let nulldoc3 = await db.dog.insert({ data: { name: undefined }})
437
+ let nullfind3 = await db.dog.findOne({ query: nulldoc3._id })
367
438
  expect(nullfind3).toEqual({ _id: nullfind3._id, name: 'Scruff' })
368
439
 
369
440
  // Default field population test
370
- // Note that addresses.1.country shouldn't be overriden
441
+ // Note that addresses.1.country shouldn't be overridden
371
442
  // Insert documents (without defaults)
372
- let inserted = await dog._insert({})
373
- let inserted2 = await user._insert({
443
+ let dog1 = await db.dog._insert({})
444
+ let user1 = await db.user._insert({
374
445
  addresses: [
375
446
  { city: 'Frankfurt' },
376
447
  { city: 'Christchurch', country: 'New Zealand' }
377
448
  ],
378
- pet: { dog: inserted._id }
449
+ pet: { dog: dog1._id }
379
450
  })
380
- await dog._update(inserted._id, { $set: { user: inserted2._id }})
451
+ await db.dog._update(dog1._id, { $set: { user: user1._id }})
381
452
 
382
- let find1 = await user.findOne({
383
- query: inserted2._id,
453
+ let find1 = await db.user.findOne({
454
+ query: user1._id,
384
455
  populate: ['pet.dog', {
385
456
  from: 'dog',
386
457
  localField: '_id',
@@ -389,20 +460,20 @@ module.exports = function(monastery, opendb) {
389
460
  }]
390
461
  })
391
462
  expect(find1).toEqual({
392
- _id: inserted2._id,
463
+ _id: user1._id,
393
464
  name: 'Martin Luther',
394
465
  addresses: [
395
466
  { city: 'Frankfurt', country: 'Germany' },
396
467
  { city: 'Christchurch', country: 'New Zealand' }
397
468
  ],
398
469
  address: { country: 'Germany' },
399
- pet: { dog: { _id: inserted._id, name: 'Scruff', user: inserted2._id }},
400
- dogs: [{ _id: inserted._id, name: 'Scruff', user: inserted2._id }]
470
+ pet: { dog: { _id: dog1._id, name: 'Scruff', user: user1._id }},
471
+ dogs: [{ _id: dog1._id, name: 'Scruff', user: user1._id }]
401
472
  })
402
473
 
403
474
  // Blacklisted default field population test
404
- let find2 = await user.findOne({
405
- query: inserted2._id,
475
+ let find2 = await db.user.findOne({
476
+ query: user1._id,
406
477
  populate: ['pet.dog', {
407
478
  from: 'dog',
408
479
  localField: '_id',
@@ -413,19 +484,19 @@ module.exports = function(monastery, opendb) {
413
484
  // ^ great test (address should cancel addresses if not stopping at the .)
414
485
  })
415
486
  expect(find2).toEqual({
416
- _id: inserted2._id,
487
+ _id: user1._id,
417
488
  name: 'Martin Luther',
418
489
  addresses: [{ city: 'Frankfurt' }, { city: 'Christchurch' }],
419
- pet: { dog: { _id: inserted._id, name: 'Scruff', user: inserted2._id }},
420
- dogs: [{ _id: inserted._id, user: inserted2._id }]
490
+ pet: { dog: { _id: dog1._id, name: 'Scruff', user: user1._id }},
491
+ dogs: [{ _id: dog1._id, user: user1._id }]
421
492
  })
422
493
 
423
494
  db.close()
424
495
  })
425
496
 
426
- test('findOneAndUpdate basics', async () => {
497
+ test('findOneAndUpdate general', async () => {
427
498
  // todo: test all findOneAndUpdate options
428
-
499
+ // todo: test find & update hooks
429
500
  let db = (await opendb(null)).db
430
501
  let dog = db.model('dog', {
431
502
  fields: {
@@ -478,7 +549,7 @@ module.exports = function(monastery, opendb) {
478
549
  db.close()
479
550
  })
480
551
 
481
- test('remove basics', async () => {
552
+ test('remove general', async () => {
482
553
  let db = (await opendb(null)).db
483
554
  let user = db.model('userRemove', {
484
555
  fields: {