@@ -6,6 +6,44 @@ import { Rider } from '../models/rider';
66import { Location } from '../models/location' ;
77import { Driver } from '../models/driver' ;
88
9+ const MAX_BATCH_GET_ITEMS = 100 ;
10+
11+ // Internal helper: perform Dynamoose batchGet in chunks of MAX_BATCH_GET_ITEMS
12+ async function batchGetAllItems < T extends Item > (
13+ model : ModelType < T > ,
14+ keys : ObjectType [ ]
15+ ) : Promise < T [ ] > {
16+ if ( ! keys . length ) return [ ] ;
17+
18+ const chunks : ObjectType [ ] [ ] = [ ] ;
19+ for ( let i = 0 ; i < keys . length ; i += MAX_BATCH_GET_ITEMS ) {
20+ chunks . push ( keys . slice ( i , i + MAX_BATCH_GET_ITEMS ) ) ;
21+ }
22+
23+ const chunkResults = await Promise . all (
24+ chunks . map (
25+ ( chunk ) =>
26+ new Promise < T [ ] > ( ( resolve , reject ) => {
27+ model . batchGet ( chunk , ( err : any , data : any ) => {
28+ if ( err ) {
29+ reject ( err ) ;
30+ } else if ( ! data ) {
31+ resolve ( [ ] ) ;
32+ } else if ( Array . isArray ( data ) ) {
33+ resolve ( data as T [ ] ) ;
34+ } else if ( typeof ( data as any ) [ Symbol . iterator ] === 'function' ) {
35+ resolve ( Array . from ( data as Iterable < T > ) ) ;
36+ } else {
37+ resolve ( [ ] ) ;
38+ }
39+ } ) ;
40+ } )
41+ )
42+ ) ;
43+
44+ return chunkResults . flat ( ) ;
45+ }
46+
947// Helper: batch fetch locations, riders, drivers and return maps keyed by id
1048async function buildEntityMapsFromSets (
1149 locationIds : Set < string > ,
@@ -14,13 +52,22 @@ async function buildEntityMapsFromSets(
1452) {
1553 const [ locationsArr , ridersArr , driversArr ] = await Promise . all ( [
1654 locationIds . size
17- ? Location . batchGet ( Array . from ( locationIds ) . map ( ( id ) => ( { id } ) ) )
55+ ? batchGetAllItems (
56+ Location ,
57+ Array . from ( locationIds ) . map ( ( id ) => ( { id } ) )
58+ )
1859 : Promise . resolve ( [ ] ) ,
1960 riderIds . size
20- ? Rider . batchGet ( Array . from ( riderIds ) . map ( ( id ) => ( { id } ) ) )
61+ ? batchGetAllItems (
62+ Rider ,
63+ Array . from ( riderIds ) . map ( ( id ) => ( { id } ) )
64+ )
2165 : Promise . resolve ( [ ] ) ,
2266 driverIds . size
23- ? Driver . batchGet ( Array . from ( driverIds ) . map ( ( id ) => ( { id } ) ) )
67+ ? batchGetAllItems (
68+ Driver ,
69+ Array . from ( driverIds ) . map ( ( id ) => ( { id } ) )
70+ )
2471 : Promise . resolve ( [ ] ) ,
2572 ] ) ;
2673
@@ -208,19 +255,43 @@ export function batchGet(
208255) {
209256 if ( ! keys . length ) {
210257 res . send ( { data : [ ] } ) ;
211- } else {
212- model . batchGet ( keys , async ( err , data ) => {
213- if ( err ) {
214- res . status ( err . statusCode || 500 ) . send ( { err : err . message } ) ;
215- } else if ( ! data ) {
216- res . status ( 400 ) . send ( { err : `items not found in ${ table } ` } ) ;
217- } else if ( callback ) {
218- callback ( ( await data . populate ( ) ) . toJSON ( ) ) ;
258+ return ;
259+ }
260+
261+ ( async ( ) => {
262+ try {
263+ // Fetch all items in chunks to respect DynamoDB's 100-item batchGet limit
264+ const items = await batchGetAllItems ( model , keys ) ;
265+
266+ // Populate each item individually (since we no longer rely on a single DocumentArray)
267+ const populatedJson : any [ ] = [ ] ;
268+ for ( const item of items as any [ ] ) {
269+ if ( item && typeof item . populate === 'function' ) {
270+ const populated = await item . populate ( ) ;
271+ populatedJson . push (
272+ typeof populated . toJSON === 'function'
273+ ? populated . toJSON ( )
274+ : populated
275+ ) ;
276+ } else if ( item && typeof item . toJSON === 'function' ) {
277+ populatedJson . push ( item . toJSON ( ) ) ;
278+ } else {
279+ populatedJson . push ( item ) ;
280+ }
281+ }
282+
283+ if ( callback ) {
284+ callback ( populatedJson ) ;
219285 } else {
220- res . status ( 200 ) . send ( { data : ( await data . populate ( ) ) . toJSON ( ) } ) ;
286+ res . status ( 200 ) . send ( { data : populatedJson } ) ;
221287 }
222- } ) ;
223- }
288+ } catch ( err : any ) {
289+ console . error ( 'Error in batchGet:' , err ) ;
290+ const status = err ?. statusCode || 500 ;
291+ const message = err ?. message || `Error fetching items from ${ table } ` ;
292+ res . status ( status ) . send ( { err : message } ) ;
293+ }
294+ } ) ( ) ;
224295}
225296
226297export function getAll (
0 commit comments