@travetto/model 8.0.0-alpha.22 → 8.0.0-alpha.24
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 +72 -67
- package/__index__.ts +7 -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/error/unique.ts +13 -0
- 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 +8 -9
- 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
|
@@ -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
|
+
}
|