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] AWS SDK v3 #224

Merged
merged 2 commits into from
Feb 21, 2024
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
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
Loading