Skip to content

Commit

Permalink
[FIX] AWS SDK v3 (#224)
Browse files Browse the repository at this point in the history
* fix(dynamodb-dao): 🐛 Refactor getAll function in dynamo/dao.ts

* fix(dynamodb-dao): 🐛 Update mountProjectionExpression to include expressionAttributeNames

---------

Co-authored-by: Jeferson Alves <[email protected]>
  • Loading branch information
marcofeliponi and Viserion77 authored Feb 21, 2024
1 parent 39802cf commit 50b8a20
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"simplificamais"
],
"conventionalCommits.scopes": [
"lambda"
"lambda",
"dynamodb-dao"
]
}
20 changes: 12 additions & 8 deletions src/dao/dynamo/dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const get = async ({
const query = async <T>({
params, fields = [], _items = [], stopOnLimit = false
}: { params: { Limit?: number } & QueryCommandInput, fields?: string[], _items?: Array<Record<string, any>>, stopOnLimit?: boolean }): Promise<Array<Record<string, T>>> => {
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]
Expand Down Expand Up @@ -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<object> => {
const put = async ({ params }: { params: PutCommandInput }): Promise<object> => {
try {
const command = new PutCommand(params)
await documentInstance.send(command)
Expand All @@ -112,7 +112,7 @@ const put = async ({ params }: { params: PutCommandInput}): Promise<object> => {
}
}

const update = async ({ params }: { params: UpdateCommandInput}): Promise<any> => {
const update = async ({ params }: { params: UpdateCommandInput }): Promise<any> => {
try {
const command = new UpdateCommand(params)
return await documentInstance.send(command)
Expand All @@ -122,7 +122,7 @@ const update = async ({ params }: { params: UpdateCommandInput}): Promise<any> =
}
}

const deleteOne = async ({ params }: { params: DeleteCommandInput}): Promise<Record<string, any> | undefined> => {
const deleteOne = async ({ params }: { params: DeleteCommandInput }): Promise<Record<string, any> | undefined> => {
try {
const command = new DeleteCommand(params)
await documentInstance.send(command)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 50b8a20

Please sign in to comment.