Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow use of index for getItem #10

Merged
merged 2 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions packages/spacecat-shared-dynamo/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<object[]>;
getItem(tableName: string, key: DynamoDbKey): Promise<object>;
getItem(tableName: string, indexName: string, key: object): Promise<object>;
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(
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
19 changes: 17 additions & 2 deletions 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 {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<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
2 changes: 1 addition & 1 deletion packages/spacecat-shared-dynamo/src/modules/removeItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object>} 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.
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
Loading