11import { chunkArray } from './utils/array' ;
22import { dateNow } from './utils/date' ;
3- import { dbClient } from './utils/dynamoDbv3' ;
3+ import { AssumeRoleOpts , getDbClient } from './utils/dynamoDbv3' ;
44import {
55 BatchGetCommand ,
66 BatchWriteCommand ,
@@ -15,6 +15,7 @@ import {
1515} from '@aws-sdk/lib-dynamodb' ;
1616import { Paginator } from '@aws-sdk/types' ;
1717import { QueryCommandOutput } from '@aws-sdk/lib-dynamodb/dist-types/commands/QueryCommand' ;
18+ import { ReturnValue } from '@aws-sdk/client-dynamodb' ;
1819
1920const getVersionCondition = ( item : BaseObject ) : string => {
2021 if ( item . version ) {
@@ -65,7 +66,7 @@ type UpdateOpts = {
6566 ExpressionAttributeValues ?: Record < string , any > ;
6667 ConditionExpression ?: string ;
6768 UpdateExpression ?: string ;
68- ReturnValues ?: string ;
69+ ReturnValues ?: ReturnValue ;
6970 } ;
7071 skipVersionCondition ?: boolean ;
7172} ;
@@ -147,9 +148,21 @@ export default class BaseModel<T extends BaseObject> {
147148 return { items, lastEvaluatedKey } ;
148149 } ;
149150
150- get = async ( keyObj , { consistentRead = true , opts = { } } : { opts ?: GetOpts ; consistentRead ?: boolean } = { } ) : Promise < T | undefined > => {
151+ get = async (
152+ keyObj ,
153+ {
154+ consistentRead = true ,
155+ opts = { } ,
156+ role
157+ } : {
158+ opts ?: GetOpts ;
159+ consistentRead ?: boolean ;
160+ role ?: AssumeRoleOpts ;
161+ } = { }
162+ ) : Promise < T | undefined > => {
151163 const key = this . createKey ( keyObj ) ;
152164 try {
165+ const dbClient = await getDbClient ( role ) ;
153166 const { Item } = await dbClient . send (
154167 new GetCommand ( {
155168 TableName : this . tableName ,
@@ -165,7 +178,8 @@ export default class BaseModel<T extends BaseObject> {
165178 }
166179 } ;
167180
168- remove = async ( keyObj ) => {
181+ remove = async ( keyObj , { role } : { role ?: AssumeRoleOpts } = { } ) => {
182+ const dbClient = await getDbClient ( role ) ;
169183 await dbClient . send (
170184 new DeleteCommand ( {
171185 TableName : this . tableName ,
@@ -174,8 +188,9 @@ export default class BaseModel<T extends BaseObject> {
174188 ) ;
175189 } ;
176190
177- removeBatch = async ( keyObjs : any [ ] ) => {
191+ removeBatch = async ( keyObjs : any [ ] , { role } : { role ?: AssumeRoleOpts } = { } ) => {
178192 try {
193+ const dbClient = await getDbClient ( role ) ;
179194 const results = await Promise . all (
180195 chunkArray ( keyObjs , 25 ) . map ( ( objs ) =>
181196 dbClient . send (
@@ -202,7 +217,12 @@ export default class BaseModel<T extends BaseObject> {
202217 }
203218 } ;
204219
205- getBatch = async ( keyObjs : any [ ] , consistentRead = true , { opts = { } } = { } ) : Promise < T [ ] > => {
220+ getBatch = async (
221+ keyObjs : any [ ] ,
222+ consistentRead = true ,
223+ { opts = { } , role } : { opts ?: any ; role ?: AssumeRoleOpts } = { }
224+ ) : Promise < T [ ] > => {
225+ const dbClient = await getDbClient ( role ) ;
206226 const results = await Promise . all (
207227 chunkArray ( keyObjs , 100 ) . map ( ( pairs ) =>
208228 dbClient . send (
@@ -232,9 +252,17 @@ export default class BaseModel<T extends BaseObject> {
232252 {
233253 opts = { ExpressionAttributeNames : { } , ExpressionAttributeValues : { } } ,
234254 rangeOp = '=' ,
235- maxRequests = 7
236- } : { opts ?: QueryOpts ; rangeOp ?: string ; pageSize ?: number ; maxRequests ?: number } = { }
255+ maxRequests = 7 ,
256+ role
257+ } : {
258+ opts ?: QueryOpts ;
259+ rangeOp ?: string ;
260+ pageSize ?: number ;
261+ maxRequests ?: number ;
262+ role ?: AssumeRoleOpts ;
263+ } = { }
237264 ) : Promise < { items : T [ ] ; lastEvaluatedKey ?: Partial < T > } > => {
265+ const dbClient = await getDbClient ( role ) ;
238266 const ind = this . keys . globalIndexes ?. [ index ] ;
239267 if ( ! ind || ! ind . hashKey ) {
240268 throw new Error ( `index "${ index } " is not defined in the model` ) ;
@@ -276,9 +304,11 @@ export default class BaseModel<T extends BaseObject> {
276304 opts = { ExpressionAttributeNames : { } , ExpressionAttributeValues : { } } ,
277305 rangeOp = '=' ,
278306 consistentRead = true ,
279- maxRequests = 7
280- } : { opts ?: QueryOpts ; rangeOp ?: string ; consistentRead ?: boolean ; pageSize ?: number ; maxRequests ?: number } = { }
307+ maxRequests = 7 ,
308+ role
309+ } : { opts ?: QueryOpts ; rangeOp ?: string ; consistentRead ?: boolean ; pageSize ?: number ; maxRequests ?: number ; role ?: AssumeRoleOpts } = { }
281310 ) : Promise < { items : T [ ] ; lastEvaluatedKey ?: Partial < T > } > => {
311+ const dbClient = await getDbClient ( role ) ;
282312 const hashKey = < string > this . keys . hashKey ;
283313 if ( ! keyObj [ hashKey ] ) {
284314 throw new Error ( `hashKey ${ hashKey } was not found on keyObj` ) ;
@@ -314,7 +344,8 @@ export default class BaseModel<T extends BaseObject> {
314344 ) ;
315345 } ;
316346
317- all = async ( ) : Promise < T [ ] | undefined > => {
347+ all = async ( { role } : { role ?: AssumeRoleOpts } = { } ) : Promise < T [ ] | undefined > => {
348+ const dbClient = await getDbClient ( role ) ;
318349 const result = await this . getPaginatedResult ( paginateScan ( { client : dbClient } , { TableName : this . tableName } ) ) ;
319350 return result . items ;
320351 } ;
@@ -342,12 +373,18 @@ export default class BaseModel<T extends BaseObject> {
342373 } ;
343374 } ;
344375
345- save = async ( item : T , userId ?, conditionExpression ?: string ) : Promise < T > => {
376+ save = async (
377+ item : T ,
378+ userId ?,
379+ { conditionExpression, role } : { conditionExpression ?: string ; role ?: AssumeRoleOpts } = { }
380+ ) : Promise < T > => {
381+ const dbClient = await getDbClient ( role ) ;
346382 const params = this . prepareSave ( item , userId , conditionExpression ) ;
347383 return < T > ( await dbClient . send ( new PutCommand ( params ) ) ) . Attributes ;
348384 } ;
349385
350- saveBatch = async ( items : any [ ] , userId ?) => {
386+ saveBatch = async ( items : any [ ] , userId ?, { role } : { role ?: AssumeRoleOpts } = { } ) => {
387+ const dbClient = await getDbClient ( role ) ;
351388 return await Promise . all (
352389 chunkArray ( items , 25 ) . map ( ( itemBatch ) =>
353390 dbClient . send (
@@ -383,7 +420,8 @@ export default class BaseModel<T extends BaseObject> {
383420 } ;
384421 } ;
385422
386- update = async ( keyObj , opts = { } ) : Promise < T | undefined > => {
423+ update = async ( keyObj , { opts, role } : { opts ?: any ; role ?: AssumeRoleOpts } = { } ) : Promise < T | undefined > => {
424+ const dbClient = await getDbClient ( role ) ;
387425 const data = await dbClient . send ( new UpdateCommand ( this . prepareUpdate ( keyObj , opts ) ) ) ;
388426 return < T > data . Attributes ;
389427 } ;
@@ -399,7 +437,7 @@ export default class BaseModel<T extends BaseObject> {
399437 ExpressionAttributeValues : { } ,
400438 ConditionExpression : undefined ,
401439 UpdateExpression : undefined ,
402- ReturnValues : ' ALL_NEW' ,
440+ ReturnValues : ReturnValue . ALL_NEW ,
403441 ...overrideOpts
404442 } ;
405443 this . validateItemKeys ( item , 'updated' ) ;
@@ -443,7 +481,13 @@ export default class BaseModel<T extends BaseObject> {
443481 } ;
444482 } ;
445483
446- updateV2 = async ( item , changes = { } , updateOpts ?: UpdateOpts , userId ?: string ) : Promise < T | undefined > => {
484+ updateV2 = async (
485+ item ,
486+ changes = { } ,
487+ { role, ...updateOpts } : UpdateOpts & { role ?: AssumeRoleOpts } = { } ,
488+ userId ?: string
489+ ) : Promise < T | undefined > => {
490+ const dbClient = await getDbClient ( role ) ;
447491 const result = await dbClient . send ( new UpdateCommand ( this . prepareUpdateV2 ( item , changes , updateOpts , userId ) ) ) ;
448492 return < T > result ?. Attributes ;
449493 } ;
0 commit comments