-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from mindler-olli/main
add support for delete item command
- Loading branch information
Showing
12 changed files
with
448 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { ExpressionNode } from "./expressionNode"; | ||
import { KeysNode } from "./keysNode"; | ||
import { ReturnOldValuesNode, ReturnValuesNode } from "./returnValuesNode"; | ||
import { TableNode } from "./tableNode"; | ||
|
||
export type DeleteNode = { | ||
readonly kind: "DeleteNode"; | ||
readonly table: TableNode; | ||
readonly conditionExpression: ExpressionNode; | ||
readonly returnValues?: ReturnValuesNode; | ||
readonly returnValuesOnConditionCheckFailure?: ReturnOldValuesNode; | ||
readonly keys?: KeysNode; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/queryBuilders/__snapshots__/deleteItemQueryBuilder.integration.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`DeleteItemQueryBuilder > handles a simple delete query 1`] = ` | ||
{ | ||
"dataTimestamp": 2, | ||
"userId": "1", | ||
} | ||
`; |
89 changes: 89 additions & 0 deletions
89
src/queryBuilders/deleteItemQueryBuilder.integration.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { DDB } from "../../test/testFixture"; | ||
import { getDDBClientFor, startDDBTestContainer } from "../../test/testUtil"; | ||
import { Tsynamo } from "./../index"; | ||
|
||
describe("DeleteItemQueryBuilder", () => { | ||
let tsynamoClient: Tsynamo<DDB>; | ||
|
||
beforeAll(async () => { | ||
const testContainer = await startDDBTestContainer(); | ||
|
||
tsynamoClient = new Tsynamo<DDB>({ | ||
ddbClient: await getDDBClientFor(testContainer), | ||
}); | ||
}); | ||
|
||
it("handles a simple delete query", async () => { | ||
await tsynamoClient | ||
.putItem("myTable") | ||
.item({ | ||
userId: "1", | ||
dataTimestamp: 2, | ||
}) | ||
.execute(); | ||
|
||
const itemBeforeDeletion = await tsynamoClient | ||
.getItem("myTable") | ||
.keys({ userId: "1", dataTimestamp: 2 }) | ||
.execute(); | ||
|
||
expect(itemBeforeDeletion).toBeDefined(); | ||
|
||
const deleteResponse = await tsynamoClient | ||
.deleteItem("myTable") | ||
.keys({ | ||
userId: "1", | ||
dataTimestamp: 2, | ||
}) | ||
.returnValues("ALL_OLD") | ||
.execute(); | ||
|
||
expect(deleteResponse).toMatchSnapshot(); | ||
|
||
const itemAfterDeletion = await tsynamoClient | ||
.getItem("myTable") | ||
.keys({ userId: "1", dataTimestamp: 2 }) | ||
.execute(); | ||
|
||
expect(itemAfterDeletion).toBeUndefined(); | ||
}); | ||
|
||
it("handles a delete query with a ConditionExpression", async () => { | ||
await tsynamoClient | ||
.putItem("myTable") | ||
.item({ | ||
userId: "1", | ||
dataTimestamp: 2, | ||
tags: ["meow"], | ||
someBoolean: true, | ||
}) | ||
.execute(); | ||
|
||
expect( | ||
tsynamoClient | ||
.deleteItem("myTable") | ||
.keys({ | ||
userId: "1", | ||
dataTimestamp: 2, | ||
}) | ||
.conditionExpression("NOT", (qb) => { | ||
return qb.expression("tags", "contains", "meow"); | ||
}) | ||
.execute() | ||
).rejects.toMatchInlineSnapshot( | ||
`[ConditionalCheckFailedException: The conditional request failed]` | ||
); | ||
|
||
const res = await tsynamoClient | ||
.deleteItem("myTable") | ||
.keys({ userId: "1", dataTimestamp: 2 }) | ||
.conditionExpression("NOT", (qb) => { | ||
return qb.expression("tags", "contains", "meow"); | ||
}) | ||
.orConditionExpression("someBoolean", "attribute_exists") | ||
.returnValues("ALL_OLD") | ||
.execute(); | ||
|
||
expect(res).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,214 @@ | ||
import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb"; | ||
import { DeleteNode } from "../nodes/deleteNode"; | ||
import { ReturnValuesOptions } from "../nodes/returnValuesNode"; | ||
import { QueryCompiler } from "../queryCompiler"; | ||
import { | ||
ExecuteOutput, | ||
ObjectKeyPaths, | ||
PickPk, | ||
PickSkRequired, | ||
} from "../typeHelpers"; | ||
import { preventAwait } from "../util/preventAwait"; | ||
import { | ||
AttributeBeginsWithExprArg, | ||
AttributeBetweenExprArg, | ||
AttributeContainsExprArg, | ||
AttributeFuncExprArg, | ||
BuilderExprArg, | ||
ComparatorExprArg, | ||
ExprArgs, | ||
ExpressionBuilder, | ||
NotExprArg, | ||
} from "./expressionBuilder"; | ||
|
||
export interface DeleteItemQueryBuilderInterface< | ||
DDB, | ||
Table extends keyof DDB, | ||
O | ||
> { | ||
// conditionExpression | ||
conditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>( | ||
...args: ComparatorExprArg<DDB, Table, Key> | ||
): DeleteItemQueryBuilderInterface<DDB, Table, O>; | ||
|
||
conditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>( | ||
...args: AttributeFuncExprArg<Key> | ||
): DeleteItemQueryBuilderInterface<DDB, Table, O>; | ||
|
||
conditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>( | ||
...args: AttributeBeginsWithExprArg<Key> | ||
): DeleteItemQueryBuilderInterface<DDB, Table, O>; | ||
|
||
conditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>( | ||
...args: AttributeContainsExprArg<DDB, Table, Key> | ||
): DeleteItemQueryBuilderInterface<DDB, Table, O>; | ||
|
||
conditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>( | ||
...args: AttributeBetweenExprArg<DDB, Table, Key> | ||
): DeleteItemQueryBuilderInterface<DDB, Table, O>; | ||
|
||
conditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>( | ||
...args: NotExprArg<DDB, Table, Key> | ||
): DeleteItemQueryBuilderInterface<DDB, Table, O>; | ||
|
||
conditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>( | ||
...args: BuilderExprArg<DDB, Table, Key> | ||
): DeleteItemQueryBuilderInterface<DDB, Table, O>; | ||
|
||
// orConditionExpression | ||
orConditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>( | ||
...args: ComparatorExprArg<DDB, Table, Key> | ||
): DeleteItemQueryBuilderInterface<DDB, Table, O>; | ||
|
||
orConditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>( | ||
...args: AttributeFuncExprArg<Key> | ||
): DeleteItemQueryBuilderInterface<DDB, Table, O>; | ||
|
||
orConditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>( | ||
...args: AttributeBeginsWithExprArg<Key> | ||
): DeleteItemQueryBuilderInterface<DDB, Table, O>; | ||
|
||
orConditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>( | ||
...args: AttributeContainsExprArg<DDB, Table, Key> | ||
): DeleteItemQueryBuilderInterface<DDB, Table, O>; | ||
|
||
orConditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>( | ||
...args: AttributeBetweenExprArg<DDB, Table, Key> | ||
): DeleteItemQueryBuilderInterface<DDB, Table, O>; | ||
|
||
orConditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>( | ||
...args: NotExprArg<DDB, Table, Key> | ||
): DeleteItemQueryBuilderInterface<DDB, Table, O>; | ||
|
||
orConditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>( | ||
...args: BuilderExprArg<DDB, Table, Key> | ||
): DeleteItemQueryBuilderInterface<DDB, Table, O>; | ||
|
||
returnValues( | ||
option: Extract<ReturnValuesOptions, "NONE" | "ALL_OLD"> | ||
): DeleteItemQueryBuilderInterface<DDB, Table, O>; | ||
|
||
returnValuesOnConditionCheckFailure( | ||
option: Extract<ReturnValuesOptions, "NONE" | "ALL_OLD"> | ||
): DeleteItemQueryBuilderInterface<DDB, Table, O>; | ||
|
||
keys<Keys extends PickPk<DDB[Table]> & PickSkRequired<DDB[Table]>>( | ||
pk: Keys | ||
): DeleteItemQueryBuilderInterface<DDB, Table, O>; | ||
|
||
execute(): Promise<ExecuteOutput<O>[] | undefined>; | ||
} | ||
|
||
/** | ||
* @todo support ReturnValuesOnConditionCheckFailure | ||
*/ | ||
export class DeleteItemQueryBuilder< | ||
DDB, | ||
Table extends keyof DDB, | ||
O extends DDB[Table] | ||
> implements DeleteItemQueryBuilderInterface<DDB, Table, O> | ||
{ | ||
readonly #props: DeleteItemQueryBuilderProps; | ||
|
||
constructor(props: DeleteItemQueryBuilderProps) { | ||
this.#props = props; | ||
} | ||
|
||
conditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>( | ||
...args: ExprArgs<DDB, Table, O, Key> | ||
): DeleteItemQueryBuilderInterface<DDB, Table, O> { | ||
const eB = new ExpressionBuilder<DDB, Table, O>({ | ||
node: { ...this.#props.node.conditionExpression }, | ||
}); | ||
|
||
const expressionNode = eB.expression(...args)._getNode(); | ||
|
||
return new DeleteItemQueryBuilder<DDB, Table, O>({ | ||
...this.#props, | ||
node: { | ||
...this.#props.node, | ||
conditionExpression: expressionNode, | ||
}, | ||
}); | ||
} | ||
|
||
orConditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>( | ||
...args: ExprArgs<DDB, Table, O, Key> | ||
): DeleteItemQueryBuilderInterface<DDB, Table, O> { | ||
const eB = new ExpressionBuilder<DDB, Table, O>({ | ||
node: { ...this.#props.node.conditionExpression }, | ||
}); | ||
|
||
const expressionNode = eB.orExpression(...args)._getNode(); | ||
|
||
return new DeleteItemQueryBuilder<DDB, Table, O>({ | ||
...this.#props, | ||
node: { | ||
...this.#props.node, | ||
conditionExpression: expressionNode, | ||
}, | ||
}); | ||
} | ||
|
||
returnValues( | ||
option: Extract<ReturnValuesOptions, "NONE" | "ALL_OLD"> | ||
): DeleteItemQueryBuilderInterface<DDB, Table, O> { | ||
return new DeleteItemQueryBuilder<DDB, Table, O>({ | ||
...this.#props, | ||
node: { | ||
...this.#props.node, | ||
returnValues: { | ||
kind: "ReturnValuesNode", | ||
option, | ||
}, | ||
}, | ||
}); | ||
} | ||
|
||
returnValuesOnConditionCheckFailure( | ||
option: Extract<ReturnValuesOptions, "NONE" | "ALL_OLD"> | ||
): DeleteItemQueryBuilderInterface<DDB, Table, O> { | ||
return new DeleteItemQueryBuilder<DDB, Table, O>({ | ||
...this.#props, | ||
node: { | ||
...this.#props.node, | ||
returnValuesOnConditionCheckFailure: { | ||
kind: "ReturnValuesNode", | ||
option, | ||
}, | ||
}, | ||
}); | ||
} | ||
|
||
keys<Keys extends PickPk<DDB[Table]> & PickSkRequired<DDB[Table]>>( | ||
keys: Keys | ||
) { | ||
return new DeleteItemQueryBuilder<DDB, Table, O>({ | ||
...this.#props, | ||
node: { | ||
...this.#props.node, | ||
keys: { | ||
kind: "KeysNode", | ||
keys, | ||
}, | ||
}, | ||
}); | ||
} | ||
|
||
execute = async (): Promise<ExecuteOutput<O>[] | undefined> => { | ||
const deleteCommand = this.#props.queryCompiler.compile(this.#props.node); | ||
const data = await this.#props.ddbClient.send(deleteCommand); | ||
return data.Attributes as any; | ||
}; | ||
} | ||
|
||
preventAwait( | ||
DeleteItemQueryBuilder, | ||
"Don't await DeleteItemQueryBuilder instances directly. To execute the query you need to call the `execute` method" | ||
); | ||
|
||
interface DeleteItemQueryBuilderProps { | ||
readonly node: DeleteNode; | ||
readonly ddbClient: DynamoDBDocumentClient; | ||
readonly queryCompiler: QueryCompiler; | ||
} |
Oops, something went wrong.