@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.
package/dist/db.d.cts CHANGED
@@ -1,7 +1,8 @@
1
1
  import { MaybeSingleResponse } from './index.cjs';
2
2
  export { a as AuthCookie, b as AuthCookieOptions, A as AuthCookieValue, C as CreateAuthCookieArgs, D as DEFAULT_AUTH_COOKIE_NAME, R as ReconciledAuthCookies, c as createAuthCookie, p as parseCookieHeader } from './auth-cookie-D4QjVz7W.cjs';
3
3
  export { uniqueChannelTopic } from '@ai-matrx/realtime';
4
- import { PostgrestError as PostgrestError$1 } from '@supabase/supabase-js';
4
+ import { PostgrestError } from '@supabase/supabase-js';
5
+ export { A as ArgsOf, C as CallOrder, a as CreateDbOptions, b as DatabaseError, D as Db, c as DoorRefusedError, F as Filter, d as FlagLoaders, e as FlagsIndex, f as FunctionDescriptor, I as InsertOf, O as OrgRule, Q as QueryLike, R as ReadOptions, g as ResultOf, h as RowOf, S as SupabaseLike, T as TableDescriptor, i as TableType, j as UpdateGuard, k as UpdateOf, U as UserCredentialsPort, W as Where, l as WriteResult, m as archive, n as assertDoor, o as call, p as callAll, q as changesOf, r as count, s as createDb, t as createServiceDb, u as insert, v as listAll, w as loadTableByName, x as page, y as read, z as remove, B as restore, E as tableByName, G as update } from './doors-D88CwoLU.cjs';
5
6
  import './credentials-B-rePKU7.cjs';
6
7
  import './net.cjs';
7
8
 
@@ -740,7 +741,7 @@ type WriteOneRowResult<Row> = {
740
741
  error: null;
741
742
  } | {
742
743
  data: null;
743
- error: PostgrestError$1;
744
+ error: PostgrestError;
744
745
  };
