import { cursorCurrentToSearch, cursorWhereFromToSearch, isPushable, keyColumnsToSearch, loadSubsetOptionsToSearch, splitLoadSubsetOptions, subsetParamsToSearch, } from './postgrest-filters' import { postgrestRequest } from './postgrest-request' import { ignoreCancelledWrite } from './realtime' import { parseRow, serializeRow, usesParsedColumn } from './row-conversions' import type { ColumnConversions } from './row-conversions' import type { DeleteMutationFnParams, InsertMutationFnParams, LoadSubsetOptions, UpdateMutationFnParams, } from '@tanstack/db' import type { QueryClient, QueryMeta } from '@tanstack/query-core' import type { SupabaseClient } from '@supabase/supabase-js' /** * PostgREST may return a superset for local filtering. A server window over * that superset can exclude rows the live query still needs. Parsed columns * can also differ in both value and order, so their comparisons stay local. */ function serverSubset( options: LoadSubsetOptions, parse: ColumnConversions['parse'], ): LoadSubsetOptions { const { where, orderBy, cursor, offset, limit } = options const localPredicate = usesParsedColumn(where, parse) const localOrder = orderBy?.some(({ expression }) => usesParsedColumn(expression, parse), ) // A cursor compares the order's columns, so a local order covers it. const capped = limit !== undefined || offset || cursor const residual = where && capped && !isPushable(where, { localTextRanges: true }) if (!localPredicate && !localOrder && !residual) return options return { ...options, where: localPredicate ? undefined : where, orderBy: localOrder ? undefined : orderBy, cursor: undefined, offset: undefined, limit: limit === 0 ? 0 : undefined, } } export const subsetOptionsToQueryKey = ( tableName: string, demand: LoadSubsetOptions, parse?: ColumnConversions['parse'], ): Array => { const ctx = serverSubset(demand, parse) // The key shares the request URL's where/order/limit encoding // (`subsetParamsToSearch`) so the two cannot drift; the constant `select` is // excluded because it never distinguishes one subset from another. // // Pagination params (cursor/offset) ARE included: query-db-collection keys row // ownership by this key, so distinct windows of the same subset (same // where/order/limit, different cursor/offset) must not collide — otherwise a // later page's result would take over the earlier page's rows and delete them. // Each window still extends the Query Collection's `[tableName]` base key. const search = subsetParamsToSearch(ctx) if (ctx.cursor) { try { for (const [k, v] of cursorWhereFromToSearch(ctx.cursor)) { search.append(k, v) } } catch { // The queryKey function must never throw — query-db-collection calls it // synchronously to key row ownership. If the cursor cannot be rendered // (an un-pushable predicate), fall back to a deterministic discriminator // so distinct windows still get distinct keys and cannot take over and // delete each other's rows. search.append('cursor', JSON.stringify(ctx.cursor.whereFrom)) } } if (ctx.offset && !ctx.cursor) { search.append('offset', `${ctx.offset}`) } const key = search.toString() return key ? [tableName, key] : [tableName] } type PageOptions = { /** The caller's row limit; `undefined` means "the full matching set". */ limit?: number /** The starting offset (always 0 for cursor reads). */ offset: number signal?: AbortSignal /** * Columns that uniquely identify a row. Pages are ordered by them after the * caller's order so that the rows tied on that order keep one position * across the page requests; see {@link withKeyOrder}. */ keys: Array } /** * Extends the `order` param with the key columns it does not already sort by. * * Offset paging is only sound over a total order. When the caller's order has * ties (or there is none), Postgres may order the tied rows differently for * each page request, so a row can show up on two pages while another shows up * on none. Ordering by the unique key last makes every page agree. */ function withKeyOrder( search: URLSearchParams, keys: Array, ): URLSearchParams { const sorts = search.get('order')?.split(',') ?? [] // Each sort is `column.direction[.nulls]`; the column is the first segment. const sortedColumns = new Set(sorts.map((sort) => sort.split('.')[0])) const missing = keys.filter((key) => !sortedColumns.has(key)) if (missing.length === 0) { return search } const ordered = new URLSearchParams(search) ordered.set( 'order', [...sorts, ...missing.map((key) => `${key}.asc`)].join(','), ) return ordered } /** * Read a full matching set (or the caller's `limit` rows) with offset paging, * looping past PostgREST's `db-max-rows` cap instead of silently truncating at * it. The first request asks for `count=exact` so the loop knows the total up * front and stops without a trailing empty probe; each later page advances the * offset by the rows actually received (self-correcting under concurrent * writes) and is capped by the observed page size and the rows still owed. * * A result that fits in one page is returned as read. When more pages are * needed, the first page is read again under a total order (see * {@link withKeyOrder}) so that all pages are cut from the same sequence. */ async function fetchAllPages( supabase: SupabaseClient, tableName: string, baseSearch: URLSearchParams, { limit, offset: startOffset, signal, keys }: PageOptions, ): Promise> { // A zero limit is an empty window: return it without touching the network. if (limit === 0) { return [] } // The base search already carries the caller's limit/offset (from the search // builders), so the first request is sent verbatim — its URL stays identical // to the single-request behaviour, and the query key that mirrors it. const first = await postgrestRequest>(supabase, tableName, { method: 'GET', search: baseSearch, signal, count: 'exact', }) let rows: Array = first.data ? [...first.data] : [] // The rows the first page returned size the server's cap; a later page that // comes back shorter than this means the server has no more rows. const pageSize = rows.length // `Content-Range` reports the whole matching set, so the rows reachable from // the starting offset are what remains past it. When the count header is // missing we fall back to paging until a short page appears. const total = first.count ?? Number.POSITIVE_INFINITY const remaining = Math.max(0, total - startOffset) const target = limit === undefined ? remaining : Math.min(limit, remaining) // The first page was read under the caller's order, whose ties the server may // break differently on the next request. If more pages follow, discard it and // page through a total order instead. let pagedSearch = baseSearch if (pageSize > 0 && rows.length < target) { pagedSearch = withKeyOrder(baseSearch, keys) if (pagedSearch !== baseSearch) { rows = [] } } let lastPageSize = pageSize while (lastPageSize === pageSize && pageSize > 0 && rows.length < target) { const pageSearch = new URLSearchParams(pagedSearch) pageSearch.set('offset', `${startOffset + rows.length}`) pageSearch.set('limit', `${Math.min(pageSize, target - rows.length)}`) const page = await postgrestRequest>(supabase, tableName, { method: 'GET', search: pageSearch, signal, }) const pageRows = page.data ?? [] rows.push(...pageRows) lastPageSize = pageRows.length } return rows } /** * Read one already-in-budget subset's full window: the keyset tie request (if * any) plus the main paged request. This runs once per chunk; an unsplit * subset is one chunk. */ async function loadWindow( supabase: SupabaseClient, tableName: string, keys: Array, options: LoadSubsetOptions, signal: AbortSignal, ): Promise> { const search = loadSubsetOptionsToSearch(options) // Cursor reads pin their own window start, so they never carry an offset (see // loadSubsetOptionsToSearch); only a cursor-less read advances from `offset`. const startOffset = options.offset && !options.cursor ? options.offset : 0 // A keyset request whose boundary column has ties (e.g. `orderBy(created_at)` // with repeated values) needs a second, unlimited request for the rows equal // to the boundary; the limited `whereFrom` page alone would skip them. // // Do not drop this on the grounds that core's ordered-source loader also // probes the boundary (`requestSnapshot({ where: eq(col, boundary) })`) and // usually pre-loads the same tie class: that probe is a window-path // optimization the adapter cannot assume ran. It is skipped when the boundary // value is unchanged, falls back to a full-source load for non-keyset orders, // and never runs for `loadSubset({ cursor })` calls made outside that path. // Honouring `whereCurrent` here is what makes the cursor correct on its own. const tiesSearch = cursorCurrentToSearch(options) if (!tiesSearch) { return await fetchAllPages(supabase, tableName, search, { limit: options.limit, offset: startOffset, signal, keys, }) } const [ties, rows] = await Promise.all([ // The tie read is deliberately unbounded by the caller's limit — every row // tied at the boundary must come back — but it is still paged so a tie class // larger than the cap is not truncated. fetchAllPages(supabase, tableName, tiesSearch, { offset: 0, signal, keys, }), fetchAllPages(supabase, tableName, search, { limit: options.limit, offset: startOffset, signal, keys, }), ]) // `whereCurrent` (== boundary) and `whereFrom` (> / < boundary) are disjoint, // so concatenation never duplicates; the collection re-sorts locally. return [...ties, ...rows] } // PostgREST has no body-carried filters for reads and the Supabase API gateway // rejects request lines over about 8 KB with 414, so a big `inArray(...)` // predicate (the common shape a lazy join's on-demand collection produces) // can make a subset's URL too long to send. This matches postgrest-js's own // `urlLengthLimit` default so the two agree on what counts as "too long". export const MAX_URL_LENGTH = 8000 // At most this many chunk requests run at once; splitting a large IN list can // produce far more chunks than are worth having in flight simultaneously. const MAX_CONCURRENT_CHUNKS = 6 // Pages after the first (`fetchAllPages`) set `offset` and `limit` on top of // the subset's own search; no real value is wider than this one. const WIDEST_PAGE_PARAM = `${Number.MAX_SAFE_INTEGER}` /** * The length of the longest URL `fetchAllPages` could send for `search`: the * base URL, the `?`, and the query string with the key-column tiebreak order * that multi-page reads add (see {@link withKeyOrder}) and `offset`/`limit` at * their widest, so every page — not only the first — stays within budget. */ function pagedUrlLength( baseLength: number, search: URLSearchParams, keys: Array, ): number { const paged = new URLSearchParams(withKeyOrder(search, keys)) paged.set('offset', WIDEST_PAGE_PARAM) paged.set('limit', WIDEST_PAGE_PARAM) return baseLength + '?'.length + paged.toString().length } /** * The length of the longest URL a subset would produce: the main request's * pages, and — when the subset has a cursor — the unlimited `whereCurrent` tie * request's pages too, since `loadWindow` sends both for a cursor read. * Measuring rendered URLs (rather than estimating) accounts for quoting and * percent-encoding exactly. */ function measureSubset( supabase: SupabaseClient, tableName: string, keys: Array, options: LoadSubsetOptions, ): number { const base = supabase.from(tableName).url.toString().length const mainLength = pagedUrlLength( base, loadSubsetOptionsToSearch(options), keys, ) const tiesSearch = cursorCurrentToSearch(options) return tiesSearch ? Math.max(mainLength, pagedUrlLength(base, tiesSearch, keys)) : mainLength } /** * Runs one `loadWindow` per chunk with at most `MAX_CONCURRENT_CHUNKS` in * flight at a time, and concatenates their rows. Chunks are disjoint on the * split column (see `splitLoadSubsetOptions`), so concatenation order does not * matter and needs no dedupe. * * On the first failure, every other chunk's request is aborted through a * local `AbortController` that also follows the caller's own signal — this * keeps the same all-or-nothing semantics an unsplit request has ("one failed * page fails the whole load") instead of surfacing a partial result, and the * first error encountered is what `Promise.all` rejects with. */ async function loadChunks( supabase: SupabaseClient, tableName: string, keys: Array, chunks: Array, signal: AbortSignal, ): Promise> { // Not `AbortSignal.any`: React Native (Hermes) and Safari before 17.4 lack it. const controller = new AbortController() const abortFromCaller = () => controller.abort(signal.reason) if (signal.aborted) { abortFromCaller() } else { signal.addEventListener('abort', abortFromCaller, { once: true }) } const results: Array> = new Array(chunks.length) let nextIndex = 0 const runWorker = async (): Promise => { while (nextIndex < chunks.length) { const index = nextIndex++ try { results[index] = await loadWindow( supabase, tableName, keys, chunks[index]!, controller.signal, ) } catch (error) { controller.abort() throw error } } } const workerCount = Math.min(MAX_CONCURRENT_CHUNKS, chunks.length) try { await Promise.all(Array.from({ length: workerCount }, runWorker)) } finally { signal.removeEventListener('abort', abortFromCaller) } return results.flat() } /** * Reads the rows a query needs, converted to the rows the collection holds * (see {@link RowConversions}). */ export const supabaseQueryFn = async ( supabase: SupabaseClient, tableName: string, ctx: { client: QueryClient queryKey: ReadonlyArray signal: AbortSignal meta: QueryMeta | undefined pageParam?: unknown direction?: unknown }, { keys = [], conversions = {}, }: { keys?: Array; conversions?: ColumnConversions } = {}, ): Promise> => { // Query Collection installs the request options on every query it runs. const options = serverSubset(ctx.meta!.loadSubsetOptions!, conversions.parse) // The query key stays the full, unchunked subset (subsetOptionsToQueryKey // never sees these chunks), so this fan-out is invisible to // query-db-collection: one query still owns every row it returns. const chunks = splitLoadSubsetOptions(options, { maxUrlLength: MAX_URL_LENGTH, measure: (chunkOptions) => measureSubset(supabase, tableName, keys, chunkOptions), }) const rows = await loadChunks( supabase, tableName, keys, chunks, ctx.signal, ) return rows.map((row) => parseRow(row, conversions)) } export const supabaseOnInsert = async ( supabase: SupabaseClient, tableName: string, { transaction, collection }: InsertMutationFnParams, conversions: ColumnConversions = {}, ) => { await Promise.all( transaction.mutations.map(async (mutation) => { const { data } = await postgrestRequest(supabase, tableName, { method: 'POST', search: new URLSearchParams(), body: serializeRow(mutation.modified, conversions), returnRows: true, single: true, }) // A single-row insert with `Prefer: return=representation` always returns // the created row. const inserted = parseRow(data as object, conversions) mutation.modified = inserted // The data has been inserted and confirmed by the server, so we can write // it to the collection. Realtime may already have echoed the insert, in // which case this is an update of the synced row rather than an insert. await collection.utils.writeUpsert(inserted) }), ) } export const supabaseOnUpdate = async ( supabase: SupabaseClient, tableName: string, keys: Array, { transaction, collection }: UpdateMutationFnParams, conversions: ColumnConversions = {}, ) => { await Promise.all( transaction.mutations.map(async (mutation) => { const { original, changes } = mutation const search = keyColumnsToSearch( keys, serializeRow(original, conversions), ) const { data } = await postgrestRequest(supabase, tableName, { method: 'PATCH', search, body: serializeRow(changes, conversions), returnRows: true, single: true, }) // A single-row update with `Prefer: return=representation` always returns // the updated row. const updated = parseRow(data as object, conversions) mutation.modified = updated await collection.utils.writeUpdate(updated) }), ) } export const supabaseOnDelete = async ( supabase: SupabaseClient, tableName: string, keys: Array, { transaction, collection }: DeleteMutationFnParams, conversions: ColumnConversions = {}, ) => { await Promise.all( transaction.mutations.map(async (mutation) => { await postgrestRequest(supabase, tableName, { method: 'DELETE', search: keyColumnsToSearch( keys, serializeRow(mutation.original, conversions), ), }) // The data has been deleted and confirmed by the server, so we can write // it to the collection. A Realtime echo of the delete is held behind this // persisting mutation and can land first; the row is gone either way, so // the delete is confirmed. await collection.utils .writeDelete(collection.getKeyFromItem(mutation.original)) .catch(ignoreCancelledWrite) }), ) }