Files
speckle-server/packages/server/modules/core/helpers/scanTable.ts
T
Kristaps Fabians Geikins 4b06f42db7 chore(server): run TS files directly (no compilation) (#5134)
* sort of works

* type fixes

* added option to run old way too
2025-07-23 11:20:40 +02:00

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)
}