@ditojs/server 2.0.4 → 2.1.0

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.
Files changed (54) hide show
  1. package/package.json +11 -13
  2. package/src/app/Application.js +228 -212
  3. package/src/app/Validator.js +53 -43
  4. package/src/cli/console.js +6 -4
  5. package/src/cli/db/createMigration.js +59 -30
  6. package/src/cli/db/migrate.js +6 -4
  7. package/src/cli/db/reset.js +8 -5
  8. package/src/cli/db/rollback.js +6 -4
  9. package/src/cli/db/seed.js +2 -1
  10. package/src/cli/index.js +1 -1
  11. package/src/controllers/AdminController.js +100 -84
  12. package/src/controllers/CollectionController.js +37 -30
  13. package/src/controllers/Controller.js +83 -43
  14. package/src/controllers/ControllerAction.js +27 -15
  15. package/src/controllers/ModelController.js +4 -1
  16. package/src/controllers/RelationController.js +19 -21
  17. package/src/controllers/UsersController.js +3 -4
  18. package/src/decorators/parameters.js +3 -1
  19. package/src/decorators/scope.js +1 -1
  20. package/src/errors/ControllerError.js +2 -1
  21. package/src/errors/DatabaseError.js +20 -11
  22. package/src/graph/DitoGraphProcessor.js +48 -40
  23. package/src/graph/expression.js +6 -8
  24. package/src/graph/graph.js +20 -11
  25. package/src/lib/EventEmitter.js +12 -12
  26. package/src/middleware/handleConnectMiddleware.js +16 -10
  27. package/src/middleware/handleError.js +6 -5
  28. package/src/middleware/handleSession.js +78 -0
  29. package/src/middleware/handleUser.js +2 -2
  30. package/src/middleware/index.js +2 -0
  31. package/src/middleware/logRequests.js +3 -3
  32. package/src/middleware/setupRequestStorage.js +14 -0
  33. package/src/mixins/AssetMixin.js +62 -58
  34. package/src/mixins/SessionMixin.js +13 -10
  35. package/src/mixins/TimeStampedMixin.js +33 -29
  36. package/src/mixins/UserMixin.js +130 -116
  37. package/src/models/Model.js +245 -194
  38. package/src/models/definitions/filters.js +14 -13
  39. package/src/query/QueryBuilder.js +252 -195
  40. package/src/query/QueryFilters.js +3 -3
  41. package/src/query/QueryParameters.js +2 -2
  42. package/src/query/Registry.js +8 -10
  43. package/src/schema/keywords/_validate.js +10 -8
  44. package/src/schema/properties.test.js +247 -206
  45. package/src/schema/relations.js +42 -20
  46. package/src/schema/relations.test.js +36 -19
  47. package/src/services/Service.js +8 -14
  48. package/src/storage/S3Storage.js +5 -3
  49. package/src/storage/Storage.js +16 -14
  50. package/src/utils/function.js +7 -4
  51. package/src/utils/function.test.js +30 -6
  52. package/src/utils/object.test.js +5 -1
  53. package/types/index.d.ts +244 -257
  54. package/src/app/SessionStore.js +0 -31
@@ -22,10 +22,12 @@ describe('convertSchema()', () => {
22
22
  type: 'array'
23
23
  }
24
24
  }
