Skip to content

Commit

Permalink
fix updateItem response type
Browse files Browse the repository at this point in the history
  • Loading branch information
Moicky committed Feb 23, 2024
1 parent 187db2c commit ab6dfbd
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 26 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
45 changes: 25 additions & 20 deletions src/operations/update.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
ReturnValue,
UpdateItemCommand,
UpdateItemCommandInput,
UpdateItemCommandOutput,
Expand Down Expand Up @@ -48,23 +49,31 @@ import { DynamoDBItem } from "../types";
* console.log(newItem); // { "PK": "User/1", "SK": "Book/1", "released": 2000 }
* ```
*/
export async function updateItem<T extends DynamoDBItem>(
key: Partial<T>,
data: Partial<T>,
args: Partial<UpdateItemCommandInput>
): Promise<T>;
type UpdateInputWithoutReturn = Partial<
Omit<UpdateItemCommandInput, "ReturnValue">
>;
type UpdateInputWithReturn = UpdateInputWithoutReturn & {
ReturnValues: ReturnValue;
};
export async function updateItem<
T extends DynamoDBItem,
K extends Partial<UpdateItemCommandInput> = Partial<UpdateItemCommandInput>
>(
key: Partial<T>,
data: Partial<T>,
args?: K
): Promise<K extends { ReturnValues: string } ? T : undefined>;
T extends DynamoDBItem = DynamoDBItem,
D extends Partial<T> = Partial<T>
>(key: Partial<T>, data: D, args?: never): Promise<undefined>;
export async function updateItem<
T extends DynamoDBItem = DynamoDBItem,
K extends UpdateInputWithReturn = UpdateInputWithReturn,
D extends Partial<T> = Partial<T>
>(key: Partial<T>, data: D, args: K): Promise<T>;
export async function updateItem<
T extends DynamoDBItem,
K extends Partial<UpdateItemCommandInput> = Partial<UpdateItemCommandInput>
>(key: Partial<T>, data: Partial<T>, args?: K): Promise<T | undefined> {
T extends DynamoDBItem = DynamoDBItem,
K extends UpdateInputWithoutReturn = UpdateInputWithoutReturn,
D extends Partial<T> = Partial<T>
>(key: Partial<T>, data: D, args: K): Promise<undefined>;
export async function updateItem(
key: Partial<DynamoDBItem>,
data: Record<string, any>,
args?: Partial<UpdateItemCommandInput>
): Promise<undefined> {
const argsWithDefaults = withDefaults(args || {}, "updateItem");

if (!Object.keys(data).includes("updatedAt")) {
Expand Down Expand Up @@ -105,11 +114,7 @@ export async function updateItem<
)
.then((res) =>
argsWithDefaults?.ReturnValues
? (unmarshallWithOptions<T>(res.Attributes) as K extends {
ReturnValues: string;
}
? T
: undefined)
? unmarshallWithOptions(res.Attributes)
: undefined
);
}
Expand Down
28 changes: 23 additions & 5 deletions test/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ async function playground() {
const put4 = (await putItem<DemoItem>(item, {
ReturnValues: "ALL_NEW",
})) satisfies DemoItem;
const put5 = (await putItem(demoItem, {
TableName: "test",
})) satisfies PutItemCommandOutput;
const put6 = (await putItem<DemoItem>(item, {
TableName: "test",
})) satisfies PutItemCommandOutput;

const query1 = (await queryItems("#PK = :PK", {
PK: "User/1",
Expand Down Expand Up @@ -82,20 +88,32 @@ async function playground() {
])) satisfies Array<DemoItem | undefined>;

const bigItem = {} as DemoItem & { big: string };
const update1 = (await updateItem(bigItem, { big: "5" })) satisfies undefined;
const update2 = (await updateItem<DemoItem>(item, {
const undefined1 = (await updateItem(bigItem, {
big: "5",
})) satisfies undefined;
const undefined2 = (await updateItem<DemoItem>(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<DemoItem>(
const demoItem2 = (await updateItem<DemoItem>(
item,
{ stars: 5 },
{ ReturnValues: "ALL_NEW" }
);
)) satisfies DemoItem;
const undefined3 = (await updateItem(
item,
{ stars: 5 },
{ TableName: "test" }
)) satisfies undefined;
const undefined4 = (await updateItem<DemoItem>(
item,
{ stars: 5 },
{ TableName: "test" }
)) satisfies undefined;

const removeAttributes1 = await removeAttributes(demoItem, ["stars"]);
}

0 comments on commit ab6dfbd

Please sign in to comment.