@travetto/model 8.0.0-alpha.22 → 8.0.0-alpha.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/README.md +22 -23
- package/__index__.ts +6 -9
- package/package.json +7 -7
- package/src/error/exists.ts +1 -1
- package/src/error/invalid-index.ts +5 -3
- package/src/error/invalid-sub-type.ts +1 -1
- package/src/error/not-found.ts +1 -1
- package/src/registry/decorator.ts +25 -19
- package/src/registry/registry-adapter.ts +7 -7
- package/src/registry/registry-index.ts +8 -7
- package/src/registry/types.ts +3 -3
- package/src/types/basic.ts +2 -2
- package/src/types/blob.ts +2 -3
- package/src/types/bulk.ts +4 -8
- package/src/types/crud.ts +2 -4
- package/src/types/expiry.ts +2 -2
- package/src/types/model.ts +2 -2
- package/src/types/storage.ts +1 -2
- package/src/util/blob.ts +2 -2
- package/src/util/bulk.ts +6 -3
- package/src/util/crud.ts +38 -28
- package/src/util/expiry.ts +4 -5
- package/src/util/storage.ts +3 -4
- package/support/base-command.ts +3 -5
- package/support/bin/candidate.ts +8 -11
- package/support/bin/export.ts +2 -2
- package/support/bin/install.ts +2 -2
- package/support/cli.model_export.ts +5 -4
- package/support/cli.model_install.ts +5 -4
- package/support/doc.support.tsx +16 -18
- package/support/test/base.ts +9 -7
- package/support/test/basic.ts +10 -8
- package/support/test/blob.ts +7 -8
- package/support/test/bulk.ts +16 -5
- package/support/test/crud.ts +108 -90
- package/support/test/expiry.ts +39 -21
- package/support/test/polymorphism.ts +39 -31
- package/support/test/suite.ts +15 -12
package/support/test/crud.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import assert from 'node:assert';
|
|
2
2
|
import timers from 'node:timers/promises';
|
|
3
3
|
|
|
4
|
+
import { Model, type ModelCrudSupport, NotFoundError, PersistValue, PrePersist } from '@travetto/model';
|
|
5
|
+
import { Precision, Required, Schema, Text } from '@travetto/schema';
|
|
4
6
|
import { Suite, Test } from '@travetto/test';
|
|
5
|
-
import { Schema, Text, Precision, Required, } from '@travetto/schema';
|
|
6
|
-
import { type ModelCrudSupport, Model, NotFoundError, PersistValue, PrePersist } from '@travetto/model';
|
|
7
7
|
|
|
8
8
|
import { BaseModelSuite } from './base.ts';
|
|
9
9
|
|
|
@@ -42,7 +42,7 @@ class SimpleList {
|
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
@Model()
|
|
45
|
-
@PrePersist(
|
|
45
|
+
@PrePersist(item => {
|
|
46
46
|
item.name = `${item.name}-suffix`;
|
|
47
47
|
})
|
|
48
48
|
class User2 {
|
|
@@ -75,28 +75,27 @@ class BigIntModel {
|
|
|
75
75
|
|
|
76
76
|
@Suite()
|
|
77
77
|
export abstract class ModelCrudSuite extends BaseModelSuite<ModelCrudSupport> {
|
|
78
|
-
|
|
79
78
|
indexLimitSkew = 0;
|
|
80
79
|
|
|
81
80
|
@Test('save it')
|
|
82
81
|
async save() {
|
|
83
82
|
const service = await this.service;
|
|
84
83
|
|
|
85
|
-
const people = [1, 2, 3, 8].map(x =>
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
await Promise.all(
|
|
97
|
-
people.map(el => service.upsert(Person, el))
|
|
84
|
+
const people = [1, 2, 3, 8].map(x =>
|
|
85
|
+
Person.from({
|
|
86
|
+
id: service.idSource.create(),
|
|
87
|
+
name: 'Bob',
|
|
88
|
+
age: 20 + x,
|
|
89
|
+
gender: 'm',
|
|
90
|
+
address: {
|
|
91
|
+
street1: 'a',
|
|
92
|
+
...(x === 1 ? { street2: 'b' } : {})
|
|
93
|
+
}
|
|
94
|
+
})
|
|
98
95
|
);
|
|
99
96
|
|
|
97
|
+
await Promise.all(people.map(el => service.upsert(Person, el)));
|
|
98
|
+
|
|
100
99
|
const single = await service.get(Person, people[2].id);
|
|
101
100
|
assert(single !== undefined);
|
|
102
101
|
assert(single.age === 23);
|
|
@@ -122,15 +121,18 @@ export abstract class ModelCrudSuite extends BaseModelSuite<ModelCrudSupport> {
|
|
|
122
121
|
@Test('Verify partial update with field removal')
|
|
123
122
|
async testPartialUpdate() {
|
|
124
123
|
const service = await this.service;
|
|
125
|
-
const o = await service.create(
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
124
|
+
const o = await service.create(
|
|
125
|
+
Person,
|
|
126
|
+
Person.from({
|
|
127
|
+
name: 'bob',
|
|
128
|
+
age: 20,
|
|
129
|
+
gender: 'm',
|
|
130
|
+
address: {
|
|
131
|
+
street1: 'road',
|
|
132
|
+
street2: 'roader'
|
|
133
|
+
}
|
|
134
|
+
})
|
|
135
|
+
);
|
|
134
136
|
assert(o.id);
|
|
135
137
|
assert(o.name === 'bob');
|
|
136
138
|
|
|
@@ -147,7 +149,7 @@ export abstract class ModelCrudSuite extends BaseModelSuite<ModelCrudSupport> {
|
|
|
147
149
|
id: o2.id,
|
|
148
150
|
gender: 'f',
|
|
149
151
|
address: {
|
|
150
|
-
street1: 'changed\n'
|
|
152
|
+
street1: 'changed\n'
|
|
151
153
|
}
|
|
152
154
|
});
|
|
153
155
|
|
|
@@ -169,20 +171,23 @@ export abstract class ModelCrudSuite extends BaseModelSuite<ModelCrudSupport> {
|
|
|
169
171
|
@Test('Verify partial update with field removal and lists')
|
|
170
172
|
async testPartialUpdateList() {
|
|
171
173
|
const service = await this.service;
|
|
172
|
-
const o = await service.create(
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
174
|
+
const o = await service.create(
|
|
175
|
+
SimpleList,
|
|
176
|
+
SimpleList.from({
|
|
177
|
+
names: ['a', 'b', 'c'],
|
|
178
|
+
simples: [
|
|
179
|
+
{
|
|
180
|
+
name: 'a'
|
|
181
|
+
},
|
|
182
|
+
{
|
|
183
|
+
name: 'b'
|
|
184
|
+
},
|
|
185
|
+
{
|
|
186
|
+
name: 'c'
|
|
187
|
+
}
|
|
188
|
+
]
|
|
189
|
+
})
|
|
190
|
+
);
|
|
186
191
|
|
|
187
192
|
const o2 = await service.updatePartial(SimpleList, {
|
|
188
193
|
id: o.id,
|
|
@@ -197,9 +202,12 @@ export abstract class ModelCrudSuite extends BaseModelSuite<ModelCrudSupport> {
|
|
|
197
202
|
@Test('Verify partial update with field removal and lists')
|
|
198
203
|
async testBlankPartialUpdate() {
|
|
199
204
|
const service = await this.service;
|
|
200
|
-
const o = await service.create(
|
|
201
|
-
|
|
202
|
-
|
|
205
|
+
const o = await service.create(
|
|
206
|
+
User2,
|
|
207
|
+
User2.from({
|
|
208
|
+
name: 'bob'
|
|
209
|
+
})
|
|
210
|
+
);
|
|
203
211
|
|
|
204
212
|
assert(o.address === undefined);
|
|
205
213
|
assert(o.name === 'bob-suffix');
|
|
@@ -247,21 +255,21 @@ export abstract class ModelCrudSuite extends BaseModelSuite<ModelCrudSupport> {
|
|
|
247
255
|
async list() {
|
|
248
256
|
const service = await this.service;
|
|
249
257
|
|
|
250
|
-
const people = [1, 2, 3].map(x =>
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
await Promise.all(
|
|
262
|
-
people.map(el => service.upsert(Person, el))
|
|
258
|
+
const people = [1, 2, 3].map(x =>
|
|
259
|
+
Person.from({
|
|
260
|
+
id: service.idSource.create(),
|
|
261
|
+
name: 'Bob',
|
|
262
|
+
age: 20 + x,
|
|
263
|
+
gender: 'm',
|
|
264
|
+
address: {
|
|
265
|
+
street1: 'a',
|
|
266
|
+
...(x === 1 ? { street2: 'b' } : {})
|
|
267
|
+
}
|
|
268
|
+
})
|
|
263
269
|
);
|
|
264
270
|
|
|
271
|
+
await Promise.all(people.map(el => service.upsert(Person, el)));
|
|
272
|
+
|
|
265
273
|
const found = (await this.toArray(service.list(Person))).toSorted((a, b) => a.age - b.age);
|
|
266
274
|
|
|
267
275
|
assert(found[0].age === people[0].age);
|
|
@@ -274,16 +282,21 @@ export abstract class ModelCrudSuite extends BaseModelSuite<ModelCrudSupport> {
|
|
|
274
282
|
const service = await this.service;
|
|
275
283
|
|
|
276
284
|
await Promise.all(
|
|
277
|
-
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(x =>
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
285
|
+
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(x =>
|
|
286
|
+
service.upsert(
|
|
287
|
+
Person,
|
|
288
|
+
Person.from({
|
|
289
|
+
id: service.idSource.create(),
|
|
290
|
+
name: 'Bob',
|
|
291
|
+
age: 20 + x,
|
|
292
|
+
gender: 'm',
|
|
293
|
+
address: {
|
|
294
|
+
street1: 'a',
|
|
295
|
+
...(x === 1 ? { street2: 'b' } : {})
|
|
296
|
+
}
|
|
297
|
+
})
|
|
298
|
+
)
|
|
299
|
+
)
|
|
287
300
|
);
|
|
288
301
|
|
|
289
302
|
const controller = new AbortController();
|
|
@@ -307,15 +320,17 @@ export abstract class ModelCrudSuite extends BaseModelSuite<ModelCrudSupport> {
|
|
|
307
320
|
const service = await this.service;
|
|
308
321
|
|
|
309
322
|
const people = await Promise.all(
|
|
310
|
-
[1, 2, 3, 8].map((x, i) =>
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
323
|
+
[1, 2, 3, 8].map((x, i) =>
|
|
324
|
+
service[i % 2 === 0 ? 'upsert' : 'create'](Person, {
|
|
325
|
+
name: 'Bob',
|
|
326
|
+
age: 20 + x,
|
|
327
|
+
gender: 'm',
|
|
328
|
+
address: {
|
|
329
|
+
street1: 'a',
|
|
330
|
+
...(x === 1 ? { street2: 'b' } : {})
|
|
331
|
+
}
|
|
332
|
+
})
|
|
333
|
+
)
|
|
319
334
|
);
|
|
320
335
|
|
|
321
336
|
const single = await service.get(Person, people[2].id);
|
|
@@ -365,10 +380,7 @@ export abstract class ModelCrudSuite extends BaseModelSuite<ModelCrudSupport> {
|
|
|
365
380
|
const service = await this.service;
|
|
366
381
|
const o = await service.create(SimpleList, {
|
|
367
382
|
names: ['rob', 'tom'],
|
|
368
|
-
simples: [
|
|
369
|
-
{ name: 'roger' },
|
|
370
|
-
{ name: 'dodger' }
|
|
371
|
-
]
|
|
383
|
+
simples: [{ name: 'roger' }, { name: 'dodger' }]
|
|
372
384
|
});
|
|
373
385
|
assert(o.names.length === 2);
|
|
374
386
|
assert(o.simples);
|
|
@@ -390,10 +402,13 @@ export abstract class ModelCrudSuite extends BaseModelSuite<ModelCrudSupport> {
|
|
|
390
402
|
const service = await this.service;
|
|
391
403
|
|
|
392
404
|
// Create with bigint values
|
|
393
|
-
const created = await service.create(
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
405
|
+
const created = await service.create(
|
|
406
|
+
BigIntModel,
|
|
407
|
+
BigIntModel.from({
|
|
408
|
+
largeNumber: 9007199254740991n, // Number.MAX_SAFE_INTEGER as bigint
|
|
409
|
+
optionalBigInt: 1234567890123456789n
|
|
410
|
+
})
|
|
411
|
+
);
|
|
397
412
|
|
|
398
413
|
assert(created.id);
|
|
399
414
|
assert.strictEqual(created.largeNumber, 9007199254740991n);
|
|
@@ -405,11 +420,14 @@ export abstract class ModelCrudSuite extends BaseModelSuite<ModelCrudSupport> {
|
|
|
405
420
|
assert.strictEqual(retrieved.optionalBigInt, 1234567890123456789n);
|
|
406
421
|
|
|
407
422
|
// Update with new bigint value
|
|
408
|
-
const updated = await service.update(
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
423
|
+
const updated = await service.update(
|
|
424
|
+
BigIntModel,
|
|
425
|
+
BigIntModel.from({
|
|
426
|
+
id: created.id,
|
|
427
|
+
largeNumber: 18014398509481982n,
|
|
428
|
+
optionalBigInt: undefined
|
|
429
|
+
})
|
|
430
|
+
);
|
|
413
431
|
|
|
414
432
|
assert.strictEqual(updated.largeNumber, 18014398509481982n);
|
|
415
433
|
assert.strictEqual(updated.optionalBigInt, undefined);
|
|
@@ -419,4 +437,4 @@ export abstract class ModelCrudSuite extends BaseModelSuite<ModelCrudSupport> {
|
|
|
419
437
|
assert.strictEqual(final.largeNumber, 18014398509481982n);
|
|
420
438
|
assert(!final.optionalBigInt);
|
|
421
439
|
}
|
|
422
|
-
}
|
|
440
|
+
}
|
package/support/test/expiry.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import assert from 'node:assert';
|
|
2
2
|
import timers from 'node:timers/promises';
|
|
3
3
|
|
|
4
|
-
import { Suite, Test } from '@travetto/test';
|
|
5
4
|
import { type TimeSpan, TimeUtil } from '@travetto/runtime';
|
|
5
|
+
import { Suite, Test } from '@travetto/test';
|
|
6
6
|
|
|
7
|
+
import { NotFoundError } from '../../src/error/not-found.ts';
|
|
7
8
|
import { ExpiresAt, Model } from '../../src/registry/decorator.ts';
|
|
8
9
|
import type { ModelExpirySupport } from '../../src/types/expiry.ts';
|
|
9
10
|
import { ModelExpiryUtil } from '../../src/util/expiry.ts';
|
|
10
|
-
import { NotFoundError } from '../../src/error/not-found.ts';
|
|
11
11
|
import { BaseModelSuite } from './base.ts';
|
|
12
12
|
|
|
13
13
|
@Model('expiry-user')
|
|
@@ -20,7 +20,6 @@ export class ExpiryUser {
|
|
|
20
20
|
|
|
21
21
|
@Suite()
|
|
22
22
|
export abstract class ModelExpirySuite extends BaseModelSuite<ModelExpirySupport> {
|
|
23
|
-
|
|
24
23
|
delayFactor: number = 1;
|
|
25
24
|
|
|
26
25
|
async wait(input: TimeSpan | number | string) {
|
|
@@ -34,9 +33,12 @@ export abstract class ModelExpirySuite extends BaseModelSuite<ModelExpirySupport
|
|
|
34
33
|
@Test()
|
|
35
34
|
async basic() {
|
|
36
35
|
const service = await this.service;
|
|
37
|
-
const result = await service.upsert(
|
|
38
|
-
|
|
39
|
-
|
|
36
|
+
const result = await service.upsert(
|
|
37
|
+
ExpiryUser,
|
|
38
|
+
ExpiryUser.from({
|
|
39
|
+
expiresAt: this.timeFromNow('2s')
|
|
40
|
+
})
|
|
41
|
+
);
|
|
40
42
|
assert(result instanceof ExpiryUser);
|
|
41
43
|
|
|
42
44
|
const expiry = ModelExpiryUtil.getExpiryState(ExpiryUser, await service.get(ExpiryUser, result.id));
|
|
@@ -46,9 +48,12 @@ export abstract class ModelExpirySuite extends BaseModelSuite<ModelExpirySupport
|
|
|
46
48
|
@Test()
|
|
47
49
|
async aging() {
|
|
48
50
|
const service = await this.service;
|
|
49
|
-
const result = await service.upsert(
|
|
50
|
-
|
|
51
|
-
|
|
51
|
+
const result = await service.upsert(
|
|
52
|
+
ExpiryUser,
|
|
53
|
+
ExpiryUser.from({
|
|
54
|
+
expiresAt: this.timeFromNow(100)
|
|
55
|
+
})
|
|
56
|
+
);
|
|
52
57
|
|
|
53
58
|
assert(result instanceof ExpiryUser);
|
|
54
59
|
|
|
@@ -60,9 +65,12 @@ export abstract class ModelExpirySuite extends BaseModelSuite<ModelExpirySupport
|
|
|
60
65
|
@Test()
|
|
61
66
|
async updateExpired() {
|
|
62
67
|
const service = await this.service;
|
|
63
|
-
const result = await service.upsert(
|
|
64
|
-
|
|
65
|
-
|
|
68
|
+
const result = await service.upsert(
|
|
69
|
+
ExpiryUser,
|
|
70
|
+
ExpiryUser.from({
|
|
71
|
+
expiresAt: this.timeFromNow(100)
|
|
72
|
+
})
|
|
73
|
+
);
|
|
66
74
|
|
|
67
75
|
assert(result instanceof ExpiryUser);
|
|
68
76
|
|
|
@@ -74,14 +82,17 @@ export abstract class ModelExpirySuite extends BaseModelSuite<ModelExpirySupport
|
|
|
74
82
|
@Test()
|
|
75
83
|
async ageWithExtension() {
|
|
76
84
|
const service = await this.service;
|
|
77
|
-
const result = await service.upsert(
|
|
78
|
-
|
|
79
|
-
|
|
85
|
+
const result = await service.upsert(
|
|
86
|
+
ExpiryUser,
|
|
87
|
+
ExpiryUser.from({
|
|
88
|
+
expiresAt: this.timeFromNow('2s')
|
|
89
|
+
})
|
|
90
|
+
);
|
|
80
91
|
assert(result instanceof ExpiryUser);
|
|
81
92
|
|
|
82
93
|
await this.wait(50);
|
|
83
94
|
|
|
84
|
-
assert(!ModelExpiryUtil.getExpiryState(ExpiryUser,
|
|
95
|
+
assert(!ModelExpiryUtil.getExpiryState(ExpiryUser, await service.get(ExpiryUser, result.id)).expired);
|
|
85
96
|
|
|
86
97
|
await service.updatePartial(ExpiryUser, {
|
|
87
98
|
id: result.id,
|
|
@@ -97,16 +108,23 @@ export abstract class ModelExpirySuite extends BaseModelSuite<ModelExpirySupport
|
|
|
97
108
|
async culling() {
|
|
98
109
|
const service = await this.service;
|
|
99
110
|
|
|
100
|
-
let total;
|
|
111
|
+
let total: number;
|
|
101
112
|
|
|
102
113
|
total = await this.getSize(ExpiryUser);
|
|
103
114
|
assert(total === 0);
|
|
104
115
|
|
|
105
116
|
// Create
|
|
106
117
|
await Promise.all(
|
|
107
|
-
Array(10)
|
|
108
|
-
|
|
109
|
-
|
|
118
|
+
Array(10)
|
|
119
|
+
.fill(0)
|
|
120
|
+
.map((x, i) =>
|
|
121
|
+
service.upsert(
|
|
122
|
+
ExpiryUser,
|
|
123
|
+
ExpiryUser.from({
|
|
124
|
+
expiresAt: this.timeFromNow(300 + i * this.delayFactor)
|
|
125
|
+
})
|
|
126
|
+
)
|
|
127
|
+
)
|
|
110
128
|
);
|
|
111
129
|
|
|
112
130
|
// Let expire
|
|
@@ -124,4 +142,4 @@ export abstract class ModelExpirySuite extends BaseModelSuite<ModelExpirySupport
|
|
|
124
142
|
total = await this.getSize(ExpiryUser);
|
|
125
143
|
assert(total === 0);
|
|
126
144
|
}
|
|
127
|
-
}
|
|
145
|
+
}
|
|
@@ -1,16 +1,12 @@
|
|
|
1
1
|
import assert from 'node:assert';
|
|
2
2
|
import timers from 'node:timers/promises';
|
|
3
3
|
|
|
4
|
-
import {
|
|
4
|
+
import { Model, type ModelCrudSupport, NotFoundError, PersistValue, SubTypeNotSupportedError } from '@travetto/model';
|
|
5
5
|
import { castTo } from '@travetto/runtime';
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
type ModelCrudSupport, Model,
|
|
9
|
-
NotFoundError, SubTypeNotSupportedError, PersistValue
|
|
10
|
-
} from '@travetto/model';
|
|
6
|
+
import { DiscriminatorField, Schema, Text, TypeMismatchError } from '@travetto/schema';
|
|
7
|
+
import { Suite, Test } from '@travetto/test';
|
|
11
8
|
|
|
12
9
|
import { ExistsError } from '../../src/error/exists.ts';
|
|
13
|
-
|
|
14
10
|
import { BaseModelSuite } from './base.ts';
|
|
15
11
|
|
|
16
12
|
@Schema()
|
|
@@ -44,7 +40,6 @@ export class Engineer extends Worker {
|
|
|
44
40
|
|
|
45
41
|
@Suite()
|
|
46
42
|
export abstract class ModelPolymorphismSuite extends BaseModelSuite<ModelCrudSupport> {
|
|
47
|
-
|
|
48
43
|
@Test('Polymorphic create and find')
|
|
49
44
|
async polymorphicCreateAndFind() {
|
|
50
45
|
const service = await this.service;
|
|
@@ -58,9 +53,7 @@ export abstract class ModelPolymorphismSuite extends BaseModelSuite<ModelCrudSup
|
|
|
58
53
|
assert(doc instanceof Doctor);
|
|
59
54
|
assert(doc.updatedDate !== undefined);
|
|
60
55
|
|
|
61
|
-
await assert.rejects(
|
|
62
|
-
() => service.get(Engineer, doc.id),
|
|
63
|
-
NotFoundError);
|
|
56
|
+
await assert.rejects(() => service.get(Engineer, doc.id), NotFoundError);
|
|
64
57
|
|
|
65
58
|
assert(doc instanceof Doctor);
|
|
66
59
|
assert(fire instanceof Firefighter);
|
|
@@ -92,10 +85,13 @@ export abstract class ModelPolymorphismSuite extends BaseModelSuite<ModelCrudSup
|
|
|
92
85
|
const engineers = await this.toArray(service.list(Engineer));
|
|
93
86
|
assert(engineers.length === 1);
|
|
94
87
|
|
|
95
|
-
await service.create(
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
88
|
+
await service.create(
|
|
89
|
+
Engineer,
|
|
90
|
+
Engineer.from({
|
|
91
|
+
major: 'foodService',
|
|
92
|
+
name: 'bob2'
|
|
93
|
+
})
|
|
94
|
+
);
|
|
99
95
|
|
|
100
96
|
const all2 = await this.toArray(service.list(Worker));
|
|
101
97
|
assert(all2.length === 4);
|
|
@@ -115,16 +111,21 @@ export abstract class ModelPolymorphismSuite extends BaseModelSuite<ModelCrudSup
|
|
|
115
111
|
|
|
116
112
|
await this.saveAll(Worker, [doc, fire, eng]);
|
|
117
113
|
|
|
118
|
-
assert(await service.get(Worker, doc.id) instanceof Doctor);
|
|
119
|
-
assert(await service.get(Worker, fire.id) instanceof Firefighter);
|
|
114
|
+
assert((await service.get(Worker, doc.id)) instanceof Doctor);
|
|
115
|
+
assert((await service.get(Worker, fire.id)) instanceof Firefighter);
|
|
120
116
|
|
|
121
117
|
const update = new Date();
|
|
122
118
|
|
|
123
119
|
await assert.rejects(
|
|
124
120
|
() =>
|
|
125
|
-
service.upsert(
|
|
126
|
-
|
|
127
|
-
|
|
121
|
+
service.upsert(
|
|
122
|
+
Doctor,
|
|
123
|
+
Doctor.from({
|
|
124
|
+
id: fire.id,
|
|
125
|
+
name: 'gob',
|
|
126
|
+
specialty: 'eyes'
|
|
127
|
+
})
|
|
128
|
+
),
|
|
128
129
|
e => e instanceof SubTypeNotSupportedError || e instanceof ExistsError
|
|
129
130
|
);
|
|
130
131
|
|
|
@@ -135,18 +136,28 @@ export abstract class ModelPolymorphismSuite extends BaseModelSuite<ModelCrudSup
|
|
|
135
136
|
await timers.setTimeout(15);
|
|
136
137
|
|
|
137
138
|
try {
|
|
138
|
-
const result = await service.upsert(
|
|
139
|
-
|
|
140
|
-
|
|
139
|
+
const result = await service.upsert(
|
|
140
|
+
Doctor,
|
|
141
|
+
Doctor.from({
|
|
142
|
+
id: doc.id,
|
|
143
|
+
name: 'gob',
|
|
144
|
+
specialty: 'eyes'
|
|
145
|
+
})
|
|
146
|
+
);
|
|
141
147
|
|
|
142
148
|
assert(result.updatedDate!.getTime() > update.getTime());
|
|
143
149
|
} catch (err) {
|
|
144
150
|
assert(err instanceof SubTypeNotSupportedError);
|
|
145
151
|
}
|
|
146
152
|
|
|
147
|
-
const resAlt = await service.upsert(
|
|
148
|
-
|
|
149
|
-
|
|
153
|
+
const resAlt = await service.upsert(
|
|
154
|
+
Worker,
|
|
155
|
+
Doctor.from({
|
|
156
|
+
id: doc.id,
|
|
157
|
+
name: 'gob',
|
|
158
|
+
specialty: 'eyes'
|
|
159
|
+
})
|
|
160
|
+
);
|
|
150
161
|
|
|
151
162
|
assert(resAlt.updatedDate!.getTime() > update.getTime());
|
|
152
163
|
|
|
@@ -159,10 +170,7 @@ export abstract class ModelPolymorphismSuite extends BaseModelSuite<ModelCrudSup
|
|
|
159
170
|
// Delete by base class
|
|
160
171
|
await service.delete(Worker, fire.id);
|
|
161
172
|
|
|
162
|
-
await assert.rejects(
|
|
163
|
-
() => service.delete(Worker, fire.id),
|
|
164
|
-
NotFoundError
|
|
165
|
-
);
|
|
173
|
+
await assert.rejects(() => service.delete(Worker, fire.id), NotFoundError);
|
|
166
174
|
|
|
167
175
|
// Delete by any subtype when id is missing
|
|
168
176
|
await assert.rejects(
|
|
@@ -170,4 +178,4 @@ export abstract class ModelPolymorphismSuite extends BaseModelSuite<ModelCrudSup
|
|
|
170
178
|
e => e instanceof SubTypeNotSupportedError || e instanceof NotFoundError
|
|
171
179
|
);
|
|
172
180
|
}
|
|
173
|
-
}
|
|
181
|
+
}
|
package/support/test/suite.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import { type Class } from '@travetto/runtime';
|
|
2
1
|
import { DependencyRegistryIndex } from '@travetto/di';
|
|
3
2
|
import { Registry } from '@travetto/registry';
|
|
4
|
-
import {
|
|
3
|
+
import type { Class } from '@travetto/runtime';
|
|
5
4
|
import { SchemaRegistryIndex } from '@travetto/schema';
|
|
5
|
+
import { type SuitePhaseHandler, SuiteRegistryIndex, TestFixtures } from '@travetto/test';
|
|
6
6
|
|
|
7
|
+
import { ModelRegistryIndex } from '../../src/registry/registry-index.ts';
|
|
7
8
|
import { ModelBlobUtil } from '../../src/util/blob.ts';
|
|
8
9
|
import { ModelStorageUtil } from '../../src/util/storage.ts';
|
|
9
|
-
import { ModelRegistryIndex } from '../../src/registry/registry-index.ts';
|
|
10
10
|
|
|
11
|
-
type ConfigType = { autoCreate?: boolean
|
|
11
|
+
type ConfigType = { autoCreate?: boolean; namespace?: string };
|
|
12
12
|
|
|
13
|
-
class ModelSuiteHandler<T extends { configClass: Class<ConfigType
|
|
13
|
+
class ModelSuiteHandler<T extends { configClass: Class<ConfigType>; serviceClass: Class }> implements SuitePhaseHandler<T> {
|
|
14
14
|
qualifier?: symbol;
|
|
15
15
|
target: Class<T>;
|
|
16
16
|
constructor(target: Class<T>, qualifier?: symbol) {
|
|
@@ -35,9 +35,11 @@ class ModelSuiteHandler<T extends { configClass: Class<ConfigType>, serviceClass
|
|
|
35
35
|
if (ModelStorageUtil.isSupported(service)) {
|
|
36
36
|
await service.createStorage();
|
|
37
37
|
if (service.upsertModel) {
|
|
38
|
-
await Promise.all(
|
|
39
|
-
.
|
|
40
|
-
|
|
38
|
+
await Promise.all(
|
|
39
|
+
ModelRegistryIndex.getClasses()
|
|
40
|
+
.filter(cls => cls === SchemaRegistryIndex.getBaseClass(cls))
|
|
41
|
+
.map(modelCls => service.upsertModel!(modelCls))
|
|
42
|
+
);
|
|
41
43
|
}
|
|
42
44
|
}
|
|
43
45
|
}
|
|
@@ -45,8 +47,7 @@ class ModelSuiteHandler<T extends { configClass: Class<ConfigType>, serviceClass
|
|
|
45
47
|
async afterEach(instance: T) {
|
|
46
48
|
const service = await DependencyRegistryIndex.getInstance<T>(instance.serviceClass, this.qualifier);
|
|
47
49
|
if (ModelStorageUtil.isSupported(service)) {
|
|
48
|
-
const models = ModelRegistryIndex.getClasses()
|
|
49
|
-
.filter(model => model === SchemaRegistryIndex.getBaseClass(model));
|
|
50
|
+
const models = ModelRegistryIndex.getClasses().filter(model => model === SchemaRegistryIndex.getBaseClass(model));
|
|
50
51
|
|
|
51
52
|
if (ModelBlobUtil.isSupported(service) && service.truncateBlob) {
|
|
52
53
|
await service.truncateBlob();
|
|
@@ -83,7 +84,9 @@ class ModelSuiteHandler<T extends { configClass: Class<ConfigType>, serviceClass
|
|
|
83
84
|
* @example opt-in
|
|
84
85
|
* @kind decorator
|
|
85
86
|
*/
|
|
86
|
-
export function ModelSuite<T extends { configClass: Class<{ autoCreate?: boolean
|
|
87
|
+
export function ModelSuite<T extends { configClass: Class<{ autoCreate?: boolean; namespace?: string }>; serviceClass: Class }>(
|
|
88
|
+
qualifier?: symbol
|
|
89
|
+
) {
|
|
87
90
|
const fixtures = new TestFixtures(['@travetto/model']);
|
|
88
91
|
return (target: Class<T>): void => {
|
|
89
92
|
target.prototype.fixtures = fixtures;
|
|
@@ -91,4 +94,4 @@ export function ModelSuite<T extends { configClass: Class<{ autoCreate?: boolean
|
|
|
91
94
|
phaseHandlers: [new ModelSuiteHandler(target, qualifier)]
|
|
92
95
|
});
|
|
93
96
|
};
|
|
94
|
-
}
|
|
97
|
+
}
|