ui/table_state.svelte.ts

Reactive state for database table pagination and data fetching.

Holds one AsyncSlot — list (the paginated row fetch). Per-row delete uses plain try/catch + scalar deleting / delete_error fields (no slot — delete_error must persist past list.run() retries so the failure message stays visible while the user refetches).

const table = new TableState(); await table.fetch('accounts', 0, 50); // pagination if (table.has_next) table.go_next(); await table.fetch(table.table_name, table.offset, table.limit); // deletion const deleted = await table.delete_row(table.rows[0]);
<script lang="ts"> import {TableState} from '@fuzdev/fuz_app/ui/table_state.svelte.ts'; const table = new TableState(); table.fetch('accounts'); </script> {#if table.list.loading} <p>loading…</p> {:else if table.list.error} <p>{table.list.error}</p> {:else} <p>showing {table.showing_start}–{table.showing_end} of {table.total}</p> {/if}
view source

Declarations
#

TableState
#

ui/table_state.svelte.ts view source

import {TableState} from '@fuzdev/fuz_app/ui/table_state.svelte.js';

list

type AsyncSlot<void, string>

readonly

table_name

type string

$state.raw

columns

type ColumnInfo[]

$state.raw

rows

type Record<string, unknown>[]

$state.raw

total

type number

$state.raw

offset

type number

$state.raw

limit

type number

$state.raw

primary_key

type string | null

$state.raw

deletable

Whether the server will accept a row DELETE on this table. False for a table with no single-column primary key *and* for one the server excludes by policy (the audit trail, singleton bookkeeping rows), which primary_key alone can't distinguish — it reports the key shape, not the policy.

type boolean

$state.raw

deleting

type string | null

$state.raw

delete_error

type string | null

$state.raw

can_delete

Whether to offer a delete affordance at all. Gate rendering on this rather than on primary_key, or a policy-excluded table shows a button whose only outcome is a table_not_deletable refusal.

type boolean

readonly $derived

showing_start

type number

readonly $derived

showing_end

type number

readonly $derived

has_prev

type boolean

readonly $derived

has_next

type boolean

readonly $derived

fetch

Fetch a page of rows for table_name from GET /api/db/tables/{table_name}. limit is clamped to [1, DB_TABLE_ROWS_LIMIT_MAX] — the same bounds the route 400s on, single-sourced so the client guard can't drift into sending a request the server refuses.

type (table_name: string, offset?: number, limit?: number): Promise<void>

table_name

type string

offset?

type number
optional default 0

limit?

type number
optional default 100
returns Promise<void>

mutates

  • this

go_prev

type (): void

returns void

go_next

type (): void

returns void

delete_row

Delete a row by its primary key via DELETE /api/db/tables/{table_name}/rows/{pk}. Optimistically drops it from rows and decrements total on success; surfaces server errors on delete_error.

type (row: Record<string, unknown>): Promise<boolean>

row

type Record<string, unknown>
returns Promise<boolean>

true when the row was removed; false when the table isn't deletable or the server refused

mutates

  • this

Depends on
#