Skip to content

Commit

Permalink
fix(db-dynamodb): scan result next method provides exact interface as…
Browse files Browse the repository at this point in the history
… scan (#3470)
  • Loading branch information
brunozoric committed Aug 14, 2023
1 parent 81db04b commit b1ece66
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions packages/db-dynamodb/src/utils/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,39 @@ export interface ScanResponse<T> {
error: any;
}

const convertResult = <T>(result: any): ScanResponse<T> => {
interface DdbScanResult<T> {
Items: T[];
Count: number;
ScannedCount: number;
LastEvaluatedKey?: DocumentClient.Key;
next?: () => Promise<DdbScanResult<T>>;
error?: any;
$response?: {
requestId: string;
};
}

type NextCb<T> = () => Promise<ScanResponse<T>>;

const createNext = <T>(result: DdbScanResult<T>): NextCb<T> | undefined => {
if (!result?.LastEvaluatedKey || !result.next) {
return undefined;
}
return async () => {
const response = await result!.next!();
return convertResult(response);
};
};

const convertResult = <T>(result: DdbScanResult<T>): ScanResponse<T> => {
return {
items: result.Items as T[],
items: result.Items,
count: result.Count,
scannedCount: result.ScannedCount,
lastEvaluatedKey: result.LastEvaluatedKey,
next: result.LastEvaluatedKey ? result.next : undefined,
lastEvaluatedKey: result.LastEvaluatedKey || undefined,
next: createNext<T>(result),
error: result.error,
requestId: result.$response.requestId
requestId: result.$response?.requestId || ""
};
};

Expand Down Expand Up @@ -56,7 +80,7 @@ export const scanWithCallback = async <T>(
await callback(result);

while (result.next) {
result = convertResult(await result.next());
result = await result.next();
await callback(result);
if (!result.next) {
return;
Expand Down

0 comments on commit b1ece66

Please sign in to comment.