@ai-matrx/data 0.26.2 → 0.28.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,296 @@
1
+ /** Registry type (`platform.entity_types.type`); null = untyped or no registry row. */
2
+ type TableType = "entity" | "detail" | "ledger" | "reference" | "system" | "restricted" | "deprecated";
3
+ type OrgRule = {
4
+ readonly rule: "binding";
5
+ } | {
6
+ readonly rule: "parent";
7
+ readonly parent: string;
8
+ readonly via: string;
9
+ } | {
10
+ readonly rule: "column";
11
+ } | {
12
+ readonly rule: "none";
13
+ };
14
+ /** Phantom carrier for the generated row types — never present at runtime. */
15
+ declare const ROW: unique symbol;
16
+ interface TableDescriptor<Row = Record<string, unknown>, Insert = Record<string, unknown>, Update = Partial<Insert>> {
17
+ readonly schema: string;
18
+ readonly name: string;
19
+ readonly kind: "table" | "view" | "materialized_view";
20
+ readonly type: TableType | null;
21
+ readonly pk: readonly string[];
22
+ readonly org: OrgRule;
23
+ readonly versioned: boolean;
24
+ readonly softDelete: boolean;
25
+ /** The archive column, when the table archives instead of (or as well as) soft-deleting. */
26
+ readonly archive: "archived_at" | "is_archived" | null;
27
+ readonly readOnly: boolean;
28
+ readonly deletesRefused: boolean;
29
+ readonly readOnlyColumns: readonly string[];
30
+ readonly listScope: string | null;
31
+ readonly realtime: boolean;
32
+ /**
33
+ * The table has a `created_by` column. A signed-in insert that omits it is stamped with the
34
+ * session's person (row security on tables like `workbench.notes` refuses an insert whose
35
+ * `created_by` is not `auth.uid()`). Optional: descriptors generated before 0.25.0 lack it.
36
+ */
37
+ readonly createdBy?: boolean;
38
+ readonly [ROW]?: {
39
+ row: Row;
40
+ insert: Insert;
41
+ update: Update;
42
+ };
43
+ }
44
+ interface FunctionDescriptor<Args = Record<string, unknown>, Result = unknown> {
45
+ readonly schema: string;
46
+ readonly name: string;
47
+ readonly roles: readonly ("anon" | "authenticated" | "service_role")[];
48
+ readonly registered: boolean;
49
+ readonly [ROW]?: {
50
+ args: Args;
51
+ result: Result;
52
+ };
53
+ }
54
+ type RowOf<T> = T extends TableDescriptor<infer R, any, any> ? R : never;
55
+ type InsertOf<T> = T extends TableDescriptor<any, infer I, any> ? I : never;
56
+ type UpdateOf<T> = T extends TableDescriptor<any, any, infer U> ? U : never;
57
+ type ArgsOf<F> = F extends FunctionDescriptor<infer A, any> ? A : never;
58
+ type ResultOf<F> = F extends FunctionDescriptor<any, infer R> ? R : never;
59
+ interface PostgrestError {
60
+ message: string;
61
+ code?: string;
62
+ details?: string | null;
63
+ hint?: string | null;
64
+ }
65
+ interface Resp<T> {
66
+ data: T | null;
67
+ error: PostgrestError | null;
68
+ count?: number | null;
69
+ }
70
+ /** The slice of a supabase-js query builder the doors use (structural — any supabase-js 2.x client). */
71
+ interface QueryLike extends PromiseLike<Resp<any>> {
72
+ select(columns?: string, opts?: {
73
+ count?: "exact" | "planned" | "estimated";
74
+ head?: boolean;
75
+ }): QueryLike;
76
+ eq(column: string, value: unknown): QueryLike;
77
+ is(column: string, value: null | boolean): QueryLike;
78
+ order(column: string, opts?: {
79
+ ascending?: boolean;
80
+ }): QueryLike;
81
+ range(from: number, to: number): QueryLike;
82
+ maybeSingle(): PromiseLike<Resp<any>>;
83
+ single(): PromiseLike<Resp<any>>;
84
+ }
85
+ /** The slice of a supabase-js client the doors use (a real `SupabaseClient` is assignable — doors.types.test.ts). */
86
+ interface SupabaseLike {
87
+ schema(name: string): {
88
+ /**
89
+ * A table builder (read as `TableLike` inside the doors). Typed `unknown` on purpose: matching
90
+ * supabase-js's deep builder generics structurally costs TS2589 in every consumer.
91
+ */
92
+ from(table: string): unknown;
93
+ rpc(fn: string, args?: Record<string, unknown>, opts?: {
94
+ count?: "exact" | "planned" | "estimated";
95
+ }): PromiseLike<Resp<any>>;
96
+ };
97
+ auth?: {
98
+ getSession(): Promise<{
99
+ data: {
100
+ session: {
101
+ access_token: string;
102
+ user?: {
103
+ id: string;
104
+ } | null;
105
+ } | null;
106
+ };
107
+ }>;
108
+ onAuthStateChange(cb: (event: string, session: unknown) => void): {
109
+ data: {
110
+ subscription: {
111
+ unsubscribe(): void;
112
+ };
113
+ };
114
+ };
115
+ };
116
+ }
117
+ /** The credential shape `@ai-matrx/api`'s `CredentialsPort` accepts (structural; data owns the session). */
118
+ interface UserCredentialsPort {
119
+ get(): Promise<{
120
+ kind: "user";
121
+ accessToken: string;
122
+ } | null>;
123
+ onChange?(cb: () => void): () => void;
124
+ }
125
+ interface Db {
126
+ readonly client: SupabaseLike;
127
+ readonly role: "user" | "service";
128
+ /** The active organization — where NEW entity rows are saved. Never a read filter. */
129
+ activeOrganization(): string | null;
130
+ /** The session as `@ai-matrx/api`'s `user` credential (one sign-in feeds both lanes). */
131
+ readonly credentials: UserCredentialsPort;
132
+ }
133
+ interface CreateDbOptions {
134
+ /** The app's existing Supabase client (browser, RSC, route or the host's chat client). */
135
+ client: SupabaseLike;
136
+ /** Where new entity rows are saved. Read lazily on every insert; never consulted by a read. */
137
+ organization?: () => string | null | undefined;
138
+ }
139
+ /** The one database connection for a signed-in (or anonymous) person. */
140
+ declare function createDb(options: CreateDbOptions): Db;
141
+ /**
142
+ * Service-role doors — servers only. No session, no binding: every entity insert
143
+ * names its organization. Throws in a browser bundle.
144
+ */
145
+ declare function createServiceDb(options: {
146
+ client: SupabaseLike;
147
+ }): Db;
148
+ declare class DoorRefusedError extends Error {
149
+ readonly door: string;
150
+ readonly reason: string;
151
+ constructor(door: string, reason: string);
152
+ }
153
+ declare class DatabaseError extends Error {
154
+ readonly door: string;
155
+ readonly cause: PostgrestError;
156
+ constructor(door: string, cause: PostgrestError);
157
+ }
158
+ type Op = "read" | "insert" | "update" | "remove" | "archive";
159
+ /** Refuse before the wire when the descriptor's rules do not open this door. */
160
+ declare function assertDoor(db: Db, table: TableDescriptor<any, any, any>, op: Op): void;
161
+ type Col<R> = Extract<keyof R, string>;
162
+ /** The typed filter a `Where` callback receives — column names and values checked against the row. */
163
+ interface Filter<R> {
164
+ eq<K extends Col<R>>(column: K, value: NonNullable<R[K]>): Filter<R>;
165
+ neq<K extends Col<R>>(column: K, value: NonNullable<R[K]>): Filter<R>;
166
+ gt<K extends Col<R>>(column: K, value: NonNullable<R[K]>): Filter<R>;
167
+ gte<K extends Col<R>>(column: K, value: NonNullable<R[K]>): Filter<R>;
168
+ lt<K extends Col<R>>(column: K, value: NonNullable<R[K]>): Filter<R>;
169
+ lte<K extends Col<R>>(column: K, value: NonNullable<R[K]>): Filter<R>;
170
+ like(column: Col<R>, pattern: string): Filter<R>;
171
+ ilike(column: Col<R>, pattern: string): Filter<R>;
172
+ in<K extends Col<R>>(column: K, values: readonly NonNullable<R[K]>[]): Filter<R>;
173
+ is(column: Col<R>, value: null | boolean): Filter<R>;
174
+ contains(column: Col<R>, value: unknown): Filter<R>;
175
+ overlaps(column: Col<R>, value: unknown): Filter<R>;
176
+ textSearch(column: Col<R>, query: string, opts?: {
177
+ type?: "plain" | "phrase" | "websearch";
178
+ config?: string;
179
+ }): Filter<R>;
180
+ /** PostgREST's raw `or` grammar (`"pinned.eq.true,starred.eq.true"`). */
181
+ or(filters: string, opts?: {
182
+ referencedTable?: string;
183
+ }): Filter<R>;
184
+ not(column: Col<R>, operator: string, value: unknown): Filter<R>;
185
+ }
186
+ /** A filter callback — the same builder `readAllRows` callers write today, typed to the row. */
187
+ type Where<R = any> = (q: Filter<R>) => Filter<R>;
188
+ interface ReadOptions<C> {
189
+ /** Only these columns (the result is typed to them). Default every column. */
190
+ columns?: readonly C[];
191
+ /** Include soft-deleted rows (default: excluded on tables with `deleted_at`). */
192
+ includeDeleted?: boolean;
193
+ }
194
+ /** One row by primary key (a composite key takes `{ col: value }`), or null. */
195
+ declare function read<R, I, U, C extends Col<R> = Col<R>>(db: Db, table: TableDescriptor<R, I, U>, id: unknown, opts?: ReadOptions<C>): Promise<Pick<R, C> | null>;
196
+ /** EVERY matching row, paged past the 1,000-row cap (ordered by the full key); throws on a short read. */
197
+ declare function listAll<R, I, U, C extends Col<R> = Col<R>>(db: Db, table: TableDescriptor<R, I, U>, where?: Where<R>, opts?: ReadOptions<C> & {
198
+ orderBy?: Col<R>;
199
+ pageSize?: number;
200
+ maxRows?: number;
201
+ }): Promise<Pick<R, C>[]>;
202
+ /** One explicit window (`from`..`to`, inclusive) plus the exact total. */
203
+ declare function page<R, I, U, C extends Col<R> = Col<R>>(db: Db, table: TableDescriptor<R, I, U>, window: ReadOptions<C> & {
204
+ from: number;
205
+ to: number;
206
+ where?: Where<R>;
207
+ orderBy?: Col<R>;
208
+ ascending?: boolean;
209
+ }): Promise<{
210
+ rows: Pick<R, C>[];
211
+ total: number | null;
212
+ }>;
213
+ /** The exact count of matching rows (no rows transferred). */
214
+ declare function count<R, I, U>(db: Db, table: TableDescriptor<R, I, U>, where?: Where<R>, opts?: {
215
+ includeDeleted?: boolean;
216
+ }): Promise<number>;
217
+ /** Insert one row (or many) and return what the database stored. */
218
+ declare function insert<R, I, U>(db: Db, table: TableDescriptor<R, I, U>, values: I): Promise<R>;
219
+ declare function insert<R, I, U>(db: Db, table: TableDescriptor<R, I, U>, values: readonly I[]): Promise<R[]>;
220
+ /** Every write on a versioned table is explicit: `{ expectedVersion }` (guarded) or `{ overwrite: true }`. */
221
+ type UpdateGuard = {
222
+ expectedVersion: number;
223
+ } | {
224
+ overwrite: true;
225
+ };
226
+ type WriteResult<R> = {
227
+ status: "saved";
228
+ row: R;
229
+ rebasedFrom?: {
230
+ expectedVersion: number;
231
+ currentVersion: number;
232
+ };
233
+ } | {
234
+ status: "conflict";
235
+ currentRow: R;
236
+ currentVersion: number;
237
+ } | {
238
+ status: "not_found";
239
+ };
240
+ /**
241
+ * Update one row by primary key. On a versioned table the guard is required:
242
+ * `{ expectedVersion }` (a conflict comes back, never a silent overwrite) or
243
+ * `{ overwrite: true }`; on an unversioned table `expectedVersion` is refused.
244
+ */
245
+ declare function update<R, I, U>(db: Db, table: TableDescriptor<R, I, U>, id: unknown, patch: U, guard?: UpdateGuard): Promise<WriteResult<R>>;
246
+ /**
247
+ * Remove one row the way the table removes: soft delete (`deleted_at`) where it
248
+ * has one; an archive-only table refuses (use `archive`); a hard delete only where
249
+ * the table has neither. Guarded like `update` on a versioned table.
250
+ */
251
+ declare function remove<R, I, U>(db: Db, table: TableDescriptor<R, I, U>, id: unknown, guard?: UpdateGuard): Promise<{
252
+ status: "soft_deleted" | "deleted" | "not_found";
253
+ } | {
254
+ status: "conflict";
255
+ currentVersion: number;
256
+ }>;
257
+ /** Archive (or, with `restore`, bring back) one row by the table's archive column. Guarded like `update`. */
258
+ declare function archive<R, I, U>(db: Db, table: TableDescriptor<R, I, U>, id: unknown, opts?: {
259
+ restore?: boolean;
260
+ guard?: UpdateGuard;
261
+ }): Promise<WriteResult<R>>;
262
+ /** Bring back an archived or soft-deleted row. */
263
+ declare const restore: <R, I, U>(db: Db, table: TableDescriptor<R, I, U>, id: unknown, guard?: UpdateGuard) => Promise<WriteResult<R>>;
264
+ /** Call a database function through its generated door (PostgREST chooses an overload by argument names). */
265
+ declare function call<F extends FunctionDescriptor<any, any>>(db: Db, fn: F, args: ArgsOf<F>): Promise<ResultOf<F>>;
266
+ /** One ordering column of a set-returning function's rows (PostgREST `order`). */
267
+ type CallOrder = string | {
268
+ column: string;
269
+ ascending?: boolean;
270
+ nullsFirst?: boolean;
271
+ };
272
+ /**
273
+ * EVERY row a set-returning function answers, paged past the 1,000-row cap by
274
+ * `range` over a stable `order` (name enough columns to make it total — end on a key);
275
+ * throws a `DatabaseError` with the database's own error, or `IncompleteReadError` on a short read.
276
+ */
277
+ declare function callAll<F extends FunctionDescriptor<any, any>>(db: Db, fn: F, args: ArgsOf<F>, opts: {
278
+ order: readonly CallOrder[];
279
+ pageSize?: number;
280
+ maxRows?: number;
281
+ }): Promise<ResultOf<F> extends readonly (infer Row)[] ? Row[] : ResultOf<F>[]>;
282
+ /** One schema's flags (`<schema>.flags.ts` from `matrx-data generate --flags-index`): "schema.table" → descriptor. */
283
+ type FlagsIndex = Readonly<Record<string, TableDescriptor>>;
284
+ /** Per-schema loaders (`flags-index.ts`): only the schema a runtime name names is loaded (pay-only). */
285
+ type FlagLoaders = Readonly<Record<string, () => Promise<FlagsIndex>>>;
286
+ /** The door for a table whose name arrives at runtime — loosely typed, the same rules. */
287
+ declare function tableByName(index: FlagsIndex, name: string): TableDescriptor;
288
+ /** `tableByName` over per-schema loaders: loads only the named schema's flags. */
289
+ declare function loadTableByName(loaders: FlagLoaders, name: string): Promise<TableDescriptor>;
290
+ /** The `postgres_changes` filter for a table — only published tables have one (an unpublished table delivers nothing). */
291
+ declare function changesOf(table: TableDescriptor<any, any, any>): {
292
+ schema: string;
293
+ table: string;
294
+ };
295
+
296
+ export { type ArgsOf as A, restore as B, type CallOrder as C, type Db as D, tableByName as E, type Filter as F, update as G, type InsertOf as I, type OrgRule as O, type QueryLike as Q, type ReadOptions as R, type SupabaseLike as S, type TableDescriptor as T, type UserCredentialsPort as U, type Where as W, type CreateDbOptions as a, DatabaseError as b, DoorRefusedError as c, type FlagLoaders as d, type FlagsIndex as e, type FunctionDescriptor as f, type ResultOf as g, type RowOf as h, type TableType as i, type UpdateGuard as j, type UpdateOf as k, type WriteResult as l, archive as m, assertDoor as n, call as o, callAll as p, changesOf as q, count as r, createDb as s, createServiceDb as t, insert as u, listAll as v, loadTableByName as w, page as x, read as y, remove as z };
@@ -0,0 +1,296 @@
1
+ /** Registry type (`platform.entity_types.type`); null = untyped or no registry row. */
2
+ type TableType = "entity" | "detail" | "ledger" | "reference" | "system" | "restricted" | "deprecated";
3
+ type OrgRule = {
4
+ readonly rule: "binding";
5
+ } | {
6
+ readonly rule: "parent";
7
+ readonly parent: string;
8
+ readonly via: string;
9
+ } | {
10
+ readonly rule: "column";
11
+ } | {
12
+ readonly rule: "none";
13
+ };
14
+ /** Phantom carrier for the generated row types — never present at runtime. */
15
+ declare const ROW: unique symbol;
16
+ interface TableDescriptor<Row = Record<string, unknown>, Insert = Record<string, unknown>, Update = Partial<Insert>> {
17
+ readonly schema: string;
18
+ readonly name: string;
19
+ readonly kind: "table" | "view" | "materialized_view";
20
+ readonly type: TableType | null;
21
+ readonly pk: readonly string[];
22
+ readonly org: OrgRule;
23
+ readonly versioned: boolean;
24
+ readonly softDelete: boolean;
25
+ /** The archive column, when the table archives instead of (or as well as) soft-deleting. */
26
+ readonly archive: "archived_at" | "is_archived" | null;
27
+ readonly readOnly: boolean;
28
+ readonly deletesRefused: boolean;
29
+ readonly readOnlyColumns: readonly string[];
30
+ readonly listScope: string | null;
31
+ readonly realtime: boolean;
32
+ /**
33
+ * The table has a `created_by` column. A signed-in insert that omits it is stamped with the
34
+ * session's person (row security on tables like `workbench.notes` refuses an insert whose
35
+ * `created_by` is not `auth.uid()`). Optional: descriptors generated before 0.25.0 lack it.
36
+ */
37
+ readonly createdBy?: boolean;
38
+ readonly [ROW]?: {
39
+ row: Row;
40
+ insert: Insert;
41
+ update: Update;
42
+ };
43
+ }
44
+ interface FunctionDescriptor<Args = Record<string, unknown>, Result = unknown> {
45
+ readonly schema: string;
46
+ readonly name: string;
47
+ readonly roles: readonly ("anon" | "authenticated" | "service_role")[];
48
+ readonly registered: boolean;
49
+ readonly [ROW]?: {
50
+ args: Args;
51
+ result: Result;
52
+ };
53
+ }
54
+ type RowOf<T> = T extends TableDescriptor<infer R, any, any> ? R : never;
55
+ type InsertOf<T> = T extends TableDescriptor<any, infer I, any> ? I : never;
56
+ type UpdateOf<T> = T extends TableDescriptor<any, any, infer U> ? U : never;
57
+ type ArgsOf<F> = F extends FunctionDescriptor<infer A, any> ? A : never;
58
+ type ResultOf<F> = F extends FunctionDescriptor<any, infer R> ? R : never;
59
+ interface PostgrestError {
60
+ message: string;
61
+ code?: string;
62
+ details?: string | null;
63
+ hint?: string | null;
64
+ }
65
+ interface Resp<T> {
66
+ data: T | null;
67
+ error: PostgrestError | null;
68
+ count?: number | null;
69
+ }
70
+ /** The slice of a supabase-js query builder the doors use (structural — any supabase-js 2.x client). */
71
+ interface QueryLike extends PromiseLike<Resp<any>> {
72
+ select(columns?: string, opts?: {
73
+ count?: "exact" | "planned" | "estimated";
74
+ head?: boolean;
75
+ }): QueryLike;
76
+ eq(column: string, value: unknown): QueryLike;
77
+ is(column: string, value: null | boolean): QueryLike;
78
+ order(column: string, opts?: {
79
+ ascending?: boolean;
80
+ }): QueryLike;
81
+ range(from: number, to: number): QueryLike;
82
+ maybeSingle(): PromiseLike<Resp<any>>;
83
+ single(): PromiseLike<Resp<any>>;
84
+ }
85
+ /** The slice of a supabase-js client the doors use (a real `SupabaseClient` is assignable — doors.types.test.ts). */
86
+ interface SupabaseLike {
87
+ schema(name: string): {
88
+ /**
89
+ * A table builder (read as `TableLike` inside the doors). Typed `unknown` on purpose: matching
90
+ * supabase-js's deep builder generics structurally costs TS2589 in every consumer.
91
+ */
92
+ from(table: string): unknown;
93
+ rpc(fn: string, args?: Record<string, unknown>, opts?: {
94
+ count?: "exact" | "planned" | "estimated";
95
+ }): PromiseLike<Resp<any>>;
96
+ };
97
+ auth?: {
98
+ getSession(): Promise<{
99
+ data: {
100
+ session: {
101
+ access_token: string;
102
+ user?: {
103
+ id: string;
104
+ } | null;
105
+ } | null;
106
+ };
107
+ }>;
108
+ onAuthStateChange(cb: (event: string, session: unknown) => void): {
109
+ data: {
110
+ subscription: {
111
+ unsubscribe(): void;
112
+ };
113
+ };
114
+ };
115
+ };
116
+ }
117
+ /** The credential shape `@ai-matrx/api`'s `CredentialsPort` accepts (structural; data owns the session). */
118
+ interface UserCredentialsPort {
119
+ get(): Promise<{
120
+ kind: "user";
121
+ accessToken: string;
122
+ } | null>;
123
+ onChange?(cb: () => void): () => void;
124
+ }
125
+ interface Db {
126
+ readonly client: SupabaseLike;
127
+ readonly role: "user" | "service";
128
+ /** The active organization — where NEW entity rows are saved. Never a read filter. */
129
+ activeOrganization(): string | null;
130
+ /** The session as `@ai-matrx/api`'s `user` credential (one sign-in feeds both lanes). */
131
+ readonly credentials: UserCredentialsPort;
132
+ }
133
+ interface CreateDbOptions {
134
+ /** The app's existing Supabase client (browser, RSC, route or the host's chat client). */
135
+ client: SupabaseLike;
136
+ /** Where new entity rows are saved. Read lazily on every insert; never consulted by a read. */
137
+ organization?: () => string | null | undefined;
138
+ }
139
+ /** The one database connection for a signed-in (or anonymous) person. */
140
+ declare function createDb(options: CreateDbOptions): Db;
141
+ /**
142
+ * Service-role doors — servers only. No session, no binding: every entity insert
143
+ * names its organization. Throws in a browser bundle.
144
+ */
145
+ declare function createServiceDb(options: {
146
+ client: SupabaseLike;
147
+ }): Db;
148
+ declare class DoorRefusedError extends Error {
149
+ readonly door: string;
150
+ readonly reason: string;
151
+ constructor(door: string, reason: string);
152
+ }
153
+ declare class DatabaseError extends Error {
154
+ readonly door: string;
155
+ readonly cause: PostgrestError;
156
+ constructor(door: string, cause: PostgrestError);
157
+ }
158
+ type Op = "read" | "insert" | "update" | "remove" | "archive";
159
+ /** Refuse before the wire when the descriptor's rules do not open this door. */
160
+ declare function assertDoor(db: Db, table: TableDescriptor<any, any, any>, op: Op): void;
161
+ type Col<R> = Extract<keyof R, string>;
162
+ /** The typed filter a `Where` callback receives — column names and values checked against the row. */
163
+ interface Filter<R> {
164
+ eq<K extends Col<R>>(column: K, value: NonNullable<R[K]>): Filter<R>;
165
+ neq<K extends Col<R>>(column: K, value: NonNullable<R[K]>): Filter<R>;
166
+ gt<K extends Col<R>>(column: K, value: NonNullable<R[K]>): Filter<R>;
167
+ gte<K extends Col<R>>(column: K, value: NonNullable<R[K]>): Filter<R>;
168
+ lt<K extends Col<R>>(column: K, value: NonNullable<R[K]>): Filter<R>;
169
+ lte<K extends Col<R>>(column: K, value: NonNullable<R[K]>): Filter<R>;
170
+ like(column: Col<R>, pattern: string): Filter<R>;
171
+ ilike(column: Col<R>, pattern: string): Filter<R>;
172
+ in<K extends Col<R>>(column: K, values: readonly NonNullable<R[K]>[]): Filter<R>;
173
+ is(column: Col<R>, value: null | boolean): Filter<R>;
174
+ contains(column: Col<R>, value: unknown): Filter<R>;
175
+ overlaps(column: Col<R>, value: unknown): Filter<R>;
176
+ textSearch(column: Col<R>, query: string, opts?: {
177
+ type?: "plain" | "phrase" | "websearch";
178
+ config?: string;
179
+ }): Filter<R>;
180
+ /** PostgREST's raw `or` grammar (`"pinned.eq.true,starred.eq.true"`). */
181
+ or(filters: string, opts?: {
182
+ referencedTable?: string;
183
+ }): Filter<R>;
184
+ not(column: Col<R>, operator: string, value: unknown): Filter<R>;
185
+ }
186
+ /** A filter callback — the same builder `readAllRows` callers write today, typed to the row. */
187
+ type Where<R = any> = (q: Filter<R>) => Filter<R>;
188
+ interface ReadOptions<C> {
189
+ /** Only these columns (the result is typed to them). Default every column. */
190
+ columns?: readonly C[];
191
+ /** Include soft-deleted rows (default: excluded on tables with `deleted_at`). */
192
+ includeDeleted?: boolean;
193
+ }
194
+ /** One row by primary key (a composite key takes `{ col: value }`), or null. */
195
+ declare function read<R, I, U, C extends Col<R> = Col<R>>(db: Db, table: TableDescriptor<R, I, U>, id: unknown, opts?: ReadOptions<C>): Promise<Pick<R, C> | null>;
196
+ /** EVERY matching row, paged past the 1,000-row cap (ordered by the full key); throws on a short read. */
197
+ declare function listAll<R, I, U, C extends Col<R> = Col<R>>(db: Db, table: TableDescriptor<R, I, U>, where?: Where<R>, opts?: ReadOptions<C> & {
198
+ orderBy?: Col<R>;
199
+ pageSize?: number;
200
+ maxRows?: number;
201
+ }): Promise<Pick<R, C>[]>;
202
+ /** One explicit window (`from`..`to`, inclusive) plus the exact total. */
203
+ declare function page<R, I, U, C extends Col<R> = Col<R>>(db: Db, table: TableDescriptor<R, I, U>, window: ReadOptions<C> & {
204
+ from: number;
205
+ to: number;
206
+ where?: Where<R>;
207
+ orderBy?: Col<R>;
208
+ ascending?: boolean;
209
+ }): Promise<{
210
+ rows: Pick<R, C>[];
211
+ total: number | null;
212
+ }>;
213
+ /** The exact count of matching rows (no rows transferred). */
214
+ declare function count<R, I, U>(db: Db, table: TableDescriptor<R, I, U>, where?: Where<R>, opts?: {
215
+ includeDeleted?: boolean;
216
+ }): Promise<number>;
217
+ /** Insert one row (or many) and return what the database stored. */
218
+ declare function insert<R, I, U>(db: Db, table: TableDescriptor<R, I, U>, values: I): Promise<R>;
219
+ declare function insert<R, I, U>(db: Db, table: TableDescriptor<R, I, U>, values: readonly I[]): Promise<R[]>;
220
+ /** Every write on a versioned table is explicit: `{ expectedVersion }` (guarded) or `{ overwrite: true }`. */
221
+ type UpdateGuard = {
222
+ expectedVersion: number;
223
+ } | {
224
+ overwrite: true;
225
+ };
226
+ type WriteResult<R> = {
227
+ status: "saved";
228
+ row: R;
229
+ rebasedFrom?: {
230
+ expectedVersion: number;
231
+ currentVersion: number;
232
+ };
233
+ } | {
234
+ status: "conflict";
235
+ currentRow: R;
236
+ currentVersion: number;
237
+ } | {
238
+ status: "not_found";
239
+ };
240
+ /**
241
+ * Update one row by primary key. On a versioned table the guard is required:
242
+ * `{ expectedVersion }` (a conflict comes back, never a silent overwrite) or
243
+ * `{ overwrite: true }`; on an unversioned table `expectedVersion` is refused.
244
+ */
245
+ declare function update<R, I, U>(db: Db, table: TableDescriptor<R, I, U>, id: unknown, patch: U, guard?: UpdateGuard): Promise<WriteResult<R>>;
246
+ /**
247
+ * Remove one row the way the table removes: soft delete (`deleted_at`) where it
248
+ * has one; an archive-only table refuses (use `archive`); a hard delete only where
249
+ * the table has neither. Guarded like `update` on a versioned table.
250
+ */
251
+ declare function remove<R, I, U>(db: Db, table: TableDescriptor<R, I, U>, id: unknown, guard?: UpdateGuard): Promise<{
252
+ status: "soft_deleted" | "deleted" | "not_found";
253
+ } | {
254
+ status: "conflict";
255
+ currentVersion: number;
256
+ }>;
257
+ /** Archive (or, with `restore`, bring back) one row by the table's archive column. Guarded like `update`. */
258
+ declare function archive<R, I, U>(db: Db, table: TableDescriptor<R, I, U>, id: unknown, opts?: {
259
+ restore?: boolean;
260
+ guard?: UpdateGuard;
261
+ }): Promise<WriteResult<R>>;
262
+ /** Bring back an archived or soft-deleted row. */
263
+ declare const restore: <R, I, U>(db: Db, table: TableDescriptor<R, I, U>, id: unknown, guard?: UpdateGuard) => Promise<WriteResult<R>>;
264
+ /** Call a database function through its generated door (PostgREST chooses an overload by argument names). */
265
+ declare function call<F extends FunctionDescriptor<any, any>>(db: Db, fn: F, args: ArgsOf<F>): Promise<ResultOf<F>>;
266
+ /** One ordering column of a set-returning function's rows (PostgREST `order`). */
267
+ type CallOrder = string | {
268
+ column: string;
269
+ ascending?: boolean;
270
+ nullsFirst?: boolean;
271
+ };
272
+ /**
273
+ * EVERY row a set-returning function answers, paged past the 1,000-row cap by
274
+ * `range` over a stable `order` (name enough columns to make it total — end on a key);
275
+ * throws a `DatabaseError` with the database's own error, or `IncompleteReadError` on a short read.
276
+ */
277
+ declare function callAll<F extends FunctionDescriptor<any, any>>(db: Db, fn: F, args: ArgsOf<F>, opts: {
278
+ order: readonly CallOrder[];
279
+ pageSize?: number;
280
+ maxRows?: number;
281
+ }): Promise<ResultOf<F> extends readonly (infer Row)[] ? Row[] : ResultOf<F>[]>;
282
+ /** One schema's flags (`<schema>.flags.ts` from `matrx-data generate --flags-index`): "schema.table" → descriptor. */
283
+ type FlagsIndex = Readonly<Record<string, TableDescriptor>>;
284
+ /** Per-schema loaders (`flags-index.ts`): only the schema a runtime name names is loaded (pay-only). */
285
+ type FlagLoaders = Readonly<Record<string, () => Promise<FlagsIndex>>>;
286
+ /** The door for a table whose name arrives at runtime — loosely typed, the same rules. */
287
+ declare function tableByName(index: FlagsIndex, name: string): TableDescriptor;
288
+ /** `tableByName` over per-schema loaders: loads only the named schema's flags. */
289
+ declare function loadTableByName(loaders: FlagLoaders, name: string): Promise<TableDescriptor>;
290
+ /** The `postgres_changes` filter for a table — only published tables have one (an unpublished table delivers nothing). */
291
+ declare function changesOf(table: TableDescriptor<any, any, any>): {
292
+ schema: string;
293
+ table: string;
294
+ };
295
+
296
+ export { type ArgsOf as A, restore as B, type CallOrder as C, type Db as D, tableByName as E, type Filter as F, update as G, type InsertOf as I, type OrgRule as O, type QueryLike as Q, type ReadOptions as R, type SupabaseLike as S, type TableDescriptor as T, type UserCredentialsPort as U, type Where as W, type CreateDbOptions as a, DatabaseError as b, DoorRefusedError as c, type FlagLoaders as d, type FlagsIndex as e, type FunctionDescriptor as f, type ResultOf as g, type RowOf as h, type TableType as i, type UpdateGuard as j, type UpdateOf as k, type WriteResult as l, archive as m, assertDoor as n, call as o, callAll as p, changesOf as q, count as r, createDb as s, createServiceDb as t, insert as u, listAll as v, loadTableByName as w, page as x, read as y, remove as z };