module import * as DynamoDB from 'aws-sdk/clients/dynamodb' ; import { randomExponentialBackoffTimer } from '../../helper/random-exponential-backoff-timer.generator' ; import { createToKeyFn , toDb } from '../../mapper/mapper' ; import { ModelConstructor } from '../../model/model-constructor' ; import { DynamoDbWrapper } from '../dynamo-db-wrapper' ; import { getTableName } from '../get-table-name.function' ; import { batchWriteItemsWriteAll } from './batch-write-utils' ; import { BATCH_WRITE_DEFAULT_TIME_SLOT , BATCH_WRITE_MAX_REQUEST_ITEM_COUNT } from './batch-write.const' ; /**
 * Request class for the BatchWriteItem operation. Put or delete multiple items in one or more table.
 */ export class BatchWriteRequest { get dynamoDB ( ) : DynamoDB { return this . dynamoDBWrapper . dynamoDB ; } readonly params : DynamoDB . BatchWriteItemInput ; private readonly dynamoDBWrapper : DynamoDbWrapper ; private itemCount = 0 ; constructor ( dynamoDB ? ) { this . dynamoDBWrapper = new DynamoDbWrapper ( dynamoDB ) ; this . params = { RequestItems : { } , } ; } /**
     * return ConsumedCapacity of the corresponding tables in the response
     */ returnConsumedCapacity ( value ) { this . params . ReturnConsumedCapacity = value ; return this ; } /**
     * return item collection metrics.
     */ returnItemCollectionMetrics ( value ) { this . params . ReturnItemCollectionMetrics = value ; return this ; } param modelClazz param keys delete < T > ( modelClazz , keys ) { this . requestItems ( modelClazz , keys . map ( this . createDeleteRequest ( modelClazz ) ) ) ; return this ; } param modelClazz param items put < T > ( modelClazz , items ) { this . requestItems ( modelClazz , items . map ( this . createPutRequest ( modelClazz ) ) ) ; return this ; } param backoffTimer param throttleTimeSlot exec ( backoffTimer = randomExponentialBackoffTimer , throttleTimeSlot = BATCH_WRITE_DEFAULT_TIME_SLOT ) { return this . write ( backoffTimer , throttleTimeSlot ) . then ( ( ) => { return ; } ) ; } param backoffTimer param throttleTimeSlot execFullResponse ( backoffTimer = randomExponentialBackoffTimer , throttleTimeSlot = BATCH_WRITE_DEFAULT_TIME_SLOT ) { return this . write ( backoffTimer , throttleTimeSlot ) ; } private requestItems ( modelClazz , items ) { if ( this . itemCount + items . length > BATCH_WRITE_MAX_REQUEST_ITEM_COUNT ) { throw new Error ( `batch write takes at max ${ BATCH_WRITE_MAX_REQUEST_ITEM_COUNT } items` ) ; } const tableName = getTableName ( modelClazz ) ; this . params . RequestItems [ tableName ] = this . params . RequestItems [ tableName ] || [ ] ; this . params . RequestItems [ tableName ] . push ( ... items ) ; this . itemCount += items . length ; } private write ( backoffTimer , throttleTimeSlot ) { return batchWriteItemsWriteAll ( this . dynamoDBWrapper , { ... this . params } , backoffTimer ( ) , throttleTimeSlot ) ; } private createDeleteRequest = < T > ( modelClazz ) => { const toKey = createToKeyFn ( modelClazz ) ; return ( item ) : DynamoDB . WriteRequest => ( { DeleteRequest : { Key : toKey ( item ) } } ) ; } ; private createPutRequest = < T > ( modelClazz ) => { return ( item ) : DynamoDB . WriteRequest => ( { PutRequest : { Item : toDb ( item , modelClazz ) } } ) ; } ; }