Skip to content

Commit

Permalink
refactor transaction -> writeTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
mindler-olli committed Apr 18, 2024
1 parent 1e57bb1 commit dfc5a0b
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 44 deletions.
4 changes: 2 additions & 2 deletions src/nodes/TransactItemNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { DeleteNode } from "./deleteNode";
import { PutNode } from "./putNode";
import { UpdateNode } from "./updateNode";

export type TransactItemNode = {
readonly kind: "TransactItemNode";
export type TransactWriteItemNode = {
readonly kind: "TransactWriteItemNode";
readonly Put?: PutNode;
readonly Delete?: DeleteNode;
readonly Update?: UpdateNode;
Expand Down
8 changes: 4 additions & 4 deletions src/nodes/transactionNode.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TransactItemNode } from "./TransactItemNode";
import { TransactWriteItemNode } from "./TransactItemNode";

export type TransactionNode = {
readonly kind: "TransactionNode";
readonly transactItems: TransactItemNode[];
export type WriteTransactionNode = {
readonly kind: "WriteTransactionNode";
readonly transactWriteItems: TransactWriteItemNode[];
};
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`TransactionBuilder > handles puts 1`] = `
[
{
"dataTimestamp": 1,
"userId": "9999",
},
{
"dataTimestamp": 2,
"userId": "9999",
},
]
`;

exports[`TransactionBuilder > handles transaction with puts 1`] = `
[
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe("TransactionBuilder", () => {
});

it("handles transaction with puts", async () => {
const trx = tsynamoClient.createTransaction();
const trx = tsynamoClient.createWriteTransaction();

trx.addItem({
Put: tsynamoClient
Expand Down Expand Up @@ -72,7 +72,7 @@ describe("TransactionBuilder", () => {

expect(foundItem).toBeDefined();

const trx = tsynamoClient.createTransaction();
const trx = tsynamoClient.createWriteTransaction();

trx.addItem({
Delete: tsynamoClient.deleteItem("myTable").keys({
Expand Down Expand Up @@ -122,7 +122,7 @@ describe("TransactionBuilder", () => {
.item({ userId: "1", dataTimestamp: 2 })
.execute();

const trx = tsynamoClient.createTransaction();
const trx = tsynamoClient.createWriteTransaction();

trx.addItem({
Update: tsynamoClient
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb";
import { TransactionNode } from "../nodes/transactionNode";
import { WriteTransactionNode } from "../nodes/transactionNode";
import { QueryCompiler } from "../queryCompiler";
import { PutItemQueryBuilder } from "./putItemQueryBuilder";
import { DeleteItemQueryBuilder } from "./deleteItemQueryBuilder";
import { PutItemQueryBuilder } from "./putItemQueryBuilder";
import { UpdateItemQueryBuilder } from "./updateItemQueryBuilder";

export interface TransactionBuilderInterface<DDB> {
export interface WriteTransactionBuilderInterface<DDB> {
/**
* TODO: ConditionCheck
*/
Expand All @@ -18,12 +18,12 @@ export interface TransactionBuilderInterface<DDB> {
execute(): Promise<void>;
}

export class TransactionBuilder<DDB>
implements TransactionBuilderInterface<DDB>
export class WriteTransactionBuilder<DDB>
implements WriteTransactionBuilderInterface<DDB>
{
readonly #props: TransactionBuilderProps;
readonly #props: WriteTransactionBuilderProps;

constructor(props: TransactionBuilderProps) {
constructor(props: WriteTransactionBuilderProps) {
this.#props = props;
}

Expand All @@ -32,8 +32,8 @@ export class TransactionBuilder<DDB>
Delete?: DeleteItemQueryBuilder<DDB, any, any>;
Update?: UpdateItemQueryBuilder<DDB, any, any>;
}) {
this.#props.node.transactItems.push({
kind: "TransactItemNode",
this.#props.node.transactWriteItems.push({
kind: "TransactWriteItemNode",
Put: item.Put?.node,
Delete: item.Delete?.node,
Update: item.Update?.node,
Expand All @@ -49,8 +49,8 @@ export class TransactionBuilder<DDB>
}
}

interface TransactionBuilderProps {
readonly node: TransactionNode;
interface WriteTransactionBuilderProps {
readonly node: WriteTransactionNode;
readonly ddbClient: DynamoDBDocumentClient;
readonly queryCompiler: QueryCompiler;
}
12 changes: 6 additions & 6 deletions src/queryCompiler/queryCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { QueryNode } from "../nodes/queryNode";
import { RemoveUpdateExpression } from "../nodes/removeUpdateExpression";
import { SetUpdateExpression } from "../nodes/setUpdateExpression";
import { SetUpdateExpressionFunction } from "../nodes/setUpdateExpressionFunction";
import { TransactionNode } from "../nodes/transactionNode";
import { WriteTransactionNode } from "../nodes/transactionNode";
import { UpdateExpression } from "../nodes/updateExpression";
import { UpdateNode } from "../nodes/updateNode";
import {
Expand All @@ -37,15 +37,15 @@ export class QueryCompiler {
compile(rootNode: PutNode): PutCommand;
compile(rootNode: DeleteNode): DeleteCommand;
compile(rootNode: UpdateNode): UpdateCommand;
compile(rootNode: TransactionNode): TransactWriteCommand;
compile(rootNode: WriteTransactionNode): TransactWriteCommand;
compile(
rootNode:
| QueryNode
| GetNode
| PutNode
| DeleteNode
| UpdateNode
| TransactionNode
| WriteTransactionNode
) {
switch (rootNode.kind) {
case "GetNode":
Expand All @@ -58,7 +58,7 @@ export class QueryCompiler {
return this.compileDeleteNode(rootNode);
case "UpdateNode":
return this.compileUpdateNode(rootNode);
case "TransactionNode":
case "WriteTransactionNode":
return this.compileTransactionNode(rootNode);
}
}
Expand Down Expand Up @@ -280,8 +280,8 @@ export class QueryCompiler {
};
}

compileTransactionNode(transactionNode: TransactionNode) {
const TransactItems = transactionNode.transactItems.map((item) => {
compileTransactionNode(transactionNode: WriteTransactionNode) {
const TransactItems = transactionNode.transactWriteItems.map((item) => {
const compiledTransactItem: TransactWriteItem = {};

if (item.Put) {
Expand Down
10 changes: 5 additions & 5 deletions src/queryCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { DeleteItemQueryBuilder } from "./queryBuilders/deleteItemQueryBuilder";
import { GetQueryBuilder } from "./queryBuilders/getItemQueryBuilder";
import { PutItemQueryBuilder } from "./queryBuilders/putItemQueryBuilder";
import { QueryQueryBuilder } from "./queryBuilders/queryQueryBuilder";
import { TransactionBuilder } from "./queryBuilders/transactionBuilder";
import { UpdateItemQueryBuilder } from "./queryBuilders/updateItemQueryBuilder";
import { WriteTransactionBuilder } from "./queryBuilders/writeTransactionBuilder";
import { QueryCompiler } from "./queryCompiler";

export class QueryCreator<DDB> {
Expand Down Expand Up @@ -155,11 +155,11 @@ export class QueryCreator<DDB> {
*
* @see https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/dynamodb/command/TransactWriteItemsCommand/
*/
createTransaction() {
return new TransactionBuilder<DDB>({
createWriteTransaction() {
return new WriteTransactionBuilder<DDB>({
node: {
kind: "TransactionNode",
transactItems: [],
kind: "WriteTransactionNode",
transactWriteItems: [],
},
ddbClient: this.#props.ddbClient,
queryCompiler: this.#props.queryCompiler,
Expand Down

0 comments on commit dfc5a0b

Please sign in to comment.