Skip to content

Commit

Permalink
[REFACTOR] Corrige tipagens no DAO (#228)
Browse files Browse the repository at this point in the history
* refactor(dynamodb-dao): 🏷️ Add QueryCommandOutput type and update get and query functions

* chore(dynamodb-dao): ⬆️ Bump version to 1.1.7 in package.json and package-lock.json
  • Loading branch information
marcofeliponi authored Feb 29, 2024
1 parent 6fd6eeb commit 73e5f0d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "adapcon-utils-js",
"version": "1.1.6",
"version": "1.1.7",
"description": "Utils library for Javascript",
"keywords": [],
"author": {
Expand Down
16 changes: 10 additions & 6 deletions src/dao/dynamo/dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import type {
UpdateCommandInput,
DeleteCommandInput,
BatchWriteCommandInput,
BatchWriteCommandOutput
BatchWriteCommandOutput,
QueryCommandOutput
} from '@aws-sdk/lib-dynamodb'
import { DynamoDB, ScanCommand } from '@aws-sdk/client-dynamodb'

Expand All @@ -31,20 +32,23 @@ const documentInstance = DynamoDBDocument.from(dynamoInstance, {
marshallOptions: { removeUndefinedValues: true }
})

const get = async ({
const get = async<T> ({
params,
fields = []
}: { params: GetCommandInput, fields?: string[] }): Promise<Record<string, any> | undefined> => {
}: { params: GetCommandInput, fields?: string[] }): Promise<T| undefined> => {
const command = new GetCommand({ ...params, ...mountProjectionExpression({ fields }) })

const { Item } = await documentInstance.send(command)
const { Item } = await documentInstance.send(command) as { Item: T }
return Item
}

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>>> => {
}: { params: { Limit?: number } & QueryCommandInput, fields?: string[], _items?: T[], stopOnLimit?: boolean }): Promise<T[]> => {
if (!Number(params.Limit) || Number.isNaN(params.Limit)) delete params.Limit

const command = new QueryCommand({ ...params, ...mountProjectionExpression({ fields, expressionAttributeNames: params.ExpressionAttributeNames }) })
const { Items = [], LastEvaluatedKey } = await documentInstance.send(command)
const { Items = [], LastEvaluatedKey } = await documentInstance.send(command) as QueryCommandOutput & { Items: T[] }

const items = [..._items, ...Items]

Expand Down
2 changes: 2 additions & 0 deletions src/lambda/lambdaService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ const executeInvoke = async <T>({
)
})

if (invocationType === InvokeType.Event) return { StatusCode: response.StatusCode }

return formattedResponse<T>(response)
}

Expand Down

0 comments on commit 73e5f0d

Please sign in to comment.