Skip to content

Commit

Permalink
Revert "fix: allow use of index for getItem (#10)"
Browse files Browse the repository at this point in the history
This reverts commit 9a984d1.
  • Loading branch information
solaris007 committed Dec 1, 2023
1 parent df67224 commit 2b52bf0
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 31 deletions.
9 changes: 7 additions & 2 deletions packages/spacecat-shared-dynamo/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@ 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, indexName: string, key: object): Promise<object>;
getItem(tableName: string, key: DynamoDbKey): Promise<object>;
putItem(tableName: string, item: object): Promise<{ message: string }>;
removeItem(tableName: string, key: object): Promise<{ message: string }>;
removeItem(tableName: string, key: DynamoDbKey): 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, indexName, key) => getItem(docClient, tableName, indexName, key, log),
getItem: (tableName, key) => getItem(docClient, tableName, key, log),
putItem: (tableName, item) => putItem(docClient, tableName, item, log),
removeItem: (tableName, key) => removeItem(docClient, tableName, key, log),
});
Expand Down
19 changes: 2 additions & 17 deletions packages/spacecat-shared-dynamo/src/modules/getItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,25 @@

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 {DynamoDbKey} 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,
indexName,
key,
log = console,
) {
async function getItem(docClient, tableName, 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 {object} key - The key object containing partitionKey and optionally sortKey.
* @param {DynamoDbKey} 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: 4 additions & 10 deletions packages/spacecat-shared-dynamo/test/modules/getItem.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,13 @@ describe('getItem', () => {

it('gets an item from the database', async () => {
const key = { partitionKey: 'testPartitionKey' };
const result = await dynamoDbClient.getItem('TestTable', null, key);
const result = await dynamoDbClient.getItem('TestTable', 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', 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);
const result = await dynamoDbClient.getItem('TestTable', key);
expect(result).to.be.an('object');
});

Expand All @@ -57,7 +51,7 @@ describe('getItem', () => {

it('throws an error for getItem with invalid key', async () => {
try {
await dynamoDbClient.getItem('TestTable', null, null);
await dynamoDbClient.getItem('TestTable', 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 @@ -70,7 +64,7 @@ describe('getItem', () => {
};

try {
await dynamoDbClient.getItem('TestTable', null, { partitionKey: 'testPartitionKey' });
await dynamoDbClient.getItem('TestTable', { 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 2b52bf0

Please sign in to comment.