@travetto/model-sql 8.0.0-alpha.10 → 8.0.0-alpha.11
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 +2 -2
- package/src/service.ts +34 -4
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.11",
|
|
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": [
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"@travetto/config": "^8.0.0-alpha.10",
|
|
32
32
|
"@travetto/context": "^8.0.0-alpha.10",
|
|
33
33
|
"@travetto/model": "^8.0.0-alpha.10",
|
|
34
|
-
"@travetto/model-indexed": "^8.0.0-alpha.
|
|
34
|
+
"@travetto/model-indexed": "^8.0.0-alpha.11",
|
|
35
35
|
"@travetto/model-query": "^8.0.0-alpha.10"
|
|
36
36
|
},
|
|
37
37
|
"peerDependencies": {
|
package/src/service.ts
CHANGED
|
@@ -340,7 +340,7 @@ export class SQLModelService implements
|
|
|
340
340
|
S extends SortedIndexSelection<T>
|
|
341
341
|
>(cls: Class<T>, idx: SingleItemIndex<T, K, S>, body: FullKeyedIndexBody<T, K, S>): Promise<T> {
|
|
342
342
|
const computed = ModelIndexedComputedIndex.get(idx, body).validate({ sort: true });
|
|
343
|
-
const results = await this.query(cls, castTo({ where: computed.project({ sort: true }) }));
|
|
343
|
+
const results = await this.query(cls, castTo({ where: computed.project({ sort: true, includeId: true }) }));
|
|
344
344
|
if (results.length !== 1) {
|
|
345
345
|
throw new NotFoundError(`${cls.name}: ${idx}`, computed.getKey({ sort: true }));
|
|
346
346
|
}
|
|
@@ -354,8 +354,11 @@ export class SQLModelService implements
|
|
|
354
354
|
K extends KeyedIndexSelection<T>,
|
|
355
355
|
S extends SortedIndexSelection<T>
|
|
356
356
|
>(cls: Class<T>, idx: SingleItemIndex<T, K, S>, body: FullKeyedIndexBody<T, K, S>): Promise<void> {
|
|
357
|
-
const
|
|
358
|
-
await this.
|
|
357
|
+
const computed = ModelIndexedComputedIndex.get(idx, body).validate({ sort: true });
|
|
358
|
+
const count = await this.deleteByQuery(cls, castTo({ where: computed.project({ sort: true, includeId: true }) }));
|
|
359
|
+
if (count === 0) {
|
|
360
|
+
throw new NotFoundError(`${cls.name}: ${idx}`, computed.getKey({ sort: true }));
|
|
361
|
+
}
|
|
359
362
|
}
|
|
360
363
|
|
|
361
364
|
@Connected()
|
|
@@ -390,7 +393,7 @@ export class SQLModelService implements
|
|
|
390
393
|
}
|
|
391
394
|
|
|
392
395
|
@Connected()
|
|
393
|
-
async
|
|
396
|
+
async pageByIndex<
|
|
394
397
|
T extends ModelType,
|
|
395
398
|
K extends KeyedIndexSelection<T>,
|
|
396
399
|
S extends SortedIndexSelection<T>
|
|
@@ -411,4 +414,31 @@ export class SQLModelService implements
|
|
|
411
414
|
}));
|
|
412
415
|
return { items, nextOffset: items.length ? JSONUtil.toBase64(offset + items.length) : undefined };
|
|
413
416
|
}
|
|
417
|
+
|
|
418
|
+
@ConnectedIterator()
|
|
419
|
+
async * listByIndex<
|
|
420
|
+
T extends ModelType,
|
|
421
|
+
K extends KeyedIndexSelection<T>,
|
|
422
|
+
S extends SortedIndexSelection<T>
|
|
423
|
+
>(
|
|
424
|
+
cls: Class<T>,
|
|
425
|
+
idx: SortedIndex<T, K, S>,
|
|
426
|
+
body: KeyedIndexBody<T, K>,
|
|
427
|
+
): AsyncIterable<T> {
|
|
428
|
+
const computed = ModelIndexedComputedIndex.get(idx, body).validate();
|
|
429
|
+
let offset = 0;
|
|
430
|
+
while (offset >= 0) {
|
|
431
|
+
const items = await this.query(cls, castTo({
|
|
432
|
+
where: computed.project(),
|
|
433
|
+
sort: idx.sortTemplate.map(part => ({ [part.path.join('.')]: part.value })),
|
|
434
|
+
limit: 100, offset
|
|
435
|
+
}));
|
|
436
|
+
if (items.length === 0) {
|
|
437
|
+
offset = -1;
|
|
438
|
+
} else {
|
|
439
|
+
offset += items.length;
|
|
440
|
+
yield* items;
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
}
|
|
414
444
|
}
|