745
746
  /**
746
747
  * The drop-in for `.update(…).eq("id", …).select(…).single()` (and the
@@ -811,298 +812,4 @@ declare class RecordUnavailableError extends Error {
811
812
  declare function recordUnavailableMessage(entity: string, reason: RecordUnavailableReason): string;
812
813
  declare function isRecordUnavailableError(value: unknown): value is RecordUnavailableError;
813
814
 
814
- /** Registry type (`platform.entity_types.type`); null = untyped or no registry row. */
815
- type TableType = "entity" | "detail" | "ledger" | "reference" | "system" | "restricted" | "deprecated";
816
- type OrgRule = {
817
- readonly rule: "binding";
818
- } | {
819
- readonly rule: "parent";
820
- readonly parent: string;
821
- readonly via: string;
822
- } | {
823
- readonly rule: "column";
824
- } | {
825
- readonly rule: "none";
826
- };
827
- /** Phantom carrier for the generated row types — never present at runtime. */
828
- declare const ROW: unique symbol;
829
- interface TableDescriptor<Row = Record<string, unknown>, Insert = Record<string, unknown>, Update = Partial<Insert>> {
830
- readonly schema: string;
831
- readonly name: string;
832
- readonly kind: "table" | "view" | "materialized_view";
833
- readonly type: TableType | null;
834
- readonly pk: readonly string[];
835
- readonly org: OrgRule;
836
- readonly versioned: boolean;
837
- readonly softDelete: boolean;
838
- /** The archive column, when the table archives instead of (or as well as) soft-deleting. */
839
- readonly archive: "archived_at" | "is_archived" | null;
840
- readonly readOnly: boolean;
841
- readonly deletesRefused: boolean;
842
- readonly readOnlyColumns: readonly string[];
843
- readonly listScope: string | null;
844
- readonly realtime: boolean;
845
- /**
846
- * The table has a `created_by` column. A signed-in insert that omits it is stamped with the
847
- * session's person (row security on tables like `workbench.notes` refuses an insert whose
848
- * `created_by` is not `auth.uid()`). Optional: descriptors generated before 0.25.0 lack it.
849
- */
850
- readonly createdBy?: boolean;
851
- readonly [ROW]?: {
852
- row: Row;
853
- insert: Insert;
854
- update: Update;
855
- };
856
- }
857
- interface FunctionDescriptor<Args = Record<string, unknown>, Result = unknown> {
858
- readonly schema: string;
859
- readonly name: string;
860
- readonly roles: readonly ("anon" | "authenticated" | "service_role")[];
861
- readonly registered: boolean;
862
- readonly [ROW]?: {
863
- args: Args;
864
- result: Result;
865
- };
866
- }
867
- type RowOf<T> = T extends TableDescriptor<infer R, any, any> ? R : never;
868
- type InsertOf<T> = T extends TableDescriptor<any, infer I, any> ? I : never;
869
- type UpdateOf<T> = T extends TableDescriptor<any, any, infer U> ? U : never;
870
- type ArgsOf<F> = F extends FunctionDescriptor<infer A, any> ? A : never;
871
- type ResultOf<F> = F extends FunctionDescriptor<any, infer R> ? R : never;
872
- interface PostgrestError {
873
- message: string;
874
- code?: string;
875
- details?: string | null;
876
- hint?: string | null;
877
- }
878
- interface Resp<T> {
879
- data: T | null;
880
- error: PostgrestError | null;
881
- count?: number | null;
882
- }
883
- /** The slice of a supabase-js query builder the doors use (structural — any supabase-js 2.x client). */
884
- interface QueryLike extends PromiseLike<Resp<any>> {
885
- select(columns?: string, opts?: {
886
- count?: "exact" | "planned" | "estimated";
887
- head?: boolean;
888
- }): QueryLike;
889
- insert(values: unknown): QueryLike;
890
- update(values: unknown): QueryLike;
891
- delete(): QueryLike;
892
- eq(column: string, value: unknown): QueryLike;
893
- is(column: string, value: null | boolean): QueryLike;
894
- order(column: string, opts?: {
895
- ascending?: boolean;
896
- }): QueryLike;
897
- range(from: number, to: number): QueryLike;
898
- maybeSingle(): PromiseLike<Resp<any>>;
899
- single(): PromiseLike<Resp<any>>;
900
- }
901
- /** The slice of a supabase-js client the doors use. */
902
- interface SupabaseLike {
903
- schema(name: string): {
904
- from(table: string): QueryLike;
905
- rpc(fn: string, args?: Record<string, unknown>, opts?: {
906
- count?: "exact" | "planned" | "estimated";
907
- }): PromiseLike<Resp<any>>;
908
- };
909
- auth?: {
910
- getSession(): Promise<{
911
- data: {
912
- session: {
913
- access_token: string;
914
- user?: {
915
- id: string;
916
- } | null;
917
- } | null;
918
- };
919
- }>;
920
- onAuthStateChange(cb: (event: string, session: unknown) => void): {
921
- data: {
922
- subscription: {
923
- unsubscribe(): void;
924
- };
925
- };
926
- };
927
- };
928
- }
929
- /** The credential shape `@ai-matrx/api`'s `CredentialsPort` accepts (structural; data owns the session). */
930
- interface UserCredentialsPort {
931
- get(): Promise<{
932
- kind: "user";
933
- accessToken: string;
934
- } | null>;
935
- onChange?(cb: () => void): () => void;
936
- }
937
- interface Db {
938
- readonly client: SupabaseLike;
939
- readonly role: "user" | "service";
940
- /** The active organization — where NEW entity rows are saved. Never a read filter. */
941
- activeOrganization(): string | null;
942
- /** The session as `@ai-matrx/api`'s `user` credential (one sign-in feeds both lanes). */
943
- readonly credentials: UserCredentialsPort;
944
- }
945
- interface CreateDbOptions {
946
- /** The app's existing Supabase client (browser, RSC, route or the host's chat client). */
947
- client: SupabaseLike;
948
- /** Where new entity rows are saved. Read lazily on every insert; never consulted by a read. */
949
- organization?: () => string | null | undefined;
950
- }
951
- /** The one database connection for a signed-in (or anonymous) person. */
952
- declare function createDb(options: CreateDbOptions): Db;
953
- /**
954
- * Service-role doors — servers only. No session, no binding: every entity insert
955
- * names its organization. Throws in a browser bundle.
956
- */
957
- declare function createServiceDb(options: {
958
- client: SupabaseLike;
959
- }): Db;
960
- declare class DoorRefusedError extends Error {
961
- readonly door: string;
962
- readonly reason: string;
963
- constructor(door: string, reason: string);
964
- }
965
- declare class DatabaseError extends Error {
966
- readonly door: string;
967
- readonly cause: PostgrestError;
968
- constructor(door: string, cause: PostgrestError);
969
- }
970
- type Op = "read" | "insert" | "update" | "remove" | "archive";
971
- /** Refuse before the wire when the descriptor's rules do not open this door. */
972
- declare function assertDoor(db: Db, table: TableDescriptor<any, any, any>, op: Op): void;
973
- type Col<R> = Extract<keyof R, string>;
974
- /** The typed filter a `Where` callback receives — column names and values checked against the row. */
975
- interface Filter<R> {
976
- eq<K extends Col<R>>(column: K, value: NonNullable<R[K]>): Filter<R>;
977
- neq<K extends Col<R>>(column: K, value: NonNullable<R[K]>): Filter<R>;
978
- gt<K extends Col<R>>(column: K, value: NonNullable<R[K]>): Filter<R>;
979
- gte<K extends Col<R>>(column: K, value: NonNullable<R[K]>): Filter<R>;
980
- lt<K extends Col<R>>(column: K, value: NonNullable<R[K]>): Filter<R>;
981
- lte<K extends Col<R>>(column: K, value: NonNullable<R[K]>): Filter<R>;
982
- like(column: Col<R>, pattern: string): Filter<R>;
983
- ilike(column: Col<R>, pattern: string): Filter<R>;
984
- in<K extends Col<R>>(column: K, values: readonly NonNullable<R[K]>[]): Filter<R>;
985
- is(column: Col<R>, value: null | boolean): Filter<R>;
986
- contains(column: Col<R>, value: unknown): Filter<R>;
987
- overlaps(column: Col<R>, value: unknown): Filter<R>;
988
- textSearch(column: Col<R>, query: string, opts?: {
989
- type?: "plain" | "phrase" | "websearch";
990
- config?: string;
991
- }): Filter<R>;
992
- /** PostgREST's raw `or` grammar (`"pinned.eq.true,starred.eq.true"`). */
993
- or(filters: string, opts?: {
994
- referencedTable?: string;
995
- }): Filter<R>;
996
- not(column: Col<R>, operator: string, value: unknown): Filter<R>;
997
- }
998
- /** A filter callback — the same builder `readAllRows` callers write today, typed to the row. */
999
- type Where<R = any> = (q: Filter<R>) => Filter<R>;
1000
- interface ReadOptions<C> {
1001
- /** Only these columns (the result is typed to them). Default every column. */
1002
- columns?: readonly C[];
1003
- /** Include soft-deleted rows (default: excluded on tables with `deleted_at`). */
1004
- includeDeleted?: boolean;
1005
- }
1006
- /** One row by primary key (a composite key takes `{ col: value }`), or null. */
1007
- 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>;
1008
- /** EVERY matching row, paged past the 1,000-row cap (ordered by the full key); throws on a short read. */
1009
- 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> & {
1010
- orderBy?: Col<R>;
1011
- pageSize?: number;
1012
- maxRows?: number;
1013
- }): Promise<Pick<R, C>[]>;
1014
- /** One explicit window (`from`..`to`, inclusive) plus the exact total. */
1015
- declare function page<R, I, U, C extends Col<R> = Col<R>>(db: Db, table: TableDescriptor<R, I, U>, window: ReadOptions<C> & {
1016
- from: number;
1017
- to: number;
1018
- where?: Where<R>;
1019
- orderBy?: Col<R>;
1020
- ascending?: boolean;
1021
- }): Promise<{
1022
- rows: Pick<R, C>[];
1023
- total: number | null;
1024
- }>;
1025
- /** The exact count of matching rows (no rows transferred). */
1026
- declare function count<R, I, U>(db: Db, table: TableDescriptor<R, I, U>, where?: Where<R>, opts?: {
1027
- includeDeleted?: boolean;
1028
- }): Promise<number>;
1029
- /** Insert one row (or many) and return what the database stored. */
1030
- declare function insert<R, I, U>(db: Db, table: TableDescriptor<R, I, U>, values: I): Promise<R>;
1031
- declare function insert<R, I, U>(db: Db, table: TableDescriptor<R, I, U>, values: readonly I[]): Promise<R[]>;
1032
- /** Every write on a versioned table is explicit: `{ expectedVersion }` (guarded) or `{ overwrite: true }`. */
1033
- type UpdateGuard = {
1034
- expectedVersion: number;
1035
- } | {
1036
- overwrite: true;
1037
- };
1038
- type WriteResult<R> = {
1039
- status: "saved";
1040
- row: R;
1041
- rebasedFrom?: {
1042
- expectedVersion: number;
1043
- currentVersion: number;
1044
- };
1045
- } | {
1046
- status: "conflict";
1047
- currentRow: R;
1048
- currentVersion: number;
1049
- } | {
1050
- status: "not_found";
1051
- };
1052
- /**
1053
- * Update one row by primary key. On a versioned table the guard is required:
1054
- * `{ expectedVersion }` (a conflict comes back, never a silent overwrite) or
1055
- * `{ overwrite: true }`; on an unversioned table `expectedVersion` is refused.
1056
- */
1057
- declare function update<R, I, U>(db: Db, table: TableDescriptor<R, I, U>, id: unknown, patch: U, guard?: UpdateGuard): Promise<WriteResult<R>>;
1058
- /**
1059
- * Remove one row the way the table removes: soft delete (`deleted_at`) where it
1060
- * has one; an archive-only table refuses (use `archive`); a hard delete only where
1061
- * the table has neither. Guarded like `update` on a versioned table.
1062
- */
1063
- declare function remove<R, I, U>(db: Db, table: TableDescriptor<R, I, U>, id: unknown, guard?: UpdateGuard): Promise<{
1064
- status: "soft_deleted" | "deleted" | "not_found";
1065
- } | {
1066
- status: "conflict";
1067
- currentVersion: number;
1068
- }>;
1069
- /** Archive (or, with `restore`, bring back) one row by the table's archive column. Guarded like `update`. */
1070
- declare function archive<R, I, U>(db: Db, table: TableDescriptor<R, I, U>, id: unknown, opts?: {
1071
- restore?: boolean;
1072
- guard?: UpdateGuard;
1073
- }): Promise<WriteResult<R>>;
1074
- /** Bring back an archived or soft-deleted row. */
1075
- declare const restore: <R, I, U>(db: Db, table: TableDescriptor<R, I, U>, id: unknown, guard?: UpdateGuard) => Promise<WriteResult<R>>;
1076
- /** Call a database function through its generated door (PostgREST chooses an overload by argument names). */
1077
- declare function call<F extends FunctionDescriptor<any, any>>(db: Db, fn: F, args: ArgsOf<F>): Promise<ResultOf<F>>;
1078
- /** One ordering column of a set-returning function's rows (PostgREST `order`). */
1079
- type CallOrder = string | {
1080
- column: string;
1081
- ascending?: boolean;
1082
- nullsFirst?: boolean;
1083
- };
1084
- /**
1085
- * EVERY row a set-returning function answers, paged past the 1,000-row cap by
1086
- * `range` over a stable `order` (name enough columns to make it total — end on a key);
1087
- * throws a `DatabaseError` with the database's own error, or `IncompleteReadError` on a short read.
1088
- */
1089
- declare function callAll<F extends FunctionDescriptor<any, any>>(db: Db, fn: F, args: ArgsOf<F>, opts: {
1090
- order: readonly CallOrder[];
1091
- pageSize?: number;
1092
- maxRows?: number;
1093
- }): Promise<ResultOf<F> extends readonly (infer Row)[] ? Row[] : ResultOf<F>[]>;
1094
- /** One schema's flags (`<schema>.flags.ts` from `matrx-data generate --flags-index`): "schema.table" → descriptor. */
1095
- type FlagsIndex = Readonly<Record<string, TableDescriptor>>;
1096
- /** Per-schema loaders (`flags-index.ts`): only the schema a runtime name names is loaded (pay-only). */
1097
- type FlagLoaders = Readonly<Record<string, () => Promise<FlagsIndex>>>;
1098
- /** The door for a table whose name arrives at runtime — loosely typed, the same rules. */
1099
- declare function tableByName(index: FlagsIndex, name: string): TableDescriptor;
1100
- /** `tableByName` over per-schema loaders: loads only the named schema's flags. */
1101
- declare function loadTableByName(loaders: FlagLoaders, name: string): Promise<TableDescriptor>;
1102
- /** The `postgres_changes` filter for a table — only published tables have one (an unpublished table delivers nothing). */
1103
- declare function changesOf(table: TableDescriptor<any, any, any>): {
1104
- schema: string;
1105
- table: string;
1106
- };
1107
-
1108
- export { type ActiveOrgCookie, type ActiveOrgCookieOptions, type ActiveOrgSelection, type ArgsOf, type AuthRetryableResult, BulkWriteError, type CallOrder, type CreateActiveOrgCookieArgs, type CreateDbOptions, type CreateOAuthRefreshFetchArgs, type CreateSessionRetryOptions, DEFAULT_ACTIVE_ORG_COOKIE_MAX_AGE, DEFAULT_ACTIVE_ORG_COOKIE_NAME, DatabaseError, type Db, DoorRefusedError, type Filter, type FlagLoaders, type FlagsIndex, type FunctionDescriptor, type GuardedUpdateArgs, type GuardedUpdateResult, IncompleteReadError, type InsertOf, type JsonObject, type MergeJsonColumnArgs, type MergeJsonColumnResult, type OAuthRefreshStorage, type OrgRule, type PagedReadQuery, type PagedReadResult, type QueryLike, type ReadAllRowsOptions, type ReadAllRowsRestOptions, type ReadOptions, RecordUnavailableError, type RecordUnavailableReason, type RecordUnavailableResolution, type ResultOf, type RowOf, type SessionSource, SessionUnavailableError, type SupabaseLike, type TableDescriptor, type TableType, type TryWriteOneResult, type UpdateGuard, type UpdateOf, type UserCredentialsPort, type VersionedRow, type Where, WriteDidNotLandError, type WriteDidNotLandReason, type WriteFailureWords, type WriteOneAction, type WriteOneOptions, type WriteOneRowResult, WriteRefusedError, type WriteResult, archive, asJsonObject, assertDoor, assertWriteLanded, call, callAll, changesOf, count, createActiveOrgCookie, createDb, createOAuthRefreshFetch, createServiceDb, createSessionRetry, decodeActiveOrgSelection, decodeOAuthRefreshClaims, describeWriteFailure, encodeActiveOrgSelection, guardedUpdate, insert, isMissingSessionError, isRecordUnavailableError, listAll, loadTableByName, mergeJsonColumn, page, personSentence, read, readAllRows, readAllRowsRest, recordUnavailableMessage, remove, restore, serverMessageFromBody, tableByName, tryReadAllRows, tryReadAllRowsRest, tryWriteOne, update, writeFailureStatus, writeOne, writeOneRow };
815
+ export { type ActiveOrgCookie, type ActiveOrgCookieOptions, type ActiveOrgSelection, type AuthRetryableResult, BulkWriteError, type CreateActiveOrgCookieArgs, type CreateOAuthRefreshFetchArgs, type CreateSessionRetryOptions, DEFAULT_ACTIVE_ORG_COOKIE_MAX_AGE, DEFAULT_ACTIVE_ORG_COOKIE_NAME, type GuardedUpdateArgs, type GuardedUpdateResult, IncompleteReadError, type JsonObject, type MergeJsonColumnArgs, type MergeJsonColumnResult, type OAuthRefreshStorage, type PagedReadQuery, type PagedReadResult, type ReadAllRowsOptions, type ReadAllRowsRestOptions, RecordUnavailableError, type RecordUnavailableReason, type RecordUnavailableResolution, type SessionSource, SessionUnavailableError, type TryWriteOneResult, type VersionedRow, WriteDidNotLandError, type WriteDidNotLandReason, type WriteFailureWords, type WriteOneAction, type WriteOneOptions, type WriteOneRowResult, WriteRefusedError, asJsonObject, assertWriteLanded, createActiveOrgCookie, createOAuthRefreshFetch, createSessionRetry, decodeActiveOrgSelection, decodeOAuthRefreshClaims, describeWriteFailure, encodeActiveOrgSelection, guardedUpdate, isMissingSessionError, isRecordUnavailableError, mergeJsonColumn, personSentence, readAllRows, readAllRowsRest, recordUnavailableMessage, serverMessageFromBody, tryReadAllRows, tryReadAllRowsRest, tryWriteOne, writeFailureStatus, writeOne, writeOneRow };
package/dist/db.d.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  import { MaybeSingleResponse } from './index.js';
2
2
  export { a as AuthCookie, b as AuthCookieOptions, A as AuthCookieValue, C as CreateAuthCookieArgs, D as DEFAULT_AUTH_COOKIE_NAME, R as ReconciledAuthCookies, c as createAuthCookie, p as parseCookieHeader } from './auth-cookie-D4QjVz7W.js';
