From ab6dfbde9c79b2582e1de8decf4ce11ededb50b9 Mon Sep 17 00:00:00 2001 From: Moicky Date: Fri, 23 Feb 2024 18:11:54 +0100 Subject: [PATCH] fix updateItem response type --- package.json | 2 +- src/operations/update.ts | 45 ++++++++++++++++++++++------------------ test/types.ts | 28 ++++++++++++++++++++----- 3 files changed, 49 insertions(+), 26 deletions(-) diff --git a/package.json b/package.json index 2c63479..09828f0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@moicky/dynamodb", - "version": "2.4.5", + "version": "2.4.6", "main": "dist/index.js", "types": "dist/index.d.ts", "description": "Contains a collection of convenience functions for working with AWS DynamoDB", diff --git a/src/operations/update.ts b/src/operations/update.ts index 395052a..7e18063 100644 --- a/src/operations/update.ts +++ b/src/operations/update.ts @@ -1,4 +1,5 @@ import { + ReturnValue, UpdateItemCommand, UpdateItemCommandInput, UpdateItemCommandOutput, @@ -48,23 +49,31 @@ import { DynamoDBItem } from "../types"; * console.log(newItem); // { "PK": "User/1", "SK": "Book/1", "released": 2000 } * ``` */ -export async function updateItem( - key: Partial, - data: Partial, - args: Partial -): Promise; +type UpdateInputWithoutReturn = Partial< + Omit +>; +type UpdateInputWithReturn = UpdateInputWithoutReturn & { + ReturnValues: ReturnValue; +}; export async function updateItem< - T extends DynamoDBItem, - K extends Partial = Partial ->( - key: Partial, - data: Partial, - args?: K -): Promise; + T extends DynamoDBItem = DynamoDBItem, + D extends Partial = Partial +>(key: Partial, data: D, args?: never): Promise; +export async function updateItem< + T extends DynamoDBItem = DynamoDBItem, + K extends UpdateInputWithReturn = UpdateInputWithReturn, + D extends Partial = Partial +>(key: Partial, data: D, args: K): Promise; export async function updateItem< - T extends DynamoDBItem, - K extends Partial = Partial ->(key: Partial, data: Partial, args?: K): Promise { + T extends DynamoDBItem = DynamoDBItem, + K extends UpdateInputWithoutReturn = UpdateInputWithoutReturn, + D extends Partial = Partial +>(key: Partial, data: D, args: K): Promise; +export async function updateItem( + key: Partial, + data: Record, + args?: Partial +): Promise { const argsWithDefaults = withDefaults(args || {}, "updateItem"); if (!Object.keys(data).includes("updatedAt")) { @@ -105,11 +114,7 @@ export async function updateItem< ) .then((res) => argsWithDefaults?.ReturnValues - ? (unmarshallWithOptions(res.Attributes) as K extends { - ReturnValues: string; - } - ? T - : undefined) + ? unmarshallWithOptions(res.Attributes) : undefined ); } diff --git a/test/types.ts b/test/types.ts index 96c30a7..53876cb 100644 --- a/test/types.ts +++ b/test/types.ts @@ -48,6 +48,12 @@ async function playground() { const put4 = (await putItem(item, { ReturnValues: "ALL_NEW", })) satisfies DemoItem; + const put5 = (await putItem(demoItem, { + TableName: "test", + })) satisfies PutItemCommandOutput; + const put6 = (await putItem(item, { + TableName: "test", + })) satisfies PutItemCommandOutput; const query1 = (await queryItems("#PK = :PK", { PK: "User/1", @@ -82,20 +88,32 @@ async function playground() { ])) satisfies Array; const bigItem = {} as DemoItem & { big: string }; - const update1 = (await updateItem(bigItem, { big: "5" })) satisfies undefined; - const update2 = (await updateItem(item, { + const undefined1 = (await updateItem(bigItem, { + big: "5", + })) satisfies undefined; + const undefined2 = (await updateItem(item, { stars: 5, })) satisfies undefined; - const update3 = (await updateItem( + const demoItem1 = (await updateItem( demoItem, { stars: 5 }, { ReturnValues: "ALL_NEW" } )) satisfies DemoItem; - const update4 = await updateItem( + const demoItem2 = (await updateItem( item, { stars: 5 }, { ReturnValues: "ALL_NEW" } - ); + )) satisfies DemoItem; + const undefined3 = (await updateItem( + item, + { stars: 5 }, + { TableName: "test" } + )) satisfies undefined; + const undefined4 = (await updateItem( + item, + { stars: 5 }, + { TableName: "test" } + )) satisfies undefined; const removeAttributes1 = await removeAttributes(demoItem, ["stars"]); }