From b475b64b66b3657cb1627d729ca4115bf0ed292f Mon Sep 17 00:00:00 2001 From: Gabriel Rocha Date: Mon, 8 Apr 2024 14:01:01 -0300 Subject: [PATCH] feat: unmarshall items on scan result (#243) --- package-lock.json | 4 ++-- package.json | 2 +- src/dao/dynamo/dao.ts | 17 +++++++++++++---- src/dao/dynamo/types.ts | 4 ++++ 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/package-lock.json b/package-lock.json index 9b7245f..3ce44b4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "adapcon-utils-js", - "version": "1.3.0", + "version": "1.3.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "adapcon-utils-js", - "version": "1.3.0", + "version": "1.3.1", "dependencies": { "@aws-sdk/client-dynamodb": "^3.496.0", "@aws-sdk/client-lambda": "^3.496.0", diff --git a/package.json b/package.json index e135e5f..17cbc0b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "adapcon-utils-js", - "version": "1.3.0", + "version": "1.3.1", "description": "Utils library for Javascript", "keywords": [], "author": { diff --git a/src/dao/dynamo/dao.ts b/src/dao/dynamo/dao.ts index 574db51..e840f0f 100644 --- a/src/dao/dynamo/dao.ts +++ b/src/dao/dynamo/dao.ts @@ -1,6 +1,6 @@ import { isObject } from '../../object' import { error } from '../../error' -import type { DynamodbParams } from './types' +import type { DynamodbParams, ScanOutput } from './types' import { DynamoDBDocument, GetCommand, @@ -9,7 +9,8 @@ import { PutCommand, UpdateCommand, DeleteCommand, - BatchWriteCommand + BatchWriteCommand, + ScanCommandInput } from '@aws-sdk/lib-dynamodb' import type { GetCommandInput, @@ -22,6 +23,7 @@ import type { QueryCommandOutput } from '@aws-sdk/lib-dynamodb' import { DynamoDB, ScanCommand } from '@aws-sdk/client-dynamodb' +import { unmarshall } from '@aws-sdk/util-dynamodb' const getOptions = () => { if (process.env.IS_OFFLINE) return { region: 'localhost', endpoint: 'http://localhost:8000' } @@ -65,9 +67,16 @@ const query = async ({ return items } -const scan = async (params) => { +async function scan(params: ScanCommandInput): Promise> { const command = new ScanCommand(params) - return documentInstance.send(command) + const result = await documentInstance.send(command) + + const Items = result.Items?.map(item => unmarshall(item)) as T[] | undefined + + return { + Items, + LastEvaluatedKey: result.LastEvaluatedKey + } } const getAll = async ({ params, list, fields = [] }: { params: DynamodbParams, list: object[], fields?: string[] }) => { diff --git a/src/dao/dynamo/types.ts b/src/dao/dynamo/types.ts index 50ff935..9a19fc2 100644 --- a/src/dao/dynamo/types.ts +++ b/src/dao/dynamo/types.ts @@ -1,3 +1,5 @@ +import { AttributeValue } from '@aws-sdk/client-dynamodb' + export enum ConditionsExpressions { EQUAL = '=', NOT_EQUAL = '<>', @@ -27,3 +29,5 @@ export interface DynamodbParams { export interface DynamodbResponseBatch { UnprocessedItems: object } + +export type ScanOutput = { Items: T[] | undefined, LastEvaluatedKey: Record | undefined }