@travetto/model-sql 8.0.0-alpha.10 → 8.0.0-alpha.12
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/package.json +8 -8
- package/src/service.ts +73 -17
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/model-sql",
|
|
3
|
-
"version": "8.0.0-alpha.
|
|
3
|
+
"version": "8.0.0-alpha.12",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "SQL backing for the travetto model module, with real-time modeling support for SQL schemas.",
|
|
6
6
|
"keywords": [
|
|
@@ -28,15 +28,15 @@
|
|
|
28
28
|
"directory": "module/model-sql"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@travetto/config": "^8.0.0-alpha.
|
|
32
|
-
"@travetto/context": "^8.0.0-alpha.
|
|
33
|
-
"@travetto/model": "^8.0.0-alpha.
|
|
34
|
-
"@travetto/model-indexed": "^8.0.0-alpha.
|
|
35
|
-
"@travetto/model-query": "^8.0.0-alpha.
|
|
31
|
+
"@travetto/config": "^8.0.0-alpha.11",
|
|
32
|
+
"@travetto/context": "^8.0.0-alpha.11",
|
|
33
|
+
"@travetto/model": "^8.0.0-alpha.11",
|
|
34
|
+
"@travetto/model-indexed": "^8.0.0-alpha.12",
|
|
35
|
+
"@travetto/model-query": "^8.0.0-alpha.11"
|
|
36
36
|
},
|
|
37
37
|
"peerDependencies": {
|
|
38
|
-
"@travetto/cli": "^8.0.0-alpha.
|
|
39
|
-
"@travetto/test": "^8.0.0-alpha.
|
|
38
|
+
"@travetto/cli": "^8.0.0-alpha.16",
|
|
39
|
+
"@travetto/test": "^8.0.0-alpha.11"
|
|
40
40
|
},
|
|
41
41
|
"peerDependenciesMeta": {
|
|
42
42
|
"@travetto/cli": {
|
package/src/service.ts
CHANGED
|
@@ -2,10 +2,11 @@ import {
|
|
|
2
2
|
type ModelType,
|
|
3
3
|
type BulkOperation, type BulkResponse, type ModelCrudSupport, type ModelStorageSupport, type ModelBulkSupport, NotFoundError,
|
|
4
4
|
ModelRegistryIndex, ExistsError, type OptionalId, type ModelIdSource, ModelExpiryUtil, ModelCrudUtil, ModelStorageUtil, ModelBulkUtil,
|
|
5
|
+
type ModelListOptions,
|
|
5
6
|
} from '@travetto/model';
|
|
6
7
|
import {
|
|
7
|
-
type ModelIndexedSupport, type KeyedIndexSelection, type KeyedIndexBody, type
|
|
8
|
-
type SingleItemIndex, type SortedIndexSelection, type
|
|
8
|
+
type ModelIndexedSupport, type KeyedIndexSelection, type KeyedIndexBody, type ModelPageOptions, ModelIndexedUtil,
|
|
9
|
+
type SingleItemIndex, type SortedIndexSelection, type ModelPageResult, type SortedIndex, type FullKeyedIndexBody,
|
|
9
10
|
type FullKeyedIndexWithPartialBody, ModelIndexedComputedIndex
|
|
10
11
|
} from '@travetto/model-indexed';
|
|
11
12
|
import { castTo, type Class, JSONUtil } from '@travetto/runtime';
|
|
@@ -103,6 +104,32 @@ export class SQLModelService implements
|
|
|
103
104
|
}
|
|
104
105
|
}
|
|
105
106
|
|
|
107
|
+
async * #scanTable<T extends ModelType>(
|
|
108
|
+
cls: Class<T>,
|
|
109
|
+
buildQuery: () => PageableModelQuery<T>,
|
|
110
|
+
options?: ModelListOptions & ModelPageOptions<number>
|
|
111
|
+
): AsyncIterable<{ items: T[], nextOffset?: number }> {
|
|
112
|
+
const batchSize = options?.batchSizeHint ?? 100;
|
|
113
|
+
const maxCount = options?.limit ?? Number.MAX_SAFE_INTEGER;
|
|
114
|
+
let offset = options?.offset ?? 0;
|
|
115
|
+
let lastOffset = -1;
|
|
116
|
+
let produced = 0;
|
|
117
|
+
while (offset !== lastOffset && produced < maxCount && !(options?.abort?.aborted)) {
|
|
118
|
+
const limit = Math.min(batchSize, maxCount - produced);
|
|
119
|
+
lastOffset = offset;
|
|
120
|
+
const items = await this.query<T>(cls, {
|
|
121
|
+
...buildQuery(),
|
|
122
|
+
limit,
|
|
123
|
+
offset
|
|
124
|
+
});
|
|
125
|
+
offset += items.length;
|
|
126
|
+
produced += items.length;
|
|
127
|
+
if (items.length) {
|
|
128
|
+
yield { items, nextOffset: items.length < limit ? undefined : offset };
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
106
133
|
@PostConstruct()
|
|
107
134
|
async initializeClient(): Promise<void> {
|
|
108
135
|
await this.#dialect.connection.init?.();
|
|
@@ -189,9 +216,9 @@ export class SQLModelService implements
|
|
|
189
216
|
}
|
|
190
217
|
|
|
191
218
|
@ConnectedIterator()
|
|
192
|
-
async * list<T extends ModelType>(cls: Class<T
|
|
193
|
-
for (const
|
|
194
|
-
yield
|
|
219
|
+
async * list<T extends ModelType>(cls: Class<T>, options?: ModelListOptions): AsyncIterable<T[]> {
|
|
220
|
+
for await (const { items } of this.#scanTable(cls, () => ({}), options)) {
|
|
221
|
+
yield items;
|
|
195
222
|
}
|
|
196
223
|
}
|
|
197
224
|
|
|
@@ -340,7 +367,7 @@ export class SQLModelService implements
|
|
|
340
367
|
S extends SortedIndexSelection<T>
|
|
341
368
|
>(cls: Class<T>, idx: SingleItemIndex<T, K, S>, body: FullKeyedIndexBody<T, K, S>): Promise<T> {
|
|
342
369
|
const computed = ModelIndexedComputedIndex.get(idx, body).validate({ sort: true });
|
|
343
|
-
const results = await this.query(cls, castTo({ where: computed.project({ sort: true }) }));
|
|
370
|
+
const results = await this.query(cls, castTo({ where: computed.project({ sort: true, includeId: true }) }));
|
|
344
371
|
if (results.length !== 1) {
|
|
345
372
|
throw new NotFoundError(`${cls.name}: ${idx}`, computed.getKey({ sort: true }));
|
|
346
373
|
}
|
|
@@ -354,8 +381,11 @@ export class SQLModelService implements
|
|
|
354
381
|
K extends KeyedIndexSelection<T>,
|
|
355
382
|
S extends SortedIndexSelection<T>
|
|
356
383
|
>(cls: Class<T>, idx: SingleItemIndex<T, K, S>, body: FullKeyedIndexBody<T, K, S>): Promise<void> {
|
|
357
|
-
const
|
|
358
|
-
await this.
|
|
384
|
+
const computed = ModelIndexedComputedIndex.get(idx, body).validate({ sort: true });
|
|
385
|
+
const count = await this.deleteByQuery(cls, castTo({ where: computed.project({ sort: true, includeId: true }) }));
|
|
386
|
+
if (count === 0) {
|
|
387
|
+
throw new NotFoundError(`${cls.name}: ${idx}`, computed.getKey({ sort: true }));
|
|
388
|
+
}
|
|
359
389
|
}
|
|
360
390
|
|
|
361
391
|
@Connected()
|
|
@@ -390,7 +420,7 @@ export class SQLModelService implements
|
|
|
390
420
|
}
|
|
391
421
|
|
|
392
422
|
@Connected()
|
|
393
|
-
async
|
|
423
|
+
async pageByIndex<
|
|
394
424
|
T extends ModelType,
|
|
395
425
|
K extends KeyedIndexSelection<T>,
|
|
396
426
|
S extends SortedIndexSelection<T>
|
|
@@ -398,17 +428,43 @@ export class SQLModelService implements
|
|
|
398
428
|
cls: Class<T>,
|
|
399
429
|
idx: SortedIndex<T, K, S>,
|
|
400
430
|
body: KeyedIndexBody<T, K>,
|
|
401
|
-
options?:
|
|
402
|
-
): Promise<
|
|
403
|
-
const offset = options?.offset ? JSONUtil.fromBase64<number>(options.offset) : 0;
|
|
404
|
-
const limit = options?.limit ?? 100;
|
|
431
|
+
options?: ModelPageOptions
|
|
432
|
+
): Promise<ModelPageResult<T>> {
|
|
405
433
|
const computed = ModelIndexedComputedIndex.get(idx, body).validate();
|
|
434
|
+
const offset = options?.offset ? JSONUtil.fromBase64<number>(options.offset) : 0;
|
|
406
435
|
|
|
407
|
-
const
|
|
436
|
+
const baseQuery = castTo<ModelQuery<T>>({
|
|
408
437
|
where: computed.project(),
|
|
409
438
|
sort: idx.sortTemplate.map(part => ({ [part.path.join('.')]: part.value })),
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
const items: T[] = [];
|
|
442
|
+
let nextOffset: number | undefined;
|
|
443
|
+
for await (const batch of this.#scanTable<T>(cls, () => baseQuery, { limit: 100, ...options, offset })) {
|
|
444
|
+
items.push(...batch.items);
|
|
445
|
+
nextOffset = batch.nextOffset;
|
|
446
|
+
}
|
|
447
|
+
return { items, nextOffset: nextOffset ? JSONUtil.toBase64(nextOffset) : undefined };
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
@ConnectedIterator()
|
|
451
|
+
async * listByIndex<
|
|
452
|
+
T extends ModelType,
|
|
453
|
+
K extends KeyedIndexSelection<T>,
|
|
454
|
+
S extends SortedIndexSelection<T>
|
|
455
|
+
>(
|
|
456
|
+
cls: Class<T>,
|
|
457
|
+
idx: SortedIndex<T, K, S>,
|
|
458
|
+
body: KeyedIndexBody<T, K>,
|
|
459
|
+
options?: ModelListOptions
|
|
460
|
+
): AsyncIterable<T[]> {
|
|
461
|
+
const computed = ModelIndexedComputedIndex.get(idx, body).validate();
|
|
462
|
+
const baseQuery = castTo<ModelQuery<T>>({
|
|
463
|
+
where: computed.project(),
|
|
464
|
+
sort: idx.sortTemplate.map(part => ({ [part.path.join('.')]: part.value })),
|
|
465
|
+
});
|
|
466
|
+
for await (const { items } of this.#scanTable<T>(cls, () => baseQuery, options)) {
|
|
467
|
+
yield items;
|
|
468
|
+
}
|
|
413
469
|
}
|
|
414
470
|
}
|