@travetto/model 6.0.0 → 6.0.1
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 +17 -0
- package/package.json +7 -7
- package/src/registry/decorator.ts +17 -0
- package/src/types/model.ts +1 -1
- package/src/util/expiry.ts +1 -1
- package/src/util/indexed.ts +1 -1
package/README.md
CHANGED
|
@@ -15,6 +15,23 @@ yarn add @travetto/model
|
|
|
15
15
|
|
|
16
16
|
This module provides a set of contracts/interfaces to data model persistence, modification and retrieval. This module builds heavily upon the [Schema](https://github.com/travetto/travetto/tree/main/module/schema#readme "Data type registry for runtime validation, reflection and binding."), which is used for data model validation.
|
|
17
17
|
|
|
18
|
+
## A Simple Model
|
|
19
|
+
A model can be simply defined by usage of the [@Model](https://github.com/travetto/travetto/tree/main/module/model/src/registry/decorator.ts#L13) decorator, which opts it into the [Schema](https://github.com/travetto/travetto/tree/main/module/schema#readme "Data type registry for runtime validation, reflection and binding.") contracts, as well as making it available to the [ModelRegistry](https://github.com/travetto/travetto/tree/main/module/model/src/registry/model.ts#L14).
|
|
20
|
+
|
|
21
|
+
**Code: Basic Structure**
|
|
22
|
+
```typescript
|
|
23
|
+
import { Model } from '@travetto/model';
|
|
24
|
+
|
|
25
|
+
@Model()
|
|
26
|
+
export class SampleModel {
|
|
27
|
+
id: string;
|
|
28
|
+
name: string;
|
|
29
|
+
age: number;
|
|
30
|
+
}
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Once the model is defined, it can be leveraged with any of the services that implement the various model storage contracts. These contracts allow for persisting and fetching of the associated model object.
|
|
34
|
+
|
|
18
35
|
## Contracts
|
|
19
36
|
The module is mainly composed of contracts. The contracts define the expected interface for various model patterns. The primary contracts are [Basic](https://github.com/travetto/travetto/tree/main/module/model/src/types/basic.ts#L8), [CRUD](https://github.com/travetto/travetto/tree/main/module/model/src/types/crud.ts#L11), [Indexed](https://github.com/travetto/travetto/tree/main/module/model/src/types/indexed.ts#L11), [Expiry](https://github.com/travetto/travetto/tree/main/module/model/src/types/expiry.ts#L10), [Blob](https://github.com/travetto/travetto/tree/main/module/model/src/types/blob.ts#L8) and [Bulk](https://github.com/travetto/travetto/tree/main/module/model/src/types/bulk.ts#L64).
|
|
20
37
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/model",
|
|
3
|
-
"version": "6.0.
|
|
3
|
+
"version": "6.0.1",
|
|
4
4
|
"description": "Datastore abstraction for core operations.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"datastore",
|
|
@@ -26,14 +26,14 @@
|
|
|
26
26
|
"directory": "module/model"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@travetto/config": "^6.0.
|
|
30
|
-
"@travetto/di": "^6.0.
|
|
31
|
-
"@travetto/registry": "^6.0.
|
|
32
|
-
"@travetto/schema": "^6.0.
|
|
29
|
+
"@travetto/config": "^6.0.1",
|
|
30
|
+
"@travetto/di": "^6.0.1",
|
|
31
|
+
"@travetto/registry": "^6.0.1",
|
|
32
|
+
"@travetto/schema": "^6.0.1"
|
|
33
33
|
},
|
|
34
34
|
"peerDependencies": {
|
|
35
|
-
"@travetto/cli": "^6.0.
|
|
36
|
-
"@travetto/test": "^6.0.
|
|
35
|
+
"@travetto/cli": "^6.0.1",
|
|
36
|
+
"@travetto/test": "^6.0.2"
|
|
37
37
|
},
|
|
38
38
|
"peerDependenciesMeta": {
|
|
39
39
|
"@travetto/cli": {
|
|
@@ -74,6 +74,23 @@ export function PersistValue<T>(handler: (curr: T | undefined) => T, scope: PreP
|
|
|
74
74
|
};
|
|
75
75
|
}
|
|
76
76
|
|
|
77
|
+
/**
|
|
78
|
+
* Prevent a field from being persisted
|
|
79
|
+
*/
|
|
80
|
+
export function Transient<T>() {
|
|
81
|
+
return function <K extends string, C extends Partial<Record<K, T>>>(tgt: C, prop: K): void {
|
|
82
|
+
ModelRegistry.registerDataHandlers(asConstructable(tgt).constructor, {
|
|
83
|
+
prePersist: [{
|
|
84
|
+
scope: 'all',
|
|
85
|
+
handler: (inst): void => {
|
|
86
|
+
const cInst: Record<K, T> = castTo(inst);
|
|
87
|
+
delete cInst[prop];
|
|
88
|
+
}
|
|
89
|
+
}]
|
|
90
|
+
});
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
77
94
|
/**
|
|
78
95
|
* Model class decorator for post-load behavior
|
|
79
96
|
*/
|
package/src/types/model.ts
CHANGED
package/src/util/expiry.ts
CHANGED
|
@@ -37,7 +37,7 @@ export class ModelExpiryUtil {
|
|
|
37
37
|
const running = new AbortController();
|
|
38
38
|
const cullInterval = TimeUtil.asMillis(svc.config?.cullRate ?? '10m');
|
|
39
39
|
|
|
40
|
-
ShutdownManager.onGracefulShutdown(async () => running.abort()
|
|
40
|
+
ShutdownManager.onGracefulShutdown(async () => running.abort());
|
|
41
41
|
|
|
42
42
|
(async (): Promise<void> => {
|
|
43
43
|
await Util.nonBlockingTimeout(1000);
|
package/src/util/indexed.ts
CHANGED
|
@@ -39,7 +39,7 @@ export class ModelIndexedUtil {
|
|
|
39
39
|
cls: Class<T>, idx: IndexConfig<T> | string, item: DeepPartial<T>, opts: ComputeConfig = {}
|
|
40
40
|
): { fields: IndexFieldPart[], sorted: IndexSortPart | undefined } {
|
|
41
41
|
const cfg = typeof idx === 'string' ? ModelRegistry.getIndex(cls, idx) : idx;
|
|
42
|
-
const sortField = cfg.type === 'sorted' ? cfg.fields
|
|
42
|
+
const sortField = cfg.type === 'sorted' ? cfg.fields.at(-1) : undefined;
|
|
43
43
|
|
|
44
44
|
const fields: IndexFieldPart[] = [];
|
|
45
45
|
let sortDir: number = 0;
|