From 41f4782c2f0a6ed6b930a47c451fd004e9d54ea5 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] fix: allow use of index for getItem --- packages/spacecat-shared-dynamo/src/index.d.ts | 2 +- packages/spacecat-shared-dynamo/src/index.js | 2 +- .../src/modules/getItem.js | 17 ++++++++++++++++- .../test/modules/getItem.test.js | 14 ++++++++++---- 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/packages/spacecat-shared-dynamo/src/index.d.ts b/packages/spacecat-shared-dynamo/src/index.d.ts index 21bb25c4..f49671ab 100644 --- a/packages/spacecat-shared-dynamo/src/index.d.ts +++ b/packages/spacecat-shared-dynamo/src/index.d.ts @@ -20,7 +20,7 @@ export declare interface Logger { export declare interface DynamoDbClient { query(originalParams: QueryCommandInput): Promise; - getItem(tableName: string, key: object): Promise; + getItem(tableName: string, indexName: string, key: object): Promise; putItem(tableName: string, item: object): Promise<{ message: string }>; removeItem(tableName: string, key: object): Promise<{ message: string }>; } 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 ba0d3223..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 {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');