monastery 3.0.21 → 3.0.23

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/test/model.js CHANGED
@@ -21,7 +21,7 @@ test('model > model on manager', async () => {
21
21
  db2.close()
22
22
  })
23
23
 
24
- test('model setup', async () => {
24
+ test('model setup basics', async () => {
25
25
  // Setup
26
26
  let user = db.model('user', { fields: {
27
27
  name: { type: 'string' },
@@ -35,60 +35,81 @@ test('model setup', async () => {
35
35
  // no fields defined
36
36
  expect(db.model('user2', { fields: {} }).fields).toEqual({
37
37
  _id: {
38
+ schema: {
38
39
  insertOnly: true,
39
40
  isId: true,
41
+ isSchema: true,
42
+ isType: 'isId',
40
43
  type: 'id',
41
44
  },
45
+ },
42
46
  createdAt: {
47
+ schema: {
43
48
  default: expect.any(Function),
44
49
  insertOnly: true,
45
50
  isInteger: true,
51
+ isSchema: true,
52
+ isType: 'isInteger',
46
53
  timestampField: true,
47
54
  type: 'integer',
48
55
  },
49
- updatedAt: {
56
+ },
57
+ updatedAt: {
58
+ schema: {
50
59
  default: expect.any(Function),
51
60
  isInteger: true,
61
+ isSchema: true,
62
+ isType: 'isInteger',
52
63
  timestampField: true,
53
64
  type: 'integer',
54
65
  },
66
+ },
55
67
  })
56
68
 
57
69
  // Has model name
58
- expect(user.name).toEqual('user')
70
+ expect(user.name)
71
+ .toEqual('user')
59
72
 
60
73
  // Basic field
61
- expect(user.fields.name).toEqual({ type: 'string', isString: true })
74
+ expect(user.fields.name.schema)
75
+ .toEqual({ type: 'string', isString: true, isSchema: true, isType: 'isString' })
62
76
 
63
- // Image field
64
- expect(user.fields.logo).toEqual({ type: 'any', isAny: true, image: true })
77
+ // Image field (not processed by plugin)
78
+ expect(user.fields.logo.schema)
79
+ .toEqual({ type: 'any', isAny: true, image: true, isSchema: true, isType: 'isAny' })
65
80
 
66
81
  // Array field
67
- expect(user.fields.pets).toContainEqual({ type: 'string', isString: true })
82
+ expect(user.fields.pets)
83
+ .toContainEqual({ schema: { type: 'string', isString: true, isType: 'isString', isSchema: true } })
68
84
 
69
85
  // Array schema
70
- expect(user.fields.pets.schema).toEqual({ type: 'array', isArray: true })
86
+ expect(user.fields.pets.schema)
87
+ .toEqual({ type: 'array', isArray: true, isSchema: true, isType: 'isArray' })
71
88
 
72
89
  // Subdocument field and schema
73
90
  expect(user.fields.colors).toEqual({
74
- red: { isString: true, type: 'string' },
75
- schema: { isObject: true, type: 'object' },
91
+ red: { schema: { isString: true, type: 'string', isSchema: true, isType: 'isString' }},
92
+ schema: { isObject: true, type: 'object', isSchema: true, isType: 'isObject' },
76
93
  })
77
94
 
78
95
  // Array array field (no array properties)
79
96
  expect(JSON.stringify(user.fields.points)).toEqual(JSON.stringify(
80
- [[{ type: 'number', isNumber: true }]]
97
+ [[{
98
+ schema: { type: 'number', isType: 'isNumber', isNumber: true, isSchema: true },
99
+ }]]
81
100
  ))
82
101
 
83
102
  // Array array schema
84
- expect(user.fields.points.schema).toEqual({ type: 'array', isArray: true })
85
- expect(user.fields.points[0].schema).toEqual({ type: 'array', isArray: true })
103
+ expect(user.fields.points.schema).toEqual({ type: 'array', isArray: true, isSchema: true, isType: 'isArray' })
104
+ expect(user.fields.points[0].schema).toEqual({ type: 'array', isArray: true, isSchema: true, isType: 'isArray' })
86
105
 
87
106
  // Array array subdocument field (no array properties)
88
107
  expect(JSON.stringify(user.fields.points2)).toEqual(JSON.stringify(
89
108
  [[{
90
- x: { type: 'number', isNumber: true },
91
- schema: { type: 'object', isObject: true },
109
+ x: {
110
+ schema: { type: 'number', isType: 'isNumber', isNumber: true, isSchema: true },
111
+ },
112
+ schema: { type: 'object', isObject: true, isSchema: true, isType: 'isObject' },
92
113
  }]]
93
114
  ))
94
115
  })
@@ -97,22 +118,34 @@ test('model setup with default fields', async () => {
97
118
  // Default fields
98
119
  expect(db.model('user2', { fields: {} }).fields).toEqual({
99
120
  _id: {
100
- insertOnly: true,
101
- isId: true,
102
- type: 'id',
121
+ schema: {
122
+ insertOnly: true,
123
+ isId: true,
124
+ isSchema: true,
125
+ isType: 'isId',
126
+ type: 'id',
127
+ },
103
128
  },
104
129
  createdAt: {
105
- default: expect.any(Function),
106
- insertOnly: true,
107
- isInteger: true,
108
- timestampField: true,
109
- type: 'integer',
130
+ schema: {
131
+ default: expect.any(Function),
132
+ insertOnly: true,
133
+ isInteger: true,
134
+ isSchema: true,
135
+ isType: 'isInteger',
136
+ timestampField: true,
137
+ type: 'integer',
138
+ },
110
139
  },
111
140
  updatedAt: {
112
- default: expect.any(Function),
113
- isInteger: true,
114
- timestampField: true,
115
- type: 'integer',
141
+ schema: {
142
+ default: expect.any(Function),
143
+ isInteger: true,
144
+ isSchema: true,
145
+ isType: 'isInteger',
146
+ timestampField: true,
147
+ type: 'integer',
148
+ },
116
149
  },
117
150
  })
118
151
  })
@@ -131,13 +164,28 @@ test('model setup with default objects', async () => {
131
164
  expect(user.fields.pets.schema).toEqual({
132
165
  type: 'array',
133
166
  isArray: true,
167
+ isSchema: true,
168
+ isType: 'isArray',
134
169
  default: expect.any(Function),
135
170
  })
136
171
 
137
172
  // Subdocument field and schema
138
173
  expect(user.fields.colors).toEqual({
139
- red: { isString: true, type: 'string' },
140
- schema: { isObject: true, type: 'object', default: expect.any(Function) },
174
+ red: {
175
+ schema: {
176
+ isString: true,
177
+ type: 'string',
178
+ isSchema: true,
179
+ isType: 'isString',
180
+ },
181
+ },
182
+ schema: {
183
+ isObject: true,
184
+ type: 'object',
185
+ isSchema: true,
186
+ isType: 'isObject',
187
+ default: expect.any(Function),
188
+ },
141
189
  })
142
190
  db2.close()
143
191
  })
@@ -157,31 +205,45 @@ test('model setup with schema', async () => {
157
205
  // Object with schema
158
206
  expect(user.fields.pet).toEqual({
159
207
  name: {
160
- type: 'string',
161
- isString: true,
162
- minLength: 5,
208
+ schema: {
209
+ type: 'string',
210
+ isString: true,
211
+ isSchema: true,
212
+ isType: 'isString',
213
+ minLength: 5,
214
+ },
163
215
  },
164
216
  schema: {
165
217
  type: 'object',
166
218
  isObject: true,
219
+ isSchema: true,
220
+ isType: 'isObject',
167
221
  virtual: true,
168
222
  },
169
223
  })
170
224
  // Array with schema
171
225
  expect(user.fields.pets[0]).toEqual({
172
226
  name: {
173
- type: 'string',
174
- isString: true,
175
- minLength: 5,
227
+ schema: {
228
+ type: 'string',
229
+ isString: true,
230
+ isSchema: true,
231
+ isType: 'isString',
232
+ minLength: 5,
233
+ },
176
234
  },
177
235
  schema: {
178
236
  type: 'object',
179
237
  isObject: true,
238
+ isSchema: true,
239
+ isType: 'isObject',
180
240
  },
181
241
  })
182
242
  expect(user.fields.pets.schema).toEqual({
183
243
  type: 'array',
184
244
  isArray: true,
245
+ isSchema: true,
246
+ isType: 'isArray',
185
247
  virtual: true,
186
248
  })
187
249
  })
@@ -313,7 +375,7 @@ test('model setup with messages', async () => {
313
375
  })
314
376
  })
315
377
 
316
- test('model reserved rules', async () => {
378
+ test('model setup reserved rules', async () => {
317
379
  // Setup
318
380
  const db2 = monastery('127.0.0.1/monastery', { logLevel: 0 })
319
381
  let user = db2.model('user-model', {
@@ -338,7 +400,7 @@ test('model reserved rules', async () => {
338
400
  db2.close()
339
401
  })
340
402
 
341
- test('model indexes', async () => {
403
+ test('model indexes basic', async () => {
342
404
  // Setup: Need to test different types of indexes
343
405
  // Setup: Drop previously tested collections
344
406
  const allCollections = await db.db.listCollections().toArray()
@@ -430,7 +492,7 @@ test('model indexes', async () => {
430
492
  }])
431
493
  })
432
494
 
433
- test('model unique indexes', async () => {
495
+ test('model indexes unique', async () => {
434
496
  // Setup: Drop previously tested collections
435
497
  if ((await db.db.listCollections().toArray()).find(o => o.name == 'userUniqueIndex')) {
436
498
  await db.db.collection('userUniqueIndex').drop()
@@ -494,7 +556,7 @@ test('model unique indexes', async () => {
494
556
  })
495
557
  })
496
558
 
497
- test('model subdocument indexes', async () => {
559
+ test('model indexes subdocument', async () => {
498
560
  // Setup: Need to test different types of indexes
499
561
  // Setup: Drop previously tested collections
500
562
  if ((await db.db.listCollections().toArray()).find(o => o.name == 'userIndexSubdoc')) {
@@ -536,7 +598,7 @@ test('model subdocument indexes', async () => {
536
598
  }])
537
599
  })
538
600
 
539
- test('model array indexes', async () => {
601
+ test('model indexes array', async () => {
540
602
  // Setup: Need to test different types of indexes
541
603
  // Setup: Drop previously tested collections
542
604
  if ((await db.db.listCollections().toArray()).find(o => o.name == 'userIndexArray')) {
@@ -578,7 +640,7 @@ test('model array indexes', async () => {
578
640
  }])
579
641
  })
580
642
 
581
- test('model 2dsphere indexes', async () => {
643
+ test('model indexes 2dsphere', async () => {
582
644
  // Setup. The tested model needs to be unique as race condition issue arises when the same model
583
645
  // with text indexes are setup at the same time
584
646
  await db.model('user99', {
@@ -599,24 +661,40 @@ test('model 2dsphere indexes', async () => {
599
661
 
600
662
  // Schema check
601
663
  expect(db.user99.fields.location).toEqual({
602
- type: { type: 'string', default: 'Point', isString: true },
603
664
  coordinates: expect.any(Array),
665
+ type: {
666
+ schema: {
667
+ default: 'Point',
668
+ isSchema: true,
669
+ isString: true,
670
+ isType: 'isString',
671
+ type: 'string',
672
+ },
673
+ },
604
674
  schema: {
605
- default: undefined,
606
675
  index: '2dsphere',
607
676
  isObject: true,
608
- nullObject: undefined,
677
+ isSchema: true,
678
+ isType: 'isObject',
609
679
  type: 'object',
610
680
  },
611
681
  })
612
682
  expect(db.user99.fields.location2).toEqual({
613
- type: { type: 'string', default: 'Point', isString: true },
614
683
  coordinates: expect.any(Array),
684
+ type: {
685
+ schema: {
686
+ type: 'string',
687
+ default: 'Point',
688
+ isSchema: true,
689
+ isString: true,
690
+ isType: 'isString',
691
+ },
692
+ },
615
693
  schema: {
616
- default: undefined,
617
694
  index: { type: '2dsphere' },
618
695
  isObject: true,
619
- nullObject: undefined,
696
+ isSchema: true,
697
+ isType: 'isObject',
620
698
  type: 'object',
621
699
  },
622
700
  })
@@ -40,9 +40,13 @@ test('images no initialisation', async () => {
40
40
 
41
41
  // schema
42
42
  expect(db2.company.fields.logo).toEqual({
43
- image: true,
44
- isAny: true,
45
- type: 'any',
43
+ schema: {
44
+ image: true,
45
+ isAny: true,
46
+ isSchema: true,
47
+ isType: 'isAny',
48
+ type: 'any',
49
+ },
46
50
  })
47
51
 
48
52
  // found company
@@ -63,27 +67,45 @@ test('images no initialisation', async () => {
63
67
  })
64
68
 
65
69
  test('images initialisation', async () => {
66
- let user = db.model('user', { fields: {
67
- logo: { type: 'image' },
68
- logos: [{ type: 'image' }],
69
- users: [{ logo: { type: 'image' } }],
70
- }})
70
+ let user = db.model('user', {
71
+ fields: {
72
+ logo: { type: 'image' },
73
+ logos: [{ type: 'image' }],
74
+ users: [{ logo: { type: 'image' } }],
75
+ },
76
+ })
71
77
 
72
78
  // Initialisation success
73
79
  expect(db.opts.imagePlugin).toEqual(imagePluginFakeOpts)
74
80
 
81
+ function schemaForType(type) {
82
+ return {
83
+ schema: {
84
+ type: type,
85
+ isType: `is${util.ucFirst(type)}`,
86
+ [`is${util.ucFirst(type)}`]: true,
87
+ isSchema: true,
88
+ },
89
+ }
90
+ }
75
91
  let expected = {
76
- bucket: { type: 'string', isString: true },
77
- date: { type: 'number', isNumber: true },
78
- filename: { type: 'string', isString: true },
79
- filesize: { type: 'number', isNumber: true },
80
- metadata: { type: 'any', isAny: true },
81
- path: { type: 'string', isString: true },
92
+ bucket: schemaForType('string'),
93
+ date: schemaForType('number'),
94
+ filename: schemaForType('string'),
95
+ filesize: schemaForType('number'),
96
+ metadata: schemaForType('any'),
97
+ path: schemaForType('string'),
98
+ uid: schemaForType('string'),
82
99
  schema: {
83
- type: 'object', isObject: true, image: true, nullObject: true, default: undefined,
100
+ type: 'object',
101
+ isObject: true,
102
+ isSchema: true,
103
+ isType: 'isObject',
104
+ nullObject: true,
105
+ // Image rules
106
+ image: true,
84
107
  isImageObject: true,
85
108
  },
86
- uid: { type: 'string', isString: true },
87
109
  }
88
110
 
89
111
  // Logo schema
package/test/virtuals.js CHANGED
@@ -20,8 +20,8 @@ test('virtuals', async () => {
20
20
  name: { type: 'string' },
21
21
  },
22
22
  })
23
- expect(user.fields.name.virtual).toEqual(undefined)
24
- expect(user.fields.myBirds[0].virtual).toEqual(true)
23
+ expect(user.fields.name.schema.virtual).toEqual(undefined)
24
+ expect(user.fields.myBirds[0].schema.virtual).toEqual(true)
25
25
  expect(user.fields.myBirds.schema.virtual).toEqual(true)
26
26
 
27
27
  // Test insert/update/validate