Skip to content

Commit bb905e5

Browse files
committed
transactions wip
1 parent fd43186 commit bb905e5

File tree

2 files changed

+88
-14
lines changed

2 files changed

+88
-14
lines changed

src/classes/Entity.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ class Entity<
326326
getTransaction(
327327
item: Partial<Schema> = {},
328328
options: { attributes?: ProjectionAttributes } = {}
329-
): DocumentClient.TransactGetItem {
329+
): { Entity: Entity<Schema> } & DocumentClient.TransactGetItem {
330330

331331
// Destructure options to check for extraneous arguments
332332
const {
@@ -339,10 +339,13 @@ class Entity<
339339
error(`Invalid get transaction options: ${Object.keys(args).join(', ')}`)
340340

341341
// Generate the get parameters
342-
let payload = this.getParams(item, options)
342+
let payload = this.getParams(item, options)
343343

344344
// Return in transaction format
345-
return { Get: payload }
345+
return {
346+
Entity: this,
347+
Get: payload
348+
}
346349
}
347350

348351

src/classes/Table.ts

Lines changed: 82 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ export interface TableConstructor {
4040
export type DynamoDBTypes = 'string' | 'boolean' | 'number' | 'list' | 'map' | 'binary' | 'set'
4141
export type DynamoDBSetTypes = 'string' | 'number' | 'binary'
4242

43+
export interface executeParse {
44+
execute?: boolean
45+
parse?: boolean
46+
}
47+
4348
export interface TableAttributeConfig {
4449
type: DynamoDBTypes,
4550
setType?: DynamoDBSetTypes
@@ -118,12 +123,25 @@ interface batchWriteOptions {
118123
parse?: boolean
119124
}
120125

121-
interface transactWriteOptions {
126+
interface transactGetParamsOptions {
127+
capacity?: DocumentClient.ReturnConsumedCapacity
128+
}
129+
130+
type transactGetOptions = transactGetParamsOptions & executeParse
131+
132+
interface transactWriteParamsOptions {
122133
capacity?: DocumentClient.ReturnConsumedCapacity
123134
metrics?: DocumentClient.ReturnItemCollectionMetrics
124135
token?: string
125136
}
126137

138+
type transactWriteOptions = transactWriteParamsOptions & executeParse
139+
140+
interface transactGetParamsMeta {
141+
Entities: (any | undefined)[]
142+
payload: DocumentClient.TransactGetItemsInput
143+
}
144+
127145
// Declare Table class
128146
class Table {
129147

@@ -858,6 +876,7 @@ class Table {
858876
return meta ? { payload, EntityProjections, TableProjections } : payload
859877
} // end query
860878

879+
861880
// BatchGet Items
862881
async batchGet(
863882
items: any,
@@ -888,7 +907,7 @@ class Table {
888907
} else {
889908
return payload
890909
} // end-if
891-
} // end put
910+
} // end batchGet
892911

893912

894913

@@ -1083,7 +1102,7 @@ class Table {
10831102

10841103

10851104

1086-
// BatchGet Items
1105+
// BatchWrite Items
10871106
async batchWrite(
10881107
items: any,
10891108
options: batchWriteOptions = {},
@@ -1201,6 +1220,48 @@ class Table {
12011220
} // batchWriteParams
12021221

12031222

1223+
1224+
/**
1225+
* Performs a transactGet operation
1226+
* @param {object} items - An array of objects generated from getTransaction entity calls.
1227+
* @param {object} [options] - Additional transactGet options
1228+
*
1229+
*/
1230+
async transactGet(
1231+
items: ({ Entity?: any } & DocumentClient.TransactGetItem)[] = [],
1232+
options: transactGetOptions = {},
1233+
params: Partial<DocumentClient.TransactGetItemsInput> = {}
1234+
) {
1235+
// Generate the payload with meta information
1236+
const { payload, Entities } = this.transactGetParams(items,options,true) as transactGetParamsMeta
1237+
1238+
// If auto execute enabled
1239+
if (options.execute || (this.autoExecute && options.execute !== false)) {
1240+
const result = await this.DocumentClient!.transactGet(payload).promise()
1241+
// If auto parse enable
1242+
if (options.parse || (this.autoParse && options.parse !== false)) {
1243+
//return this.parseBatchGetResponse(result)
1244+
//return result as DocumentClient.TransactGetItemsOutput
1245+
return Object.assign(
1246+
result,
1247+
result.Responses ? {
1248+
Responses: result.Responses!.map((res,i) => {
1249+
return { Item: Entities[i].parse ? Entities[i].parse(res.Item) : res.Item }
1250+
})
1251+
} : null
1252+
) as DocumentClient.TransactGetItemsOutput
1253+
1254+
} else {
1255+
return result as DocumentClient.TransactGetItemsOutput
1256+
}
1257+
} else {
1258+
return payload as DocumentClient.TransactGetItemsInput
1259+
} // end-if
1260+
} // end batchGet
1261+
1262+
1263+
1264+
12041265
/**
12051266
* Generate parameters for a transactGet operation
12061267
* @param {object} items - An array of objects generated from getTransaction entity calls.
@@ -1209,16 +1270,20 @@ class Table {
12091270
* Creates a TransactGetItems object: https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_TransactGetItems.html
12101271
*/
12111272
transactGetParams(
1212-
items: DocumentClient.TransactGetItemList = [],
1213-
options: { capacity?: DocumentClient.ReturnConsumedCapacity } = {}
1214-
): DocumentClient.TransactGetItemsInput {
1273+
items: ({ Entity?: any } & DocumentClient.TransactGetItem)[] = [],
1274+
options: transactGetParamsOptions = {},
1275+
meta: boolean = false
1276+
) {
12151277

12161278
// Extract valid options
12171279
const {
12181280
capacity, // ReturnConsumedCapacity (none, total, or indexes)
1219-
...args
1281+
..._args
12201282
} = options
12211283

1284+
// Remove other valid options from options
1285+
const args = Object.keys(_args).filter(x => !['execute','parse'].includes(x))
1286+
12221287
// Error on extraneous arguments
12231288
if (Object.keys(args).length > 0)
12241289
error(`Invalid transactGet options: ${Object.keys(args).join(', ')}`)
@@ -1228,20 +1293,26 @@ class Table {
12281293
&& (typeof capacity !== 'string' || !['NONE','TOTAL','INDEXES'].includes(capacity.toUpperCase())))
12291294
error(`'capacity' must be one of 'NONE','TOTAL', OR 'INDEXES'`)
12301295

1296+
let Entities: (any | undefined)[] = []
1297+
12311298
// Generate the payload
12321299
const payload = Object.assign(
12331300
{
12341301
// Loop through items and verify transaction objects
1235-
TransactItems: items.map(item => {
1236-
if (!('Get' in item) || Object.keys(item).length > 1)
1302+
TransactItems: items.map(item => {
1303+
let { Entity, ..._item } = item
1304+
Entities.push(Entity)
1305+
if (!('Get' in _item) || Object.keys(_item).length > 1)
12371306
error(`Invalid transaction item. Use the 'getTransaction' method on an entity.`)
12381307
return item
12391308
})
12401309
},
12411310
capacity ? { ReturnConsumedCapacity: capacity.toUpperCase() } : null
12421311
)
12431312

1244-
return payload
1313+
return meta ?
1314+
{ Entities, payload } as transactGetParamsMeta
1315+
: payload as DocumentClient.TransactGetItemsInput
12451316
} // end transactGetParams
12461317

12471318

@@ -1256,7 +1327,7 @@ class Table {
12561327
*/
12571328
transactWriteParams(
12581329
items: DocumentClient.TransactGetItemList = [],
1259-
options: transactWriteOptions = {}
1330+
options: transactWriteParamsOptions = {}
12601331
): DocumentClient.TransactGetItemsInput {
12611332

12621333
// Extract valid options

0 commit comments

Comments
 (0)