diff --git a/src/queryBuilders/deleteItemQueryBuilder.integration.test.ts b/src/queryBuilders/deleteItemQueryBuilder.integration.test.ts index 916cfa1..daa0ea4 100644 --- a/src/queryBuilders/deleteItemQueryBuilder.integration.test.ts +++ b/src/queryBuilders/deleteItemQueryBuilder.integration.test.ts @@ -86,4 +86,48 @@ describe("DeleteItemQueryBuilder", () => { expect(res).toBeDefined(); }); + it("doesn't return values if no returnValues is specified or its set to NONE", async () => { + await tsynamoClient + .putItem("myTable") + .item({ + userId: "1", + dataTimestamp: 2, + }) + .execute(); + + const res = await tsynamoClient + .deleteItem("myTable") + .keys({ userId: "1", dataTimestamp: 2 }) + .execute(); + + expectTypeOf(res).toBeNever(); + expect(res).toBeUndefined(); + + const res2 = await tsynamoClient + .deleteItem("myTable") + .keys({ userId: "1", dataTimestamp: 2 }) + .returnValues("NONE") + .execute(); + + expectTypeOf(res2).toBeNever(); + expect(res2).toBeUndefined(); + }); + it("does return values if returnValues is specified", async () => { + await tsynamoClient + .putItem("myTable") + .item({ + userId: "1", + dataTimestamp: 2, + }) + .execute(); + + const res = await tsynamoClient + .deleteItem("myTable") + .keys({ userId: "1", dataTimestamp: 2 }) + .returnValues("ALL_OLD") + .execute(); + + expectTypeOf(res).not.toBeNever(); + expect(res).toBeDefined(); + }); }); diff --git a/src/queryBuilders/deleteItemQueryBuilder.ts b/src/queryBuilders/deleteItemQueryBuilder.ts index e85a63a..269f046 100644 --- a/src/queryBuilders/deleteItemQueryBuilder.ts +++ b/src/queryBuilders/deleteItemQueryBuilder.ts @@ -24,7 +24,7 @@ import { export interface DeleteItemQueryBuilderInterface< DDB, Table extends keyof DDB, - O extends DDB[Table] + O > { /** * A condition that must be satisfied in order for a DeleteItem operation to be executed. @@ -134,17 +134,25 @@ export interface DeleteItemQueryBuilderInterface< * * The values returned are strongly consistent. */ - returnValues( - option: Extract - ): DeleteItemQueryBuilder; + returnValues( + option: Extract + ): DeleteItemQueryBuilder< + DDB, + Table, + "NONE" extends Choice ? O : ExecuteOutput + >; /** * * Returns the item attributes for a DeleteItem operation that failed a condition check. */ - returnValuesOnConditionCheckFailure( - option: Extract - ): DeleteItemQueryBuilder; + returnValuesOnConditionCheckFailure( + option: Extract + ): DeleteItemQueryBuilder< + DDB, + Table, + "NONE" extends Choice ? O : ExecuteOutput + >; /** * An object of attribute names to attribute values, representing the primary key of the item to delete. @@ -171,17 +179,18 @@ export interface DeleteItemQueryBuilderInterface< * Compiles into an DynamoDB DocumentClient Command. */ compile(): DeleteCommand; + /** * Executes the command and returns its output. */ - execute(): Promise[] | undefined>; + execute(): Promise; } -export class DeleteItemQueryBuilder< - DDB, - Table extends keyof DDB, - O extends DDB[Table] -> implements DeleteItemQueryBuilderInterface +/** + * @todo support ReturnValuesOnConditionCheckFailure + */ +export class DeleteItemQueryBuilder + implements DeleteItemQueryBuilderInterface { readonly #props: DeleteItemQueryBuilderProps; @@ -225,10 +234,14 @@ export class DeleteItemQueryBuilder< }); } - returnValues( - option: Extract - ): DeleteItemQueryBuilder { - return new DeleteItemQueryBuilder({ + returnValues( + option: Extract + ): DeleteItemQueryBuilder< + DDB, + Table, + "NONE" extends Choice ? O : ExecuteOutput + > { + return new DeleteItemQueryBuilder({ ...this.#props, node: { ...this.#props.node, @@ -240,10 +253,14 @@ export class DeleteItemQueryBuilder< }); } - returnValuesOnConditionCheckFailure( - option: Extract - ): DeleteItemQueryBuilder { - return new DeleteItemQueryBuilder({ + returnValuesOnConditionCheckFailure( + option: Extract + ): DeleteItemQueryBuilder< + DDB, + Table, + "NONE" extends Choice ? O : ExecuteOutput + > { + return new DeleteItemQueryBuilder({ ...this.#props, node: { ...this.#props.node, @@ -274,7 +291,7 @@ export class DeleteItemQueryBuilder< return this.#props.queryCompiler.compile(this.#props.node); }; - execute = async (): Promise[] | undefined> => { + execute = async (): Promise => { const deleteCommand = this.compile(); const data = await this.#props.ddbClient.send(deleteCommand); return data.Attributes as any; diff --git a/src/queryBuilders/expressionBuilder.ts b/src/queryBuilders/expressionBuilder.ts index 6aadc89..0569f4c 100644 --- a/src/queryBuilders/expressionBuilder.ts +++ b/src/queryBuilders/expressionBuilder.ts @@ -228,11 +228,8 @@ export type ExprArgs< | AttributeBetweenExprArg | BuilderExprArg | NotExprArg; -export class ExpressionBuilder< - DDB, - Table extends keyof DDB, - O extends DDB[Table] -> implements ExpressionBuilderInterface +export class ExpressionBuilder + implements ExpressionBuilderInterface { readonly #props: ExpressionBuilderProps; diff --git a/src/queryBuilders/putItemQueryBuilder.integration.test.ts b/src/queryBuilders/putItemQueryBuilder.integration.test.ts index cf06626..b1af9eb 100644 --- a/src/queryBuilders/putItemQueryBuilder.integration.test.ts +++ b/src/queryBuilders/putItemQueryBuilder.integration.test.ts @@ -44,6 +44,25 @@ describe("PutItemQueryBuilder", () => { expect(result).toEqual(itemToPut); }); + it("doesnt return values without returnValues or when its set to NONE", async () => { + let result = await tsynamoClient + .putItem("myTable") + .item(itemToPut) + .execute(); + + expectTypeOf(result).toBeNever(); + expect(result).toBeUndefined(); + + let result2 = await tsynamoClient + .putItem("myTable") + .item(itemToPut) + .returnValues("NONE") + .execute(); + + expectTypeOf(result2).toBeNever(); + expect(result2).toBeUndefined(); + }); + it("handles ReturnValues option", async () => { let result = await tsynamoClient .putItem("myTable") @@ -51,6 +70,8 @@ describe("PutItemQueryBuilder", () => { .returnValues("ALL_OLD") .execute(); + expectTypeOf(result).not.toBeNever(); + expect(result).toBeUndefined(); result = await tsynamoClient diff --git a/src/queryBuilders/putItemQueryBuilder.ts b/src/queryBuilders/putItemQueryBuilder.ts index a21885c..c08d89b 100644 --- a/src/queryBuilders/putItemQueryBuilder.ts +++ b/src/queryBuilders/putItemQueryBuilder.ts @@ -16,11 +16,7 @@ import { NotExprArg, } from "./expressionBuilder"; -export interface PutItemQueryBuilderInterface< - DDB, - Table extends keyof DDB, - O extends DDB[Table] -> { +export interface PutItemQueryBuilderInterface { /** * A condition that must be satisfied in order for a PutItem operation to be executed. * @@ -129,9 +125,13 @@ export interface PutItemQueryBuilderInterface< * * The values returned are strongly consistent. */ - returnValues( - option: Extract - ): PutItemQueryBuilder; + returnValues( + option: Extract + ): PutItemQueryBuilder< + DDB, + Table, + "NONE" extends Choice ? O : ExecuteOutput + >; /** * The item that is put into the table. @@ -155,7 +155,7 @@ export interface PutItemQueryBuilderInterface< * .execute() * ``` */ - item>( + item>( item: Item ): PutItemQueryBuilder; @@ -163,17 +163,15 @@ export interface PutItemQueryBuilderInterface< * Compiles into an DynamoDB DocumentClient Command. */ compile(): PutCommand; + /** * Executes the command and returns its output. */ - execute(): Promise[] | undefined>; + execute(): Promise; } -export class PutItemQueryBuilder< - DDB, - Table extends keyof DDB, - O extends DDB[Table] -> implements PutItemQueryBuilderInterface +export class PutItemQueryBuilder + implements PutItemQueryBuilderInterface { readonly #props: PutItemQueryBuilderProps; @@ -217,7 +215,7 @@ export class PutItemQueryBuilder< }); } - item>( + item>( item: Item ): PutItemQueryBuilder { return new PutItemQueryBuilder({ @@ -232,10 +230,14 @@ export class PutItemQueryBuilder< }); } - returnValues( - option: Extract - ): PutItemQueryBuilder { - return new PutItemQueryBuilder({ + returnValues( + option: Extract + ): PutItemQueryBuilder< + DDB, + Table, + "NONE" extends Choice ? O : ExecuteOutput + > { + return new PutItemQueryBuilder({ ...this.#props, node: { ...this.#props.node, @@ -251,7 +253,7 @@ export class PutItemQueryBuilder< return this.#props.queryCompiler.compile(this.#props.node); }; - execute = async (): Promise[] | undefined> => { + execute = async (): Promise => { const putCommand = this.compile(); const data = await this.#props.ddbClient.send(putCommand); return data.Attributes as any; diff --git a/src/queryCreator.ts b/src/queryCreator.ts index 2db6674..0a8bbef 100644 --- a/src/queryCreator.ts +++ b/src/queryCreator.ts @@ -123,10 +123,10 @@ export class QueryCreator { * * @see https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/dynamodb/command/PutItemCommand/ */ - putItem( + putItem
( table: Table - ): PutItemQueryBuilder { - return new PutItemQueryBuilder({ + ): PutItemQueryBuilder { + return new PutItemQueryBuilder({ node: { kind: "PutNode", table: { @@ -166,10 +166,10 @@ export class QueryCreator { * * @see https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/dynamodb/command/DeleteItemCommand/ */ - deleteItem
( + deleteItem
( table: Table - ): DeleteItemQueryBuilder { - return new DeleteItemQueryBuilder({ + ): DeleteItemQueryBuilder { + return new DeleteItemQueryBuilder({ node: { kind: "DeleteNode", table: {