@travetto/model-dynamodb 4.0.0-rc.2 → 4.0.0-rc.4
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 +4 -4
- package/src/service.ts +16 -13
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/model-dynamodb",
|
|
3
|
-
"version": "4.0.0-rc.
|
|
3
|
+
"version": "4.0.0-rc.4",
|
|
4
4
|
"description": "DynamoDB backing for the travetto model module.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -26,11 +26,11 @@
|
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
28
|
"@aws-sdk/client-dynamodb": "^3.506.0",
|
|
29
|
-
"@travetto/config": "^4.0.0-rc.
|
|
30
|
-
"@travetto/model": "^4.0.0-rc.
|
|
29
|
+
"@travetto/config": "^4.0.0-rc.4",
|
|
30
|
+
"@travetto/model": "^4.0.0-rc.4"
|
|
31
31
|
},
|
|
32
32
|
"peerDependencies": {
|
|
33
|
-
"@travetto/command": "^4.0.0-rc.
|
|
33
|
+
"@travetto/command": "^4.0.0-rc.4"
|
|
34
34
|
},
|
|
35
35
|
"peerDependenciesMeta": {
|
|
36
36
|
"@travetto/command": {
|
package/src/service.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
import
|
|
1
|
+
import {
|
|
2
|
+
AttributeDefinition, AttributeValue, DynamoDB, GlobalSecondaryIndex,
|
|
3
|
+
KeySchemaElement, PutItemCommandInput, PutItemCommandOutput
|
|
4
|
+
} from '@aws-sdk/client-dynamodb';
|
|
2
5
|
|
|
3
6
|
import { ShutdownManager, type Class } from '@travetto/base';
|
|
4
7
|
import { DeepPartial } from '@travetto/schema';
|
|
@@ -22,8 +25,8 @@ function simpleName(idx: string): string {
|
|
|
22
25
|
return idx.replace(/[^A-Za-z0-9]/g, '');
|
|
23
26
|
}
|
|
24
27
|
|
|
25
|
-
function toValue(val: string | number | boolean | Date | undefined | null, forceString?: boolean):
|
|
26
|
-
function toValue(val: unknown, forceString?: boolean):
|
|
28
|
+
function toValue(val: string | number | boolean | Date | undefined | null, forceString?: boolean): AttributeValue;
|
|
29
|
+
function toValue(val: unknown, forceString?: boolean): AttributeValue | undefined {
|
|
27
30
|
if (val === undefined || val === null || val === '') {
|
|
28
31
|
return { NULL: true };
|
|
29
32
|
} else if (typeof val === 'string' || forceString) {
|
|
@@ -58,7 +61,7 @@ async function loadAndCheckExpiry<T extends ModelType>(cls: Class<T>, doc: strin
|
|
|
58
61
|
export class DynamoDBModelService implements ModelCrudSupport, ModelExpirySupport, ModelStorageSupport, ModelIndexedSupport {
|
|
59
62
|
|
|
60
63
|
idSource = ModelCrudUtil.uuidSource();
|
|
61
|
-
client:
|
|
64
|
+
client: DynamoDB;
|
|
62
65
|
|
|
63
66
|
constructor(public readonly config: DynamoDBModelConfig) { }
|
|
64
67
|
|
|
@@ -70,7 +73,7 @@ export class DynamoDBModelService implements ModelCrudSupport, ModelExpirySuppor
|
|
|
70
73
|
return table;
|
|
71
74
|
}
|
|
72
75
|
|
|
73
|
-
async #putItem<T extends ModelType>(cls: Class<T>, id: string, item: T, mode: 'create' | 'update' | 'upsert'): Promise<
|
|
76
|
+
async #putItem<T extends ModelType>(cls: Class<T>, id: string, item: T, mode: 'create' | 'update' | 'upsert'): Promise<PutItemCommandOutput> {
|
|
74
77
|
const config = ModelRegistry.get(cls);
|
|
75
78
|
let expiry: number | undefined;
|
|
76
79
|
|
|
@@ -92,7 +95,7 @@ export class DynamoDBModelService implements ModelCrudSupport, ModelExpirySuppor
|
|
|
92
95
|
indices[`${prop}_sort__`] = toValue(+sort);
|
|
93
96
|
}
|
|
94
97
|
}
|
|
95
|
-
const query:
|
|
98
|
+
const query: PutItemCommandInput = {
|
|
96
99
|
TableName: this.#resolveTable(cls),
|
|
97
100
|
ConditionExpression: 'attribute_not_exists(body)',
|
|
98
101
|
Item: {
|
|
@@ -148,16 +151,16 @@ export class DynamoDBModelService implements ModelCrudSupport, ModelExpirySuppor
|
|
|
148
151
|
}
|
|
149
152
|
}
|
|
150
153
|
|
|
151
|
-
#computeIndexConfig<T extends ModelType>(cls: Class<T>): { indices?:
|
|
154
|
+
#computeIndexConfig<T extends ModelType>(cls: Class<T>): { indices?: GlobalSecondaryIndex[], attributes: AttributeDefinition[] } {
|
|
152
155
|
const config = ModelRegistry.get(cls);
|
|
153
|
-
const attributes:
|
|
154
|
-
const indices:
|
|
156
|
+
const attributes: AttributeDefinition[] = [];
|
|
157
|
+
const indices: GlobalSecondaryIndex[] = [];
|
|
155
158
|
|
|
156
159
|
for (const idx of config.indices ?? []) {
|
|
157
160
|
const idxName = simpleName(idx.name);
|
|
158
161
|
attributes.push({ AttributeName: `${idxName}__`, AttributeType: 'S' });
|
|
159
162
|
|
|
160
|
-
const keys:
|
|
163
|
+
const keys: KeySchemaElement[] = [{
|
|
161
164
|
AttributeName: `${idxName}__`,
|
|
162
165
|
KeyType: 'HASH'
|
|
163
166
|
}];
|
|
@@ -185,7 +188,7 @@ export class DynamoDBModelService implements ModelCrudSupport, ModelExpirySuppor
|
|
|
185
188
|
}
|
|
186
189
|
|
|
187
190
|
async postConstruct(): Promise<void> {
|
|
188
|
-
this.client = new
|
|
191
|
+
this.client = new DynamoDB({ ...this.config.client });
|
|
189
192
|
await ModelStorageUtil.registerModelChangeListener(this);
|
|
190
193
|
ShutdownManager.onGracefulShutdown(async () => this.client.destroy(), this);
|
|
191
194
|
}
|
|
@@ -328,7 +331,7 @@ export class DynamoDBModelService implements ModelCrudSupport, ModelExpirySuppor
|
|
|
328
331
|
|
|
329
332
|
async * list<T extends ModelType>(cls: Class<T>): AsyncIterable<T> {
|
|
330
333
|
let done = false;
|
|
331
|
-
let token: Record<string,
|
|
334
|
+
let token: Record<string, AttributeValue> | undefined;
|
|
332
335
|
while (!done) {
|
|
333
336
|
const batch = await this.client.scan({
|
|
334
337
|
TableName: this.#resolveTable(cls),
|
|
@@ -415,7 +418,7 @@ export class DynamoDBModelService implements ModelCrudSupport, ModelExpirySuppor
|
|
|
415
418
|
const idxName = simpleName(idx);
|
|
416
419
|
|
|
417
420
|
let done = false;
|
|
418
|
-
let token: Record<string,
|
|
421
|
+
let token: Record<string, AttributeValue> | undefined;
|
|
419
422
|
while (!done) {
|
|
420
423
|
const batch = await this.client.query({
|
|
421
424
|
TableName: this.#resolveTable(cls),
|