Files
accounted/lib/supabase/fetch-all.ts
T
Jakob Wennberg fce6faff2c fix(api): stabilize report pagination + declare real { data, meta } envelope on v1 single/write endpoints (#811)
* fix(reports): stabilize fetchAllRows paging to stop doubled/dropped balances (#790, #791)

PostgREST `.range()` paging is only correct when the underlying query has a
stable TOTAL order. Several aggregating report queries (general ledger, trial
balance, grundbok, supplier/AR ledgers, etc.) paginated without `.order()`, so
on datasets larger than one 1000-row page Postgres could return rows in a
different order between requests — silently DUPLICATING or SKIPPING rows on a
page boundary and doubling or dropping financial totals.

- fetch-all.ts: document the ordering invariant and add an optional
  `dedupeBy` defense-in-depth that drops cross-page duplicates and warns when
  it fires (surfaces a missing `.order()` in logs instead of corrupting money).
- Add a stable `.order()` (line PK or account_number) to every paginated query
  in lib/reports/ and the account-balances route; pass `dedupeBy` on the
  money-aggregating line queries.
- Add fetch-all unit tests and update report test fixtures to carry row ids.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(api): declare the real { data, meta } envelope on v1 single/write/204 endpoints (#794)

The OpenAPI generator derives each endpoint's documented body purely from its
registered `response.success` Zod schema, and that schema is never validated at
runtime — so a route could advertise a shape its handler never sends. #802
fixed this for list endpoints; the same drift was latent on single-resource and
write endpoints, which declared the bare resource schema instead of the
`{ data, meta }` envelope the handlers actually return.

- registry.ts: extend `ResponseMetaSchema` with the optional `audit` block and
  `partial_expansions` list that writes/expansions emit; add the `NoBodyResponse`
  sentinel so 204 DELETE handlers document a bare 204 instead of a phantom 200.
- Wrap every single/write endpoint's `response.success` in `dataEnvelope(...)`
  (or `NoBodyResponse` for 204s) across the v1 routes.
- Add a response-envelope contract test that fails CI if any JSON endpoint
  forgets to wrap its schema, with binary downloads and 204s as the only
  exemptions.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(reports): extend paging dedupeBy to rc-basis-gaps and opening-balances

Address PR review: these two money-aggregating line queries already had the
stable `.order('id')` (so paging was correct) but didn't carry `id` in the
select, so they couldn't use the `dedupeBy` defense-in-depth that general-ledger
and trial-balance got. Select `id` and pass `dedupeBy: r => r.id` so the whole
report layer applies the ordering invariant consistently.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-28 13:42:50 +02:00

105 lines
3.6 KiB
TypeScript

import { createLogger } from '@/lib/logger'
const log = createLogger('fetch-all')
const PAGE_SIZE = 1000
export interface FetchAllRowsOptions<T> {
/**
* Stable de-duplication key. When supplied AND more than one page was
* fetched, rows are de-duplicated by this key after all pages are collected,
* and a warn is logged if any duplicates were dropped.
*
* This is a safety net, NOT the fix: PostgREST `.range()` paging is only
* correct when the underlying query has a stable TOTAL order (see the
* ordering invariant below). If a duplicate is ever observed here it means a
* caller's query is missing that `.order()` — the warn surfaces the
* regression in logs instead of letting it silently double financial totals.
* Note this only catches *duplicates*; *skipped* rows can only be prevented
* by ordering on a unique column at the call site.
*/
dedupeBy?: (row: T) => string | number
}
/**
* Fetches all rows from a Supabase query by paginating through results.
* Overcomes PostgREST's default 1000-row limit.
*
* **Ordering invariant:** any query that can return more than `PAGE_SIZE` rows
* MUST `.order()` on a unique column (e.g. the table's `id` PK). Postgres
* returns rows in an undefined order that can differ between the two `.range()`
* requests, so without a stable total order, rows on a page boundary are
* silently DUPLICATED and/or SKIPPED across pages. For aggregating reports
* (general ledger, trial balance, grundbok) that means doubled or missing
* balances. Order is purely for paging stability — callers that need a
* different display order should re-sort after fetching.
*
* The callback receives `{ from, to }` range values — append `.range(from, to)`
* to your query builder, AFTER a stable `.order()`:
*
* ```ts
* const accounts = await fetchAllRows(({ from, to }) =>
* supabase
* .from('chart_of_accounts')
* .select('account_number, account_name')
* .eq('company_id', companyId)
* .order('account_number', { ascending: true }) // stable total order
* .range(from, to)
* )
* ```
*
* Pass `{ dedupeBy }` as defense-in-depth for queries where a missing/regressed
* order would corrupt money:
*
* ```ts
* const lines = await fetchAllRows(
* ({ from, to }) => q.order('id').range(from, to),
* { dedupeBy: (r) => r.id },
* )
* ```
*/
export async function fetchAllRows<T>(
queryFn: (range: { from: number; to: number }) => PromiseLike<{
data: T[] | null
error: { message: string } | null
}>,
options?: FetchAllRowsOptions<T>
): Promise<T[]> {
const allRows: T[] = []
let from = 0
let pages = 0
while (true) {
const { data, error } = await queryFn({ from, to: from + PAGE_SIZE - 1 })
if (error) throw new Error(error.message)
if (!data || data.length === 0) break
allRows.push(...data)
pages += 1
if (data.length < PAGE_SIZE) break
from += PAGE_SIZE
}
// Duplicates are only possible across page boundaries, so single-page results
// never need the dedup pass.
if (options?.dedupeBy && pages > 1) {
const seen = new Set<string | number>()
const deduped: T[] = []
for (const row of allRows) {
const key = options.dedupeBy(row)
if (seen.has(key)) continue
seen.add(key)
deduped.push(row)
}
const dropped = allRows.length - deduped.length
if (dropped > 0) {
log.warn(
'fetchAllRows dropped duplicate rows across pages — a paginated query is missing a stable .order() on a unique column',
{ dropped, total: allRows.length, pages }
)
return deduped
}
}
return allRows
}