forked from woltsu/tsynamo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ed2e564
commit 0b813d3
Showing
6 changed files
with
246 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { ExpressionNode } from "./expressionNode"; | ||
import { ItemNode } from "./itemNode"; | ||
import { ReturnValuesNode } from "./returnValuesNode"; | ||
import { TableNode } from "./tableNode"; | ||
|
||
export type UpdateNode = { | ||
readonly kind: "UpdateNode"; | ||
readonly table: TableNode; | ||
readonly conditionExpression: ExpressionNode; | ||
readonly item?: ItemNode; | ||
readonly returnValues?: ReturnValuesNode; | ||
}; |
17 changes: 17 additions & 0 deletions
17
src/queryBuilders/updateItemQueryBuilder.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,17 @@ | ||
import { DDB } from "../../test/testFixture"; | ||
import { getDDBClientFor, startDDBTestContainer } from "../../test/testUtil"; | ||
import { Tsynamo } from "./../index"; | ||
|
||
describe("UpdateItemQueryBuilder", () => { | ||
let tsynamoClient: Tsynamo<DDB>; | ||
|
||
beforeAll(async () => { | ||
const testContainer = await startDDBTestContainer(); | ||
|
||
tsynamoClient = new Tsynamo<DDB>({ | ||
ddbClient: await getDDBClientFor(testContainer), | ||
}); | ||
}); | ||
|
||
it.todo("handles a simple update item query"); | ||
}); |
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,173 @@ | ||
import { DynamoDBDocumentClient, UpdateCommand } from "@aws-sdk/lib-dynamodb"; | ||
import { ReturnValuesOptions } from "../nodes/returnValuesNode"; | ||
import { UpdateNode } from "../nodes/updateNode"; | ||
import { QueryCompiler } from "../queryCompiler"; | ||
import { ExecuteOutput, ObjectKeyPaths } from "../typeHelpers"; | ||
import { preventAwait } from "../util/preventAwait"; | ||
import { | ||
AttributeBeginsWithExprArg, | ||
AttributeBetweenExprArg, | ||
AttributeContainsExprArg, | ||
AttributeFuncExprArg, | ||
BuilderExprArg, | ||
ComparatorExprArg, | ||
ExprArgs, | ||
ExpressionBuilder, | ||
NotExprArg, | ||
} from "./expressionBuilder"; | ||
|
||
export interface UpdateItemQueryBuilderInterface< | ||
DDB, | ||
Table extends keyof DDB, | ||
O | ||
> { | ||
// conditionExpression | ||
conditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>( | ||
...args: ComparatorExprArg<DDB, Table, Key> | ||
): UpdateItemQueryBuilderInterface<DDB, Table, O>; | ||
|
||
conditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>( | ||
...args: AttributeFuncExprArg<Key> | ||
): UpdateItemQueryBuilderInterface<DDB, Table, O>; | ||
|
||
conditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>( | ||
...args: AttributeBeginsWithExprArg<Key> | ||
): UpdateItemQueryBuilderInterface<DDB, Table, O>; | ||
|
||
conditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>( | ||
...args: AttributeContainsExprArg<DDB, Table, Key> | ||
): UpdateItemQueryBuilderInterface<DDB, Table, O>; | ||
|
||
conditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>( | ||
...args: AttributeBetweenExprArg<DDB, Table, Key> | ||
): UpdateItemQueryBuilderInterface<DDB, Table, O>; | ||
|
||
conditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>( | ||
...args: NotExprArg<DDB, Table, Key> | ||
): UpdateItemQueryBuilderInterface<DDB, Table, O>; | ||
|
||
conditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>( | ||
...args: BuilderExprArg<DDB, Table, Key> | ||
): UpdateItemQueryBuilderInterface<DDB, Table, O>; | ||
|
||
// orConditionExpression | ||
orConditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>( | ||
...args: ComparatorExprArg<DDB, Table, Key> | ||
): UpdateItemQueryBuilderInterface<DDB, Table, O>; | ||
|
||
orConditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>( | ||
...args: AttributeFuncExprArg<Key> | ||
): UpdateItemQueryBuilderInterface<DDB, Table, O>; | ||
|
||
orConditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>( | ||
...args: AttributeBeginsWithExprArg<Key> | ||
): UpdateItemQueryBuilderInterface<DDB, Table, O>; | ||
|
||
orConditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>( | ||
...args: AttributeContainsExprArg<DDB, Table, Key> | ||
): UpdateItemQueryBuilderInterface<DDB, Table, O>; | ||
|
||
orConditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>( | ||
...args: AttributeBetweenExprArg<DDB, Table, Key> | ||
): UpdateItemQueryBuilderInterface<DDB, Table, O>; | ||
|
||
orConditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>( | ||
...args: NotExprArg<DDB, Table, Key> | ||
): UpdateItemQueryBuilderInterface<DDB, Table, O>; | ||
|
||
orConditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>( | ||
...args: BuilderExprArg<DDB, Table, Key> | ||
): UpdateItemQueryBuilderInterface<DDB, Table, O>; | ||
|
||
returnValues( | ||
option: ReturnValuesOptions | ||
): UpdateItemQueryBuilderInterface<DDB, Table, O>; | ||
|
||
compile(): UpdateCommand; | ||
execute(): Promise<ExecuteOutput<O>[] | undefined>; | ||
} | ||
|
||
export class UpdateItemQueryBuilder< | ||
DDB, | ||
Table extends keyof DDB, | ||
O extends DDB[Table] | ||
> implements UpdateItemQueryBuilderInterface<DDB, Table, O> | ||
{ | ||
readonly #props: UpdateItemQueryBuilderProps; | ||
|
||
constructor(props: UpdateItemQueryBuilderProps) { | ||
this.#props = props; | ||
} | ||
|
||
conditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>( | ||
...args: ExprArgs<DDB, Table, O, Key> | ||
): UpdateItemQueryBuilderInterface<DDB, Table, O> { | ||
const eB = new ExpressionBuilder<DDB, Table, O>({ | ||
node: { ...this.#props.node.conditionExpression }, | ||
}); | ||
|
||
const expressionNode = eB.expression(...args)._getNode(); | ||
|
||
return new UpdateItemQueryBuilder<DDB, Table, O>({ | ||
...this.#props, | ||
node: { | ||
...this.#props.node, | ||
conditionExpression: expressionNode, | ||
}, | ||
}); | ||
} | ||
|
||
orConditionExpression<Key extends ObjectKeyPaths<DDB[Table]>>( | ||
...args: ExprArgs<DDB, Table, O, Key> | ||
): UpdateItemQueryBuilderInterface<DDB, Table, O> { | ||
const eB = new ExpressionBuilder<DDB, Table, O>({ | ||
node: { ...this.#props.node.conditionExpression }, | ||
}); | ||
|
||
const expressionNode = eB.orExpression(...args)._getNode(); | ||
|
||
return new UpdateItemQueryBuilder<DDB, Table, O>({ | ||
...this.#props, | ||
node: { | ||
...this.#props.node, | ||
conditionExpression: expressionNode, | ||
}, | ||
}); | ||
} | ||
|
||
returnValues( | ||
option: ReturnValuesOptions | ||
): UpdateItemQueryBuilderInterface<DDB, Table, O> { | ||
return new UpdateItemQueryBuilder<DDB, Table, O>({ | ||
...this.#props, | ||
node: { | ||
...this.#props.node, | ||
returnValues: { | ||
kind: "ReturnValuesNode", | ||
option, | ||
}, | ||
}, | ||
}); | ||
} | ||
|
||
compile = (): UpdateCommand => { | ||
return this.#props.queryCompiler.compile(this.#props.node); | ||
}; | ||
|
||
execute = async (): Promise<ExecuteOutput<O>[] | undefined> => { | ||
const putCommand = this.compile(); | ||
const data = await this.#props.ddbClient.send(putCommand); | ||
return data.Attributes as any; | ||
}; | ||
} | ||
|
||
preventAwait( | ||
UpdateItemQueryBuilder, | ||
"Don't await UpdateItemQueryBuilder instances directly. To execute the query you need to call the `execute` method" | ||
); | ||
|
||
interface UpdateItemQueryBuilderProps { | ||
readonly node: UpdateNode; | ||
readonly ddbClient: DynamoDBDocumentClient; | ||
readonly queryCompiler: QueryCompiler; | ||
} |
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