Skip to content

Commit

Permalink
fix delte and put item typing when no returnValues set
Browse files Browse the repository at this point in the history
  • Loading branch information
sasumaki committed Jun 6, 2024
1 parent fd6a3d4 commit efe9e98
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 42 deletions.
36 changes: 36 additions & 0 deletions src/queryBuilders/deleteItemQueryBuilder.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,40 @@ describe("DeleteItemQueryBuilder", () => {

expect(res).toBeDefined();
});
it("doesn't return values if no 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 })
.execute();

expectTypeOf(res).toBeNever();

expect(res).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();
});
});
25 changes: 11 additions & 14 deletions src/queryBuilders/deleteItemQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
export interface DeleteItemQueryBuilderInterface<
DDB,
Table extends keyof DDB,
O extends DDB[Table]
O
> {
// conditionExpression
conditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>(
Expand Down Expand Up @@ -86,28 +86,25 @@ export interface DeleteItemQueryBuilderInterface<

returnValues(
option: Extract<ReturnValuesOptions, "NONE" | "ALL_OLD">
): DeleteItemQueryBuilder<DDB, Table, O>;
): DeleteItemQueryBuilder<DDB, Table, ExecuteOutput<DDB[Table]>>;

returnValuesOnConditionCheckFailure(
option: Extract<ReturnValuesOptions, "NONE" | "ALL_OLD">
): DeleteItemQueryBuilder<DDB, Table, O>;
): DeleteItemQueryBuilder<DDB, Table, ExecuteOutput<DDB[Table]>>;

keys<Keys extends PickPk<DDB[Table]> & PickSkRequired<DDB[Table]>>(
pk: Keys
): DeleteItemQueryBuilder<DDB, Table, O>;

compile(): DeleteCommand;
execute(): Promise<ExecuteOutput<O>[] | undefined>;
execute(): Promise<O[] | undefined>;
}