25
- expect(convertSchema({
26
- type: 'object',
27
- properties
28
- })).toEqual({
25
+ expect(
26
+ convertSchema({
27
+ type: 'object',
28
+ properties
29
+ })
30
+ ).toEqual({
29
31
  type: 'object',
30
32
  properties,
31
33
  additionalProperties: false
@@ -59,10 +61,12 @@ describe('convertSchema()', () => {
59
61
  default: null
60
62
  }
61
63
  }
62
- expect(convertSchema({
63
- type: 'object',
64
- properties
65
- })).toEqual({
64
+ expect(
65
+ convertSchema({
66
+ type: 'object',
67
+ properties
68
+ })
69
+ ).toEqual({
66
70
  type: 'object',
67
71
  properties,
68
72
  additionalProperties: false
@@ -70,14 +74,16 @@ describe('convertSchema()', () => {
70
74
  })
71
75
 
72
76
  it(`expands 'text' types to 'string' JSON schema types`, () => {
73
- expect(convertSchema({
74
- type: 'object',
75
- properties: {
76
- myText: {
77
- type: 'text'
77
+ expect(
78
+ convertSchema({
79
+ type: 'object',
80
+ properties: {
81
+ myText: {
82
+ type: 'text'
83
+ }
78
84
  }
79
- }
80
- })).toEqual({
85
+ })
86
+ ).toEqual({
81
87
  type: 'object',
82
88
  properties: {
83
89
  myText: {
@@ -89,19 +95,21 @@ describe('convertSchema()', () => {
89
95
  })
90
96
 
91
97
  it('adds `required` arrays and formats for required properties', () => {
92
- expect(convertSchema({
93
- type: 'object',
94
- properties: {
95
- myString: {
96
- type: 'string',
97
- required: true
98
- },
99
- myNumber: {
100
- type: 'number',
101
- required: true
98
+ expect(
99
+ convertSchema({
100
+ type: 'object',
101
+ properties: {
102
+ myString: {
103
+ type: 'string',
104
+ required: true
105
+ },
106
+ myNumber: {
107
+ type: 'number',
108
+ required: true
109
+ }
102
110
  }
103
- }
104
- })).toEqual({
111
+ })
112
+ ).toEqual({
105
113
  type: 'object',
106
114
  properties: {
107
115
  myString: {
@@ -119,14 +127,16 @@ describe('convertSchema()', () => {
119
127
  })
120
128
 
121
129
  it('preserves JSON schema-style `required` arrays', () => {
122
- expect(convertSchema({
123
- type: 'object',
124
- required: ['myString', 'myNumber'],
125
- properties: {
126
- myString: { type: 'string' },
127
- myNumber: { type: 'number' }
128
- }
129
- })).toEqual({
130
+ expect(
131
+ convertSchema({
132
+ type: 'object',
133
+ required: ['myString', 'myNumber'],
134
+ properties: {
135
+ myString: { type: 'string' },
136
+ myNumber: { type: 'number' }
137
+ }
138
+ })
139
+ ).toEqual({
130
140
  type: 'object',
131
141
  properties: {
132
142
  myString: { type: 'string' },
@@ -138,15 +148,17 @@ describe('convertSchema()', () => {
138
148
  })
139
149
 
140
150
  it(`expands 'object' schemas with properties to JSON schemas allowing no additional properties`, () => {
141
- expect(convertSchema({
142
- type: 'object',
143
- properties: {
144
- myText: {
145
- type: 'object',
146
- properties: {}
151
+ expect(
152
+ convertSchema({
153
+ type: 'object',
154
+ properties: {
155
+ myText: {
156
+ type: 'object',
157
+ properties: {}
158
+ }
147
159
  }
148
- }
149
- })).toEqual({
160
+ })
161
+ ).toEqual({
150
162
  type: 'object',
151
163
  properties: {
152
164
  myText: {
@@ -160,16 +172,18 @@ describe('convertSchema()', () => {
160
172
  })
161
173
 
162
174
  it('preserves pre-existing settings for no additional properties', () => {
163
- expect(convertSchema({
164
- type: 'object',
165
- properties: {
166
- myText: {
167
- type: 'object',
168
- additionalProperties: true,
169
- properties: {}
175
+ expect(
176
+ convertSchema({
177
+ type: 'object',
178
+ properties: {
179
+ myText: {
180
+ type: 'object',
181
+ additionalProperties: true,
182
+ properties: {}
183
+ }
170
184
  }
171
- }
172
- })).toEqual({
185
+ })
186
+ ).toEqual({
173
187
  type: 'object',
174
188
  properties: {
175
189
  myText: {
@@ -183,20 +197,22 @@ describe('convertSchema()', () => {
183
197
  })
184
198
 
185
199
  it('expands nested object schemas with required properties', () => {
186
- expect(convertSchema({
187
- type: 'object',
188
- properties: {
189
- myText: {
190
- type: 'object',
191
- properties: {
192
- myProperty: {
193
- type: 'text',
194
- required: true
200
+ expect(
201
+ convertSchema({
202
+ type: 'object',
203
+ properties: {
204
+ myText: {
205
+ type: 'object',
206
+ properties: {
207
+ myProperty: {
208
+ type: 'text',
209
+ required: true
210
+ }
195
211
  }
196
212
  }
197
213
  }
198
- }
199
- })).toEqual({
214
+ })
215
+ ).toEqual({
200
216
  type: 'object',
201
217
  properties: {
202
218
  myText: {
@@ -216,19 +232,21 @@ describe('convertSchema()', () => {
216
232
  })
217
233
 
218
234
  it(`expands 'object' schemas with patternProperties`, () => {
219
- expect(convertSchema({
220
- type: 'object',
221
- properties: {
222
- myText: {
223
- type: 'object',
224
- patternProperties: {
225
- '^.*$': {
226
- type: 'text'
235
+ expect(
236
+ convertSchema({
237
+ type: 'object',
238
+ properties: {
239
+ myText: {
240
+ type: 'object',
241
+ patternProperties: {
242
+ '^.*$': {
243
+ type: 'text'
244
+ }
227
245
  }
228
246
  }
229
247
  }
230
- }
231
- })).toEqual({
248
+ })
249
+ ).toEqual({
232
250
  type: 'object',
233
251
  properties: {
234
252
  myText: {
@@ -246,20 +264,22 @@ describe('convertSchema()', () => {
246
264
  })
247
265
 
248
266
  it('expands datetime types to their JSON schema representation', () => {
249
- expect(convertSchema({
250
- type: 'object',
251
- properties: {
252
- myDate: {
253
- type: 'date'
254
- },
255
- myDateTime: {
256
- type: 'datetime'
257
- },
258
- myTimeStamp: {
259
- type: 'timestamp'
267
+ expect(
268
+ convertSchema({
269
+ type: 'object',
270
+ properties: {
271
+ myDate: {
272
+ type: 'date'
273
+ },
274
+ myDateTime: {
275
+ type: 'datetime'
276
+ },
277
+ myTimeStamp: {
278
+ type: 'timestamp'
279
+ }
260
280
  }
261
- }
262
- })).toEqual({
281
+ })
282
+ ).toEqual({
263
283
  type: 'object',
264
284
  properties: {
265
285
  myDate: {
@@ -280,14 +300,16 @@ describe('convertSchema()', () => {
280
300
  })
281
301
 
282
302
  it('expands unrecognized types to `$ref` references', () => {
283
- expect(convertSchema({
284
- type: 'object',
285
- properties: {
286
- myModel: {
287
- type: 'MyModel'
303
+ expect(
304
+ convertSchema({
305
+ type: 'object',
306
+ properties: {
307
+ myModel: {
308
+ type: 'MyModel'
309
+ }
288
310
  }
289
- }
290
- })).toEqual({
311
+ })
312
+ ).toEqual({
291
313
  type: 'object',
292
314
  properties: {
293
315
  myModel: {
@@ -299,16 +321,21 @@ describe('convertSchema()', () => {
299
321
  })
300
322
 
301
323
  it(`expands unrecognized types to \`instanceof\` keywords when the \`useInstanceOf\` option is provided`, () => {
302
- expect(convertSchema({
303
- type: 'object',
304
- properties: {
305
- myModel: {
306
- type: 'MyModel'
324
+ expect(
325
+ convertSchema(
326
+ {
327
+ type: 'object',
328
+ properties: {
329
+ myModel: {
330
+ type: 'MyModel'
331
+ }
332
+ }
333
+ },
334
+ {
335
+ useInstanceOf: true
307
336
  }
308
- }
309
- }, {
310
- useInstanceOf: true
311
- })).toEqual({
337
+ )
338
+ ).toEqual({
312
339
  type: 'object',
313
340
  properties: {
314
341
  myModel: {
@@ -321,15 +348,17 @@ describe('convertSchema()', () => {
321
348
  })
322
349
 
323
350
  it('handles `nullable: true` correctly (now natively supported)', () => {
324
- expect(convertSchema({
325
- type: 'object',
326
- properties: {
327
- myString: {
328
- type: 'string',
329
- nullable: true
351
+ expect(
352
+ convertSchema({
353
+ type: 'object',
354
+ properties: {
355
+ myString: {
356
+ type: 'string',
357
+ nullable: true
358
+ }
330
359
  }
331
- }
332
- })).toEqual({
360
+ })
361
+ ).toEqual({
333
362
  type: 'object',
334
363
  properties: {
335
364
  myString: {
@@ -342,15 +371,17 @@ describe('convertSchema()', () => {
342
371
  })
343
372
 
344
373
  it(`handles \`nullable: true\` references correctly`, () => {
345
- expect(convertSchema({
346
- type: 'object',
347
- properties: {
348
- myModel: {
349
- type: 'MyModel',
350
- nullable: true
374
+ expect(
375
+ convertSchema({
376
+ type: 'object',
377
+ properties: {
378
+ myModel: {
379
+ type: 'MyModel',
380
+ nullable: true
381
+ }
351
382
  }
352
- }
353
- })).toEqual({
383
+ })
384
+ ).toEqual({
354
385
  type: 'object',
355
386
  properties: {
356
387
  myModel: {
@@ -365,15 +396,17 @@ describe('convertSchema()', () => {
365
396
  })
366
397
 
367
398
  it(`handles \`nullable: true\` dates correctly (now natively supported)`, () => {
368
- expect(convertSchema({
369
- type: 'object',
370
- properties: {
371
- myDate: {
372
- type: 'date',
373
- nullable: true
399
+ expect(
400
+ convertSchema({
401
+ type: 'object',
402
+ properties: {
403
+ myDate: {
404
+ type: 'date',
405
+ nullable: true
406
+ }
374
407
  }
375
- }
376
- })).toEqual({
408
+ })
409
+ ).toEqual({
377
410
  type: 'object',
378
411
  properties: {
379
412
  myDate: {
@@ -387,16 +420,18 @@ describe('convertSchema()', () => {
387
420
  })
388
421
 
389
422
  it(`handles \`nullable: true\` enums correctly`, () => {
390
- expect(convertSchema({
391
- type: 'object',
392
- properties: {
393
- myEnum: {
394
- type: 'string',
395
- enum: ['one', 'two', 'three'],
396
- nullable: true
423
+ expect(
424
+ convertSchema({
425
+ type: 'object',
426
+ properties: {
427
+ myEnum: {
428
+ type: 'string',
429
+ enum: ['one', 'two', 'three'],
430
+ nullable: true
431
+ }
397
432
  }
398
- }
399
- })).toEqual({
433
+ })
434
+ ).toEqual({
400
435
  type: 'object',
401
436
  properties: {
402
437
  myEnum: {
@@ -410,44 +445,46 @@ describe('convertSchema()', () => {
410
445
  })
411
446
 
412
447
  it('converts schemas within oneOf properties', () => {
413
- expect(convertSchema({
414
- type: 'object',
415
- properties: {
416
- myList: {
417
- type: 'array',
418
- items: {
419
- oneOf: [
420
- {
421
- type: 'object',
422
- properties: {
423
- prop1: {
424
- type: 'string',
425
- required: true
426
- },
427
- prop2: {
428
- type: 'number',
429
- required: true
448
+ expect(
449
+ convertSchema({
450
+ type: 'object',
451
+ properties: {
452
+ myList: {
453
+ type: 'array',
454
+ items: {
455
+ oneOf: [
456
+ {
457
+ type: 'object',
458
+ properties: {
459
+ prop1: {
460
+ type: 'string',
461
+ required: true
462
+ },
463
+ prop2: {
464
+ type: 'number',
465
+ required: true
466
+ }
430
467
  }
431
- }
432
- },
433
- {
434
- type: 'object',
435
- properties: {
436
- prop3: {
437
- type: 'string',
438
- required: true
439
- },
440
- prop4: {
441
- type: 'number',
442
- required: true
468
+ },
469
+ {
470
+ type: 'object',
471
+ properties: {
472
+ prop3: {
473
+ type: 'string',
474
+ required: true
475
+ },
476
+ prop4: {
477
+ type: 'number',
478
+ required: true
479
+ }
443
480
  }
444
481
  }
445
- }
446
- ]
482
+ ]
483
+ }
447
484
  }
448
485
  }
449
- }
450
- })).toEqual({
486
+ })
487
+ ).toEqual({
451
488
  type: 'object',
452
489
  properties: {
453
490
  myList: {
@@ -493,25 +530,27 @@ describe('convertSchema()', () => {
493
530
  })
494
531
 
495
532
  it('supports `required: true` on object', () => {
496
- expect(convertSchema({
497
- type: 'object',
498
- properties: {
499
- myObject: {
500
- type: 'object',
501
- required: true,
502
- properties: {
503
- prop1: {
504
- type: 'string',
505
- required: true
506
- },
507
- prop2: {
508
- type: 'number',
509
- required: true
533
+ expect(
534
+ convertSchema({
535
+ type: 'object',
536
+ properties: {
537
+ myObject: {
538
+ type: 'object',
539
+ required: true,
540
+ properties: {
541
+ prop1: {
542
+ type: 'string',
543
+ required: true
544
+ },
545
+ prop2: {
546
+ type: 'number',
547
+ required: true
548
+ }
510
549
  }
511
550
  }
512
551
  }
513
- }
514
- })).toEqual({
552
+ })
553
+ ).toEqual({
515
554
  type: 'object',
516
555
  properties: {
517
556
  myObject: {
@@ -537,31 +576,33 @@ describe('convertSchema()', () => {
537
576
  })
538
577
 
539
578
  it('processes discriminator schemas correctly', () => {
540
- expect(convertSchema({
541
- type: 'object',
542
- discriminator: { propertyName: 'foo' },
543
- required: ['foo'],
544
- oneOf: [
545
- {
546
- properties: {
547
- foo: { const: 'x' },
548
- a: {
549
- type: 'string',
550
- required: true
579
+ expect(
580
+ convertSchema({
581
+ type: 'object',
582
+ discriminator: { propertyName: 'foo' },
583
+ required: ['foo'],
584
+ oneOf: [
585
+ {
586
+ properties: {
587
+ foo: { const: 'x' },
588
+ a: {
589
+ type: 'string',
590
+ required: true
591
+ }
551
592
  }
552
- }
553
- },
554
- {
555
- properties: {
556
- foo: { enum: ['y', 'z'] },
557
- b: {
558
- type: 'string',
559
- required: true
593
+ },
594
+ {
595
+ properties: {
596
+ foo: { enum: ['y', 'z'] },
597
+ b: {
598
+ type: 'string',
599
+ required: true
600
+ }
560
601
  }
561
602
  }
562
- }
563
- ]
564
- })).toEqual({
603
+ ]
604
+ })
605
+ ).toEqual({
565
606
  type: 'object',
566
607
  discriminator: { propertyName: 'foo' },
567
608
  required: ['foo'],