@twin.org/entity-storage-connector-dynamodb 0.0.1-next.10

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.
@@ -0,0 +1,725 @@
1
+ import { waitUntilTableExists, DynamoDB, QueryCommand } from '@aws-sdk/client-dynamodb';
2
+ import { GetCommand, PutCommand, DeleteCommand, DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb';
3
+ import { unmarshall } from '@aws-sdk/util-dynamodb';
4
+ import { Guards, Is, BaseError, GeneralError, Coerce, ObjectHelper, Converter } from '@twin.org/core';
5
+ import { EntitySchemaFactory, EntitySchemaHelper, ComparisonOperator, LogicalOperator, SortDirection } from '@twin.org/entity';
6
+ import { LoggingConnectorFactory } from '@twin.org/logging-models';
7
+
8
+ // Copyright 2024 IOTA Stiftung.
9
+ // SPDX-License-Identifier: Apache-2.0.
10
+ /**
11
+ * Class for performing entity storage operations using Dynamo DB.
12
+ */
13
+ class DynamoDbEntityStorageConnector {
14
+ /**
15
+ * Limit the number of entities when finding.
16
+ * @internal
17
+ */
18
+ static _PAGE_SIZE = 40;
19
+ /**
20
+ * Partition id field name.
21
+ * @internal
22
+ */
23
+ static _PARTITION_ID_NAME = "partitionId";
24
+ /**
25
+ * Partition id field value.
26
+ * @internal
27
+ */
28
+ static _PARTITION_ID_VALUE = "1";
29
+ /**
30
+ * Runtime name for the class.
31
+ */
32
+ CLASS_NAME = "DynamoDbEntityStorageConnector";
33
+ /**
34
+ * The schema for the entity.
35
+ * @internal
36
+ */
37
+ _entitySchema;
38
+ /**
39
+ * The primary key.
40
+ * @internal
41
+ */
42
+ _primaryKey;
43
+ /**
44
+ * The configuration for the connector.
45
+ * @internal
46
+ */
47
+ _config;
48
+ /**
49
+ * Create a new instance of DynamoDbEntityStorageConnector.
50
+ * @param options The options for the connector.
51
+ * @param options.entitySchema The schema for the entity.
52
+ * @param options.loggingConnectorType The type of logging connector to use, defaults to no logging.
53
+ * @param options.config The configuration for the connector.
54
+ */
55
+ constructor(options) {
56
+ Guards.object(this.CLASS_NAME, "options", options);
57
+ Guards.stringValue(this.CLASS_NAME, "options.entitySchema", options.entitySchema);
58
+ Guards.object(this.CLASS_NAME, "options.config", options.config);
59
+ Guards.stringValue(this.CLASS_NAME, "options.config.accessKeyId", options.config.accessKeyId);
60
+ Guards.stringValue(this.CLASS_NAME, "options.config.secretAccessKey", options.config.secretAccessKey);
61
+ Guards.stringValue(this.CLASS_NAME, "options.config.region", options.config.region);
62
+ Guards.stringValue(this.CLASS_NAME, "options.config.tableName", options.config.tableName);
63
+ this._entitySchema = EntitySchemaFactory.get(options.entitySchema);
64
+ this._primaryKey = EntitySchemaHelper.getPrimaryKey(this._entitySchema);
65
+ this._config = options.config;
66
+ this._config.endpoint = Is.stringValue(this._config.endpoint)
67
+ ? this._config.endpoint
68
+ : undefined;
69
+ }
70
+ /**
71
+ * Bootstrap the component by creating and initializing any resources it needs.
72
+ * @param nodeLoggingConnectorType The node logging connector type, defaults to "node-logging".
73
+ * @returns True if the bootstrapping process was successful.
74
+ */
75
+ async bootstrap(nodeLoggingConnectorType) {
76
+ const nodeLogging = LoggingConnectorFactory.getIfExists(nodeLoggingConnectorType ?? "node-logging");
77
+ if (!(await this.tableExists(this._config.tableName))) {
78
+ await nodeLogging?.log({
79
+ level: "info",
80
+ source: this.CLASS_NAME,
81
+ ts: Date.now(),
82
+ message: "tableCreating",
83
+ data: {
84
+ tableName: this._config.tableName
85
+ }
86
+ });
87
+ try {
88
+ const dbConnection = this.createConnection();
89
+ const tableParams = {
90
+ AttributeDefinitions: [],
91
+ KeySchema: [],
92
+ ProvisionedThroughput: {
93
+ ReadCapacityUnits: 1,
94
+ WriteCapacityUnits: 1
95
+ },
96
+ TableName: this._config.tableName
97
+ };
98
+ // We always add a partition key to the table as a non optional hash key
99
+ // is always required when querying using sort parameters
100
+ tableParams.AttributeDefinitions?.push({
101
+ AttributeName: DynamoDbEntityStorageConnector._PARTITION_ID_NAME,
102
+ AttributeType: "S"
103
+ });
104
+ tableParams.KeySchema?.push({
105
+ AttributeName: DynamoDbEntityStorageConnector._PARTITION_ID_NAME,
106
+ KeyType: "HASH"
107
+ });
108
+ const gsi = [];
109
+ if (Is.arrayValue(this._entitySchema.properties)) {
110
+ for (const prop of this._entitySchema.properties) {
111
+ if (prop.isPrimary) {
112
+ tableParams.AttributeDefinitions?.push({
113
+ AttributeName: prop.property,
114
+ AttributeType: prop.type === "integer" || prop.type === "number" ? "N" : "S"
115
+ });
116
+ tableParams.KeySchema?.push({
117
+ AttributeName: prop.property,
118
+ KeyType: "RANGE"
119
+ });
120
+ }
121
+ else if (Is.stringValue(prop.sortDirection) || prop.isSecondary) {
122
+ // You can only query and sort items if you have a secondary index
123
+ // defined for the property
124
+ tableParams.AttributeDefinitions?.push({
125
+ AttributeName: prop.property,
126
+ AttributeType: prop.type === "integer" || prop.type === "number" ? "N" : "S"
127
+ });
128
+ gsi.push({
129
+ IndexName: `${prop.property}Index`,
130
+ KeySchema: [
131
+ {
132
+ AttributeName: DynamoDbEntityStorageConnector._PARTITION_ID_NAME,
133
+ KeyType: "HASH"
134
+ },
135
+ {
136
+ AttributeName: prop.property,
137
+ KeyType: "RANGE"
138
+ }
139
+ ],
140
+ Projection: {
141
+ ProjectionType: "ALL"
142
+ },
143
+ ProvisionedThroughput: {
144
+ ReadCapacityUnits: 1,
145
+ WriteCapacityUnits: 1
146
+ }
147
+ });
148
+ }
149
+ }
150
+ }
151
+ if (gsi.length > 0) {
152
+ tableParams.GlobalSecondaryIndexes = gsi;
153
+ }
154
+ await dbConnection.createTable(tableParams);
155
+ // Wait for table to exist
156
+ await waitUntilTableExists({
157
+ client: dbConnection,
158
+ maxWaitTime: 60000
159
+ }, {
160
+ TableName: this._config.tableName
161
+ });
162
+ await nodeLogging?.log({
163
+ level: "info",
164
+ source: this.CLASS_NAME,
165
+ ts: Date.now(),
166
+ message: "tableCreated",
167
+ data: {
168
+ tableName: this._config.tableName
169
+ }
170
+ });
171
+ }
172
+ catch (err) {
173
+ if (BaseError.isErrorCode(err, "ResourceInUseException")) {
174
+ await nodeLogging?.log({
175
+ level: "info",
176
+ source: this.CLASS_NAME,
177
+ ts: Date.now(),
178
+ message: "tableExists",
179
+ data: {
180
+ tableName: this._config.tableName
181
+ }
182
+ });
183
+ }
184
+ else {
185
+ const errors = err instanceof AggregateError ? err.errors : [err];
186
+ for (const error of errors) {
187
+ await nodeLogging?.log({
188
+ level: "error",
189
+ source: this.CLASS_NAME,
190
+ ts: Date.now(),
191
+ message: "tableCreateFailed",
192
+ error: BaseError.fromError(error),
193
+ data: {
194
+ tableName: this._config.tableName
195
+ }
196
+ });
197
+ }
198
+ }
199
+ return false;
200
+ }
201
+ }
202
+ else {
203
+ await nodeLogging?.log({
204
+ level: "info",
205
+ source: this.CLASS_NAME,
206
+ ts: Date.now(),
207
+ message: "tableExists",
208
+ data: {
209
+ tableName: this._config.tableName
210
+ }
211
+ });
212
+ }
213
+ return true;
214
+ }
215
+ /**
216
+ * Get the schema for the entities.
217
+ * @returns The schema for the entities.
218
+ */
219
+ getSchema() {
220
+ return this._entitySchema;
221
+ }
222
+ /**
223
+ * Get an entity.
224
+ * @param id The id of the entity to get, or the index value if secondaryIndex is set.
225
+ * @param secondaryIndex Get the item using a secondary index.
226
+ * @param conditions The optional conditions to match for the entities.
227
+ * @returns The object if it can be found or undefined.
228
+ */
229
+ async get(id, secondaryIndex, conditions) {
230
+ Guards.stringValue(this.CLASS_NAME, "id", id);
231
+ try {
232
+ const docClient = this.createDocClient();
233
+ if (Is.empty(secondaryIndex) && Is.empty(conditions)) {
234
+ const getCommand = new GetCommand({
235
+ TableName: this._config.tableName,
236
+ Key: {
237
+ [DynamoDbEntityStorageConnector._PARTITION_ID_NAME]: DynamoDbEntityStorageConnector._PARTITION_ID_VALUE,
238
+ [this._primaryKey.property]: id
239
+ }
240
+ });
241
+ const response = await docClient.send(getCommand);
242
+ delete response.Item?.[DynamoDbEntityStorageConnector._PARTITION_ID_NAME];
243
+ return response.Item;
244
+ }
245
+ const finalConditions = {
246
+ conditions: []
247
+ };
248
+ if (Is.stringValue(secondaryIndex)) {
249
+ finalConditions.conditions.push({
250
+ property: secondaryIndex,
251
+ comparison: ComparisonOperator.Equals,
252
+ value: id
253
+ });
254
+ }
255
+ if (Is.arrayValue(conditions)) {
256
+ for (const c of conditions) {
257
+ finalConditions.conditions.push({
258
+ property: c.property,
259
+ comparison: ComparisonOperator.Equals,
260
+ value: c.value
261
+ });
262
+ }
263
+ }
264
+ const queryResult = await this.internalQuery(finalConditions, undefined, undefined, undefined, 1, secondaryIndex);
265
+ return queryResult.entities[0];
266
+ }
267
+ catch (err) {
268
+ if (BaseError.isErrorCode(err, "ResourceNotFoundException")) {
269
+ throw new GeneralError(this.CLASS_NAME, "tableDoesNotExist", {
270
+ table: this._config.tableName
271
+ }, err);
272
+ }
273
+ throw new GeneralError(this.CLASS_NAME, "getFailed", {
274
+ id
275
+ }, err);
276
+ }
277
+ }
278
+ /**
279
+ * Set an entity.
280
+ * @param entity The entity to set.
281
+ * @param conditions The optional conditions to match for the entities.
282
+ * @returns The id of the entity.
283
+ */
284
+ async set(entity, conditions) {
285
+ Guards.object(this.CLASS_NAME, "entity", entity);
286
+ const id = entity[this._primaryKey.property];
287
+ try {
288
+ const docClient = this.createDocClient();
289
+ const { conditionExpression, attributeNames, attributeValues } = this.buildConditionExpression(conditions);
290
+ const putCommand = new PutCommand({
291
+ TableName: this._config.tableName,
292
+ Item: {
293
+ [DynamoDbEntityStorageConnector._PARTITION_ID_NAME]: DynamoDbEntityStorageConnector._PARTITION_ID_VALUE,
294
+ ...entity
295
+ },
296
+ ConditionExpression: conditionExpression,
297
+ ExpressionAttributeNames: attributeNames,
298
+ ExpressionAttributeValues: attributeValues
299
+ });
300
+ await docClient.send(putCommand);
301
+ }
302
+ catch (err) {
303
+ if (BaseError.isErrorCode(err, "ResourceNotFoundException")) {
304
+ throw new GeneralError(this.CLASS_NAME, "tableDoesNotExist", {
305
+ tableName: this._config.tableName
306
+ }, err);
307
+ }
308
+ throw new GeneralError(this.CLASS_NAME, "setFailed", {
309
+ id
310
+ }, err);
311
+ }
312
+ }
313
+ /**
314
+ * Remove the entity.
315
+ * @param id The id of the entity to remove.
316
+ * @param conditions The optional conditions to match for the entities.
317
+ * @returns Nothing.
318
+ */
319
+ async remove(id, conditions) {
320
+ Guards.stringValue(this.CLASS_NAME, "id", id);
321
+ try {
322
+ const docClient = this.createDocClient();
323
+ const { conditionExpression, attributeNames, attributeValues } = this.buildConditionExpression(conditions);
324
+ const deleteCommand = new DeleteCommand({
325
+ TableName: this._config.tableName,
326
+ Key: {
327
+ [DynamoDbEntityStorageConnector._PARTITION_ID_NAME]: DynamoDbEntityStorageConnector._PARTITION_ID_VALUE,
328
+ [this._primaryKey.property]: id
329
+ },
330
+ ConditionExpression: conditionExpression,
331
+ ExpressionAttributeNames: attributeNames,
332
+ ExpressionAttributeValues: attributeValues
333
+ });
334
+ await docClient.send(deleteCommand);
335
+ }
336
+ catch (err) {
337
+ if (BaseError.isErrorName(err, "ConditionalCheckFailedException")) {
338
+ return;
339
+ }
340
+ if (BaseError.isErrorCode(err, "ResourceNotFoundException")) {
341
+ throw new GeneralError(this.CLASS_NAME, "tableDoesNotExist", {
342
+ table: this._config.tableName
343
+ }, err);
344
+ }
345
+ throw new GeneralError(this.CLASS_NAME, "removeFailed", {
346
+ id
347
+ }, err);
348
+ }
349
+ }
350
+ /**
351
+ * Find all the entities which match the conditions.
352
+ * @param conditions The conditions to match for the entities.
353
+ * @param sortProperties The optional sort order.
354
+ * @param properties The optional properties to return, defaults to all.
355
+ * @param cursor The cursor to request the next page of entities.
356
+ * @param pageSize The suggested number of entities to return in each chunk, in some scenarios can return a different amount.
357
+ * @returns All the entities for the storage matching the conditions,
358
+ * and a cursor which can be used to request more entities.
359
+ */
360
+ async query(conditions, sortProperties, properties, cursor, pageSize) {
361
+ return this.internalQuery(conditions, sortProperties, properties, cursor, pageSize);
362
+ }
363
+ /**
364
+ * Delete the table.
365
+ * @returns Nothing.
366
+ */
367
+ async tableDelete() {
368
+ try {
369
+ const dbConnection = this.createConnection();
370
+ await dbConnection.deleteTable({ TableName: this._config.tableName });
371
+ }
372
+ catch { }
373
+ }
374
+ /**
375
+ * Create the parameters for a query.
376
+ * @param objectPath The path for the nested object.
377
+ * @param condition The conditions to create the query from.
378
+ * @param attributeNames The attribute names to use in the query.
379
+ * @param attributeValues The attribute values to use in the query.
380
+ * @returns The condition clause.
381
+ * @internal
382
+ */
383
+ buildQueryParameters(objectPath, condition, attributeNames, attributeValues, secondaryIndex) {
384
+ // If no conditions are defined then return empty string
385
+ if (Is.undefined(condition)) {
386
+ return {
387
+ keyCondition: "",
388
+ filterCondition: ""
389
+ };
390
+ }
391
+ if ("conditions" in condition) {
392
+ if (condition.conditions.length === 0) {
393
+ return {
394
+ keyCondition: "",
395
+ filterCondition: ""
396
+ };
397
+ }
398
+ // It's a group of comparisons, so check the individual items and combine with the logical operator
399
+ const joinConditions = condition.conditions.map(c => this.buildQueryParameters(objectPath, c, attributeNames, attributeValues, secondaryIndex));
400
+ const logicalOperator = this.mapConditionalOperator(condition.logicalOperator);
401
+ const keyCondition = joinConditions
402
+ .filter(j => j.keyCondition.length > 0)
403
+ .map(j => j.keyCondition)
404
+ .join(` ${logicalOperator} `);
405
+ const filterCondition = joinConditions
406
+ .filter(j => j.filterCondition.length > 0)
407
+ .map(j => j.filterCondition)
408
+ .join(` ${logicalOperator} `);
409
+ return {
410
+ keyCondition: Is.stringValue(keyCondition) ? ` (${keyCondition}) ` : "",
411
+ filterCondition: Is.stringValue(filterCondition) ? ` (${filterCondition}) ` : ""
412
+ };
413
+ }
414
+ const schemaProp = this._entitySchema.properties?.find(p => p.property === condition.property);
415
+ // It's a single value so just create the property comparison for the condition
416
+ const comparison = this.mapComparisonOperator(objectPath, condition, schemaProp?.type, attributeNames, attributeValues);
417
+ const isKey = schemaProp?.isPrimary || (schemaProp?.isSecondary && schemaProp?.property === secondaryIndex);
418
+ return {
419
+ keyCondition: isKey ? comparison : "",
420
+ filterCondition: !isKey ? comparison : ""
421
+ };
422
+ }
423
+ /**
424
+ * Map the framework comparison operators to those in DynamoDB.
425
+ * @param objectPath The prefix to use for the condition.
426
+ * @param comparator The operator to map.
427
+ * @param type The type of the property.
428
+ * @param attributeNames The attribute names to use in the query.
429
+ * @param attributeValues The attribute values to use in the query.
430
+ * @returns The comparison expression.
431
+ * @throws GeneralError if the comparison operator is not supported.
432
+ * @internal
433
+ */
434
+ mapComparisonOperator(objectPath, comparator, type, attributeNames, attributeValues) {
435
+ let prop = objectPath;
436
+ if (prop.length > 0) {
437
+ prop += ".";
438
+ }
439
+ prop += comparator.property;
440
+ let attributeName = this.populateAttributeNames(prop, attributeNames);
441
+ let propName = `:${attributeName.replace(/\./g, "").replace(/#/g, "")}`;
442
+ if (Is.array(comparator.value)) {
443
+ const dbValues = comparator.value.map(v => this.propertyToDbValue(v, type));
444
+ const arrAttributeNames = [];
445
+ for (let i = 0; i < dbValues.length; i++) {
446
+ const arrAttributeName = `${propName}${i}`;
447
+ attributeValues[arrAttributeName] = dbValues[i];
448
+ arrAttributeNames.push(arrAttributeName);
449
+ }
450
+ propName = attributeName;
451
+ attributeName = `(${arrAttributeNames.join(", ")})`;
452
+ }
453
+ else {
454
+ attributeValues[propName] = this.propertyToDbValue(comparator.value, type);
455
+ }
456
+ if (comparator.comparison === ComparisonOperator.Equals) {
457
+ return `${attributeName} = ${propName}`;
458
+ }
459
+ else if (comparator.comparison === ComparisonOperator.NotEquals) {
460
+ return `${attributeName} <> ${propName}`;
461
+ }
462
+ else if (comparator.comparison === ComparisonOperator.GreaterThan) {
463
+ return `${attributeName} > ${propName}`;
464
+ }
465
+ else if (comparator.comparison === ComparisonOperator.LessThan) {
466
+ return `${attributeName} < ${propName}`;
467
+ }
468
+ else if (comparator.comparison === ComparisonOperator.GreaterThanOrEqual) {
469
+ return `${attributeName} >= ${propName}`;
470
+ }
471
+ else if (comparator.comparison === ComparisonOperator.LessThanOrEqual) {
472
+ return `${attributeName} <= ${propName}`;
473
+ }
474
+ else if (comparator.comparison === ComparisonOperator.Includes) {
475
+ return `contains(${attributeName}, ${propName})`;
476
+ }
477
+ else if (comparator.comparison === ComparisonOperator.NotIncludes) {
478
+ return `notContains(${attributeName}, ${propName})`;
479
+ }
480
+ else if (comparator.comparison === ComparisonOperator.In) {
481
+ return `${propName} IN ${attributeName}`;
482
+ }
483
+ throw new GeneralError(this.CLASS_NAME, "comparisonNotSupported", {
484
+ comparison: comparator.comparison
485
+ });
486
+ }
487
+ /**
488
+ * Create a unique name for the attribute.
489
+ * @param name The name to create a unique name for.
490
+ * @param attributeNames The attribute names to use in the query.
491
+ * @returns The unique name.
492
+ * @internal
493
+ */
494
+ populateAttributeNames(name, attributeNames) {
495
+ const parts = name.split(".");
496
+ const attributeNameParts = [];
497
+ for (const part of parts) {
498
+ const hashPart = `#${part}`;
499
+ if (Is.empty(attributeNames[hashPart])) {
500
+ attributeNames[hashPart] = part;
501
+ }
502
+ attributeNameParts.push(hashPart);
503
+ }
504
+ return attributeNameParts.join(".");
505
+ }
506
+ /**
507
+ * Map the framework conditional operators to those in DynamoDB.
508
+ * @param operator The operator to map.
509
+ * @returns The conditional operator.
510
+ * @throws GeneralError if the conditional operator is not supported.
511
+ * @internal
512
+ */
513
+ mapConditionalOperator(operator) {
514
+ if ((operator ?? LogicalOperator.And) === LogicalOperator.And) {
515
+ return "AND";
516
+ }
517
+ else if (operator === LogicalOperator.Or) {
518
+ return "OR";
519
+ }
520
+ throw new GeneralError(this.CLASS_NAME, "conditionalNotSupported", { operator });
521
+ }
522
+ /**
523
+ * Format a value to insert into DB.
524
+ * @param value The value to format.
525
+ * @param type The type for the property.
526
+ * @returns The value after conversion.
527
+ * @internal
528
+ */
529
+ propertyToDbValue(value, type) {
530
+ if (Is.object(value)) {
531
+ const map = {};
532
+ for (const key in value) {
533
+ map[key] = this.propertyToDbValue(value[key]);
534
+ }
535
+ return {
536
+ M: map
537
+ };
538
+ }
539
+ if (type === "integer" || type === "number") {
540
+ return { N: Coerce.string(value) ?? "" };
541
+ }
542
+ else if (type === "boolean") {
543
+ return { BOOL: Coerce.boolean(value) ?? false };
544
+ }
545
+ return { S: Coerce.string(value) ?? "" };
546
+ }
547
+ /**
548
+ * Create a doc client connection.
549
+ * @returns The dynamo db document client.
550
+ * @internal
551
+ */
552
+ createDocClient() {
553
+ return DynamoDBDocumentClient.from(new DynamoDB({
554
+ apiVersion: "2012-10-08",
555
+ ...this.createConnectionConfig()
556
+ }), {
557
+ marshallOptions: {
558
+ removeUndefinedValues: true
559
+ }
560
+ });
561
+ }
562
+ /**
563
+ * Create a new DB connection.
564
+ * @returns The dynamo db connection.
565
+ * @internal
566
+ */
567
+ createConnection() {
568
+ return new DynamoDB(this.createConnectionConfig());
569
+ }
570
+ /**
571
+ * Create a new DB connection configuration.
572
+ * @returns The dynamo db connection configuration.
573
+ * @internal
574
+ */
575
+ createConnectionConfig() {
576
+ return {
577
+ credentials: {
578
+ accessKeyId: this._config.accessKeyId,
579
+ secretAccessKey: this._config.secretAccessKey
580
+ },
581
+ endpoint: this._config.endpoint,
582
+ region: this._config.region
583
+ };
584
+ }
585
+ /**
586
+ * Check if the table exists.
587
+ * @param tableName The table to check.
588
+ * @returns True if the table exists.
589
+ * @internal
590
+ */
591
+ async tableExists(tableName) {
592
+ try {
593
+ const dbConnection = this.createConnection();
594
+ await dbConnection.describeTable({ TableName: tableName });
595
+ return true;
596
+ }
597
+ catch {
598
+ return false;
599
+ }
600
+ }
601
+ /**
602
+ * Find all the entities which match the conditions.
603
+ * @param conditions The conditions to match for the entities.
604
+ * @param sortProperties The optional sort order.
605
+ * @param properties The optional properties to return, defaults to all.
606
+ * @param cursor The cursor to request the next page of entities.
607
+ * @param pageSize The suggested number of entities to return in each chunk, in some scenarios can return a different amount.
608
+ * @param secondaryIndex The secondary index to use for the query.
609
+ * @returns All the entities for the storage matching the conditions,
610
+ * and a cursor which can be used to request more entities.
611
+ * @internal
612
+ */
613
+ async internalQuery(conditions, sortProperties, properties, cursor, pageSize, secondaryIndex) {
614
+ try {
615
+ const returnSize = pageSize ?? DynamoDbEntityStorageConnector._PAGE_SIZE;
616
+ let indexName = Is.stringValue(secondaryIndex)
617
+ ? `${secondaryIndex}Index`
618
+ : undefined;
619
+ // If we have a sortable property defined in the descriptor then we must use
620
+ // the secondary index for the query
621
+ let scanAscending = true;
622
+ if (Is.arrayValue(sortProperties)) {
623
+ if (sortProperties.length > 1) {
624
+ throw new GeneralError(this.CLASS_NAME, "sortSingle");
625
+ }
626
+ for (const sortProperty of sortProperties) {
627
+ const propertySchema = this._entitySchema.properties?.find(e => e.property === sortProperty.property);
628
+ if (Is.undefined(propertySchema) ||
629
+ (!propertySchema.isPrimary &&
630
+ !propertySchema.isSecondary &&
631
+ Is.empty(propertySchema.sortDirection))) {
632
+ throw new GeneralError(this.CLASS_NAME, "sortNotIndexed", {
633
+ property: sortProperty.property
634
+ });
635
+ }
636
+ indexName = propertySchema.isPrimary
637
+ ? undefined
638
+ : `${sortProperty.property}Index`;
639
+ scanAscending = sortProperty.sortDirection === SortDirection.Ascending;
640
+ }
641
+ }
642
+ const attributeNames = { "#partitionId": "partitionId" };
643
+ const attributeValues = {
644
+ [`:${DynamoDbEntityStorageConnector._PARTITION_ID_NAME}`]: {
645
+ S: DynamoDbEntityStorageConnector._PARTITION_ID_VALUE
646
+ }
647
+ };
648
+ const expressions = this.buildQueryParameters("", conditions, attributeNames, attributeValues, secondaryIndex);
649
+ let keyExpression = "#partitionId = :partitionId";
650
+ if (expressions.keyCondition.length > 0) {
651
+ keyExpression += ` AND ${expressions.keyCondition}`;
652
+ }
653
+ const query = new QueryCommand({
654
+ TableName: this._config.tableName,
655
+ IndexName: indexName,
656
+ KeyConditionExpression: keyExpression,
657
+ FilterExpression: Is.stringValue(expressions.filterCondition)
658
+ ? expressions.filterCondition
659
+ : undefined,
660
+ ExpressionAttributeNames: attributeNames,
661
+ ExpressionAttributeValues: attributeValues,
662
+ ProjectionExpression: properties?.map(p => p).join(", "),
663
+ Limit: returnSize,
664
+ ScanIndexForward: scanAscending,
665
+ ExclusiveStartKey: Is.empty(cursor)
666
+ ? undefined
667
+ : ObjectHelper.fromBytes(Converter.base64ToBytes(cursor))
668
+ });
669
+ const connection = this.createDocClient();
670
+ const results = await connection.send(query);
671
+ let entities = [];
672
+ if (Is.arrayValue(results.Items)) {
673
+ entities = results.Items.map(item => unmarshall(item));
674
+ }
675
+ return {
676
+ entities,
677
+ cursor: Is.empty(results.LastEvaluatedKey)
678
+ ? undefined
679
+ : Converter.bytesToBase64(ObjectHelper.toBytes(results.LastEvaluatedKey))
680
+ };
681
+ }
682
+ catch (err) {
683
+ if (BaseError.isErrorCode(err, "ResourceNotFoundException")) {
684
+ throw new GeneralError(this.CLASS_NAME, "tableDoesNotExist", {
685
+ table: this._config.tableName
686
+ }, err);
687
+ }
688
+ throw new GeneralError(this.CLASS_NAME, "queryFailed", undefined, err);
689
+ }
690
+ }
691
+ /**
692
+ * Build the condition expression for the query.
693
+ * @param conditions The conditions to build the expression from.
694
+ * @returns The condition expression.
695
+ * @throws GeneralError if the property is not found in the schema.
696
+ * @internal
697
+ */
698
+ buildConditionExpression(conditions) {
699
+ let conditionExpression;
700
+ let attributeNames;
701
+ let attributeValues;
702
+ if (Is.arrayValue(conditions)) {
703
+ const expressions = [];
704
+ for (const c of conditions) {
705
+ const schemaProp = this._entitySchema.properties?.find(p => p.property === c.property);
706
+ if (Is.undefined(schemaProp)) {
707
+ throw new GeneralError(this.CLASS_NAME, "propertyNotFound", {
708
+ property: c.property
709
+ });
710
+ }
711
+ const attributeName = `#${c.property}`;
712
+ const attributeValueName = `:${c.property}`;
713
+ attributeNames ??= {};
714
+ attributeValues ??= {};
715
+ attributeNames[attributeName] = c.property;
716
+ attributeValues[attributeValueName] = c.value;
717
+ expressions.push(`${attributeName} = ${attributeValueName}`);
718
+ }
719
+ conditionExpression = expressions.join(" AND ");
720
+ }
721
+ return { conditionExpression, attributeNames, attributeValues };
722
+ }
723
+ }
724
+
725
+ export { DynamoDbEntityStorageConnector };