Skip to content

Commit

Permalink
add support for remove update expr
Browse files Browse the repository at this point in the history
  • Loading branch information
mindler-olli committed Mar 28, 2024
1 parent fe04ecf commit aad28b2
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 12 deletions.
4 changes: 4 additions & 0 deletions src/nodes/removeUpdateExpression.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type RemoveUpdateExpression = {
readonly kind: "RemoveUpdateExpression";
readonly attribute: string;
};
2 changes: 2 additions & 0 deletions src/nodes/updateExpression.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { RemoveUpdateExpression } from "./removeUpdateExpression";
import { SetUpdateExpression } from "./setUpdateExpression";

export type UpdateExpression = {
readonly kind: "UpdateExpression";
readonly setUpdateExpressions: SetUpdateExpression[];
readonly removeUpdateExpressions: RemoveUpdateExpression[];
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`UpdateItemQueryBuilder > handles update item query with REMOVE statements 1`] = `
{
"dataTimestamp": 200,
"someBoolean": true,
"userId": "1010",
}
`;

exports[`UpdateItemQueryBuilder > handles update item query with SET statements 1`] = `
{
"dataTimestamp": 2,
Expand Down
30 changes: 29 additions & 1 deletion src/queryBuilders/updateItemQueryBuilder.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,39 @@ describe("UpdateItemQueryBuilder", () => {
);
})
.set("somethingElse", "+=", (qb) => {
return [qb.ifNotExists("somethingElse", 1), 2]
return [qb.ifNotExists("somethingElse", 1), 2];
})
.returnValues("ALL_NEW")
.execute();

expect(res).toMatchSnapshot();
});

it("handles update item query with REMOVE statements", async () => {
await tsynamoClient
.putItem("myTable")
.item({
userId: "1010",
dataTimestamp: 200,
somethingElse: 313,
someBoolean: true,
})
.execute();

await tsynamoClient
.updateItem("myTable")
.keys({ userId: "1010", dataTimestamp: 200 })
.remove("somethingElse")
.execute();

const foundItem = await tsynamoClient
.getItem("myTable")
.keys({
userId: "1010",
dataTimestamp: 200,
})
.execute();

expect(foundItem).toMatchSnapshot();
});
});
24 changes: 23 additions & 1 deletion src/queryBuilders/updateItemQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ export interface UpdateItemQueryBuilderInterface<
pk: Keys
): UpdateItemQueryBuilderInterface<DDB, Table, O>;

// TODO: remove
remove<Key extends ObjectKeyPaths<PickNonKeys<DDB[Table]>>>(
attribute: Key
): UpdateItemQueryBuilderInterface<DDB, Table, O>;

// TODO: add
// TODO: delete?

Expand Down Expand Up @@ -290,6 +293,25 @@ export class UpdateItemQueryBuilder<
}
}

remove<Key extends ObjectKeyPaths<PickNonKeys<DDB[Table]>>>(
attribute: Key
): UpdateItemQueryBuilderInterface<DDB, Table, O> {
return new UpdateItemQueryBuilder<DDB, Table, O>({
...this.#props,
node: {
...this.#props.node,
updateExpression: {
...this.#props.node.updateExpression,
removeUpdateExpressions:
this.#props.node.updateExpression.removeUpdateExpressions.concat({
kind: "RemoveUpdateExpression",
attribute,
}),
},
},
});
}

returnValues(
option: ReturnValuesOptions
): UpdateItemQueryBuilderInterface<DDB, Table, O> {
Expand Down
32 changes: 22 additions & 10 deletions src/queryCompiler/queryCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,17 +443,29 @@ export class QueryCompiler {
updateExpressionAttributeValues: Map<string, unknown>,
attributeNames: Map<string, string>
) {
let res = "SET ";
let res = "";

res += node.setUpdateExpressions
.map((setUpdateExpression) => {
return this.compileSetUpdateExpression(
setUpdateExpression,
updateExpressionAttributeValues,
attributeNames
);
})
.join(", ");
if (node.setUpdateExpressions.length > 0) {
res += "SET ";
res += node.setUpdateExpressions
.map((setUpdateExpression) => {
return this.compileSetUpdateExpression(
setUpdateExpression,
updateExpressionAttributeValues,
attributeNames
);
})
.join(", ");
}

if (node.removeUpdateExpressions.length > 0) {
res += " REMOVE ";
res += node.removeUpdateExpressions
.map((removeUpdateExpression) => {
return removeUpdateExpression.attribute;
})
.join(", ");
}

return res;
}
Expand Down
1 change: 1 addition & 0 deletions src/queryCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ export class QueryCreator<DDB> {
updateExpression: {
kind: "UpdateExpression",
setUpdateExpressions: [],
removeUpdateExpressions: [],
},
},
ddbClient: this.#props.ddbClient,
Expand Down

0 comments on commit aad28b2

Please sign in to comment.