module import * as DynamoDB from 'aws-sdk/clients/dynamodb' ; import { metadataForModel } from '../../decorator/metadata/metadata-for-model.function' ; import { randomExponentialBackoffTimer } from '../../helper/random-exponential-backoff-timer.generator' ; import { createToKeyFn , fromDb } from '../../mapper/mapper' ; import { Attributes } from '../../mapper/type/attribute.type' ; import { ModelConstructor } from '../../model/model-constructor' ; import { DynamoDbWrapper } from '../dynamo-db-wrapper' ; import { getTableName } from '../get-table-name.function' ; import { BatchGetFullResponse } from './batch-get-full.response' ; import { batchGetItemsFetchAll } from './batch-get-utils' ; import { BATCH_GET_DEFAULT_TIME_SLOT , BATCH_GET_MAX_REQUEST_ITEM_COUNT } from './batch-get.const' ; import { BatchGetResponse } from './batch-get.response' ; /**
 * Request class for the BatchGetItem operation. Read multiple items from one or more tables.
 */ export class BatchGetRequest { get dynamoDB ( ) : DynamoDB { return this . dynamoDBWrapper . dynamoDB ; } readonly params : DynamoDB . BatchGetItemInput ; private readonly dynamoDBWrapper : DynamoDbWrapper ; private readonly tables : Map < string , ModelConstructor < any > > = new Map ( ) ; private itemCounter = 0 ; constructor ( dynamoDB ? ) { this . dynamoDBWrapper = new DynamoDbWrapper ( dynamoDB ) ; this . params = { RequestItems : { } , } ; } /**
     * return ConsumedCapacity of the corresponding tables in the response
     */ returnConsumedCapacity ( level ) { this . params . ReturnConsumedCapacity = level ; return this ; } param modelClazz param keys param consistentRead forModel < T > ( modelClazz , keys , consistentRead = false ) { const metadata = metadataForModel ( modelClazz ) ; if ( ! metadata . modelOptions ) { throw new Error ( 'given ModelConstructor has no @Model decorator' ) ; } const tableName = getTableName ( metadata ) ; if ( this . tables . has ( tableName ) ) { throw new Error ( 'table name already exists, please provide all the keys for the same table at once' ) ; } this . tables . set ( tableName , modelClazz ) ; if ( this . itemCounter + keys . length > BATCH_GET_MAX_REQUEST_ITEM_COUNT ) { throw new Error ( `you can request at max ${ BATCH_GET_MAX_REQUEST_ITEM_COUNT } items per request` ) ; } this . params . RequestItems [ tableName ] = { Keys : keys . map ( createToKeyFn ( modelClazz ) ) , ConsistentRead : consistentRead , } ; this . itemCounter += keys . length ; return this ; } param backoffTimer param throttleTimeSlot execNoMap ( backoffTimer = randomExponentialBackoffTimer , throttleTimeSlot = BATCH_GET_DEFAULT_TIME_SLOT ) { return this . fetch ( backoffTimer , throttleTimeSlot ) ; } param backoffTimer param throttleTimeSlot execFullResponse ( backoffTimer = randomExponentialBackoffTimer , throttleTimeSlot = BATCH_GET_DEFAULT_TIME_SLOT ) { return this . fetch ( backoffTimer , throttleTimeSlot ) . then ( this . mapResponse ) ; } param backoffTimer param throttleTimeSlot exec ( backoffTimer = randomExponentialBackoffTimer , throttleTimeSlot = BATCH_GET_DEFAULT_TIME_SLOT ) { return this . fetch ( backoffTimer , throttleTimeSlot ) . then ( this . mapResponse ) . then ( ( r ) => r . Responses ) ; } private fetch ( backoffTimer = randomExponentialBackoffTimer , throttleTimeSlot = BATCH_GET_DEFAULT_TIME_SLOT ) { return batchGetItemsFetchAll ( this . dynamoDBWrapper , { ... this . params } , backoffTimer ( ) , throttleTimeSlot ) ; } private mapResponse = ( response ) : BatchGetFullResponse => { let Responses = { } ; if ( response . Responses && Object . keys ( response . Responses ) . length ) { Responses = Object . entries ( response . Responses ) . reduce ( ( u , [ key , val ] ) => { u [ key ] = val . map ( ( attributes ) => fromDb ( < Attributes > attributes , this . tables . get ( key ) ) ) ; return u ; } , { } ) ; } return { ConsumedCapacity : response . ConsumedCapacity , UnprocessedKeys : response . UnprocessedKeys , Responses , } ; } ; }