@@ -50,6 +50,7 @@ export class Transaction {
5050 private updatedAt : any ;
5151 private operations : { [ itemKey : string ] : ItemOperation } = { } ;
5252 private shouldSplitTransactions : boolean ;
53+ private hasBeenExecuted : boolean = false ;
5354
5455 constructor ( {
5556 tableName,
@@ -79,6 +80,12 @@ export class Transaction {
7980 item : WithoutReferences < T > ,
8081 args ?: CreateOperation [ "args" ]
8182 ) {
83+ if ( this . hasBeenExecuted ) {
84+ throw new Error (
85+ "[@moicky/dynamodb]: Transaction has already been executed"
86+ ) ;
87+ }
88+
8289 const itemKey = this . getItemKey ( item , args ?. TableName ) ;
8390
8491 const createOperation : CreateOperation = {
@@ -92,15 +99,30 @@ export class Transaction {
9299 return new CreateOperations < T > ( createOperation , this ) ;
93100 }
94101 update < T extends DynamoDBItemKey > (
95- item : OnlyKey < T > ,
102+ item : T extends any ? OnlyKey < T > : T ,
96103 args ?: UpdateOperation [ "args" ]
97104 ) {
105+ if ( this . hasBeenExecuted ) {
106+ throw new Error (
107+ "[@moicky/dynamodb]: Transaction has already been executed"
108+ ) ;
109+ }
110+
98111 const itemKey = this . getItemKey ( item , args ?. TableName ) ;
99112
113+ const actions : UpdateAction [ ] = [
114+ { _type : "set" , values : { updatedAt : this . updatedAt } } ,
115+ ] ;
116+
117+ const existingOperation = this . operations [ itemKey ] ;
118+ if ( existingOperation && existingOperation . _type === "update" ) {
119+ actions . push ( ...existingOperation . actions ) ;
120+ }
121+
100122 const updateOperation : UpdateOperation = {
101123 _type : "update" ,
102124 item,
103- actions : [ { _type : "set" , values : { updatedAt : this . updatedAt } } ] ,
125+ actions,
104126 args : { TableName : this . tableName , ...args } ,
105127 } ;
106128
@@ -109,19 +131,33 @@ export class Transaction {
109131 return new UpdateOperations < T > ( updateOperation , this ) ;
110132 }
111133 delete ( item : DynamoDBItemKey , args ?: DeleteOperation [ "args" ] ) {
134+ if ( this . hasBeenExecuted ) {
135+ throw new Error (
136+ "[@moicky/dynamodb]: Transaction has already been executed"
137+ ) ;
138+ }
139+
112140 const itemKey = this . getItemKey ( item , args ?. TableName ) ;
113141
114142 this . operations [ itemKey ] = {
115143 _type : "delete" ,
116144 item,
117145 args : { TableName : this . tableName , ...args } ,
118146 } ;
147+
148+ return this ;
119149 }
120150 addConditionFor < T extends DynamoDBItemKey > (
121151 item : T ,
122152 args ?: Partial < ConditionOperation [ "args" ] >
123153 ) {
124- return new ConditionOperations < T > ( this . operations , item , {
154+ if ( this . hasBeenExecuted ) {
155+ throw new Error (
156+ "[@moicky/dynamodb]: Transaction has already been executed"
157+ ) ;
158+ }
159+
160+ return new ConditionOperations < T > ( this , this . operations , item , {
125161 TableName : this . tableName ,
126162 ...args ,
127163 } ) ;
@@ -234,6 +270,16 @@ export class Transaction {
234270
235271 return new Promise < Record < string , ResponseItem [ ] > > (
236272 async ( resolve , reject ) => {
273+ if ( this . hasBeenExecuted ) {
274+ reject (
275+ new Error (
276+ "[@moicky/dynamodb]: Transaction has already been executed"
277+ )
278+ ) ;
279+ }
280+
281+ this . hasBeenExecuted = true ;
282+
237283 const operations = Object . values ( this . operations ) ;
238284
239285 if (
0 commit comments