@travetto/model 2.0.3 → 2.1.2
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 +4 -4
- package/package.json +6 -6
- package/src/provider/file.ts +16 -15
- package/test-support/stream.ts +5 -4
package/README.md
CHANGED
|
@@ -196,7 +196,7 @@ export interface ModelBulkSupport extends ModelCrudSupport {
|
|
|
196
196
|
```
|
|
197
197
|
|
|
198
198
|
## Declaration
|
|
199
|
-
Models are declared via the [@Model](https://github.com/travetto/travetto/tree/main/module/model/src/registry/decorator.ts#
|
|
199
|
+
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)
|
|
200
200
|
|
|
201
201
|
**Code: ModelType**
|
|
202
202
|
```typescript
|
|
@@ -236,7 +236,7 @@ All fields are optional, but the `id` and `type` are important as those field ty
|
|
|
236
236
|
|[S3 Model Support](https://github.com/travetto/travetto/tree/main/module/model-s3#readme "S3 backing for the travetto model module.")|X|X| |X|X| |
|
|
237
237
|
|[SQL Model Service](https://github.com/travetto/travetto/tree/main/module/model-sql#readme "SQL backing for the travetto model module, with real-time modeling support for SQL schemas.")|X|X|X|X| |X|
|
|
238
238
|
|[MemoryModelService](https://github.com/travetto/travetto/tree/main/module/model/src/provider/memory.ts#L50)|X|X|X|X|X|X|
|
|
239
|
-
|[FileModelService](https://github.com/travetto/travetto/tree/main/module/model/src/provider/file.ts#
|
|
239
|
+
|[FileModelService](https://github.com/travetto/travetto/tree/main/module/model/src/provider/file.ts#L47)|X|X| |X|X|X|
|
|
240
240
|
|
|
241
241
|
## Custom Model Service
|
|
242
242
|
In addition to the provided contracts, the module also provides common utilities and shared test suites. The common utilities are useful for
|
|
@@ -375,7 +375,7 @@ export class MemoryPolymorphicSuite extends ModelPolymorphismSuite {
|
|
|
375
375
|
|
|
376
376
|
## CLI - model:export
|
|
377
377
|
|
|
378
|
-
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#
|
|
378
|
+
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.
|
|
379
379
|
|
|
380
380
|
**Terminal: Running model export**
|
|
381
381
|
```bash
|
|
@@ -390,7 +390,7 @@ Options:
|
|
|
390
390
|
|
|
391
391
|
## CLI - model:install
|
|
392
392
|
|
|
393
|
-
The module provides the ability to install all the various [@Model](https://github.com/travetto/travetto/tree/main/module/model/src/registry/decorator.ts#
|
|
393
|
+
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 targetted. This is useful for being able to prepare the datastore manually.
|
|
394
394
|
|
|
395
395
|
**Terminal: Running model install**
|
|
396
396
|
```bash
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/model",
|
|
3
3
|
"displayName": "Data Modeling Support",
|
|
4
|
-
"version": "2.
|
|
4
|
+
"version": "2.1.2",
|
|
5
5
|
"description": "Datastore abstraction for core operations.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"datastore",
|
|
@@ -28,13 +28,13 @@
|
|
|
28
28
|
"directory": "module/model"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@travetto/di": "^2.
|
|
32
|
-
"@travetto/config": "^2.
|
|
33
|
-
"@travetto/registry": "^2.
|
|
34
|
-
"@travetto/schema": "^2.
|
|
31
|
+
"@travetto/di": "^2.1.2",
|
|
32
|
+
"@travetto/config": "^2.1.2",
|
|
33
|
+
"@travetto/registry": "^2.1.2",
|
|
34
|
+
"@travetto/schema": "^2.1.2"
|
|
35
35
|
},
|
|
36
36
|
"optionalPeerDependencies": {
|
|
37
|
-
"@travetto/cli": "^2.
|
|
37
|
+
"@travetto/cli": "^2.1.2"
|
|
38
38
|
},
|
|
39
39
|
"publishConfig": {
|
|
40
40
|
"access": "public"
|
package/src/provider/file.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import * as fs from 'fs';
|
|
1
|
+
import * as fs from 'fs/promises';
|
|
2
|
+
import { createReadStream } from 'fs';
|
|
2
3
|
import * as os from 'os';
|
|
3
4
|
import * as path from 'path';
|
|
4
5
|
|
|
@@ -47,8 +48,8 @@ export class FileModelConfig {
|
|
|
47
48
|
export class FileModelService implements ModelCrudSupport, ModelStreamSupport, ModelExpirySupport, ModelStorageSupport {
|
|
48
49
|
|
|
49
50
|
private static async * scanFolder(folder: string, suffix: string) {
|
|
50
|
-
for (const sub of await fs.
|
|
51
|
-
for (const file of await fs.
|
|
51
|
+
for (const sub of await fs.readdir(folder)) {
|
|
52
|
+
for (const file of await fs.readdir(PathUtil.resolveUnix(folder, sub))) {
|
|
52
53
|
if (file.endsWith(suffix)) {
|
|
53
54
|
yield [file.replace(suffix, ''), PathUtil.resolveUnix(folder, sub, file)] as [id: string, file: string];
|
|
54
55
|
}
|
|
@@ -79,7 +80,7 @@ export class FileModelService implements ModelCrudSupport, ModelStreamSupport, M
|
|
|
79
80
|
dir = path.dirname(resolved);
|
|
80
81
|
}
|
|
81
82
|
if (!await FsUtil.exists(dir)) {
|
|
82
|
-
await fs.
|
|
83
|
+
await fs.mkdir(dir, { recursive: true });
|
|
83
84
|
}
|
|
84
85
|
return resolved;
|
|
85
86
|
}
|
|
@@ -114,7 +115,7 @@ export class FileModelService implements ModelCrudSupport, ModelStreamSupport, M
|
|
|
114
115
|
const file = await this.#resolveName(cls, '.json', id);
|
|
115
116
|
|
|
116
117
|
if (await FsUtil.exists(file)) {
|
|
117
|
-
const content = await StreamUtil.streamToBuffer(
|
|
118
|
+
const content = await StreamUtil.streamToBuffer(createReadStream(file));
|
|
118
119
|
return this.checkExpiry(cls, await ModelCrudUtil.load(cls, content));
|
|
119
120
|
}
|
|
120
121
|
|
|
@@ -129,7 +130,7 @@ export class FileModelService implements ModelCrudSupport, ModelStreamSupport, M
|
|
|
129
130
|
const file = await this.#resolveName(cls, '.json', item.id);
|
|
130
131
|
|
|
131
132
|
if (await FsUtil.exists(file)) {
|
|
132
|
-
throw new ExistsError(cls, item.id);
|
|
133
|
+
throw new ExistsError(cls, item.id!);
|
|
133
134
|
}
|
|
134
135
|
|
|
135
136
|
return await this.upsert(cls, item);
|
|
@@ -145,7 +146,7 @@ export class FileModelService implements ModelCrudSupport, ModelStreamSupport, M
|
|
|
145
146
|
const prepped = await ModelCrudUtil.preStore(cls, item, this);
|
|
146
147
|
|
|
147
148
|
const file = await this.#resolveName(cls, '.json', item.id);
|
|
148
|
-
await fs.
|
|
149
|
+
await fs.writeFile(file, JSON.stringify(item), { encoding: 'utf8' });
|
|
149
150
|
|
|
150
151
|
return prepped;
|
|
151
152
|
}
|
|
@@ -155,14 +156,14 @@ export class FileModelService implements ModelCrudSupport, ModelStreamSupport, M
|
|
|
155
156
|
const id = item.id;
|
|
156
157
|
item = await ModelCrudUtil.naivePartialUpdate(cls, item, view, () => this.get(cls, id));
|
|
157
158
|
const file = await this.#resolveName(cls, '.json', item.id);
|
|
158
|
-
await fs.
|
|
159
|
+
await fs.writeFile(file, JSON.stringify(item), { encoding: 'utf8' });
|
|
159
160
|
|
|
160
161
|
return item as T;
|
|
161
162
|
}
|
|
162
163
|
|
|
163
164
|
async delete<T extends ModelType>(cls: Class<T>, id: string) {
|
|
164
165
|
const file = await this.#find(cls, '.json', id);
|
|
165
|
-
await fs.
|
|
166
|
+
await fs.unlink(file);
|
|
166
167
|
}
|
|
167
168
|
|
|
168
169
|
async * list<T extends ModelType>(cls: Class<T>) {
|
|
@@ -182,18 +183,18 @@ export class FileModelService implements ModelCrudSupport, ModelStreamSupport, M
|
|
|
182
183
|
const file = await this.#resolveName(STREAMS, BIN, location);
|
|
183
184
|
await Promise.all([
|
|
184
185
|
StreamUtil.writeToFile(input, file),
|
|
185
|
-
fs.
|
|
186
|
+
fs.writeFile(file.replace(BIN, META), JSON.stringify(meta), 'utf8')
|
|
186
187
|
]);
|
|
187
188
|
}
|
|
188
189
|
|
|
189
190
|
async getStream(location: string) {
|
|
190
191
|
const file = await this.#find(STREAMS, BIN, location);
|
|
191
|
-
return
|
|
192
|
+
return createReadStream(file);
|
|
192
193
|
}
|
|
193
194
|
|
|
194
195
|
async describeStream(location: string) {
|
|
195
196
|
const file = await this.#find(STREAMS, META, location);
|
|
196
|
-
const content = await StreamUtil.streamToBuffer(
|
|
197
|
+
const content = await StreamUtil.streamToBuffer(createReadStream(file));
|
|
197
198
|
const text = JSON.parse(content.toString('utf8'));
|
|
198
199
|
return text as StreamMeta;
|
|
199
200
|
}
|
|
@@ -202,8 +203,8 @@ export class FileModelService implements ModelCrudSupport, ModelStreamSupport, M
|
|
|
202
203
|
const file = await this.#resolveName(STREAMS, BIN, location);
|
|
203
204
|
if (await FsUtil.exists(file)) {
|
|
204
205
|
await Promise.all([
|
|
205
|
-
fs.
|
|
206
|
-
fs.
|
|
206
|
+
fs.unlink(file),
|
|
207
|
+
fs.unlink(file.replace('.bin', META))
|
|
207
208
|
]);
|
|
208
209
|
} else {
|
|
209
210
|
throw new NotFoundError('Stream', location);
|
|
@@ -224,7 +225,7 @@ export class FileModelService implements ModelCrudSupport, ModelStreamSupport, M
|
|
|
224
225
|
// Storage mgmt
|
|
225
226
|
async createStorage() {
|
|
226
227
|
const dir = PathUtil.resolveUnix(this.config.folder, this.config.namespace);
|
|
227
|
-
await fs.
|
|
228
|
+
await fs.mkdir(dir, { recursive: true });
|
|
228
229
|
}
|
|
229
230
|
|
|
230
231
|
async deleteStorage() {
|
package/test-support/stream.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as assert from 'assert';
|
|
2
|
-
import * as fs from 'fs';
|
|
2
|
+
import * as fs from 'fs/promises';
|
|
3
|
+
import { createReadStream } from 'fs';
|
|
3
4
|
import * as crypto from 'crypto';
|
|
4
5
|
|
|
5
6
|
import { PathUtil } from '@travetto/boot';
|
|
@@ -25,12 +26,12 @@ export abstract class ModelStreamSuite extends BaseModelSuite<ModelStreamSupport
|
|
|
25
26
|
|
|
26
27
|
async getStream(resource: string) {
|
|
27
28
|
const file = await ResourceManager.findAbsolute(resource);
|
|
28
|
-
const stat = await fs.
|
|
29
|
-
const hash = await this.getHash(
|
|
29
|
+
const stat = await fs.stat(file);
|
|
30
|
+
const hash = await this.getHash(createReadStream(file));
|
|
30
31
|
|
|
31
32
|
return [
|
|
32
33
|
{ size: stat.size, contentType: '', hash, filename: resource },
|
|
33
|
-
|
|
34
|
+
createReadStream(file)
|
|
34
35
|
] as const;
|
|
35
36
|
}
|
|
36
37
|
|