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