From 50b8a20e53d012ee522adf950fcdb3dcd59aaaea Mon Sep 17 00:00:00 2001 From: Marco Antonio <79797841+marcofeliponi@users.noreply.github.com> Date: Wed, 21 Feb 2024 16:50:49 -0300 Subject: [PATCH] [FIX] AWS SDK v3 (#224) * fix(dynamodb-dao): :bug: Refactor getAll function in dynamo/dao.ts * fix(dynamodb-dao): :bug: Update mountProjectionExpression to include expressionAttributeNames --------- Co-authored-by: Jeferson Alves --- .vscode/settings.json | 3 ++- src/dao/dynamo/dao.ts | 20 ++++++++++++-------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 38cf529..4ecd70c 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -6,6 +6,7 @@ "simplificamais" ], "conventionalCommits.scopes": [ - "lambda" + "lambda", + "dynamodb-dao" ] } diff --git a/src/dao/dynamo/dao.ts b/src/dao/dynamo/dao.ts index 2383709..23a921b 100644 --- a/src/dao/dynamo/dao.ts +++ b/src/dao/dynamo/dao.ts @@ -43,7 +43,7 @@ const get = async ({ const query = async ({ params, fields = [], _items = [], stopOnLimit = false }: { params: { Limit?: number } & QueryCommandInput, fields?: string[], _items?: Array>, stopOnLimit?: boolean }): Promise>> => { - const command = new QueryCommand({ ...params, ...mountProjectionExpression({ fields }) }) + const command = new QueryCommand({ ...params, ...mountProjectionExpression({ fields, expressionAttributeNames: params.ExpressionAttributeNames }) }) const { Items = [], LastEvaluatedKey } = await documentInstance.send(command) const items = [..._items, ...Items] @@ -92,14 +92,14 @@ const getAll = async ({ params, list, fields = [] }: { params: DynamodbParams, l } const command = new BatchGetCommand(opts) - const response = await documentInstance.send(command) - return response[params.TableName] + const { Responses } = await documentInstance.send(command) + return Responses?.[params.TableName] })) return data.reduce((acc: any, i) => acc.concat(i), []) } -const put = async ({ params }: { params: PutCommandInput}): Promise => { +const put = async ({ params }: { params: PutCommandInput }): Promise => { try { const command = new PutCommand(params) await documentInstance.send(command) @@ -112,7 +112,7 @@ const put = async ({ params }: { params: PutCommandInput}): Promise => { } } -const update = async ({ params }: { params: UpdateCommandInput}): Promise => { +const update = async ({ params }: { params: UpdateCommandInput }): Promise => { try { const command = new UpdateCommand(params) return await documentInstance.send(command) @@ -122,7 +122,7 @@ const update = async ({ params }: { params: UpdateCommandInput}): Promise = } } -const deleteOne = async ({ params }: { params: DeleteCommandInput}): Promise | undefined> => { +const deleteOne = async ({ params }: { params: DeleteCommandInput }): Promise | undefined> => { try { const command = new DeleteCommand(params) await documentInstance.send(command) @@ -328,10 +328,14 @@ const dynamicFilters = ({ } } -const mountProjectionExpression = ({ fields = [], options }: { fields?: string[], options?: DynamodbParams }): object => { +const mountProjectionExpression = ({ fields = [], expressionAttributeNames, options }: { + fields?: string[] + expressionAttributeNames?: QueryCommandInput['ExpressionAttributeNames'] + options?: DynamodbParams +}): object => { if (fields.length > 0) { const ProjectionExpression = fields.map((_, index) => `#Projection_${index}`).join(',') - const { ExpressionAttributeNames = {} } = options ?? {} + const { ExpressionAttributeNames = expressionAttributeNames ?? {} } = options ?? {} fields.forEach((i, index) => { ExpressionAttributeNames[`#Projection_${index}`] = i