4b06f42db7
* sort of works * type fixes * added option to run old way too
27 lines
705 B
TypeScript
27 lines
705 B
TypeScript
import { LogicError } from '@/modules/shared/errors'
|
|
import type { Knex } from 'knex'
|
|
|
|
export const scanTableFactory = <TRecord extends object>({
|
|
db
|
|
}: {
|
|
db: Knex<TRecord>
|
|
}) =>
|
|
async function* (
|
|
{ tableName, batchSize }: { tableName: string; batchSize: number },
|
|
options?: { failsafeLimitMultiplier?: number }
|
|
) {
|
|
let offset = 0
|
|
const failsafeLimit = batchSize * (options?.failsafeLimitMultiplier ?? 1000)
|
|
|
|
let rows = []
|
|
do {
|
|
rows = await db<TRecord>(tableName).limit(batchSize).offset(offset)
|
|
yield rows
|
|
offset += batchSize
|
|
|
|
if (offset > failsafeLimit) {
|
|
throw new LogicError('Never ending loop')
|
|
}
|
|
} while (rows.length > 0)
|
|
}
|