@travetto/model-sql 8.0.0-alpha.11 → 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 +53 -27
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
|
|
|
@@ -401,18 +428,23 @@ export class SQLModelService implements
|
|
|
401
428
|
cls: Class<T>,
|
|
402
429
|
idx: SortedIndex<T, K, S>,
|
|
403
430
|
body: KeyedIndexBody<T, K>,
|
|
404
|
-
options?:
|
|
405
|
-
): Promise<
|
|
406
|
-
const offset = options?.offset ? JSONUtil.fromBase64<number>(options.offset) : 0;
|
|
407
|
-
const limit = options?.limit ?? 100;
|
|
431
|
+
options?: ModelPageOptions
|
|
432
|
+
): Promise<ModelPageResult<T>> {
|
|
408
433
|
const computed = ModelIndexedComputedIndex.get(idx, body).validate();
|
|
434
|
+
const offset = options?.offset ? JSONUtil.fromBase64<number>(options.offset) : 0;
|
|
409
435
|
|
|
410
|
-
const
|
|
436
|
+
const baseQuery = castTo<ModelQuery<T>>({
|
|
411
437
|
where: computed.project(),
|
|
412
438
|
sort: idx.sortTemplate.map(part => ({ [part.path.join('.')]: part.value })),
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
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 };
|
|
416
448
|
}
|
|
417
449
|
|
|
418
450
|
@ConnectedIterator()
|
|
@@ -424,21 +456,15 @@ export class SQLModelService implements
|
|
|
424
456
|
cls: Class<T>,
|
|
425
457
|
idx: SortedIndex<T, K, S>,
|
|
426
458
|
body: KeyedIndexBody<T, K>,
|
|
427
|
-
|
|
459
|
+
options?: ModelListOptions
|
|
460
|
+
): AsyncIterable<T[]> {
|
|
428
461
|
const computed = ModelIndexedComputedIndex.get(idx, body).validate();
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
}));
|
|
436
|
-
if (items.length === 0) {
|
|
437
|
-
offset = -1;
|
|
438
|
-
} else {
|
|
439
|
-
offset += items.length;
|
|
440
|
-
yield* items;
|
|
441
|
-
}
|
|
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;
|
|
442
468
|
}
|
|
443
469
|
}
|
|
444
470
|
}
|