@travetto/model 3.1.1 → 3.1.3

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
@@ -60,9 +60,9 @@ The [CRUD](https://github.com/travetto/travetto/tree/main/module/model/src/servi
60
60
  export interface ModelCrudSupport extends ModelBasicSupport {
61
61
 
62
62
  /**
63
- * Uuid Generator
63
+ * Id Source
64
64
  */
65
- uuid: UuidGenerator;
65
+ idSource: ModelIdSource;
66
66
 
67
67
  /**
68
68
  * Update an item
@@ -195,7 +195,7 @@ export interface ModelBulkSupport extends ModelCrudSupport {
195
195
  ```
196
196
 
197
197
  ## Declaration
198
- Models are declared via the [@Model](https://github.com/travetto/travetto/tree/main/module/model/src/registry/decorator.ts#L12) decorator, which allows the system to know that this is a class that is compatible with the module. The only requirement for a model is the [ModelType](https://github.com/travetto/travetto/tree/main/module/model/src/types/model.ts#L4)
198
+ Models are declared via the [@Model](https://github.com/travetto/travetto/tree/main/module/model/src/registry/decorator.ts#L12) decorator, which allows the system to know that this is a class that is compatible with the module. The only requirement for a model is the [ModelType](https://github.com/travetto/travetto/tree/main/module/model/src/types/model.ts#L9)
199
199
 
200
200
  **Code: ModelType**
201
201
  ```typescript
@@ -289,7 +289,7 @@ export class MemoryModelService implements ModelCrudSupport, ModelStreamSupport,
289
289
  sorted: new Map<string, Map<string, Map<string, number>>>(),
290
290
  unsorted: new Map<string, Map<string, Set<string>>>()
291
291
  };
292
- uuid = ModelCrudUtil.uuidGenerator();
292
+ idSource = ModelCrudUtil.uuidSource();
293
293
  get client(): Map<string, StoreType>;
294
294
  constructor(public readonly config: MemoryModelConfig) { }
295
295
  async postConstruct(): Promise<void>;
package/__index__.ts CHANGED
@@ -9,7 +9,6 @@ export * from './src/service/indexed';
9
9
  export * from './src/service/expiry';
10
10
  export * from './src/service/storage';
11
11
  export * from './src/service/stream';
12
- export * from './src/service/types';
13
12
 
14
13
  export * from './src/provider/file';
15
14
  export * from './src/provider/memory';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/model",
3
- "version": "3.1.1",
3
+ "version": "3.1.3",
4
4
  "description": "Datastore abstraction for core operations.",
5
5
  "keywords": [
6
6
  "datastore",
@@ -1,9 +1,8 @@
1
1
  import { Class } from '@travetto/base';
2
2
 
3
3
  import { BulkOp } from '../../service/bulk';
4
- import { ModelUuidGenerator } from '../../service/types';
5
4
  import { ModelType } from '../../types/model';
6
- import { ModelCrudUtil } from './crud';
5
+ import { ModelCrudProvider, ModelCrudUtil } from './crud';
7
6
 
8
7
  export type BulkPreStore<T extends ModelType> = {
9
8
  insertedIds: Map<number, string>;
@@ -18,9 +17,9 @@ export class ModelBulkUtil {
18
17
  * Prepares bulk ops for storage
19
18
  * @param cls
20
19
  * @param operations
21
- * @param idSource
20
+ * @param provider
22
21
  */
23
- static async preStore<T extends ModelType>(cls: Class<T>, operations: BulkOp<T>[], idSource: { uuid: ModelUuidGenerator }): Promise<BulkPreStore<T>> {
22
+ static async preStore<T extends ModelType>(cls: Class<T>, operations: BulkOp<T>[], provider: ModelCrudProvider): Promise<BulkPreStore<T>> {
24
23
  const insertedIds = new Map<number, string>();
25
24
  const upsertedIds = new Map<number, string>();
26
25
  const updatedIds = new Map<number, string>();
@@ -30,14 +29,14 @@ export class ModelBulkUtil {
30
29
  let i = 0;
31
30
  for (const op of operations) {
32
31
  if ('insert' in op && op.insert) {
33
- op.insert = await ModelCrudUtil.preStore(cls, op.insert, idSource);
32
+ op.insert = await ModelCrudUtil.preStore(cls, op.insert, provider);
34
33
  insertedIds.set(i, op.insert.id!);
35
34
  } else if ('update' in op && op.update) {
36
- op.update = await ModelCrudUtil.preStore(cls, op.update, idSource);
35
+ op.update = await ModelCrudUtil.preStore(cls, op.update, provider);
37
36
  updatedIds.set(i, op.update.id);
38
37
  } else if ('upsert' in op && op.upsert) {
39
38
  const isNew = !op.upsert.id;
40
- op.upsert = await ModelCrudUtil.preStore(cls, op.upsert, idSource);
39
+ op.upsert = await ModelCrudUtil.preStore(cls, op.upsert, provider);
41
40
  if (isNew) {
42
41
  upsertedIds.set(i, op.upsert.id!);
43
42
  } else {
@@ -4,11 +4,14 @@ import { Class, ObjectUtil, Util } from '@travetto/base';
4
4
  import { SchemaRegistry, SchemaValidator, ValidationError, ValidationResultError } from '@travetto/schema';
5
5
 
6
6
  import { ModelRegistry } from '../../registry/model';
7
- import { ModelType, OptionalId } from '../../types/model';
7
+ import { ModelIdSource, ModelType, OptionalId } from '../../types/model';
8
8
  import { NotFoundError } from '../../error/not-found';
9
9
  import { ExistsError } from '../../error/exists';
10
10
  import { SubTypeNotSupportedError } from '../../error/invalid-sub-type';
11
- import { ModelUuidGenerator } from '../../service/types';
11
+
12
+ export type ModelCrudProvider = {
13
+ idSource: ModelIdSource;
14
+ };
12
15
 
13
16
  /**
14
17
  * Crud utilities
@@ -18,19 +21,10 @@ export class ModelCrudUtil {
18
21
  /**
19
22
  * Build a uuid generator
20
23
  */
21
- static uuidGenerator(create: () => string, valid: (id: string) => boolean): ModelUuidGenerator;
22
- static uuidGenerator(len?: number): ModelUuidGenerator;
23
- static uuidGenerator(lenOrCreate: (() => string) | number = 32, valid?: (id: string) => boolean): ModelUuidGenerator {
24
- if (typeof lenOrCreate === 'number') {
25
- const len = lenOrCreate;
26
- const create = (): string => Util.uuid(len);
27
- create.valid = (id: string): boolean => id.length === len && /^[0-9a-f]+$/i.test(id);
28
- return create;
29
- } else {
30
- const create = (): string => lenOrCreate();
31
- create.valid = valid!;
32
- return create;
33
- }
24
+ static uuidSource(len: number = 32): ModelIdSource {
25
+ const create = (): string => Util.uuid(len);
26
+ const valid = (id: string): boolean => id.length === len && /^[0-9a-f]+$/i.test(id);
27
+ return { create, valid };
34
28
  }
35
29
 
36
30
  /**
@@ -80,9 +74,9 @@ export class ModelCrudUtil {
80
74
  * @param cls Type to store for
81
75
  * @param item Item to store
82
76
  */
83
- static async preStore<T extends ModelType>(cls: Class<T>, item: Partial<OptionalId<T>>, idSource: { uuid: ModelUuidGenerator }): Promise<T> {
77
+ static async preStore<T extends ModelType>(cls: Class<T>, item: Partial<OptionalId<T>>, provider: ModelCrudProvider): Promise<T> {
84
78
  if (!item.id) {
85
- item.id = idSource.uuid();
79
+ item.id = provider.idSource.create();
86
80
  }
87
81
 
88
82
  if (ObjectUtil.isPlainObject(item)) {
@@ -109,7 +103,7 @@ export class ModelCrudUtil {
109
103
  }
110
104
  }
111
105
 
112
- if (!idSource.uuid.valid(item.id!)) {
106
+ if (!provider.idSource.valid(item.id!)) {
113
107
  errors.push({ kind: 'invalid', path: 'id', value: item.id!, type: 'string', message: `${item.id!} is an invalid value for \`id\`` });
114
108
  }
115
109
 
@@ -59,7 +59,7 @@ export class FileModelService implements ModelCrudSupport, ModelStreamSupport, M
59
59
  }
60
60
  }
61
61
 
62
- uuid = ModelCrudUtil.uuidGenerator();
62
+ idSource = ModelCrudUtil.uuidSource();
63
63
 
64
64
  get client(): string {
65
65
  return this.config.folder;
@@ -121,7 +121,7 @@ export class FileModelService implements ModelCrudSupport, ModelStreamSupport, M
121
121
 
122
122
  async create<T extends ModelType>(cls: Class<T>, item: OptionalId<T>): Promise<T> {
123
123
  if (!item.id) {
124
- item.id = this.uuid();
124
+ item.id = this.idSource.create();
125
125
  }
126
126
 
127
127
  const file = await this.#resolveName(cls, '.json', item.id);
@@ -58,7 +58,7 @@ export class MemoryModelService implements ModelCrudSupport, ModelStreamSupport,
58
58
  unsorted: new Map<string, Map<string, Set<string>>>()
59
59
  };
60
60
 
61
- uuid = ModelCrudUtil.uuidGenerator();
61
+ idSource = ModelCrudUtil.uuidSource();
62
62
  get client(): Map<string, StoreType> { return this.#store; }
63
63
 
64
64
  constructor(public readonly config: MemoryModelConfig) { }
@@ -188,7 +188,7 @@ export class MemoryModelService implements ModelCrudSupport, ModelStreamSupport,
188
188
 
189
189
  async create<T extends ModelType>(cls: Class<T>, item: OptionalId<T>): Promise<T> {
190
190
  if (!item.id) {
191
- item.id = this.uuid();
191
+ item.id = this.idSource.create();
192
192
  }
193
193
  this.#find(cls, item.id, 'data');
194
194
  return await this.upsert(cls, item);
@@ -1,8 +1,8 @@
1
1
  import { Class } from '@travetto/base';
2
2
 
3
- import { ModelType, OptionalId } from '../types/model';
3
+ import { ModelType, OptionalId, ModelIdSource } from '../types/model';
4
+
4
5
  import { ModelBasicSupport } from './basic';
5
- import { UuidGenerator } from './types';
6
6
 
7
7
  /**
8
8
  * Interface for simple CRUD
@@ -11,9 +11,9 @@ import { UuidGenerator } from './types';
11
11
  export interface ModelCrudSupport extends ModelBasicSupport {
12
12
 
13
13
  /**
14
- * Uuid Generator
14
+ * Id Source
15
15
  */
16
- uuid: UuidGenerator;
16
+ idSource: ModelIdSource;
17
17
 
18
18
  /**
19
19
  * Update an item
@@ -1,3 +1,8 @@
1
+ /**
2
+ * Model Id Source
3
+ */
4
+ export type ModelIdSource = { create: () => string, valid: (id: string) => boolean };
5
+
1
6
  /**
2
7
  * Simple model interface
3
8
  */
@@ -21,7 +21,7 @@ export abstract class ModelBasicSuite extends BaseModelSuite<ModelCrudSupport> {
21
21
  const service = await this.service;
22
22
 
23
23
  const person = Person.from({
24
- id: service.uuid(),
24
+ id: service.idSource.create(),
25
25
  name: 'Bob',
26
26
  age: 25,
27
27
  gender: 'm'
@@ -34,7 +34,7 @@ export abstract class ModelBasicSuite extends BaseModelSuite<ModelCrudSupport> {
34
34
  assert(single.age === 25);
35
35
 
36
36
  await assert.rejects(async () => {
37
- await service.get(Person, service.uuid());
37
+ await service.get(Person, service.idSource.create());
38
38
  }, NotFoundError);
39
39
 
40
40
  await service.delete(Person, person.id);
@@ -50,7 +50,7 @@ export abstract class ModelBasicSuite extends BaseModelSuite<ModelCrudSupport> {
50
50
  const service = await this.service;
51
51
 
52
52
  const { id } = await service.create(Person, {
53
- id: service.uuid(),
53
+ id: service.idSource.create(),
54
54
  name: 'Bob',
55
55
  age: 25,
56
56
  gender: 'm'
@@ -46,7 +46,7 @@ export abstract class ModelBulkSuite extends BaseModelSuite<ModelBulkSupport> {
46
46
  @Test()
47
47
  async bulkUpdate() {
48
48
  const service = await this.service;
49
- const users = [0, 1, 2, 4].map(x => User.from({ name: `name-${x}`, id: service.uuid() }));
49
+ const users = [0, 1, 2, 4].map(x => User.from({ name: `name-${x}`, id: service.idSource.create() }));
50
50
 
51
51
  const res = await service.processBulk(User, users.map(u => ({ insert: u })));
52
52
  assert(res.counts.insert === 4);
@@ -60,7 +60,7 @@ export abstract class ModelBulkSuite extends BaseModelSuite<ModelBulkSupport> {
60
60
  @Test()
61
61
  async bulkDelete() {
62
62
  const service = await this.service;
63
- const users = [0, 1, 2, 4].map(x => User.from({ name: `name-${x}`, id: service.uuid() }));
63
+ const users = [0, 1, 2, 4].map(x => User.from({ name: `name-${x}`, id: service.idSource.create() }));
64
64
 
65
65
  const res = await service.processBulk(User, users.map(u => ({ insert: u })));
66
66
  assert(res.counts.insert === 4);
@@ -62,7 +62,7 @@ export abstract class ModelCrudSuite extends BaseModelSuite<ModelCrudSupport> {
62
62
  const service = await this.service;
63
63
 
64
64
  const people = [1, 2, 3, 8].map(x => Person.from({
65
- id: service.uuid(),
65
+ id: service.idSource.create(),
66
66
  name: 'Bob',
67
67
  age: 20 + x,
68
68
  gender: 'm',
@@ -81,7 +81,7 @@ export abstract class ModelCrudSuite extends BaseModelSuite<ModelCrudSupport> {
81
81
  assert(single.age === 23);
82
82
 
83
83
  await assert.rejects(async () => {
84
- await service.get(Person, service.uuid());
84
+ await service.get(Person, service.idSource.create());
85
85
  }, NotFoundError);
86
86
  }
87
87
 
@@ -203,7 +203,7 @@ export abstract class ModelCrudSuite extends BaseModelSuite<ModelCrudSupport> {
203
203
  const service = await this.service;
204
204
 
205
205
  const people = [1, 2, 3].map(x => Person.from({
206
- id: service.uuid(),
206
+ id: service.idSource.create(),
207
207
  name: 'Bob',
208
208
  age: 20 + x,
209
209
  gender: 'm',
@@ -1,4 +0,0 @@
1
- /**
2
- * Model UUID Generator
3
- */
4
- export type ModelUuidGenerator = (() => string) & { valid: (id: string) => boolean };