/**
* @todo support ReturnValuesOnConditionCheckFailure
*/
export class DeleteItemQueryBuilder<
DDB,
Table extends keyof DDB,
O extends DDB[Table]
> implements DeleteItemQueryBuilderInterface<DDB, Table, O>
export class DeleteItemQueryBuilder<DDB, Table extends keyof DDB, O>
implements DeleteItemQueryBuilderInterface<DDB, Table, O>
{
readonly #props: DeleteItemQueryBuilderProps;

Expand Down Expand Up @@ -153,8 +150,8 @@ export class DeleteItemQueryBuilder<

returnValues(
option: Extract<ReturnValuesOptions, "NONE" | "ALL_OLD">
): DeleteItemQueryBuilder<DDB, Table, O> {
return new DeleteItemQueryBuilder<DDB, Table, O>({
): DeleteItemQueryBuilder<DDB, Table, ExecuteOutput<DDB[Table]>> {
return new DeleteItemQueryBuilder({
...this.#props,
node: {
...this.#props.node,
Expand All @@ -168,8 +165,8 @@ export class DeleteItemQueryBuilder<

returnValuesOnConditionCheckFailure(
option: Extract<ReturnValuesOptions, "NONE" | "ALL_OLD">
): DeleteItemQueryBuilder<DDB, Table, O> {
return new DeleteItemQueryBuilder<DDB, Table, O>({
): DeleteItemQueryBuilder<DDB, Table, ExecuteOutput<DDB[Table]>> {
return new DeleteItemQueryBuilder({
...this.#props,
node: {
...this.#props.node,
Expand Down Expand Up @@ -200,7 +197,7 @@ export class DeleteItemQueryBuilder<
return this.#props.queryCompiler.compile(this.#props.node);
};

execute = async (): Promise<ExecuteOutput<O>[] | undefined> => {
execute = async (): Promise<unknown extends O ? never : O[] | undefined> => {
const deleteCommand = this.compile();
const data = await this.#props.ddbClient.send(deleteCommand);
return data.Attributes as any;
Expand Down
7 changes: 2 additions & 5 deletions src/queryBuilders/expressionBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,8 @@ export type ExprArgs<
| AttributeBetweenExprArg<DDB, Table, Key>
| BuilderExprArg<DDB, Table, O, AllowKeysInExpression>
| NotExprArg<DDB, Table, O, AllowKeysInExpression>;
export class ExpressionBuilder<
DDB,
Table extends keyof DDB,
O extends DDB[Table]
> implements ExpressionBuilderInterface<DDB, Table, O>
export class ExpressionBuilder<DDB, Table extends keyof DDB, O>
implements ExpressionBuilderInterface<DDB, Table, O>
{
readonly #props: ExpressionBuilderProps;

Expand Down
12 changes: 12 additions & 0 deletions src/queryBuilders/putItemQueryBuilder.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,25 @@ describe("PutItemQueryBuilder", () => {
expect(result).toEqual(itemToPut);
});

it("doesnt return values without returnValues", async () => {
let result = await tsynamoClient
.putItem("myTable")
.item(itemToPut)
.execute();

expectTypeOf(result).toBeNever();
expect(result).toBeUndefined();
});

it("handles ReturnValues option", async () => {
let result = await tsynamoClient
.putItem("myTable")
.item(itemToPut)
.returnValues("ALL_OLD")
.execute();

expectTypeOf(result).not.toBeNever();

expect(result).toBeUndefined();

result = await tsynamoClient
Expand Down
27 changes: 10 additions & 17 deletions src/queryBuilders/putItemQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ import {
NotExprArg,
} from "./expressionBuilder";

export interface PutItemQueryBuilderInterface<
DDB,
Table extends keyof DDB,
O extends DDB[Table]
> {
export interface PutItemQueryBuilderInterface<DDB, Table extends keyof DDB, O> {
// conditionExpression
conditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>(
...args: ComparatorExprArg<DDB, Table, Key>
Expand Down Expand Up @@ -81,21 +77,18 @@ export interface PutItemQueryBuilderInterface<

returnValues(
option: Extract<ReturnValuesOptions, "NONE" | "ALL_OLD">
): PutItemQueryBuilder<DDB, Table, O>;
): PutItemQueryBuilder<DDB, Table, ExecuteOutput<DDB[Table]>>;

item<Item extends ExecuteOutput<O>>(
item<Item extends ExecuteOutput<DDB[Table]>>(
item: Item
): PutItemQueryBuilder<DDB, Table, O>;

compile(): PutCommand;
execute(): Promise<ExecuteOutput<O>[] | undefined>;
execute(): Promise<O[] | undefined>;
}

export class PutItemQueryBuilder<
DDB,
Table extends keyof DDB,
O extends DDB[Table]
> implements PutItemQueryBuilderInterface<DDB, Table, O>
export class PutItemQueryBuilder<DDB, Table extends keyof DDB, O>
implements PutItemQueryBuilderInterface<DDB, Table, O>
{
readonly #props: PutItemQueryBuilderProps;

Expand Down Expand Up @@ -139,7 +132,7 @@ export class PutItemQueryBuilder<
});
}

item<Item extends ExecuteOutput<O>>(
item<Item extends ExecuteOutput<DDB[Table]>>(
item: Item
): PutItemQueryBuilder<DDB, Table, O> {
return new PutItemQueryBuilder<DDB, Table, O>({
Expand All @@ -156,8 +149,8 @@ export class PutItemQueryBuilder<

returnValues(
option: Extract<ReturnValuesOptions, "NONE" | "ALL_OLD">
): PutItemQueryBuilder<DDB, Table, O> {
return new PutItemQueryBuilder<DDB, Table, O>({
): PutItemQueryBuilder<DDB, Table, ExecuteOutput<DDB[Table]>> {
return new PutItemQueryBuilder({
...this.#props,
node: {
...this.#props.node,
Expand All @@ -173,7 +166,7 @@ export class PutItemQueryBuilder<
return this.#props.queryCompiler.compile(this.#props.node);
};

execute = async (): Promise<ExecuteOutput<O>[] | undefined> => {
execute = async (): Promise<unknown extends O ? never : O[] | undefined> => {
const putCommand = this.compile();
const data = await this.#props.ddbClient.send(putCommand);
return data.Attributes as any;
Expand Down
12 changes: 6 additions & 6 deletions src/queryCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ export class QueryCreator<DDB> {
*
* @see https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/dynamodb/command/PutItemCommand/
*/
putItem<Table extends keyof DDB & string>(
putItem<Table extends keyof DDB & string, O>(
table: Table
): PutItemQueryBuilder<DDB, Table, DDB[Table]> {
return new PutItemQueryBuilder<DDB, Table, DDB[Table]>({
): PutItemQueryBuilder<DDB, Table, O> {
return new PutItemQueryBuilder({
node: {
kind: "PutNode",
table: {
Expand All @@ -97,10 +97,10 @@ export class QueryCreator<DDB> {
*
* @see https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/dynamodb/command/DeleteItemCommand/
*/
deleteItem<Table extends keyof DDB & string>(
deleteItem<Table extends keyof DDB & string, O>(
table: Table
): DeleteItemQueryBuilder<DDB, Table, DDB[Table]> {
return new DeleteItemQueryBuilder<DDB, Table, DDB[Table]>({
): DeleteItemQueryBuilder<DDB, Table, O> {
return new DeleteItemQueryBuilder({
node: {
kind: "DeleteNode",
table: {
Expand Down

0 comments on commit efe9e98

Please sign in to comment.