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.
intial work for supporting transactions
- Loading branch information
1 parent
dc22939
commit 47c9ce2
Showing
8 changed files
with
211 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { PutNode } from "./putNode"; | ||
|
||
export type TransactItemNode = { | ||
readonly kind: "TransactItemNode"; | ||
readonly Put?: PutNode; | ||
}; |
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,6 @@ | ||
import { TransactItemNode } from "./TransactItemNode"; | ||
|
||
export type TransactionNode = { | ||
readonly kind: "TransactionNode"; | ||
readonly transactItems: TransactItemNode[]; | ||
}; |
14 changes: 14 additions & 0 deletions
14
src/queryBuilders/__snapshots__/transactionBuilder.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,14 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`TransactionBuilder > handles puts 1`] = ` | ||
[ | ||
{ | ||
"dataTimestamp": 1, | ||
"userId": "9999", | ||
}, | ||
{ | ||
"dataTimestamp": 2, | ||
"userId": "9999", | ||
}, | ||
] | ||
`; |
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,43 @@ | ||
import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb"; | ||
import { DDB } from "../../test/testFixture"; | ||
import { getDDBClientFor, startDDBTestContainer } from "../../test/testUtil"; | ||
import { Tsynamo } from "../index"; | ||
|
||
describe("TransactionBuilder", () => { | ||
let tsynamoClient: Tsynamo<DDB>; | ||
let ddbClient: DynamoDBDocumentClient; | ||
|
||
beforeAll(async () => { | ||
const testContainer = await startDDBTestContainer(); | ||
ddbClient = await getDDBClientFor(testContainer); | ||
|
||
tsynamoClient = new Tsynamo<DDB>({ | ||
ddbClient, | ||
}); | ||
}); | ||
|
||
it("handles puts", async () => { | ||
const trx = tsynamoClient.createTransaction(); | ||
|
||
trx.addItem({ | ||
Put: tsynamoClient | ||
.putItem("myTable") | ||
.item({ userId: "9999", dataTimestamp: 1 }), | ||
}); | ||
|
||
trx.addItem({ | ||
Put: tsynamoClient | ||
.putItem("myTable") | ||
.item({ userId: "9999", dataTimestamp: 2 }), | ||
}); | ||
|
||
await trx.execute(); | ||
|
||
const result = await tsynamoClient | ||
.query("myTable") | ||
.keyCondition("userId", "=", "9999") | ||
.execute(); | ||
|
||
expect(result).toMatchSnapshot(); | ||
}); | ||
}); |
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,44 @@ | ||
import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb"; | ||
import { TransactionNode } from "../nodes/transactionNode"; | ||
import { QueryCompiler } from "../queryCompiler"; | ||
import { PutItemQueryBuilder } from "./putItemQueryBuilder"; | ||
|
||
export interface TransactionBuilderInterface<DDB> { | ||
/** | ||
* TODO: Update, Put, Delete, ConditionCheck | ||
*/ | ||
addItem(item: { Put?: PutItemQueryBuilder<DDB, any, any> }): void; | ||
|
||
execute(): Promise<void>; | ||
} | ||
|
||
export class TransactionBuilder<DDB> | ||
implements TransactionBuilderInterface<DDB> | ||
{ | ||
readonly #props: TransactionBuilderProps; | ||
|
||
constructor(props: TransactionBuilderProps) { | ||
this.#props = props; | ||
} | ||
|
||
addItem(item: { Put?: PutItemQueryBuilder<DDB, any, any> }) { | ||
this.#props.node.transactItems.push({ | ||
kind: "TransactItemNode", | ||
Put: item.Put?.node, | ||
}); | ||
} | ||
|
||
async execute() { | ||
const transactionCommand = this.#props.queryCompiler.compile( | ||
this.#props.node | ||
); | ||
|
||
await this.#props.ddbClient.send(transactionCommand); | ||
} | ||
} | ||
|
||
interface TransactionBuilderProps { | ||
readonly node: TransactionNode; | ||
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
Oops, something went wrong.