Skip to content

Commit ecdcecc

Browse files
committed
add transactWrite WIP
1 parent bb905e5 commit ecdcecc

File tree

1 file changed

+41
-9
lines changed

1 file changed

+41
-9
lines changed

src/classes/Table.ts

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,8 +1240,7 @@ class Table {
12401240
const result = await this.DocumentClient!.transactGet(payload).promise()
12411241
// If auto parse enable
12421242
if (options.parse || (this.autoParse && options.parse !== false)) {
1243-
//return this.parseBatchGetResponse(result)
1244-
//return result as DocumentClient.TransactGetItemsOutput
1243+
// Parse the items using the appropriate entity
12451244
return Object.assign(
12461245
result,
12471246
result.Responses ? {
@@ -1257,9 +1256,7 @@ class Table {
12571256
} else {
12581257
return payload as DocumentClient.TransactGetItemsInput
12591258
} // end-if
1260-
} // end batchGet
1261-
1262-
1259+
} // end transactGet
12631260

12641261

12651262
/**
@@ -1281,7 +1278,7 @@ class Table {
12811278
..._args
12821279
} = options
12831280

1284-
// Remove other valid options from options
1281+
// Remove other valid options from options
12851282
const args = Object.keys(_args).filter(x => !['execute','parse'].includes(x))
12861283

12871284
// Error on extraneous arguments
@@ -1318,6 +1315,38 @@ class Table {
13181315

13191316

13201317

1318+
/**
1319+
* Performs a transactWrite operation
1320+
* @param {object} items - An array of objects generated from putTransaction, updateTransaction, or deleteTransaction entity calls.
1321+
* @param {object} [options] - Additional transactWrite options
1322+
*
1323+
*/
1324+
async transactWrite(
1325+
items: DocumentClient.TransactWriteItemList,
1326+
options: transactWriteOptions = {},
1327+
params: Partial<DocumentClient.TransactWriteItemsInput> = {}
1328+
) {
1329+
// Generate the payload with meta information
1330+
const payload = this.transactWriteParams(items,options)
1331+
1332+
// If auto execute enabled
1333+
if (options.execute || (this.autoExecute && options.execute !== false)) {
1334+
const result = await this.DocumentClient!.transactWrite(payload).promise()
1335+
// If auto parse enable
1336+
if (options.parse || (this.autoParse && options.parse !== false)) {
1337+
1338+
return result as DocumentClient.TransactWriteItemsOutput
1339+
1340+
} else {
1341+
return result as DocumentClient.TransactWriteItemsOutput
1342+
}
1343+
} else {
1344+
return payload as DocumentClient.TransactWriteItemsInput
1345+
} // end-if
1346+
} // end transactGet
1347+
1348+
1349+
13211350
/**
13221351
* Generate parameters for a transactWrite operation
13231352
* @param {object} items - An array of objects generated from putTransaction, updateTransaction, or deleteTransaction entity calls.
@@ -1326,18 +1355,21 @@ class Table {
13261355
* Creates a TransactWriteItems object: https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_TransactWriteItems.html
13271356
*/
13281357
transactWriteParams(
1329-
items: DocumentClient.TransactGetItemList = [],
1358+
items: DocumentClient.TransactWriteItemList = [],
13301359
options: transactWriteParamsOptions = {}
1331-
): DocumentClient.TransactGetItemsInput {
1360+
): DocumentClient.TransactWriteItemsInput {
13321361

13331362
// Extract valid options
13341363
const {
13351364
capacity, // ReturnConsumedCapacity (none, total, or indexes)
13361365
metrics, // ReturnItemCollectionMetrics (size or none)
13371366
token, // ClientRequestToken (1-36 characters)
1338-
...args
1367+
..._args
13391368
} = options
13401369

1370+
// Remove other valid options from options
1371+
const args = Object.keys(_args).filter(x => !['execute','parse'].includes(x))
1372+
13411373
// Error on extraneous arguments
13421374
if (Object.keys(args).length > 0)
13431375
error(`Invalid transactWrite options: ${Object.keys(args).join(', ')}`)

0 commit comments

Comments
 (0)