Skip to content

Commit

Permalink
SLSCMN-3 reshape dynamo service
Browse files Browse the repository at this point in the history
  • Loading branch information
mwarman committed Nov 25, 2023
1 parent d8bce7b commit d38c89c
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 55 deletions.
127 changes: 73 additions & 54 deletions src/services/dynamo.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,85 +41,104 @@ const unmarshallOptions = {
};
const translateConfig: TranslateConfig = { marshallOptions, unmarshallOptions };

console.log(`DYNAMO::CREATE DOCUMENT CLIENT`);
/**
* Create a DynamoDBDocumentClient. This will be created once during AWS Lambda function
* cold start and placed in the module loader. It will be reused across function invocations.
*/
const documentClient = DynamoDBDocumentClient.from(
new DynamoDBClient(dynamoDbClientConfig),
translateConfig,
);

export const batchWriteItems = (
input: BatchWriteCommandInput,
): Promise<BatchWriteCommandOutput> => {
/**
* Modify a DynamoDB Table with a batch of Put and/or Delete operations.
* @param input A `BatchWriteCommandInput` object.
* @returns A Promise which resolves to a `BatchWriteCommandOputput` object.
*/
const batchWriteItems = (input: BatchWriteCommandInput): Promise<BatchWriteCommandOutput> => {
return documentClient.send(new BatchWriteCommand(input));
};

export const deleteItem = (input: DeleteCommandInput): Promise<DeleteCommandOutput> => {
/**
* Delete a single item from a table.
* @param input A `DeleteCommandInput` object.
* @returns A Promise which resolves to a `DeleteCommandOutput` object.
*/
const deleteItem = (input: DeleteCommandInput): Promise<DeleteCommandOutput> => {
return documentClient.send(new DeleteCommand(input));
};

export const getItem = (input: GetCommandInput): Promise<GetCommandOutput> => {
/**
* Get a single item from a table.
* @param input A `GetCommandInput` object.
* @returns A Promise which resolves to a `GetCommandOutput` object.
*/
const getItem = (input: GetCommandInput): Promise<GetCommandOutput> => {
return documentClient.send(new GetCommand(input));
};

export const putItem = (input: PutCommandInput): Promise<PutCommandOutput> => {
/**
* Creates or replaces a single item in a table.
* @param input A `PutCommandInput` object.
* @returns A Promise which resolves to a `PutCommandOutput` object.
*/
const putItem = (input: PutCommandInput): Promise<PutCommandOutput> => {
return documentClient.send(new PutCommand(input));
};

export const queryItems = (input: QueryCommandInput): Promise<QueryCommandOutput> => {
/**
* Searches a table for items matching query criteria.
* @param input A `QueryCommandInput` object.
* @returns A Promise which resolves to a `QueryCommandOutput` object.
*/
const queryItems = (input: QueryCommandInput): Promise<QueryCommandOutput> => {
return documentClient.send(new QueryCommand(input));
};

export const scanItems = (input: ScanCommandInput): Promise<ScanCommandOutput> => {
/**
* Returns all items from a table with optional filter critera.
* @param input A `ScanCommandInput` object.
* @returns A Promise which resolves to a `ScanCommandOutput` object.
*/
const scanItems = (input: ScanCommandInput): Promise<ScanCommandOutput> => {
return documentClient.send(new ScanCommand(input));
};

export const updateItem = (input: UpdateCommandInput): Promise<UpdateCommandOutput> => {
/**
* Edits the attributes of an existing item or creates a new item if none exists. Use
* conditional updates to prevent undesired item creation.
* @param input An `UpdateCommandInput` object.
* @returns A Promise which resolves to an `UpdateCommandOutput` object.
*/
const updateItem = (input: UpdateCommandInput): Promise<UpdateCommandOutput> => {
return documentClient.send(new UpdateCommand(input));
};

export class DynamoService {
private static instance: DynamoService;
private client: DynamoDBDocumentClient;

private constructor() {
this.client = DynamoDBDocumentClient.from(
new DynamoDBClient(dynamoDbClientConfig),
translateConfig,
);
}

public static getInstance() {
if (!DynamoService.instance) {
DynamoService.instance = new DynamoService();
}
return DynamoService.instance;
}

batchWrite(input: BatchWriteCommandInput): Promise<BatchWriteCommandOutput> {
return this.client.send(new BatchWriteCommand(input));
}

delete(input: DeleteCommandInput): Promise<DeleteCommandOutput> {
return this.client.send(new DeleteCommand(input));
}

get(input: GetCommandInput): Promise<GetCommandOutput> {
return this.client.send(new GetCommand(input));
}

put(input: PutCommandInput): Promise<PutCommandOutput> {
return this.client.send(new PutCommand(input));
}

query(input: QueryCommandInput): Promise<QueryCommandOutput> {
return this.client.send(new QueryCommand(input));
}
/**
* A `DynamoClient` provides operations for accessing and mutating one or more Items
* within an AWS DynamoDB Table.
*/
type DynamoClient = {
batchWriteItems: (input: BatchWriteCommandInput) => Promise<BatchWriteCommandOutput>;
deleteItem: (input: DeleteCommandInput) => Promise<DeleteCommandOutput>;
getItem: (input: GetCommandInput) => Promise<GetCommandOutput>;
putItem: (input: PutCommandInput) => Promise<PutCommandOutput>;
queryItems: (input: QueryCommandInput) => Promise<QueryCommandOutput>;
scanItems: (input: ScanCommandInput) => Promise<ScanCommandOutput>;
updateItem: (input: UpdateCommandInput) => Promise<UpdateCommandOutput>;
};

scan(input: ScanCommandInput): Promise<ScanCommandOutput> {
return this.client.send(new ScanCommand(input));
}
/**
* Use the `DynamoService` to access or mutate Items within an AWS DynamoDB Table.
*/
const DynamoService: DynamoClient = {
batchWriteItems,
deleteItem,
getItem,
putItem,
queryItems,
scanItems,
updateItem,
};

update(input: UpdateCommandInput): Promise<UpdateCommandOutput> {
return this.client.send(new UpdateCommand(input));
}
}
export default DynamoService;
3 changes: 2 additions & 1 deletion src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export {
lambdaConfigValues,
validateConfig,
} from './config.service';
export { DynamoService } from './dynamo.service';

export { default as DynamoService } from './dynamo.service';

0 comments on commit d38c89c

Please sign in to comment.