From 4b83e00a811228a419c8d8e1539749668aa2b3f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominique=20J=C3=A4ggi?= Date: Thu, 30 Nov 2023 14:11:54 +0100 Subject: [PATCH 1/2] fix: allow use of index for getItem --- .../spacecat-shared-dynamo/src/index.d.ts | 9 ++------- packages/spacecat-shared-dynamo/src/index.js | 2 +- .../src/modules/getItem.js | 19 +++++++++++++++++-- .../test/modules/getItem.test.js | 14 ++++++++++---- 4 files changed, 30 insertions(+), 14 deletions(-) diff --git a/packages/spacecat-shared-dynamo/src/index.d.ts b/packages/spacecat-shared-dynamo/src/index.d.ts index 8599d9c1..f49671ab 100644 --- a/packages/spacecat-shared-dynamo/src/index.d.ts +++ b/packages/spacecat-shared-dynamo/src/index.d.ts @@ -18,16 +18,11 @@ export declare interface Logger { info(message: string, ...args: unknown[]): void; } -export declare interface DynamoDbKey { - partitionKey: string; - sortKey?: string; -} - export declare interface DynamoDbClient { query(originalParams: QueryCommandInput): Promise; - getItem(tableName: string, key: DynamoDbKey): Promise; + getItem(tableName: string, indexName: string, key: object): Promise; putItem(tableName: string, item: object): Promise<{ message: string }>; - removeItem(tableName: string, key: DynamoDbKey): Promise<{ message: string }>; + removeItem(tableName: string, key: object): Promise<{ message: string }>; } export function createClient( diff --git a/packages/spacecat-shared-dynamo/src/index.js b/packages/spacecat-shared-dynamo/src/index.js index 51301776..e42d60ba 100644 --- a/packages/spacecat-shared-dynamo/src/index.js +++ b/packages/spacecat-shared-dynamo/src/index.js @@ -32,7 +32,7 @@ const createClient = ( docClient = DynamoDBDocument.from(dbClient), ) => ({ query: (params) => query(docClient, params, log), - getItem: (tableName, key) => getItem(docClient, tableName, key, log), + getItem: (tableName, indexName, key) => getItem(docClient, tableName, indexName, key, log), putItem: (tableName, item) => putItem(docClient, tableName, item, log), removeItem: (tableName, key) => removeItem(docClient, tableName, key, log), }); diff --git a/packages/spacecat-shared-dynamo/src/modules/getItem.js b/packages/spacecat-shared-dynamo/src/modules/getItem.js index 5a4e24e8..16430761 100644 --- a/packages/spacecat-shared-dynamo/src/modules/getItem.js +++ b/packages/spacecat-shared-dynamo/src/modules/getItem.js @@ -12,6 +12,8 @@ import { performance } from 'perf_hooks'; +import { hasText } from '@adobe/spacecat-shared-utils'; + import { guardKey, guardTableName } from '../utils/guards.js'; /** @@ -19,18 +21,31 @@ import { guardKey, guardTableName } from '../utils/guards.js'; * * @param {DynamoDBDocumentClient} docClient - The AWS SDK DynamoDB Document client instance. * @param {string} tableName - The name of the DynamoDB table. - * @param {DynamoDbKey} key - The key object containing partitionKey and optionally sortKey. + * @param {string} [indexName] - Optional. The name of the DynamoDB index. + * @param {object} key - The key object containing partitionKey and optionally sortKey. * @param {Logger} log - The logging object, defaults to console. * @returns {Promise} A promise that resolves to the retrieved item. * @throws {Error} Throws an error if the DynamoDB get operation fails or input validation fails. */ -async function getItem(docClient, tableName, key, log = console) { +async function getItem( + docClient, + tableName, + indexName, + key, + log = console, +) { guardTableName(tableName); guardKey(key); + const indexProperties = {}; + if (hasText(indexName)) { + indexProperties.IndexName = indexName; + } + const params = { TableName: tableName, Key: key, + ...indexProperties, }; try { diff --git a/packages/spacecat-shared-dynamo/test/modules/getItem.test.js b/packages/spacecat-shared-dynamo/test/modules/getItem.test.js index 205c0101..677b19c0 100644 --- a/packages/spacecat-shared-dynamo/test/modules/getItem.test.js +++ b/packages/spacecat-shared-dynamo/test/modules/getItem.test.js @@ -29,13 +29,19 @@ describe('getItem', () => { it('gets an item from the database', async () => { const key = { partitionKey: 'testPartitionKey' }; - const result = await dynamoDbClient.getItem('TestTable', key); + const result = await dynamoDbClient.getItem('TestTable', null, key); expect(result).to.be.an('object'); }); it('gets an item from the database with sort key', async () => { const key = { partitionKey: 'testPartitionKey', sortKey: 'testSortKey' }; - const result = await dynamoDbClient.getItem('TestTable', key); + const result = await dynamoDbClient.getItem('TestTable', null, key); + expect(result).to.be.an('object'); + }); + + it('gets an item from the database with index', async () => { + const key = { partitionKey: 'testPartitionKey', sortKey: 'testSortKey' }; + const result = await dynamoDbClient.getItem('TestTable', 'test-index', key); expect(result).to.be.an('object'); }); @@ -51,7 +57,7 @@ describe('getItem', () => { it('throws an error for getItem with invalid key', async () => { try { - await dynamoDbClient.getItem('TestTable', null); + await dynamoDbClient.getItem('TestTable', null, null); expect.fail('getItem did not throw with invalid key'); } catch (error) { expect(error.message).to.equal('Key must be a non-empty object.'); @@ -64,7 +70,7 @@ describe('getItem', () => { }; try { - await dynamoDbClient.getItem('TestTable', { partitionKey: 'testPartitionKey' }); + await dynamoDbClient.getItem('TestTable', null, { partitionKey: 'testPartitionKey' }); expect.fail('getItem did not throw as expected'); } catch (error) { expect(error.message).to.equal('Get failed'); From 41f54dc44bb1251f0fea2df588683078d1cc5306 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominique=20J=C3=A4ggi?= Date: Thu, 30 Nov 2023 14:22:40 +0100 Subject: [PATCH 2/2] fix: remove key type ref --- packages/spacecat-shared-dynamo/src/modules/removeItem.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/spacecat-shared-dynamo/src/modules/removeItem.js b/packages/spacecat-shared-dynamo/src/modules/removeItem.js index 2978278c..3ab723e9 100644 --- a/packages/spacecat-shared-dynamo/src/modules/removeItem.js +++ b/packages/spacecat-shared-dynamo/src/modules/removeItem.js @@ -19,7 +19,7 @@ import { guardKey, guardTableName } from '../utils/guards.js'; * * @param {DynamoDBDocumentClient} docClient - The AWS SDK DynamoDB Document client instance. * @param {string} tableName - The name of the DynamoDB table. - * @param {DynamoDbKey} key - The key object containing partitionKey and optionally sortKey. + * @param {object} key - The key object containing partitionKey and optionally sortKey. * @param {Logger} log - The logging object, defaults to console. * @returns {Promise} A promise that resolves to a message indicating successful removal. * @throws {Error} Throws an error if the DynamoDB delete operation fails or input validation fails.