3
3
  export { uniqueChannelTopic } from '@ai-matrx/realtime';
4
- import { PostgrestError as PostgrestError$1 } from '@supabase/supabase-js';
4
+ import { PostgrestError } from '@supabase/supabase-js';
5
+ export { A as ArgsOf, C as CallOrder, a as CreateDbOptions, b as DatabaseError, D as Db, c as DoorRefusedError, F as Filter, d as FlagLoaders, e as FlagsIndex, f as FunctionDescriptor, I as InsertOf, O as OrgRule, Q as QueryLike, R as ReadOptions, g as ResultOf, h as RowOf, S as SupabaseLike, T as TableDescriptor, i as TableType, j as UpdateGuard, k as UpdateOf, U as UserCredentialsPort, W as Where, l as WriteResult, m as archive, n as assertDoor, o as call, p as callAll, q as changesOf, r as count, s as createDb, t as createServiceDb, u as insert, v as listAll, w as loadTableByName, x as page, y as read, z as remove, B as restore, E as tableByName, G as update } from './doors-D88CwoLU.js';
5
6
  import './credentials-B-rePKU7.js';
6
7
  import './net.js';
7
8
 
@@ -740,7 +741,7 @@ type WriteOneRowResult<Row> = {
740
741
  error: null;
741
742
  } | {
742
743
  data: null;
743
- error: PostgrestError$1;
744
+ error: PostgrestError;
744
745
  };
