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
5a48f58
commit e1a616b
Showing
12 changed files
with
164 additions
and
18 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 { TransactGetItemNode } from "./transactGetItemNode"; | ||
|
||
export type ReadTransactionNode = { | ||
readonly kind: "ReadTransactionNode"; | ||
readonly transactGetItems: TransactGetItemNode[]; | ||
}; |
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 { GetNode } from "./getNode"; | ||
|
||
export type TransactGetItemNode = { | ||
readonly kind: "TransactGetItemNode"; | ||
readonly Get: GetNode; | ||
}; |
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
src/nodes/transactionNode.ts → src/nodes/writeTransactionNode.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
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
32 changes: 32 additions & 0 deletions
32
src/queryBuilders/readTransactionBuilder.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,32 @@ | ||
import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb"; | ||
import { DDB } from "../../test/testFixture"; | ||
import { getDDBClientFor, startDDBTestContainer } from "../../test/testUtil"; | ||
import { Tsynamo } from "../index"; | ||
|
||
describe("ReadTransactionBuilder", () => { | ||
let tsynamoClient: Tsynamo<DDB>; | ||
let ddbClient: DynamoDBDocumentClient; | ||
|
||
beforeAll(async () => { | ||
const testContainer = await startDDBTestContainer(); | ||
ddbClient = await getDDBClientFor(testContainer); | ||
|
||
tsynamoClient = new Tsynamo<DDB>({ | ||
ddbClient, | ||
}); | ||
}); | ||
|
||
it("handles transaction with gets", async () => { | ||
const trx = tsynamoClient.createReadTransaction(); | ||
|
||
trx.addItem({ | ||
Get: tsynamoClient.getItem("myTable").keys({ | ||
userId: "123", | ||
dataTimestamp: 222, | ||
}), | ||
}); | ||
|
||
const result = await trx.execute(); | ||
console.log("result", result); | ||
}); | ||
}); |
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,48 @@ | ||
import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb"; | ||
import { ReadTransactionNode } from "../nodes/readTransactionNode"; | ||
import { QueryCompiler } from "../queryCompiler"; | ||
import { GetQueryBuilder } from "./getItemQueryBuilder"; | ||
|
||
/** | ||
* @todo What to show as return type? | ||
*/ | ||
export interface ReadTransactionBuilderInterface<DDB> { | ||
addItem<Table extends keyof DDB, O extends DDB[Table]>(item: { | ||
Get: GetQueryBuilder<DDB, Table, O>; | ||
}): void; | ||
|
||
execute(): Promise<unknown[]>; | ||
} | ||
|
||
export class ReadTransactionBuilder<DDB, X> | ||
implements ReadTransactionBuilderInterface<DDB> | ||
{ | ||
readonly #props: ReadTransactionBuilderProps; | ||
|
||
constructor(props: ReadTransactionBuilderProps) { | ||
this.#props = props; | ||
} | ||
|
||
addItem(item: { Get: GetQueryBuilder<DDB, any, any> }) { | ||
this.#props.node.transactGetItems.push({ | ||
kind: "TransactGetItemNode", | ||
Get: item.Get.node, | ||
}); | ||
} | ||
|
||
async execute() { | ||
const transactionCommand = this.#props.queryCompiler.compile( | ||
this.#props.node | ||
); | ||
|
||
return ( | ||
await this.#props.ddbClient.send(transactionCommand) | ||
).Responses?.map((o) => o.Item) as unknown[]; | ||
} | ||
} | ||
|
||
interface ReadTransactionBuilderProps { | ||
readonly node: ReadTransactionNode; | ||
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
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