11import { DynamoDB } from 'aws-sdk'
2- import { Observable , of } from 'rxjs'
3- import { delay , map , mergeMap } from 'rxjs/operators'
2+ import { Observable } from 'rxjs'
3+ import { map } from 'rxjs/operators'
44import { metadataForClass } from '../../decorator/metadata/metadata-helper'
55import { randomExponentialBackoffTimer } from '../../helper'
66import { createToKeyFn , fromDb } from '../../mapper'
@@ -13,6 +13,7 @@ import { REGEX_TABLE_NAME } from '../request/regex'
1313import { SessionValidityEnsurer } from '../session-validity-ensurer.type'
1414import { TableNameResolver } from '../table-name-resolver.type'
1515import { BatchGetFullResponse } from './batch-get-full.response'
16+ import { batchGetItemsFetchAll } from './batch-get-utils'
1617import { BatchGetResponse } from './batch-get.response'
1718
1819const MAX_REQUEST_ITEM_COUNT = 100
@@ -34,6 +35,10 @@ export class BatchGetRequest {
3435 }
3536 }
3637
38+ private fetch ( backoffTimer = randomExponentialBackoffTimer , throttleTimeSlot = DEFAULT_TIME_SLOT ) {
39+ return batchGetItemsFetchAll ( this . dynamoRx , { ...this . params } , backoffTimer ( ) , throttleTimeSlot )
40+ }
41+
3742 /**
3843 * @param {ModelConstructor<T> } modelClazz
3944 * @param {Partial<T>[] } keys a partial of T that contains Partition key and SortKey (if necessary). Throws if missing.
@@ -49,7 +54,7 @@ export class BatchGetRequest {
4954
5055 // check if modelClazz is really an @Model () decorated class
5156 const metadata = metadataForClass ( modelClazz )
52- if ( metadata . modelOptions === null ) { throw new Error ( 'given ModelConstructor has no @Model decorator' ) }
57+ if ( ! metadata . modelOptions ) { throw new Error ( 'given ModelConstructor has no @Model decorator' ) }
5358
5459 // check if keys to add do not exceed max count
5560 if ( this . itemCounter + keys . length > MAX_REQUEST_ITEM_COUNT ) { throw new Error ( `you can request at max ${ MAX_REQUEST_ITEM_COUNT } items per request` ) }
@@ -75,19 +80,6 @@ export class BatchGetRequest {
7580 return tableName
7681 }
7782
78- /**
79- * combines a first with a second response. ConsumedCapacity is always from the latter.
80- * @param response1
81- */
82- private combineResponses = ( response1 : DynamoDB . BatchGetItemOutput ) => ( response2 : DynamoDB . BatchGetItemOutput ) : DynamoDB . BatchGetItemOutput => {
83- const Responses = Object . entries ( response1 . Responses || { } )
84- . reduce ( ( u , [ tableName , items ] ) => ( { [ tableName ] : [ ...items , ...( response2 . Responses ? response2 . Responses [ tableName ] : [ ] ) ] } ) , { } )
85- return {
86- Responses,
87- ConsumedCapacity : response2 . ConsumedCapacity ,
88- }
89- }
90-
9183 private mapResponse = ( response : DynamoDB . BatchGetItemOutput ) : BatchGetFullResponse => {
9284 let Responses : BatchGetResponse = { }
9385
@@ -105,39 +97,19 @@ export class BatchGetRequest {
10597 }
10698 }
10799
108- private fetch ( params : DynamoDB . BatchGetItemInput , backoffTimer : IterableIterator < number > , throttleTimeSlot : number ) : Observable < DynamoDB . BatchGetItemOutput > {
109- return this . dynamoRx . batchGetItems ( params )
110- . pipe (
111- mergeMap ( response => {
112- if ( ! ! response . UnprocessedKeys && Object . entries ( response . UnprocessedKeys ) . some ( t => ! ! t && t . length > 0 ) ) {
113- return of ( response . UnprocessedKeys )
114- . pipe (
115- delay ( backoffTimer . next ( ) . value * throttleTimeSlot ) ,
116- mergeMap ( ( UnprocessedKeys : DynamoDB . BatchGetRequestMap ) => {
117- const nextParams = { ...params , RequestItems : UnprocessedKeys }
118- return this . fetch ( nextParams , backoffTimer , throttleTimeSlot )
119- } ) ,
120- map ( this . combineResponses ( response ) ) ,
121- )
122- }
123- return of ( response )
124- } ) ,
125- )
126- }
127-
128100 execNoMap ( backoffTimer = randomExponentialBackoffTimer , throttleTimeSlot = DEFAULT_TIME_SLOT ) : Observable < DynamoDB . BatchGetItemOutput > {
129- return this . fetch ( { ... this . params } , backoffTimer ( ) , throttleTimeSlot )
101+ return this . fetch ( backoffTimer , throttleTimeSlot )
130102 }
131103
132104 execFullResponse ( backoffTimer = randomExponentialBackoffTimer , throttleTimeSlot = DEFAULT_TIME_SLOT ) : Observable < BatchGetFullResponse > {
133- return this . fetch ( { ... this . params } , backoffTimer ( ) , throttleTimeSlot )
105+ return this . fetch ( backoffTimer , throttleTimeSlot )
134106 . pipe (
135107 map ( this . mapResponse ) ,
136108 )
137109 }
138110
139111 exec ( backoffTimer = randomExponentialBackoffTimer , throttleTimeSlot = DEFAULT_TIME_SLOT ) : Observable < BatchGetResponse > {
140- return this . fetch ( { ... this . params } , backoffTimer ( ) , throttleTimeSlot )
112+ return this . fetch ( backoffTimer , throttleTimeSlot )
141113 . pipe (
142114 map ( this . mapResponse ) ,
143115 map ( r => r . Responses ) ,
0 commit comments