Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial work for updateItemQuery #21

Merged
merged 13 commits into from
Apr 10, 2024
Merged
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,18 @@ await tsynamoClient

### Update item

WIP
```ts
await tsynamoClient
.updateItem("myTable")
.keys({ userId: "1", dataTimestamp: 2 })
.set("nested.nestedBoolean", "=", true)
.remove("nested.nestedString")
.add("somethingElse", 10)
.add("someSet", new Set(["4", "5"]))
.delete("nested.nestedSet", new Set(["4", "5"]))
.conditionExpression("somethingElse", ">", 0)
.execute();
```

## Contributors

Expand Down
5 changes: 5 additions & 0 deletions src/nodes/addUpdateExpression.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type AddUpdateExpression = {
readonly kind: "AddUpdateExpression";
readonly key: string;
readonly value: unknown;
};
5 changes: 5 additions & 0 deletions src/nodes/deleteUpdateExpression.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type DeleteUpdateExpression = {
readonly kind: "DeleteUpdateExpression";
readonly key: string;
readonly value: unknown;
};
2 changes: 2 additions & 0 deletions src/nodes/operands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ export type NotExpression = "NOT";

export type KeyConditionComparators = "=" | "<" | "<=" | ">" | ">=";
export type ExpressionConditionComparators = KeyConditionComparators | "<>";

export type UpdateExpressionOperands = "=" | "+=" | "-=";
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;
};
11 changes: 11 additions & 0 deletions src/nodes/setUpdateExpression.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { UpdateExpressionOperands } from "./operands";
import { SetUpdateExpressionFunction } from "./setUpdateExpressionFunction";
import { SetUpdateExpressionValue } from "./setUpdateExpressionValue";

export type SetUpdateExpression = {
readonly kind: "SetUpdateExpression";
readonly key: string;
readonly operation: UpdateExpressionOperands;
readonly right: SetUpdateExpressionFunction | SetUpdateExpressionValue;
readonly value?: number;
};
20 changes: 20 additions & 0 deletions src/nodes/setUpdateExpressionFunction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { SetUpdateExpressionValue } from "./setUpdateExpressionValue";

export type SetUpdateExpressionFunction = {
readonly kind: "SetUpdateExpressionFunction";
readonly function:
| SetUpdateExpressionIfNotExistsFunction
| SetUpdateExpressionListAppendFunction;
};

export type SetUpdateExpressionIfNotExistsFunction = {
readonly kind: "SetUpdateExpressionIfNotExistsFunction";
readonly path: string;
readonly right: SetUpdateExpressionFunction | SetUpdateExpressionValue;
};

export type SetUpdateExpressionListAppendFunction = {
readonly kind: "SetUpdateExpressionListAppendFunction";
readonly left: SetUpdateExpressionFunction | string;
readonly right: SetUpdateExpressionFunction | SetUpdateExpressionValue;
};
4 changes: 4 additions & 0 deletions src/nodes/setUpdateExpressionValue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type SetUpdateExpressionValue<O = unknown> = {
readonly kind: "SetUpdateExpressionValue";
value: O;
};
12 changes: 12 additions & 0 deletions src/nodes/updateExpression.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { AddUpdateExpression } from "./addUpdateExpression";
import { DeleteUpdateExpression } from "./deleteUpdateExpression";
import { RemoveUpdateExpression } from "./removeUpdateExpression";
import { SetUpdateExpression } from "./setUpdateExpression";

export type UpdateExpression = {
readonly kind: "UpdateExpression";
readonly setUpdateExpressions: SetUpdateExpression[];
readonly removeUpdateExpressions: RemoveUpdateExpression[];
readonly addUpdateExpressions: AddUpdateExpression[];
readonly deleteUpdateExpressions: DeleteUpdateExpression[];
};
15 changes: 15 additions & 0 deletions src/nodes/updateNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ExpressionNode } from "./expressionNode";
import { ItemNode } from "./itemNode";
import { KeysNode } from "./keysNode";
import { ReturnValuesNode } from "./returnValuesNode";
import { TableNode } from "./tableNode";
import { UpdateExpression } from "./updateExpression";

export type UpdateNode = {
readonly kind: "UpdateNode";
readonly table: TableNode;
readonly conditionExpression: ExpressionNode;
readonly updateExpression: UpdateExpression;
readonly keys?: KeysNode;
readonly returnValues?: ReturnValuesNode;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`UpdateItemQueryBuilder > handles update item query with ADD statements 1`] = `
{
"dataTimestamp": 200,
"someBoolean": true,
"someSet": Set {
"item1",
"item2",
},
"somethingElse": 7,
"userId": "1010",
}
`;

exports[`UpdateItemQueryBuilder > handles update item query with DELETE statements 1`] = `
{
"dataTimestamp": 2,
"nested": {
"nestedSet": Set {
"5",
},
},
"someSet": Set {
"1",
},
"userId": "1",
}
`;

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,
"someBoolean": true,
"somethingElse": 3,
"tags": [
"test_tag",
],
"userId": "1",
}
`;

exports[`UpdateItemQueryBuilder > handles update item query with condition expressions 2`] = `
{
"dataTimestamp": 2,
"someSet": Set {
"1",
"2",
"3",
},
"somethingElse": 0,
"userId": "1",
}
`;

exports[`UpdateItemQueryBuilder > handles update item query with multiple different operations 1`] = `
{
"dataTimestamp": 2,
"nested": {
"nestedBoolean": true,
},
"someSet": Set {
"1",
"2",
"3",
"4",
"5",
},
"somethingElse": 10,
"userId": "1",
}
`;
Loading
Loading