@travetto/model 3.4.5 → 4.0.0-rc.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 +3 -3
- package/package.json +7 -7
- package/src/internal/service/crud.ts +1 -1
- package/src/internal/service/expiry.ts +6 -8
- package/src/internal/service/storage.ts +2 -2
- package/src/provider/file.ts +6 -6
- package/src/provider/memory.ts +1 -1
- package/src/registry/model.ts +2 -2
- package/src/service/basic.ts +1 -1
- package/src/service/bulk.ts +1 -1
- package/src/service/crud.ts +1 -1
- package/src/service/expiry.ts +1 -1
- package/src/service/indexed.ts +1 -1
- package/src/service/storage.ts +1 -1
- package/src/service/stream.ts +2 -2
- package/support/base-command.ts +2 -3
- package/support/bin/install.ts +1 -2
- package/support/doc.support.tsx +3 -3
- package/support/test/basic.ts +1 -1
- package/support/test/bulk.ts +1 -1
- package/support/test/crud.ts +1 -1
- package/support/test/expiry.ts +3 -2
- package/support/test/indexed.ts +1 -1
- package/support/test/polymorphism.ts +2 -2
- package/support/test/stream.ts +8 -12
package/README.md
CHANGED
|
@@ -239,7 +239,7 @@ In addition to the provided contracts, the module also provides common utilities
|
|
|
239
239
|
|
|
240
240
|
**Code: Memory Service**
|
|
241
241
|
```typescript
|
|
242
|
-
import { Readable } from 'stream';
|
|
242
|
+
import { Readable } from 'node:stream';
|
|
243
243
|
import { StreamUtil, Class, TimeSpan } from '@travetto/base';
|
|
244
244
|
import { DeepPartial } from '@travetto/schema';
|
|
245
245
|
import { Injectable } from '@travetto/di';
|
|
@@ -382,7 +382,7 @@ Usage: model:export [options] <provider:string> <models...:string>
|
|
|
382
382
|
|
|
383
383
|
Options:
|
|
384
384
|
-e, --env <string> Application environment
|
|
385
|
-
-m, --module <
|
|
385
|
+
-m, --module <module> Module to run for
|
|
386
386
|
-h, --help display help for command
|
|
387
387
|
|
|
388
388
|
Providers
|
|
@@ -405,7 +405,7 @@ Usage: model:install [options] <provider:string> <models...:string>
|
|
|
405
405
|
|
|
406
406
|
Options:
|
|
407
407
|
-e, --env <string> Application environment
|
|
408
|
-
-m, --module <
|
|
408
|
+
-m, --module <module> Module to run for
|
|
409
409
|
-h, --help display help for command
|
|
410
410
|
|
|
411
411
|
Providers
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/model",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0-rc.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": "^
|
|
30
|
-
"@travetto/di": "^
|
|
31
|
-
"@travetto/registry": "^
|
|
32
|
-
"@travetto/schema": "^
|
|
29
|
+
"@travetto/config": "^4.0.0-rc.1",
|
|
30
|
+
"@travetto/di": "^4.0.0-rc.1",
|
|
31
|
+
"@travetto/registry": "^4.0.0-rc.1",
|
|
32
|
+
"@travetto/schema": "^4.0.0-rc.1"
|
|
33
33
|
},
|
|
34
34
|
"peerDependencies": {
|
|
35
|
-
"@travetto/cli": "^
|
|
36
|
-
"@travetto/test": "^
|
|
35
|
+
"@travetto/cli": "^4.0.0-rc.1",
|
|
36
|
+
"@travetto/test": "^4.0.0-rc.1"
|
|
37
37
|
},
|
|
38
38
|
"peerDependenciesMeta": {
|
|
39
39
|
"@travetto/cli": {
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import timers from 'node:timers/promises';
|
|
2
|
+
|
|
1
3
|
import { ShutdownManager, Class, TimeSpan, TimeUtil } from '@travetto/base';
|
|
2
4
|
|
|
3
5
|
import { ModelRegistry } from '../../registry/model';
|
|
@@ -36,16 +38,12 @@ export class ModelExpiryUtil {
|
|
|
36
38
|
let running = true;
|
|
37
39
|
const cullInterval = TimeUtil.timeToMs(svc.config?.cullRate ?? '10m');
|
|
38
40
|
|
|
39
|
-
ShutdownManager.
|
|
40
|
-
|
|
41
|
-
running = false;
|
|
42
|
-
cb?.();
|
|
43
|
-
}
|
|
44
|
-
});
|
|
41
|
+
ShutdownManager.onGracefulShutdown(async () => { running = false; }, this);
|
|
42
|
+
|
|
45
43
|
(async (): Promise<void> => {
|
|
46
|
-
await
|
|
44
|
+
await timers.setTimeout(1000); // Wait a second to start culling
|
|
47
45
|
while (running) {
|
|
48
|
-
await
|
|
46
|
+
await timers.setTimeout(cullInterval);
|
|
49
47
|
await Promise.all(cullable.map(cls => svc.deleteExpired(cls)));
|
|
50
48
|
}
|
|
51
49
|
})();
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Class,
|
|
1
|
+
import { Class, Env } from '@travetto/base';
|
|
2
2
|
import { SchemaChangeListener } from '@travetto/schema';
|
|
3
3
|
|
|
4
4
|
import { ModelRegistry } from '../../registry/model';
|
|
@@ -13,7 +13,7 @@ export class ModelStorageUtil {
|
|
|
13
13
|
* Register change listener on startup
|
|
14
14
|
*/
|
|
15
15
|
static async registerModelChangeListener(storage: ModelStorageSupport, target?: Class): Promise<void> {
|
|
16
|
-
if (!
|
|
16
|
+
if (!Env.dynamic || !(storage?.config?.autoCreate ?? !Env.production)) {
|
|
17
17
|
return;
|
|
18
18
|
}
|
|
19
19
|
|
package/src/provider/file.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import fs from 'fs/promises';
|
|
2
|
-
import { createReadStream } from 'fs';
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import { createReadStream } from 'node:fs';
|
|
3
3
|
|
|
4
|
-
import os from 'os';
|
|
4
|
+
import os from 'node:os';
|
|
5
5
|
|
|
6
|
-
import { Readable } from 'stream';
|
|
6
|
+
import { Readable } from 'node:stream';
|
|
7
7
|
|
|
8
|
-
import { path,
|
|
8
|
+
import { path, RuntimeContext } from '@travetto/manifest';
|
|
9
9
|
import { StreamUtil, Class, TimeSpan } from '@travetto/base';
|
|
10
10
|
import { Injectable } from '@travetto/di';
|
|
11
11
|
import { Config } from '@travetto/config';
|
|
@@ -37,7 +37,7 @@ export class FileModelConfig {
|
|
|
37
37
|
cullRate?: number | TimeSpan;
|
|
38
38
|
|
|
39
39
|
async postConstruct(): Promise<void> {
|
|
40
|
-
this.folder ??= path.resolve(os.tmpdir(), `trv_file_${
|
|
40
|
+
this.folder ??= path.resolve(os.tmpdir(), `trv_file_${RuntimeContext.main.name.replace(/[^a-z]/ig, '_')}`);
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
43
|
|
package/src/provider/memory.ts
CHANGED
package/src/registry/model.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { RuntimeIndex } from '@travetto/manifest';
|
|
2
2
|
import { SchemaRegistry } from '@travetto/schema';
|
|
3
3
|
import { MetadataRegistry } from '@travetto/registry';
|
|
4
4
|
import { DependencyRegistry } from '@travetto/di';
|
|
@@ -52,7 +52,7 @@ class $ModelRegistry extends MetadataRegistry<ModelOptions<ModelType>> {
|
|
|
52
52
|
}
|
|
53
53
|
|
|
54
54
|
createPending(cls: Class): Partial<ModelOptions<ModelType>> {
|
|
55
|
-
return { class: cls, indices: [], autoCreate: true, baseType:
|
|
55
|
+
return { class: cls, indices: [], autoCreate: true, baseType: RuntimeIndex.getFunctionMetadata(cls)?.abstract, postLoad: [], prePersist: [] };
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
registerDataHandlers(cls: Class, pConfig?: Partial<ModelOptions<ModelType>>): void {
|
package/src/service/basic.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { ModelType, OptionalId } from '../types/model';
|
|
|
4
4
|
/**
|
|
5
5
|
* Interface for basic data interface
|
|
6
6
|
*
|
|
7
|
-
* @concrete ../internal/service/common
|
|
7
|
+
* @concrete ../internal/service/common#ModelBasicSupportTarget
|
|
8
8
|
*/
|
|
9
9
|
export interface ModelBasicSupport<C = unknown> {
|
|
10
10
|
/**
|
package/src/service/bulk.ts
CHANGED
|
@@ -65,7 +65,7 @@ export class BulkProcessError extends AppError {
|
|
|
65
65
|
/**
|
|
66
66
|
* Determines if model allows for bulk operations
|
|
67
67
|
*
|
|
68
|
-
* @concrete ../internal/service/common
|
|
68
|
+
* @concrete ../internal/service/common#ModelBulkSupportTarget
|
|
69
69
|
*/
|
|
70
70
|
export interface ModelBulkSupport extends ModelCrudSupport {
|
|
71
71
|
processBulk<T extends ModelType>(cls: Class<T>, operations: BulkOp<T>[]): Promise<BulkResponse>;
|
package/src/service/crud.ts
CHANGED
|
@@ -6,7 +6,7 @@ import { ModelBasicSupport } from './basic';
|
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Interface for simple CRUD
|
|
9
|
-
* @concrete ../internal/service/common
|
|
9
|
+
* @concrete ../internal/service/common#ModelCrudSupportTarget
|
|
10
10
|
*/
|
|
11
11
|
export interface ModelCrudSupport extends ModelBasicSupport {
|
|
12
12
|
|
package/src/service/expiry.ts
CHANGED
|
@@ -6,7 +6,7 @@ import { ModelCrudSupport } from './crud';
|
|
|
6
6
|
/**
|
|
7
7
|
* Support for managing expiration of data
|
|
8
8
|
*
|
|
9
|
-
* @concrete ../internal/service/common
|
|
9
|
+
* @concrete ../internal/service/common#ModelExpirySupportTarget
|
|
10
10
|
*/
|
|
11
11
|
export interface ModelExpirySupport extends ModelCrudSupport {
|
|
12
12
|
/**
|
package/src/service/indexed.ts
CHANGED
|
@@ -7,7 +7,7 @@ import { ModelBasicSupport } from './basic';
|
|
|
7
7
|
/**
|
|
8
8
|
* Support for simple indexed activity
|
|
9
9
|
*
|
|
10
|
-
* @concrete ../internal/service/common
|
|
10
|
+
* @concrete ../internal/service/common#ModelIndexedSupportTarget
|
|
11
11
|
*/
|
|
12
12
|
export interface ModelIndexedSupport extends ModelBasicSupport {
|
|
13
13
|
/**
|
package/src/service/storage.ts
CHANGED
|
@@ -10,7 +10,7 @@ import { ModelType } from '../types/model';
|
|
|
10
10
|
*
|
|
11
11
|
* This is intended to be used during development only for rapid prototyping.
|
|
12
12
|
*
|
|
13
|
-
* @concrete ../internal/service/common
|
|
13
|
+
* @concrete ../internal/service/common#ModelStorageSupportTarget
|
|
14
14
|
*/
|
|
15
15
|
export interface ModelStorageSupport {
|
|
16
16
|
|
package/src/service/stream.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Readable } from 'stream';
|
|
1
|
+
import { Readable } from 'node:stream';
|
|
2
2
|
|
|
3
3
|
export interface StreamMeta {
|
|
4
4
|
/**
|
|
@@ -43,7 +43,7 @@ export interface PartialStream {
|
|
|
43
43
|
/**
|
|
44
44
|
* Support for Streams CRD. Stream update is not supported.
|
|
45
45
|
*
|
|
46
|
-
* @concrete ../internal/service/common
|
|
46
|
+
* @concrete ../internal/service/common#ModelStreamSupportTarget
|
|
47
47
|
*/
|
|
48
48
|
export interface ModelStreamSupport {
|
|
49
49
|
|
package/support/base-command.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Env } from '@travetto/base';
|
|
2
2
|
import { CliValidationError, CliCommandShape, cliTpl } from '@travetto/cli';
|
|
3
3
|
import { RootRegistry } from '@travetto/registry';
|
|
4
4
|
|
|
@@ -17,7 +17,7 @@ export abstract class BaseModelCommand implements CliCommandShape {
|
|
|
17
17
|
abstract getOp(): keyof ModelStorageSupport;
|
|
18
18
|
|
|
19
19
|
preMain(): void {
|
|
20
|
-
|
|
20
|
+
Env.DEBUG.set(false);
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
async help(): Promise<string[]> {
|
|
@@ -36,7 +36,6 @@ export abstract class BaseModelCommand implements CliCommandShape {
|
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
async validate(provider: string, models: string[]): Promise<CliValidationError | undefined> {
|
|
39
|
-
ConsoleManager.setDebug(false);
|
|
40
39
|
await RootRegistry.init();
|
|
41
40
|
|
|
42
41
|
const candidates = await ModelCandidateUtil.export(this.getOp());
|
package/support/bin/install.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { Class } from '@travetto/base';
|
|
2
2
|
import type { ModelStorageSupport } from '@travetto/model/src/service/storage';
|
|
3
3
|
import type { ModelType } from '@travetto/model/src/types/model';
|
|
4
4
|
|
|
@@ -11,6 +11,5 @@ export class ModelInstallUtil {
|
|
|
11
11
|
console.log('Installing', { name: m.Ⲑid });
|
|
12
12
|
await provider.createModel(m);
|
|
13
13
|
}
|
|
14
|
-
ShutdownManager.execute(-1); // Release database
|
|
15
14
|
}
|
|
16
15
|
}
|
package/support/doc.support.tsx
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/** @jsxImportSource @travetto/doc */
|
|
2
|
-
import { readFileSync } from 'fs';
|
|
2
|
+
import { readFileSync } from 'node:fs';
|
|
3
3
|
|
|
4
|
-
import {
|
|
4
|
+
import { RuntimeIndex } from '@travetto/manifest';
|
|
5
5
|
import { d, c, DocJSXElementByFn, DocJSXElement } from '@travetto/doc';
|
|
6
6
|
import { Config } from '@travetto/config';
|
|
7
7
|
|
|
@@ -16,7 +16,7 @@ export const Links = {
|
|
|
16
16
|
|
|
17
17
|
export const ModelTypes = (file: string | Function): DocJSXElement[] => {
|
|
18
18
|
if (typeof file !== 'string') {
|
|
19
|
-
file =
|
|
19
|
+
file = RuntimeIndex.getFunctionMetadata(file)!.source;
|
|
20
20
|
}
|
|
21
21
|
const contents = readFileSync(file, 'utf8');
|
|
22
22
|
const found: DocJSXElementByFn<'CodeLink'>[] = [];
|
package/support/test/basic.ts
CHANGED
package/support/test/bulk.ts
CHANGED
package/support/test/crud.ts
CHANGED
package/support/test/expiry.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import assert from 'assert';
|
|
1
|
+
import assert from 'node:assert';
|
|
2
|
+
import timers from 'node:timers/promises';
|
|
2
3
|
|
|
3
4
|
import { Suite, Test } from '@travetto/test';
|
|
4
5
|
import { TimeSpan, TimeUnit, TimeUtil } from '@travetto/base';
|
|
@@ -22,7 +23,7 @@ export abstract class ModelExpirySuite extends BaseModelSuite<ModelExpirySupport
|
|
|
22
23
|
delayFactor: number = 1;
|
|
23
24
|
|
|
24
25
|
async wait(n: number | TimeSpan) {
|
|
25
|
-
await
|
|
26
|
+
await timers.setTimeout(TimeUtil.timeToMs(n) * this.delayFactor);
|
|
26
27
|
}
|
|
27
28
|
|
|
28
29
|
timeFromNow(v: number | TimeSpan, unit?: TimeUnit) {
|
package/support/test/indexed.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import assert from 'assert';
|
|
2
|
-
import timers from 'timers/promises';
|
|
1
|
+
import assert from 'node:assert';
|
|
2
|
+
import timers from 'node:timers/promises';
|
|
3
3
|
|
|
4
4
|
import { Suite, Test } from '@travetto/test';
|
|
5
5
|
import { SubTypeField, Text, TypeMismatchError } from '@travetto/schema';
|
package/support/test/stream.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import fs from 'fs/promises';
|
|
2
|
-
import assert from 'assert';
|
|
3
|
-
import crypto from 'crypto';
|
|
4
|
-
import { Readable } from 'stream';
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import assert from 'node:assert';
|
|
3
|
+
import crypto from 'node:crypto';
|
|
4
|
+
import { Readable } from 'node:stream';
|
|
5
|
+
import { pipeline } from 'node:stream/promises';
|
|
5
6
|
|
|
6
7
|
import { Suite, Test, TestFixtures } from '@travetto/test';
|
|
7
8
|
import { StreamUtil } from '@travetto/base';
|
|
@@ -15,14 +16,9 @@ export abstract class ModelStreamSuite extends BaseModelSuite<ModelStreamSupport
|
|
|
15
16
|
fixture = new TestFixtures(['@travetto/model']);
|
|
16
17
|
|
|
17
18
|
async getHash(stream: Readable): Promise<string> {
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
stream.on('end', res);
|
|
22
|
-
stream.on('error', rej);
|
|
23
|
-
stream.pipe(hash);
|
|
24
|
-
});
|
|
25
|
-
return hash.read() as string;
|
|
19
|
+
const hasher = crypto.createHash('sha1').setEncoding('hex');
|
|
20
|
+
await pipeline(stream, hasher);
|
|
21
|
+
return hasher.read().toString();
|
|
26
22
|
}
|
|
27
23
|
|
|
28
24
|
async getStream(resource: string): Promise<readonly [{ size: number, contentType: string, hash: string, filename: string }, Readable]> {
|