@travetto/model 3.1.7 → 3.1.9

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
@@ -205,7 +205,7 @@ export interface ModelBulkSupport extends ModelCrudSupport {
205
205
  ```
206
206
 
207
207
  ## Declaration
208
- 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)
208
+ Models are declared via the [@Model](https://github.com/travetto/travetto/tree/main/module/model/src/registry/decorator.ts#L13) 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)
209
209
 
210
210
  **Code: ModelType**
211
211
  ```typescript
@@ -317,7 +317,7 @@ export class MemoryModelService implements ModelCrudSupport, ModelStreamSupport,
317
317
  async getStreamPartial(location: string, start: number, end?: number): Promise<PartialStream>;
318
318
  async describeStream(location: string): Promise<StreamMeta>;
319
319
  async deleteStream(location: string): Promise<void>;
320
- // Expiry Support
320
+ // Expiry
321
321
  async deleteExpired<T extends ModelType>(cls: Class<T>): Promise<number>;
322
322
  // Storage Support
323
323
  async createStorage(): Promise<void>;
@@ -384,7 +384,7 @@ export class MemoryPolymorphicSuite extends ModelPolymorphismSuite {
384
384
  ```
385
385
 
386
386
  ## CLI - model:export
387
- The module provides the ability to generate an export of the model structure from all the various [@Model](https://github.com/travetto/travetto/tree/main/module/model/src/registry/decorator.ts#L12)s within the application. This is useful for being able to generate the appropriate files to manually create the data schemas in production.
387
+ The module provides the ability to generate an export of the model structure from all the various [@Model](https://github.com/travetto/travetto/tree/main/module/model/src/registry/decorator.ts#L13)s within the application. This is useful for being able to generate the appropriate files to manually create the data schemas in production.
388
388
 
389
389
  **Terminal: Running model export**
390
390
  ```bash
@@ -407,7 +407,7 @@ Models
407
407
  ```
408
408
 
409
409
  ## CLI - model:install
410
- The module provides the ability to install all the various [@Model](https://github.com/travetto/travetto/tree/main/module/model/src/registry/decorator.ts#L12)s within the application given the current configuration being targeted. This is useful for being able to prepare the datastore manually.
410
+ The module provides the ability to install all the various [@Model](https://github.com/travetto/travetto/tree/main/module/model/src/registry/decorator.ts#L13)s within the application given the current configuration being targeted. This is useful for being able to prepare the datastore manually.
411
411
 
412
412
  **Terminal: Running model install**
413
413
  ```bash
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/model",
3
- "version": "3.1.7",
3
+ "version": "3.1.9",
4
4
  "description": "Datastore abstraction for core operations.",
5
5
  "keywords": [
6
6
  "datastore",
@@ -26,13 +26,13 @@
26
26
  "directory": "module/model"
27
27
  },
28
28
  "dependencies": {
29
- "@travetto/config": "^3.1.2",
29
+ "@travetto/config": "^3.1.3",
30
30
  "@travetto/di": "^3.1.1",
31
31
  "@travetto/registry": "^3.1.1",
32
- "@travetto/schema": "^3.1.2"
32
+ "@travetto/schema": "^3.1.3"
33
33
  },
34
34
  "peerDependencies": {
35
- "@travetto/cli": "^3.1.2",
35
+ "@travetto/cli": "^3.1.3",
36
36
  "@travetto/test": "^3.1.1"
37
37
  },
38
38
  "peerDependenciesMeta": {
@@ -6,9 +6,16 @@ export const StreamModel: Class<ModelType> = Cls;
6
6
  export const STREAMS = '_streams';
7
7
 
8
8
  export class ModelStreamUtil {
9
- static checkRange(start: number, end: number, size: number): void {
10
- if (Number.isNaN(start) || Number.isNaN(end) || !Number.isFinite(start) || start < 0 || end >= size) {
9
+ static enforceRange(start: number, end: number | undefined, size: number): [start: number, end: number] {
10
+
11
+ end ??= size - 1;
12
+
13
+ if (Number.isNaN(start) || Number.isNaN(end) || !Number.isFinite(start) || start >= size || start < 0) {
11
14
  throw new AppError('Invalid position, out of range', 'data');
12
15
  }
16
+ if (end >= size) {
17
+ end = size - 1;
18
+ }
19
+ return [start, end];
13
20
  }
14
21
  }
@@ -193,9 +193,8 @@ export class FileModelService implements ModelCrudSupport, ModelStreamSupport, M
193
193
  async getStreamPartial(location: string, start: number, end?: number): Promise<PartialStream> {
194
194
  const file = await this.#find(STREAMS, BIN, location);
195
195
  const meta = await this.describeStream(location);
196
- end ??= meta.size - 1;
197
196
 
198
- ModelStreamUtil.checkRange(start, end, meta.size);
197
+ [start, end] = ModelStreamUtil.enforceRange(start, end, meta.size);
199
198
 
200
199
  const stream = createReadStream(file, { start, end });
201
200
  return { stream, range: [start, end] };
@@ -252,8 +252,9 @@ export class MemoryModelService implements ModelCrudSupport, ModelStreamSupport,
252
252
  async getStreamPartial(location: string, start: number, end?: number): Promise<PartialStream> {
253
253
  const streams = this.#find(STREAMS, location, 'notfound');
254
254
  const buffer = streams.get(location)!;
255
- end ??= (buffer.length - 1);
256
- ModelStreamUtil.checkRange(start, end, buffer.length);
255
+
256
+ [start, end] = ModelStreamUtil.enforceRange(start, end, buffer.length);
257
+
257
258
  const stream = await StreamUtil.bufferToStream(buffer.subarray(start, end + 1));
258
259
  return { stream, range: [start, end] };
259
260
  }
@@ -1,4 +1,5 @@
1
1
  import { Class } from '@travetto/base';
2
+ import { SchemaRegistry } from '@travetto/schema';
2
3
 
3
4
  import { ModelType } from '../types/model';
4
5
  import { ModelRegistry } from './model';
@@ -15,6 +16,7 @@ export function Model(conf: Partial<ModelOptions<ModelType>> | string = {}) {
15
16
  conf = { store: conf };
16
17
  }
17
18
  ModelRegistry.register(target, conf);
19
+ SchemaRegistry.register(target, { baseType: conf.baseType });
18
20
  return target;
19
21
  };
20
22
  }
@@ -64,8 +64,8 @@ class $ModelRegistry extends MetadataRegistry<ModelOptions<ModelType>> {
64
64
  delete view.id.required; // Allow ids to be optional
65
65
 
66
66
  if ('type' in view && this.getBaseModel(cls) !== cls) {
67
- config.subType = schema.subType; // Copy from schema
68
- delete view.type.required; // Allow type to be optional
67
+ config.subType = !!schema.subTypeName; // Copy from schema
68
+ delete view[schema.subTypeField].required; // Allow type to be optional
69
69
  }
70
70
  return config;
71
71
  }
@@ -98,7 +98,12 @@ export abstract class ModelStreamSuite extends BaseModelSuite<ModelStreamSupport
98
98
  assert(subContent3.length === 1);
99
99
  assert(subContent3 === 'k');
100
100
 
101
+ const partialOverbounded = await service.getStreamPartial(meta.hash, 20, 40);
102
+ const subContent4 = (await StreamUtil.toBuffer(partialOverbounded.stream)).toString('utf8');
103
+ assert(subContent4.length === 6);
104
+ assert(subContent4.endsWith('xyz'));
105
+
101
106
  await assert.rejects(() => service.getStreamPartial(meta.hash, -10, 10));
102
- await assert.rejects(() => service.getStreamPartial(meta.hash, 0, 27));
107
+ await assert.rejects(() => service.getStreamPartial(meta.hash, 30, 37));
103
108
  }
104
109
  }