Skip to content

Commit

Permalink
fix: allow use of index for getItem
Browse files Browse the repository at this point in the history
  • Loading branch information
solaris007 committed Nov 30, 2023
1 parent d167973 commit 41f4782
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/spacecat-shared-dynamo/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export declare interface Logger {

export declare interface DynamoDbClient {
query(originalParams: QueryCommandInput): Promise<object[]>;
getItem(tableName: string, key: object): Promise<object>;
getItem(tableName: string, indexName: string, key: object): Promise<object>;
putItem(tableName: string, item: object): Promise<{ message: string }>;
removeItem(tableName: string, key: object): Promise<{ message: string }>;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/spacecat-shared-dynamo/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
});
Expand Down
17 changes: 16 additions & 1 deletion packages/spacecat-shared-dynamo/src/modules/getItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,40 @@

import { performance } from 'perf_hooks';

import { hasText } from '@adobe/spacecat-shared-utils';

import { guardKey, guardTableName } from '../utils/guards.js';

/**
* Retrieves an item from DynamoDB using a table name and key object.
*
* @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<Object>} 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 {
Expand Down
14 changes: 10 additions & 4 deletions packages/spacecat-shared-dynamo/test/modules/getItem.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});

Expand All @@ -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.');
Expand All @@ -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');
Expand Down

0 comments on commit 41f4782

Please sign in to comment.