745
746
  /**
746
747
  * The drop-in for `.update(…).eq("id", …).select(…).single()` (and the
@@ -811,298 +812,4 @@ declare class RecordUnavailableError extends Error {
811
812
  declare function recordUnavailableMessage(entity: string, reason: RecordUnavailableReason): string;
812
813
  declare function isRecordUnavailableError(value: unknown): value is RecordUnavailableError;
813
814
 
814
- /** Registry type (`platform.entity_types.type`); null = untyped or no registry row. */
815
- type TableType = "entity" | "detail" | "ledger" | "reference" | "system" | "restricted" | "deprecated";
816
- type OrgRule = {
817
- readonly rule: "binding";
818
- } | {
819
- readonly rule: "parent";
820
- readonly parent: string;
821
- readonly via: string;
822
- } | {
823
- readonly rule: "column";
824
- } | {
825
- readonly rule: "none";
826
- };
827
- /** Phantom carrier for the generated row types — never present at runtime. */
828
- declare const ROW: unique symbol;
829
- interface TableDescriptor<Row = Record<string, unknown>, Insert = Record<string, unknown>, Update = Partial<Insert>> {
830
- readonly schema: string;
831
- readonly name: string;
832
- readonly kind: "table" | "view" | "materialized_view";
833
- readonly type: TableType | null;
834
- readonly pk: readonly string[];
835
- readonly org: OrgRule;
836
- readonly versioned: boolean;
837
- readonly softDelete: boolean;
838
- /** The archive column, when the table archives instead of (or as well as) soft-deleting. */
839
- readonly archive: "archived_at" | "is_archived" | null;
840
- readonly readOnly: boolean;
841
- readonly deletesRefused: boolean;
842
- readonly readOnlyColumns: readonly string[];
843
- readonly listScope: string | null;
844
- readonly realtime: boolean;
845
- /**
846
- * The table has a `created_by` column. A signed-in insert that omits it is stamped with the
847
- * session's person (row security on tables like `workbench.notes` refuses an insert whose
848
- * `created_by` is not `auth.uid()`). Optional: descriptors generated before 0.25.0 lack it.
849
- */
850
- readonly createdBy?: boolean;
851
- readonly [ROW]?: {
852
- row: Row;
853
- insert: Insert;
854
- update: Update;
855
- };
856
- }
857
- interface FunctionDescriptor<Args = Record<string, unknown>, Result = unknown> {
858
- readonly schema: string;
859
- readonly name: string;
860
- readonly roles: readonly ("anon" | "authenticated" | "service_role")[];
861
- readonly registered: boolean;
862
- readonly [ROW]?: {
863
- args: Args;
864
- result: Result;
865
- };
866
- }
867
- type RowOf<T> = T extends TableDescriptor<infer R, any, any> ? R : never;
868
- type InsertOf<T> = T extends TableDescriptor<any, infer I, any> ? I : never;
869
- type UpdateOf<T> = T extends TableDescriptor<any, any, infer U> ? U : never;
870
- type ArgsOf<F> = F extends FunctionDescriptor<infer A, any> ? A : never;
871
- type ResultOf<F> = F extends FunctionDescriptor<any, infer R> ? R : never;
872
- interface PostgrestError {
873
- message: string;
874
- code?: string;
875
- details?: string | null;
876
- hint?: string | null;
877
- }
878
- interface Resp<T> {
879
- data: T | null;
880
- error: PostgrestError | null;
881
- count?: number | null;
882
- }
883
- /** The slice of a supabase-js query builder the doors use (structural — any supabase-js 2.x client). */
884
- interface QueryLike extends PromiseLike<Resp<any>> {
885
- select(columns?: string, opts?: {
886
- count?: "exact" | "planned" | "estimated";
887
- head?: boolean;
888
- }): QueryLike;
889
- insert(values: unknown): QueryLike;
890
- update(values: unknown): QueryLike;
891
- delete(): QueryLike;
892
- eq(column: string, value: unknown): QueryLike;
893
- is(column: string, value: null | boolean): QueryLike;
894
- order(column: string, opts?: {
895
- ascending?: boolean;
896
- }): QueryLike;
897
- range(from: number, to: number): QueryLike;
898
- maybeSingle(): PromiseLike<Resp<any>>;
899
- single(): PromiseLike<Resp<any>>;
900
- }
901
- /** The slice of a supabase-js client the doors use. */
902
- interface SupabaseLike {
903
- schema(name: string): {
904
- from(table: string): QueryLike;
905
- rpc(fn: string, args?: Record<string, unknown>, opts?: {
906
- count?: "exact" | "planned" | "estimated";
907
- }): PromiseLike<Resp<any>>;
908
- };
909
- auth?: {
910
- getSession(): Promise<{
911
- data: {
912
- session: {
913
- access_token: string;
914
- user?: {
915
- id: string;
916
- } | null;
917
- } | null;
918
- };
919
- }>;
920
- onAuthStateChange(cb: (event: string, session: unknown) => void): {
921
- data: {
922
- subscription: {
923
- unsubscribe(): void;
924
- };
925
- };
926
- };
927
- };
928
- }
929
- /** The credential shape `@ai-matrx/api`'s `CredentialsPort` accepts (structural; data owns the session). */
930
- interface UserCredentialsPort {
931
- get(): Promise<{
932
- kind: "user";
933
- accessToken: string;
934
- } | null>;
935
- onChange?(cb: () => void): () => void;
936
- }
937
- interface Db {
938
- readonly client: SupabaseLike;
939
- readonly role: "user" | "service";
940
- /** The active organization — where NEW entity rows are saved. Never a read filter. */
941
- activeOrganization(): string | null;
942
- /** The session as `@ai-matrx/api`'s `user` credential (one sign-in feeds both lanes). */
943
- readonly credentials: UserCredentialsPort;
944
- }
945
- interface CreateDbOptions {
946
- /** The app's existing Supabase client (browser, RSC, route or the host's chat client). */
947
- client: SupabaseLike;
948
- /** Where new entity rows are saved. Read lazily on every insert; never consulted by a read. */
949
- organization?: () => string | null | undefined;
950
- }
951
- /** The one database connection for a signed-in (or anonymous) person. */
952
- declare function createDb(options: CreateDbOptions): Db;
953
- /**
954
- * Service-role doors — servers only. No session, no binding: every entity insert
955
- * names its organization. Throws in a browser bundle.
956
- */
957
- declare function createServiceDb(options: {
958
- client: SupabaseLike;
959
- }): Db;
960
- declare class DoorRefusedError extends Error {
961
- readonly door: string;
962
- readonly reason: string;
963
- constructor(door: string, reason: string);
964
- }
965
- declare class DatabaseError extends Error {
966
- readonly door: string;
967
- readonly cause: PostgrestError;
968
- constructor(door: string, cause: PostgrestError);
969
- }
970
- type Op = "read" | "insert" | "update" | "remove" | "archive";
971
- /** Refuse before the wire when the descriptor's rules do not open this door. */
972
- declare function assertDoor(db: Db, table: TableDescriptor<any, any, any>, op: Op): void;
973
- type Col<R> = Extract<keyof R, string>;
974
- /** The typed filter a `Where` callback receives — column names and values checked against the row. */
975
- interface Filter<R> {
976
- eq<K extends Col<R>>(column: K, value: NonNullable<R[K]>): Filter<R>;
977
- neq<K extends Col<R>>(column: K, value: NonNullable<R[K]>): Filter<R>;
978
- gt<K extends Col<R>>(column: K, value: NonNullable<R[K]>): Filter<R>;
979
- gte<K extends Col<R>>(column: K, value: NonNullable<R[K]>): Filter<R>;
980
- lt<K extends Col<R>>(column: K, value: NonNullable<R[K]>): Filter<R>;
981
- lte<K extends Col<R>>(column: K, value: NonNullable<R[K]>): Filter<R>;
982
- like(column: Col<R>, pattern: string): Filter<R>;
983
- ilike(column: Col<R>, pattern: string): Filter<R>;
984
- in<K extends Col<R>>(column: K, values: readonly NonNullable<R[K]>[]): Filter<R>;
985
- is(column: Col<R>, value: null | boolean): Filter<R>;
986
- contains(column: Col<R>, value: unknown): Filter<R>;
987
- overlaps(column: Col<R>, value: unknown): Filter<R>;
988
- textSearch(column: Col<R>, query: string, opts?: {
989
- type?: "plain" | "phrase" | "websearch";
990
- config?: string;
991
- }): Filter<R>;
992
- /** PostgREST's raw `or` grammar (`"pinned.eq.true,starred.eq.true"`). */
993
- or(filters: string, opts?: {
994
- referencedTable?: string;
995
- }): Filter<R>;
996
- not(column: Col<R>, operator: string, value: unknown): Filter<R>;
997
- }
998
- /** A filter callback — the same builder `readAllRows` callers write today, typed to the row. */
999
- type Where<R = any> = (q: Filter<R>) => Filter<R>;
1000
- interface ReadOptions<C> {
1001
- /** Only these columns (the result is typed to them). Default every column. */
1002
- columns?: readonly C[];
1003
- /** Include soft-deleted rows (default: excluded on tables with `deleted_at`). */
1004
- includeDeleted?: boolean;
1005
- }
1006
- /** One row by primary key (a composite key takes `{ col: value }`), or null. */
1007
- 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>;
1008
- /** EVERY matching row, paged past the 1,000-row cap (ordered by the full key); throws on a short read. */
1009
- 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> & {
1010
- orderBy?: Col<R>;
1011
- pageSize?: number;
1012
- maxRows?: number;
1013
- }): Promise<Pick<R, C>[]>;
1014
- /** One explicit window (`from`..`to`, inclusive) plus the exact total. */
1015
- declare function page<R, I, U, C extends Col<R> = Col<R>>(db: Db, table: TableDescriptor<R, I, U>, window: ReadOptions<C> & {
1016
- from: number;
1017
- to: number;
1018
- where?: Where<R>;
1019
- orderBy?: Col<R>;
1020
- ascending?: boolean;
1021
- }): Promise<{
1022
- rows: Pick<R, C>[];
1023
- total: number | null;
1024
- }>;
1025
- /** The exact count of matching rows (no rows transferred). */
1026
- declare function count<R, I, U>(db: Db, table: TableDescriptor<R, I, U>, where?: Where<R>, opts?: {
1027
- includeDeleted?: boolean;
1028
- }): Promise<number>;
1029
- /** Insert one row (or many) and return what the database stored. */
1030
- declare function insert<R, I, U>(db: Db, table: TableDescriptor<R, I, U>, values: I): Promise<R>;
1031
- declare function insert<R, I, U>(db: Db, table: TableDescriptor<R, I, U>, values: readonly I[]): Promise<R[]>;
1032
- /** Every write on a versioned table is explicit: `{ expectedVersion }` (guarded) or `{ overwrite: true }`. */
1033
- type UpdateGuard = {
1034
- expectedVersion: number;
1035
- } | {
1036
- overwrite: true;
1037
- };
1038
- type WriteResult<R> = {
1039
- status: "saved";
1040
- row: R;
1041
- rebasedFrom?: {
1042
- expectedVersion: number;
1043
- currentVersion: number;
1044
- };
1045
- } | {
1046
- status: "conflict";
1047
- currentRow: R;
1048
- currentVersion: number;
1049
- } | {
1050
- status: "not_found";
1051
- };
1052
- /**
1053
- * Update one row by primary key. On a versioned table the guard is required:
1054
- * `{ expectedVersion }` (a conflict comes back, never a silent overwrite) or
1055
- * `{ overwrite: true }`; on an unversioned table `expectedVersion` is refused.
1056
- */
1057
- declare function update<R, I, U>(db: Db, table: TableDescriptor<R, I, U>, id: unknown, patch: U, guard?: UpdateGuard): Promise<WriteResult<R>>;
1058
- /**
1059
- * Remove one row the way the table removes: soft delete (`deleted_at`) where it
1060
- * has one; an archive-only table refuses (use `archive`); a hard delete only where
1061
- * the table has neither. Guarded like `update` on a versioned table.
1062
- */
1063
- declare function remove<R, I, U>(db: Db, table: TableDescriptor<R, I, U>, id: unknown, guard?: UpdateGuard): Promise<{
1064
- status: "soft_deleted" | "deleted" | "not_found";
1065
- } | {
1066
- status: "conflict";
1067
- currentVersion: number;
1068
- }>;
1069
- /** Archive (or, with `restore`, bring back) one row by the table's archive column. Guarded like `update`. */
1070
- declare function archive<R, I, U>(db: Db, table: TableDescriptor<R, I, U>, id: unknown, opts?: {
1071
- restore?: boolean;
1072
- guard?: UpdateGuard;
1073
- }): Promise<WriteResult<R>>;
1074
- /** Bring back an archived or soft-deleted row. */
1075
- declare const restore: <R, I, U>(db: Db, table: TableDescriptor<R, I, U>, id: unknown, guard?: UpdateGuard) => Promise<WriteResult<R>>;
1076
- /** Call a database function through its generated door (PostgREST chooses an overload by argument names). */
1077
- declare function call<F extends FunctionDescriptor<any, any>>(db: Db, fn: F, args: ArgsOf<F>): Promise<ResultOf<F>>;
1078
- /** One ordering column of a set-returning function's rows (PostgREST `order`). */
1079
- type CallOrder = string | {
1080
- column: string;
1081
- ascending?: boolean;
1082
- nullsFirst?: boolean;
1083
- };
1084
- /**
1085
- * EVERY row a set-returning function answers, paged past the 1,000-row cap by
1086
- * `range` over a stable `order` (name enough columns to make it total — end on a key);
1087
- * throws a `DatabaseError` with the database's own error, or `IncompleteReadError` on a short read.
1088
- */
1089
- declare function callAll<F extends FunctionDescriptor<any, any>>(db: Db, fn: F, args: ArgsOf<F>, opts: {
1090
- order: readonly CallOrder[];
1091
- pageSize?: number;
1092
- maxRows?: number;
1093
- }): Promise<ResultOf<F> extends readonly (infer Row)[] ? Row[] : ResultOf<F>[]>;
1094
- /** One schema's flags (`<schema>.flags.ts` from `matrx-data generate --flags-index`): "schema.table" → descriptor. */
1095
- type FlagsIndex = Readonly<Record<string, TableDescriptor>>;
1096
- /** Per-schema loaders (`flags-index.ts`): only the schema a runtime name names is loaded (pay-only). */
1097
- type FlagLoaders = Readonly<Record<string, () => Promise<FlagsIndex>>>;
1098
- /** The door for a table whose name arrives at runtime — loosely typed, the same rules. */
1099
- declare function tableByName(index: FlagsIndex, name: string): TableDescriptor;
1100
- /** `tableByName` over per-schema loaders: loads only the named schema's flags. */
1101
- declare function loadTableByName(loaders: FlagLoaders, name: string): Promise<TableDescriptor>;
1102
- /** The `postgres_changes` filter for a table — only published tables have one (an unpublished table delivers nothing). */
1103
- declare function changesOf(table: TableDescriptor<any, any, any>): {
1104
- schema: string;
1105
- table: string;
1106
- };
1107
-
1108
- export { type ActiveOrgCookie, type ActiveOrgCookieOptions, type ActiveOrgSelection, type ArgsOf, type AuthRetryableResult, BulkWriteError, type CallOrder, type CreateActiveOrgCookieArgs, type CreateDbOptions, type CreateOAuthRefreshFetchArgs, type CreateSessionRetryOptions, DEFAULT_ACTIVE_ORG_COOKIE_MAX_AGE, DEFAULT_ACTIVE_ORG_COOKIE_NAME, DatabaseError, type Db, DoorRefusedError, type Filter, type FlagLoaders, type FlagsIndex, type FunctionDescriptor, type GuardedUpdateArgs, type GuardedUpdateResult, IncompleteReadError, type InsertOf, type JsonObject, type MergeJsonColumnArgs, type MergeJsonColumnResult, type OAuthRefreshStorage, type OrgRule, type PagedReadQuery, type PagedReadResult, type QueryLike, type ReadAllRowsOptions, type ReadAllRowsRestOptions, type ReadOptions, RecordUnavailableError, type RecordUnavailableReason, type RecordUnavailableResolution, type ResultOf, type RowOf, type SessionSource, SessionUnavailableError, type SupabaseLike, type TableDescriptor, type TableType, type TryWriteOneResult, type UpdateGuard, type UpdateOf, type UserCredentialsPort, type VersionedRow, type Where, WriteDidNotLandError, type WriteDidNotLandReason, type WriteFailureWords, type WriteOneAction, type WriteOneOptions, type WriteOneRowResult, WriteRefusedError, type WriteResult, archive, asJsonObject, assertDoor, assertWriteLanded, call, callAll, changesOf, count, createActiveOrgCookie, createDb, createOAuthRefreshFetch, createServiceDb, createSessionRetry, decodeActiveOrgSelection, decodeOAuthRefreshClaims, describeWriteFailure, encodeActiveOrgSelection, guardedUpdate, insert, isMissingSessionError, isRecordUnavailableError, listAll, loadTableByName, mergeJsonColumn, page, personSentence, read, readAllRows, readAllRowsRest, recordUnavailableMessage, remove, restore, serverMessageFromBody, tableByName, tryReadAllRows, tryReadAllRowsRest, tryWriteOne, update, writeFailureStatus, writeOne, writeOneRow };
815
+ export { type ActiveOrgCookie, type ActiveOrgCookieOptions, type ActiveOrgSelection, type AuthRetryableResult, BulkWriteError, type CreateActiveOrgCookieArgs, type CreateOAuthRefreshFetchArgs, type CreateSessionRetryOptions, DEFAULT_ACTIVE_ORG_COOKIE_MAX_AGE, DEFAULT_ACTIVE_ORG_COOKIE_NAME, type GuardedUpdateArgs, type GuardedUpdateResult, IncompleteReadError, type JsonObject, type MergeJsonColumnArgs, type MergeJsonColumnResult, type OAuthRefreshStorage, type PagedReadQuery, type PagedReadResult, type ReadAllRowsOptions, type ReadAllRowsRestOptions, RecordUnavailableError, type RecordUnavailableReason, type RecordUnavailableResolution, type SessionSource, SessionUnavailableError, type TryWriteOneResult, type VersionedRow, WriteDidNotLandError, type WriteDidNotLandReason, type WriteFailureWords, type WriteOneAction, type WriteOneOptions, type WriteOneRowResult, WriteRefusedError, asJsonObject, assertWriteLanded, createActiveOrgCookie, createOAuthRefreshFetch, createSessionRetry, decodeActiveOrgSelection, decodeOAuthRefreshClaims, describeWriteFailure, encodeActiveOrgSelection, guardedUpdate, isMissingSessionError, isRecordUnavailableError, mergeJsonColumn, personSentence, readAllRows, readAllRowsRest, recordUnavailableMessage, serverMessageFromBody, tryReadAllRows, tryReadAllRowsRest, tryWriteOne, writeFailureStatus, writeOne, writeOneRow };