@travetto/model 3.0.3 → 3.1.0-rc.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.
package/README.md CHANGED
@@ -381,11 +381,20 @@ The module provides the ability to generate an export of the model structure fro
381
381
  ```bash
382
382
  $ trv model:export --help
383
383
 
384
- Usage: model:export [options] [provider] [models...]
384
+ Usage: model:export [options] <provider:string> <models...:string>
385
385
 
386
386
  Options:
387
- -e, --env <env> Application environment
388
- -h, --help display help for command
387
+ -e, --env <string> Application environment
388
+ -m, --module <string> Module to run for
389
+ -h, --help display help for command
390
+
391
+ Providers
392
+ --------------------
393
+ * SQL
394
+
395
+ Models
396
+ --------------------
397
+ * samplemodel
389
398
  ```
390
399
 
391
400
  ## CLI - model:install
@@ -395,9 +404,19 @@ The module provides the ability to install all the various [@Model](https://gith
395
404
  ```bash
396
405
  $ trv model:install --help
397
406
 
398
- Usage: model:install [options] [provider] [models...]
407
+ Usage: model:install [options] <provider:string> <models...:string>
399
408
 
400
409
  Options:
401
- -e, --env <env> Application environment
402
- -h, --help display help for command
410
+ -e, --env <string> Application environment
411
+ -m, --module <string> Module to run for
412
+ -h, --help display help for command
413
+
414
+ Providers
415
+ --------------------
416
+ * Memory
417
+ * SQL
418
+
419
+ Models
420
+ --------------------
421
+ * samplemodel
403
422
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/model",
3
- "version": "3.0.3",
3
+ "version": "3.1.0-rc.0",
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": "^3.0.3",
30
- "@travetto/di": "^3.0.3",
31
- "@travetto/registry": "^3.0.3",
32
- "@travetto/schema": "^3.0.3"
29
+ "@travetto/config": "^3.1.0-rc.0",
30
+ "@travetto/di": "^3.1.0-rc.0",
31
+ "@travetto/registry": "^3.1.0-rc.0",
32
+ "@travetto/schema": "^3.1.0-rc.0"
33
33
  },
34
34
  "peerDependencies": {
35
- "@travetto/cli": "^3.0.3",
36
- "@travetto/test": "^3.0.3"
35
+ "@travetto/cli": "^3.1.0-rc.0",
36
+ "@travetto/test": "^3.1.0-rc.0"
37
37
  },
38
38
  "peerDependenciesMeta": {
39
39
  "@travetto/cli": {
@@ -0,0 +1,61 @@
1
+ import { ConsoleManager, GlobalEnvConfig } from '@travetto/base';
2
+ import { CliValidationError, CliCommandShape, cliTpl } from '@travetto/cli';
3
+ import { RootRegistry } from '@travetto/registry';
4
+
5
+ import type { ModelStorageSupport } from '../src/service/storage';
6
+
7
+ import { ModelCandidateUtil } from './bin/candidate';
8
+
9
+ /**
10
+ * CLI Entry point for exporting model schemas
11
+ */
12
+ export abstract class BaseModelCommand implements CliCommandShape {
13
+
14
+ /** Application Environment */
15
+ env?: string;
16
+
17
+ abstract getOp(): keyof ModelStorageSupport;
18
+
19
+ envInit(): GlobalEnvConfig {
20
+ return { debug: false };
21
+ }
22
+
23
+ async help(): Promise<string[]> {
24
+ await RootRegistry.init();
25
+
26
+ const candidates = await ModelCandidateUtil.export(this.getOp());
27
+ return [
28
+ cliTpl`${{ title: 'Providers' }}`,
29
+ '-'.repeat(20),
30
+ ...candidates.providers.map(p => cliTpl` * ${{ type: p }}`),
31
+ '',
32
+ cliTpl`${{ title: 'Models' }}`,
33
+ '-'.repeat(20),
34
+ ...candidates.models.map(p => cliTpl` * ${{ param: p }}`)
35
+ ];
36
+ }
37
+
38
+ async validate(provider: string, models: string[]): Promise<CliValidationError | undefined> {
39
+ ConsoleManager.setDebug(false);
40
+ await RootRegistry.init();
41
+
42
+ const candidates = await ModelCandidateUtil.export(this.getOp());
43
+ if (!candidates.providers.includes(provider)) {
44
+ return {
45
+ message: `${provider} is not a valid provider`,
46
+ path: 'provider',
47
+ kind: 'invalid'
48
+ };
49
+ }
50
+ const badModel = models.find(x => x !== '*' && !candidates.models.includes(x));
51
+ if (badModel) {
52
+ return {
53
+ message: `${badModel} is not a valid model`,
54
+ path: 'models',
55
+ kind: 'invalid'
56
+ };
57
+ }
58
+ }
59
+
60
+ abstract main(...args: unknown[]): ReturnType<CliCommandShape['main']>;
61
+ }
@@ -1,24 +1,21 @@
1
- import { RootIndex } from '@travetto/manifest';
2
- import { Class, ExecUtil } from '@travetto/base';
3
- import { GlobalTerminal } from '@travetto/terminal';
4
- import { ModelRegistry } from '@travetto/model';
1
+ import { Class } from '@travetto/base';
2
+ import { ModelRegistry } from '@travetto/model/src/registry/model';
5
3
  import { InjectableConfig, DependencyRegistry } from '@travetto/di';
6
- import { RootRegistry } from '@travetto/registry';
7
-
8
4
  import { ModelStorageSupportTarget } from '@travetto/model/src/internal/service/common';
9
5
 
10
6
  import type { ModelStorageSupport } from '../../src/service/storage';
11
7
  import type { ModelType } from '../../src/types/model';
12
8
 
13
- type CandidateNames = { providers: string[], models: string[] };
14
-
15
9
  /**
16
10
  * Utilities for finding candidates for model operations
17
11
  */
18
12
  export class ModelCandidateUtil {
19
13
 
20
14
  static async export(op: keyof ModelStorageSupport): Promise<{ models: string[], providers: string[] }> {
21
- return { models: await this.getModelNames(), providers: await this.getProviderNames(op) };
15
+ return {
16
+ models: await this.getModelNames(),
17
+ providers: await this.getProviderNames(op)
18
+ };
22
19
  }
23
20
 
24
21
  /**
@@ -65,17 +62,6 @@ export class ModelCandidateUtil {
65
62
  return DependencyRegistry.getInstance<ModelStorageSupport>(config.class, config.qualifier);
66
63
  }
67
64
 
68
- /**
69
- * Get candidates asynchronously
70
- * @returns
71
- */
72
- static async getCandidates(op: keyof ModelStorageSupport): Promise<CandidateNames> {
73
- return GlobalTerminal.withWaiting('Resolving', ExecUtil.worker<CandidateNames>(
74
- RootIndex.resolveFileImport('@travetto/cli/support/entry.cli'),
75
- ['main', '@travetto/model/support/bin/candidate.ts', op]
76
- ).message);
77
- }
78
-
79
65
  /**
80
66
  * Get resolved instances/classes/configs
81
67
  * @param provider
@@ -88,12 +74,4 @@ export class ModelCandidateUtil {
88
74
  models: await this.#getModels(models)
89
75
  };
90
76
  }
91
- }
92
-
93
- /**
94
- * Handles direct invocation
95
- */
96
- export async function main(op: keyof ModelStorageSupport): Promise<{ models: string[], providers: string[] }> {
97
- await RootRegistry.init();
98
- return await ModelCandidateUtil.export(op);
99
77
  }
@@ -1,33 +1,19 @@
1
- import { ConsoleManager } from '@travetto/base';
2
- import { RootRegistry } from '@travetto/registry';
1
+ import { CliCommand } from '@travetto/cli';
3
2
 
4
- import { BaseModelCommand } from './cli.base-command';
3
+ import { BaseModelCommand } from './base-command';
5
4
  import { ModelExportUtil } from './bin/export';
5
+ import { ModelCandidateUtil } from './bin/candidate';
6
6
 
7
7
  /**
8
- * CLI Entry point for exporting model schemas
8
+ * Exports model schemas
9
9
  */
10
+ @CliCommand({ fields: ['env', 'module'] })
10
11
  export class ModelExportCommand extends BaseModelCommand {
11
- name = 'model:export';
12
- op = 'exportModel' as const;
13
12
 
14
- async action(provider: string, models: string[]): Promise<void> {
15
- try {
16
- ConsoleManager.setDebug(false);
17
- await RootRegistry.init();
13
+ getOp(): 'exportModel' { return 'exportModel'; }
18
14
 
19
- if (!await this.validate(provider, models)) {
20
- return this.exit(1);
21
- }
22
-
23
- const resolved = await this.resolve(provider, models);
24
- await ModelExportUtil.run(resolved.provider, resolved.models);
25
- } catch (err) {
26
- if (err instanceof Error) {
27
- console.error(err.message);
28
- } else {
29
- console.error((err && err instanceof Error) ? err.message : err);
30
- }
31
- }
15
+ async main(provider: string, models: string[]): Promise<void> {
16
+ const resolved = await ModelCandidateUtil.resolve(provider, models);
17
+ await ModelExportUtil.run(resolved.provider, resolved.models);
32
18
  }
33
19
  }
@@ -1,31 +1,20 @@
1
- import { cliTpl } from '@travetto/cli';
2
- import { ConsoleManager } from '@travetto/base';
3
- import { RootRegistry } from '@travetto/registry';
1
+ import { CliCommand, cliTpl } from '@travetto/cli';
4
2
 
5
- import { BaseModelCommand } from './cli.base-command';
3
+ import { BaseModelCommand } from './base-command';
6
4
  import { ModelInstallUtil } from './bin/install';
5
+ import { ModelCandidateUtil } from './bin/candidate';
7
6
 
8
7
  /**
9
- * CLI Entry point for installing models
8
+ * Installing models
10
9
  */
10
+ @CliCommand({ fields: ['env', 'module'] })
11
11
  export class ModelInstallCommand extends BaseModelCommand {
12
- name = 'model:install';
13
- op = 'createModel' as const;
14
12
 
15
- async action(provider: string, models: string[]): Promise<void> {
16
- try {
17
- ConsoleManager.setDebug(false);
18
- await RootRegistry.init();
13
+ getOp(): 'createModel' { return 'createModel'; }
19
14
 
20
- if (!await this.validate(provider, models)) {
21
- return this.exit(1);
22
- }
23
-
24
- const resolved = await this.resolve(provider, models);
25
- await ModelInstallUtil.run(resolved.provider, resolved.models);
26
- console.log(cliTpl`${{ success: 'Successfully' }} installed ${{ param: models.length.toString() }} model(s)`);
27
- } catch (err) {
28
- console.error((err && err instanceof Error) ? err.message : err);
29
- }
15
+ async main(provider: string, models: string[]): Promise<void> {
16
+ const resolved = await ModelCandidateUtil.resolve(provider, models);
17
+ await ModelInstallUtil.run(resolved.provider, resolved.models);
18
+ console.log(cliTpl`${{ success: 'Successfully' }} installed ${{ param: models.length.toString() }} model(s)`);
30
19
  }
31
20
  }
@@ -1,54 +0,0 @@
1
- import { CliCommand, cliTpl, OptionConfig } from '@travetto/cli';
2
- import type { ModelStorageSupport } from '@travetto/model/src/service/storage';
3
-
4
- import { ModelCandidateUtil } from './bin/candidate';
5
-
6
- type Options = {
7
- env: OptionConfig<string>;
8
- };
9
-
10
- /**
11
- * CLI Entry point for exporting model schemas
12
- */
13
- export abstract class BaseModelCommand extends CliCommand<Options> {
14
-
15
- restoreEnv?: (err: Error) => unknown;
16
-
17
- op: keyof ModelStorageSupport;
18
-
19
- resolve = ModelCandidateUtil.resolve.bind(ModelCandidateUtil);
20
-
21
- getArgs(): string {
22
- return '[provider] [models...]';
23
- }
24
-
25
- getOptions(): Options {
26
- return { env: this.option({ desc: 'Application environment' }) };
27
- }
28
-
29
- usage({ providers, models }: { providers: string[], models: string[] }, err = ''): Promise<void> {
30
- return this.showHelp(err, cliTpl`
31
- ${{ title: 'Providers' }}:
32
- ${providers.map(p => cliTpl` * ${{ type: p }}`).join('\n')}
33
-
34
- ${{ title: 'Models' }}:
35
- ${models.map(p => cliTpl` * ${{ param: p }}`).join('\n')}
36
- `, false);
37
- }
38
-
39
- async validate(provider: string, models: string[]): Promise<true | void> {
40
- const candidates = await ModelCandidateUtil.getCandidates(this.op);
41
- if (!provider) {
42
- return this.usage(candidates);
43
- } else {
44
- if (!candidates.providers.includes(provider)) {
45
- return this.usage(candidates, cliTpl`${{ param: provider }} is not a valid provider`);
46
- }
47
- const badModel = models.find(x => x !== '*' && !candidates.models.includes(x));
48
- if (badModel) {
49
- return this.usage(candidates, cliTpl`${{ param: badModel }} is not a valid model`);
50
- }
51
- }
52
- return true;
53
- }
54
- }