@travetto/model-dynamodb 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/README.md CHANGED
@@ -18,7 +18,7 @@ This module provides an [DynamoDB](https://aws.amazon.com/dynamodb/)-based imple
18
18
  Supported features:
19
19
  * [CRUD](https://github.com/travetto/travetto/tree/main/module/model/src/types/crud.ts#L11)
20
20
  * [Expiry](https://github.com/travetto/travetto/tree/main/module/model/src/types/expiry.ts#L10)
21
- * [Indexed](https://github.com/travetto/travetto/tree/main/module/model-indexed/src/types/service.ts#L23)
21
+ * [Indexed](https://github.com/travetto/travetto/tree/main/module/model-indexed/src/types/service.ts#L15)
22
22
 
23
23
  Out of the box, by installing the module, everything should be wired up by default.If you need to customize any aspect of the source or config, you can override and register it with the [Dependency Injection](https://github.com/travetto/travetto/tree/main/module/di#readme "Dependency registration/management and injection support.") module.
24
24
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/model-dynamodb",
3
- "version": "8.0.0-alpha.10",
3
+ "version": "8.0.0-alpha.11",
4
4
  "type": "module",
5
5
  "description": "DynamoDB backing for the travetto model module.",
6
6
  "keywords": [
@@ -29,7 +29,7 @@
29
29
  "@aws-sdk/client-dynamodb": "^3.1019.0",
30
30
  "@travetto/config": "^8.0.0-alpha.10",
31
31
  "@travetto/model": "^8.0.0-alpha.10",
32
- "@travetto/model-indexed": "^8.0.0-alpha.10"
32
+ "@travetto/model-indexed": "^8.0.0-alpha.11"
33
33
  },
34
34
  "peerDependencies": {
35
35
  "@travetto/cli": "^8.0.0-alpha.15"
package/src/service.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { type AttributeValue, DynamoDB, type PutItemCommandInput, type PutItemCommandOutput, type QueryCommandOutput } from '@aws-sdk/client-dynamodb';
1
+ import { type AttributeValue, DynamoDB, type PutItemCommandInput, type PutItemCommandOutput, type QueryCommandInput, type QueryCommandOutput } from '@aws-sdk/client-dynamodb';
2
2
 
3
3
  import { castTo, JSONUtil, ShutdownManager, TimeUtil, type Class } from '@travetto/runtime';
4
4
  import { Injectable, PostConstruct } from '@travetto/di';
@@ -101,16 +101,22 @@ export class DynamoDBModelService implements ModelCrudSupport, ModelExpirySuppor
101
101
  const safeName = DynamoDBUtil.toSafeName(idx.name);
102
102
  const sorted = idx.type === 'indexed:sorted';
103
103
 
104
- const query = {
104
+ const query: QueryCommandInput = {
105
105
  TableName: this.#resolveTable(cls),
106
106
  IndexName: safeName,
107
107
  ProjectionExpression: 'id',
108
- KeyConditionExpression: [sorted ? `${safeName}_sort__ = :${safeName}_sort` : '', `${safeName}__ = :${safeName}`]
109
- .filter(expr => !!expr)
108
+ KeyConditionExpression: [
109
+ ...(sorted ? [`${safeName}_sort__ = :${safeName}_sort`] : []),
110
+ `${safeName}__ = :${safeName}`
111
+ ]
110
112
  .join(' and '),
113
+ ...(computed.idPart ? {
114
+ FilterExpression: 'id = :id'
115
+ } : {}),
111
116
  ExpressionAttributeValues: {
112
117
  [`:${safeName}`]: getKey(computed),
113
- ...(sorted ? { [`:${safeName}_sort`]: getSort(computed) } : {})
118
+ ...(sorted ? { [`:${safeName}_sort`]: getSort(computed) } : {}),
119
+ ...(computed.idPart ? { ':id': DynamoDBUtil.toValue(computed.idPart.value) } : {})
114
120
  }
115
121
  };
116
122
 
@@ -445,7 +451,7 @@ export class DynamoDBModelService implements ModelCrudSupport, ModelExpirySuppor
445
451
  return this.update(cls, item);
446
452
  }
447
453
 
448
- async listByIndex<
454
+ async pageByIndex<
449
455
  T extends ModelType,
450
456
  K extends KeyedIndexSelection<T>,
451
457
  S extends SortedIndexSelection<T>
@@ -486,4 +492,26 @@ export class DynamoDBModelService implements ModelCrudSupport, ModelExpirySuppor
486
492
 
487
493
  return { items, nextOffset };
488
494
  }
495
+
496
+ async * listByIndex<
497
+ T extends ModelType,
498
+ K extends KeyedIndexSelection<T>,
499
+ S extends SortedIndexSelection<T>
500
+ >(
501
+ cls: Class<T>,
502
+ idx: SortedIndex<T, K, S>,
503
+ body: KeyedIndexBody<T, K>,
504
+ ): AsyncIterable<T> {
505
+ for await (const batch of this.#scanIndex(cls, idx, body, { limit: Number.MAX_SAFE_INTEGER })) {
506
+ for (const item of batch.Items ?? []) {
507
+ try {
508
+ yield await DynamoDBUtil.loadAndCheckExpiry(cls, item.body.S!);
509
+ } catch (error) {
510
+ if (!(error instanceof NotFoundError)) {
511
+ throw error;
512
+ }
513
+ }
514
+ }
515
+ }
516
+ }
489
517
  }