module import * as DynamoDB from 'aws-sdk/clients/dynamodb' ; import { promiseDelay } from '../../helper/promise-delay.function' ; import { DynamoDbWrapper } from '../dynamo-db-wrapper' ; param dynamoDBWrapper param params param backoffTimer param throttleTimeSlot hidden export function batchGetItemsFetchAll ( dynamoDBWrapper , params , backoffTimer , throttleTimeSlot ) { return dynamoDBWrapper . batchGetItems ( params ) . then ( ( response ) => { if ( hasUnprocessedKeys ( response ) ) { return ( Promise . resolve ( response . UnprocessedKeys ) . then ( promiseDelay ( backoffTimer . next ( ) . value * throttleTimeSlot ) ) . then ( ( UnprocessedKeys ) => { const nextParams = { ... params , RequestItems : UnprocessedKeys } ; return batchGetItemsFetchAll ( dynamoDBWrapper , nextParams , backoffTimer , throttleTimeSlot ) ; } ) . then ( combineBatchGetResponses ( response ) ) ) ; } return response ; } ) ; } hidden export type BatchGetItemOutputWithUnprocessedKeys = DynamoDB . BatchGetItemOutput & { UnprocessedKeys : DynamoDB . BatchGetRequestMap ; } ; hidden export function hasUnprocessedKeys ( response ) { if ( ! response . UnprocessedKeys ) { return false ; } return Object . values ( response . UnprocessedKeys ) . some ( ( t ) => ! ! t && t . Keys && t . Keys . length > 0 ) ; } hidden export function combineBatchGetResponses ( response1 ) { return ( response2 ) : DynamoDB . BatchGetItemOutput => { const tableNames = Object . keys ( response1 . Responses || { } ) ; Object . keys ( response2 . Responses || { } ) . filter ( ( tn ) => ! tableNames . includes ( tn ) ) . forEach ( ( tn ) => tableNames . push ( tn ) ) ; const Responses = tableNames . reduce ( ( u , tableName ) => ( { ... u , [ tableName ] : [ ... ( ( response1 . Responses && response1 . Responses [ tableName ] ) || [ ] ) , ... ( ( response2 . Responses && response2 . Responses [ tableName ] ) || [ ] ) , ] , } ) , { } ) ; return { ... response2 , Responses , } ; } ; }