@ai-matrx/data 0.22.33 → 0.23.0
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/CHANGELOG.md +26 -0
- package/bin/direct-db.mjs +80 -0
- package/bin/emit.mjs +355 -0
- package/bin/generate.mjs +196 -0
- package/bin/matrx-data.mjs +47 -0
- package/dist/db.cjs +240 -19
- package/dist/db.cjs.map +1 -1
- package/dist/db.d.cts +220 -3
- package/dist/db.d.ts +220 -3
- package/dist/db.js +236 -15
- package/dist/db.js.map +1 -1
- package/package.json +6 -2
package/dist/db.d.cts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
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 } from '@supabase/supabase-js';
|
|
4
|
+
import { PostgrestError as PostgrestError$1 } from '@supabase/supabase-js';
|
|
5
5
|
import './credentials-B-rePKU7.cjs';
|
|
6
6
|
import './net.cjs';
|
|
7
7
|
|
|
@@ -740,7 +740,7 @@ type WriteOneRowResult<Row> = {
|
|
|
740
740
|
error: null;
|
|
741
741
|
} | {
|
|
742
742
|
data: null;
|
|
743
|
-
error: PostgrestError;
|
|
743
|
+
error: PostgrestError$1;
|
|
744
744
|
};
|
|
745
745
|
/**
|
|
746
746
|
* The drop-in for `.update(…).eq("id", …).select(…).single()` (and the
|
|
@@ -811,4 +811,221 @@ declare class RecordUnavailableError extends Error {
|
|
|
811
811
|
declare function recordUnavailableMessage(entity: string, reason: RecordUnavailableReason): string;
|
|
812
812
|
declare function isRecordUnavailableError(value: unknown): value is RecordUnavailableError;
|
|
813
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
|
+
readonly [ROW]?: {
|
|
846
|
+
row: Row;
|
|
847
|
+
insert: Insert;
|
|
848
|
+
update: Update;
|
|
849
|
+
};
|
|
850
|
+
}
|
|
851
|
+
interface FunctionDescriptor<Args = Record<string, unknown>, Result = unknown> {
|
|
852
|
+
readonly schema: string;
|
|
853
|
+
readonly name: string;
|
|
854
|
+
readonly roles: readonly ("anon" | "authenticated" | "service_role")[];
|
|
855
|
+
readonly registered: boolean;
|
|
856
|
+
readonly [ROW]?: {
|
|
857
|
+
args: Args;
|
|
858
|
+
result: Result;
|
|
859
|
+
};
|
|
860
|
+
}
|
|
861
|
+
type RowOf<T> = T extends TableDescriptor<infer R, any, any> ? R : never;
|
|
862
|
+
type InsertOf<T> = T extends TableDescriptor<any, infer I, any> ? I : never;
|
|
863
|
+
type UpdateOf<T> = T extends TableDescriptor<any, any, infer U> ? U : never;
|
|
864
|
+
type ArgsOf<F> = F extends FunctionDescriptor<infer A, any> ? A : never;
|
|
865
|
+
type ResultOf<F> = F extends FunctionDescriptor<any, infer R> ? R : never;
|
|
866
|
+
interface PostgrestError {
|
|
867
|
+
message: string;
|
|
868
|
+
code?: string;
|
|
869
|
+
details?: string | null;
|
|
870
|
+
hint?: string | null;
|
|
871
|
+
}
|
|
872
|
+
interface Resp<T> {
|
|
873
|
+
data: T | null;
|
|
874
|
+
error: PostgrestError | null;
|
|
875
|
+
count?: number | null;
|
|
876
|
+
}
|
|
877
|
+
/** The slice of a supabase-js query builder the doors use (structural — any supabase-js 2.x client). */
|
|
878
|
+
interface QueryLike extends PromiseLike<Resp<any>> {
|
|
879
|
+
select(columns?: string, opts?: {
|
|
880
|
+
count?: "exact" | "planned" | "estimated";
|
|
881
|
+
head?: boolean;
|
|
882
|
+
}): QueryLike;
|
|
883
|
+
insert(values: unknown): QueryLike;
|
|
884
|
+
update(values: unknown): QueryLike;
|
|
885
|
+
delete(): QueryLike;
|
|
886
|
+
eq(column: string, value: unknown): QueryLike;
|
|
887
|
+
order(column: string, opts?: {
|
|
888
|
+
ascending?: boolean;
|
|
889
|
+
}): QueryLike;
|
|
890
|
+
range(from: number, to: number): QueryLike;
|
|
891
|
+
maybeSingle(): PromiseLike<Resp<any>>;
|
|
892
|
+
single(): PromiseLike<Resp<any>>;
|
|
893
|
+
}
|
|
894
|
+
/** The slice of a supabase-js client the doors use. */
|
|
895
|
+
interface SupabaseLike {
|
|
896
|
+
schema(name: string): {
|
|
897
|
+
from(table: string): QueryLike;
|
|
898
|
+
rpc(fn: string, args?: Record<string, unknown>): PromiseLike<Resp<any>>;
|
|
899
|
+
};
|
|
900
|
+
auth?: {
|
|
901
|
+
getSession(): Promise<{
|
|
902
|
+
data: {
|
|
903
|
+
session: {
|
|
904
|
+
access_token: string;
|
|
905
|
+
} | null;
|
|
906
|
+
};
|
|
907
|
+
}>;
|
|
908
|
+
onAuthStateChange(cb: (event: string, session: unknown) => void): {
|
|
909
|
+
data: {
|
|
910
|
+
subscription: {
|
|
911
|
+
unsubscribe(): void;
|
|
912
|
+
};
|
|
913
|
+
};
|
|
914
|
+
};
|
|
915
|
+
};
|
|
916
|
+
}
|
|
917
|
+
/** The credential shape `@ai-matrx/api`'s `CredentialsPort` accepts (structural; data owns the session). */
|
|
918
|
+
interface UserCredentialsPort {
|
|
919
|
+
get(): Promise<{
|
|
920
|
+
kind: "user";
|
|
921
|
+
accessToken: string;
|
|
922
|
+
} | null>;
|
|
923
|
+
onChange?(cb: () => void): () => void;
|
|
924
|
+
}
|
|
925
|
+
interface Db {
|
|
926
|
+
readonly client: SupabaseLike;
|
|
927
|
+
readonly role: "user" | "service";
|
|
928
|
+
/** The active organization — where NEW entity rows are saved. Never a read filter. */
|
|
929
|
+
activeOrganization(): string | null;
|
|
930
|
+
/** The session as `@ai-matrx/api`'s `user` credential (one sign-in feeds both lanes). */
|
|
931
|
+
readonly credentials: UserCredentialsPort;
|
|
932
|
+
}
|
|
933
|
+
interface CreateDbOptions {
|
|
934
|
+
/** The app's existing Supabase client (browser, RSC, route or the host's chat client). */
|
|
935
|
+
client: SupabaseLike;
|
|
936
|
+
/** Where new entity rows are saved. Read lazily on every insert; never consulted by a read. */
|
|
937
|
+
organization?: () => string | null | undefined;
|
|
938
|
+
}
|
|
939
|
+
/** The one database connection for a signed-in (or anonymous) person. */
|
|
940
|
+
declare function createDb(options: CreateDbOptions): Db;
|
|
941
|
+
/**
|
|
942
|
+
* Service-role doors — servers only. No session, no binding: every entity insert
|
|
943
|
+
* names its organization. Throws in a browser bundle.
|
|
944
|
+
*/
|
|
945
|
+
declare function createServiceDb(options: {
|
|
946
|
+
client: SupabaseLike;
|
|
947
|
+
}): Db;
|
|
948
|
+
declare class DoorRefusedError extends Error {
|
|
949
|
+
readonly door: string;
|
|
950
|
+
readonly reason: string;
|
|
951
|
+
constructor(door: string, reason: string);
|
|
952
|
+
}
|
|
953
|
+
declare class DatabaseError extends Error {
|
|
954
|
+
readonly door: string;
|
|
955
|
+
readonly cause: PostgrestError;
|
|
956
|
+
constructor(door: string, cause: PostgrestError);
|
|
957
|
+
}
|
|
958
|
+
type Op = "read" | "insert" | "update" | "remove" | "archive";
|
|
959
|
+
/** Refuse before the wire when the descriptor's rules do not open this door. */
|
|
960
|
+
declare function assertDoor(db: Db, table: TableDescriptor<any, any, any>, op: Op): void;
|
|
961
|
+
/** One row by primary key (a composite key takes `{ col: value }`), or null. */
|
|
962
|
+
declare function read<T extends TableDescriptor<any, any, any>>(db: Db, table: T, id: unknown, columns?: string): Promise<RowOf<T> | null>;
|
|
963
|
+
/** A filter callback — the same builder `readAllRows` callers write today (`q => q.or(…).gte(…)`). */
|
|
964
|
+
type Where = (q: any) => any;
|
|
965
|
+
/** EVERY matching row, paged past the 1,000-row cap; throws on a short read. */
|
|
966
|
+
declare function listAll<T extends TableDescriptor<any, any, any>>(db: Db, table: T, where?: Where, opts?: {
|
|
967
|
+
columns?: string;
|
|
968
|
+
orderBy?: string;
|
|
969
|
+
pageSize?: number;
|
|
970
|
+
maxRows?: number;
|
|
971
|
+
}): Promise<RowOf<T>[]>;
|
|
972
|
+
/** One explicit window (`from`..`to`, inclusive) plus the exact total. */
|
|
973
|
+
declare function page<T extends TableDescriptor<any, any, any>>(db: Db, table: T, window: {
|
|
974
|
+
from: number;
|
|
975
|
+
to: number;
|
|
976
|
+
where?: Where;
|
|
977
|
+
columns?: string;
|
|
978
|
+
orderBy?: string;
|
|
979
|
+
ascending?: boolean;
|
|
980
|
+
}): Promise<{
|
|
981
|
+
rows: RowOf<T>[];
|
|
982
|
+
total: number | null;
|
|
983
|
+
}>;
|
|
984
|
+
/** The exact count of matching rows (no rows transferred). */
|
|
985
|
+
declare function count<T extends TableDescriptor<any, any, any>>(db: Db, table: T, where?: Where): Promise<number>;
|
|
986
|
+
/** Insert one row (or many) and return what the database stored. */
|
|
987
|
+
declare function insert<T extends TableDescriptor<any, any, any>>(db: Db, table: T, values: InsertOf<T>): Promise<RowOf<T>>;
|
|
988
|
+
declare function insert<T extends TableDescriptor<any, any, any>>(db: Db, table: T, values: InsertOf<T>[]): Promise<RowOf<T>[]>;
|
|
989
|
+
type UpdateGuard = {
|
|
990
|
+
expectedVersion: number;
|
|
991
|
+
} | {
|
|
992
|
+
overwrite: true;
|
|
993
|
+
};
|
|
994
|
+
/**
|
|
995
|
+
* Update one row by primary key. On a versioned table the guard is required:
|
|
996
|
+
* `{ expectedVersion }` (a conflict comes back, never a silent overwrite) or
|
|
997
|
+
* `{ overwrite: true }`.
|
|
998
|
+
*/
|
|
999
|
+
declare function update<T extends TableDescriptor<any, any, any>>(db: Db, table: T, id: unknown, patch: UpdateOf<T>, guard?: UpdateGuard): Promise<GuardedUpdateResult<RowOf<T> & {
|
|
1000
|
+
version: number;
|
|
1001
|
+
}> | {
|
|
1002
|
+
status: "saved";
|
|
1003
|
+
row: RowOf<T>;
|
|
1004
|
+
} | {
|
|
1005
|
+
status: "not_found";
|
|
1006
|
+
}>;
|
|
1007
|
+
/**
|
|
1008
|
+
* Remove one row the way the table removes: soft delete (`deleted_at`) where it
|
|
1009
|
+
* has one; an archive-only table refuses (use `archive`); a hard delete only where
|
|
1010
|
+
* the table has neither.
|
|
1011
|
+
*/
|
|
1012
|
+
declare function remove<T extends TableDescriptor<any, any, any>>(db: Db, table: T, id: unknown): Promise<"soft_deleted" | "deleted" | "not_found">;
|
|
1013
|
+
/** Archive (or, with `restore`, bring back) one row by the table's archive column. */
|
|
1014
|
+
declare function archive<T extends TableDescriptor<any, any, any>>(db: Db, table: T, id: unknown, opts?: {
|
|
1015
|
+
restore?: boolean;
|
|
1016
|
+
}): Promise<RowOf<T> | null>;
|
|
1017
|
+
/** Bring back an archived or soft-deleted row. */
|
|
1018
|
+
declare const restore: <T extends TableDescriptor<any, any, any>>(db: Db, table: T, id: unknown) => Promise<RowOf<T> | null>;
|
|
1019
|
+
/** Call a database function through its generated door (PostgREST chooses an overload by argument names). */
|
|
1020
|
+
declare function call<F extends FunctionDescriptor<any, any>>(db: Db, fn: F, args: ArgsOf<F>): Promise<ResultOf<F>>;
|
|
1021
|
+
/** The compact flags index `matrx-data generate --flags-index` writes: "schema.table" → descriptor. */
|
|
1022
|
+
type FlagsIndex = Readonly<Record<string, TableDescriptor>>;
|
|
1023
|
+
/** The door for a table whose name arrives at runtime — loosely typed, the same rules. */
|
|
1024
|
+
declare function tableByName(index: FlagsIndex, name: string): TableDescriptor;
|
|
1025
|
+
/** The `postgres_changes` filter for a table — only published tables have one (an unpublished table delivers nothing). */
|
|
1026
|
+
declare function changesOf(table: TableDescriptor<any, any, any>): {
|
|
1027
|
+
schema: string;
|
|
1028
|
+
table: string;
|
|
1029
|
+
};
|
|
1030
|
+
|
|
1031
|
+
export { type ActiveOrgCookie, type ActiveOrgCookieOptions, type ActiveOrgSelection, type ArgsOf, type AuthRetryableResult, BulkWriteError, type CreateActiveOrgCookieArgs, type CreateDbOptions, type CreateOAuthRefreshFetchArgs, type CreateSessionRetryOptions, DEFAULT_ACTIVE_ORG_COOKIE_MAX_AGE, DEFAULT_ACTIVE_ORG_COOKIE_NAME, DatabaseError, type Db, DoorRefusedError, 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, 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, archive, asJsonObject, assertDoor, assertWriteLanded, call, changesOf, count, createActiveOrgCookie, createDb, createOAuthRefreshFetch, createServiceDb, createSessionRetry, decodeActiveOrgSelection, decodeOAuthRefreshClaims, describeWriteFailure, encodeActiveOrgSelection, guardedUpdate, insert, isMissingSessionError, isRecordUnavailableError, listAll, mergeJsonColumn, page, personSentence, read, readAllRows, readAllRowsRest, recordUnavailableMessage, remove, restore, serverMessageFromBody, tableByName, tryReadAllRows, tryReadAllRowsRest, tryWriteOne, update, writeFailureStatus, writeOne, writeOneRow };
|
package/dist/db.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
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 } from '@supabase/supabase-js';
|
|
4
|
+
import { PostgrestError as PostgrestError$1 } from '@supabase/supabase-js';
|
|
5
5
|
import './credentials-B-rePKU7.js';
|
|
6
6
|
import './net.js';
|
|
7
7
|
|
|
@@ -740,7 +740,7 @@ type WriteOneRowResult<Row> = {
|
|
|
740
740
|
error: null;
|
|
741
741
|
} | {
|
|
742
742
|
data: null;
|
|
743
|
-
error: PostgrestError;
|
|
743
|
+
error: PostgrestError$1;
|
|
744
744
|
};
|
|
745
745
|
/**
|
|
746
746
|
* The drop-in for `.update(…).eq("id", …).select(…).single()` (and the
|
|
@@ -811,4 +811,221 @@ declare class RecordUnavailableError extends Error {
|
|
|
811
811
|
declare function recordUnavailableMessage(entity: string, reason: RecordUnavailableReason): string;
|
|
812
812
|
declare function isRecordUnavailableError(value: unknown): value is RecordUnavailableError;
|
|
813
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
|
+
readonly [ROW]?: {
|
|
846
|
+
row: Row;
|
|
847
|
+
insert: Insert;
|
|
848
|
+
update: Update;
|
|
849
|
+
};
|
|
850
|
+
}
|
|
851
|
+
interface FunctionDescriptor<Args = Record<string, unknown>, Result = unknown> {
|
|
852
|
+
readonly schema: string;
|
|
853
|
+
readonly name: string;
|
|
854
|
+
readonly roles: readonly ("anon" | "authenticated" | "service_role")[];
|
|
855
|
+
readonly registered: boolean;
|
|
856
|
+
readonly [ROW]?: {
|
|
857
|
+
args: Args;
|
|
858
|
+
result: Result;
|
|
859
|
+
};
|
|
860
|
+
}
|
|
861
|
+
type RowOf<T> = T extends TableDescriptor<infer R, any, any> ? R : never;
|
|
862
|
+
type InsertOf<T> = T extends TableDescriptor<any, infer I, any> ? I : never;
|
|
863
|
+
type UpdateOf<T> = T extends TableDescriptor<any, any, infer U> ? U : never;
|
|
864
|
+
type ArgsOf<F> = F extends FunctionDescriptor<infer A, any> ? A : never;
|
|
865
|
+
type ResultOf<F> = F extends FunctionDescriptor<any, infer R> ? R : never;
|
|
866
|
+
interface PostgrestError {
|
|
867
|
+
message: string;
|
|
868
|
+
code?: string;
|
|
869
|
+
details?: string | null;
|
|
870
|
+
hint?: string | null;
|
|
871
|
+
}
|
|
872
|
+
interface Resp<T> {
|
|
873
|
+
data: T | null;
|
|
874
|
+
error: PostgrestError | null;
|
|
875
|
+
count?: number | null;
|
|
876
|
+
}
|
|
877
|
+
/** The slice of a supabase-js query builder the doors use (structural — any supabase-js 2.x client). */
|
|
878
|
+
interface QueryLike extends PromiseLike<Resp<any>> {
|
|
879
|
+
select(columns?: string, opts?: {
|
|
880
|
+
count?: "exact" | "planned" | "estimated";
|
|
881
|
+
head?: boolean;
|
|
882
|
+
}): QueryLike;
|
|
883
|
+
insert(values: unknown): QueryLike;
|
|
884
|
+
update(values: unknown): QueryLike;
|
|
885
|
+
delete(): QueryLike;
|
|
886
|
+
eq(column: string, value: unknown): QueryLike;
|
|
887
|
+
order(column: string, opts?: {
|
|
888
|
+
ascending?: boolean;
|
|
889
|
+
}): QueryLike;
|
|
890
|
+
range(from: number, to: number): QueryLike;
|
|
891
|
+
maybeSingle(): PromiseLike<Resp<any>>;
|
|
892
|
+
single(): PromiseLike<Resp<any>>;
|
|
893
|
+
}
|
|
894
|
+
/** The slice of a supabase-js client the doors use. */
|
|
895
|
+
interface SupabaseLike {
|
|
896
|
+
schema(name: string): {
|
|
897
|
+
from(table: string): QueryLike;
|
|
898
|
+
rpc(fn: string, args?: Record<string, unknown>): PromiseLike<Resp<any>>;
|
|
899
|
+
};
|
|
900
|
+
auth?: {
|
|
901
|
+
getSession(): Promise<{
|
|
902
|
+
data: {
|
|
903
|
+
session: {
|
|
904
|
+
access_token: string;
|
|
905
|
+
} | null;
|
|
906
|
+
};
|
|
907
|
+
}>;
|
|
908
|
+
onAuthStateChange(cb: (event: string, session: unknown) => void): {
|
|
909
|
+
data: {
|
|
910
|
+
subscription: {
|
|
911
|
+
unsubscribe(): void;
|
|
912
|
+
};
|
|
913
|
+
};
|
|
914
|
+
};
|
|
915
|
+
};
|
|
916
|
+
}
|
|
917
|
+
/** The credential shape `@ai-matrx/api`'s `CredentialsPort` accepts (structural; data owns the session). */
|
|
918
|
+
interface UserCredentialsPort {
|
|
919
|
+
get(): Promise<{
|
|
920
|
+
kind: "user";
|
|
921
|
+
accessToken: string;
|
|
922
|
+
} | null>;
|
|
923
|
+
onChange?(cb: () => void): () => void;
|
|
924
|
+
}
|
|
925
|
+
interface Db {
|
|
926
|
+
readonly client: SupabaseLike;
|
|
927
|
+
readonly role: "user" | "service";
|
|
928
|
+
/** The active organization — where NEW entity rows are saved. Never a read filter. */
|
|
929
|
+
activeOrganization(): string | null;
|
|
930
|
+
/** The session as `@ai-matrx/api`'s `user` credential (one sign-in feeds both lanes). */
|
|
931
|
+
readonly credentials: UserCredentialsPort;
|
|
932
|
+
}
|
|
933
|
+
interface CreateDbOptions {
|
|
934
|
+
/** The app's existing Supabase client (browser, RSC, route or the host's chat client). */
|
|
935
|
+
client: SupabaseLike;
|
|
936
|
+
/** Where new entity rows are saved. Read lazily on every insert; never consulted by a read. */
|
|
937
|
+
organization?: () => string | null | undefined;
|
|
938
|
+
}
|
|
939
|
+
/** The one database connection for a signed-in (or anonymous) person. */
|
|
940
|
+
declare function createDb(options: CreateDbOptions): Db;
|
|
941
|
+
/**
|
|
942
|
+
* Service-role doors — servers only. No session, no binding: every entity insert
|
|
943
|
+
* names its organization. Throws in a browser bundle.
|
|
944
|
+
*/
|
|
945
|
+
declare function createServiceDb(options: {
|
|
946
|
+
client: SupabaseLike;
|
|
947
|
+
}): Db;
|
|
948
|
+
declare class DoorRefusedError extends Error {
|
|
949
|
+
readonly door: string;
|
|
950
|
+
readonly reason: string;
|
|
951
|
+
constructor(door: string, reason: string);
|
|
952
|
+
}
|
|
953
|
+
declare class DatabaseError extends Error {
|
|
954
|
+
readonly door: string;
|
|
955
|
+
readonly cause: PostgrestError;
|
|
956
|
+
constructor(door: string, cause: PostgrestError);
|
|
957
|
+
}
|
|
958
|
+
type Op = "read" | "insert" | "update" | "remove" | "archive";
|
|
959
|
+
/** Refuse before the wire when the descriptor's rules do not open this door. */
|
|
960
|
+
declare function assertDoor(db: Db, table: TableDescriptor<any, any, any>, op: Op): void;
|
|
961
|
+
/** One row by primary key (a composite key takes `{ col: value }`), or null. */
|
|
962
|
+
declare function read<T extends TableDescriptor<any, any, any>>(db: Db, table: T, id: unknown, columns?: string): Promise<RowOf<T> | null>;
|
|
963
|
+
/** A filter callback — the same builder `readAllRows` callers write today (`q => q.or(…).gte(…)`). */
|
|
964
|
+
type Where = (q: any) => any;
|
|
965
|
+
/** EVERY matching row, paged past the 1,000-row cap; throws on a short read. */
|
|
966
|
+
declare function listAll<T extends TableDescriptor<any, any, any>>(db: Db, table: T, where?: Where, opts?: {
|
|
967
|
+
columns?: string;
|
|
968
|
+
orderBy?: string;
|
|
969
|
+
pageSize?: number;
|
|
970
|
+
maxRows?: number;
|
|
971
|
+
}): Promise<RowOf<T>[]>;
|
|
972
|
+
/** One explicit window (`from`..`to`, inclusive) plus the exact total. */
|
|
973
|
+
declare function page<T extends TableDescriptor<any, any, any>>(db: Db, table: T, window: {
|
|
974
|
+
from: number;
|
|
975
|
+
to: number;
|
|
976
|
+
where?: Where;
|
|
977
|
+
columns?: string;
|
|
978
|
+
orderBy?: string;
|
|
979
|
+
ascending?: boolean;
|
|
980
|
+
}): Promise<{
|
|
981
|
+
rows: RowOf<T>[];
|
|
982
|
+
total: number | null;
|
|
983
|
+
}>;
|
|
984
|
+
/** The exact count of matching rows (no rows transferred). */
|
|
985
|
+
declare function count<T extends TableDescriptor<any, any, any>>(db: Db, table: T, where?: Where): Promise<number>;
|
|
986
|
+
/** Insert one row (or many) and return what the database stored. */
|
|
987
|
+
declare function insert<T extends TableDescriptor<any, any, any>>(db: Db, table: T, values: InsertOf<T>): Promise<RowOf<T>>;
|
|
988
|
+
declare function insert<T extends TableDescriptor<any, any, any>>(db: Db, table: T, values: InsertOf<T>[]): Promise<RowOf<T>[]>;
|
|
989
|
+
type UpdateGuard = {
|
|
990
|
+
expectedVersion: number;
|
|
991
|
+
} | {
|
|
992
|
+
overwrite: true;
|
|
993
|
+
};
|
|
994
|
+
/**
|
|
995
|
+
* Update one row by primary key. On a versioned table the guard is required:
|
|
996
|
+
* `{ expectedVersion }` (a conflict comes back, never a silent overwrite) or
|
|
997
|
+
* `{ overwrite: true }`.
|
|
998
|
+
*/
|
|
999
|
+
declare function update<T extends TableDescriptor<any, any, any>>(db: Db, table: T, id: unknown, patch: UpdateOf<T>, guard?: UpdateGuard): Promise<GuardedUpdateResult<RowOf<T> & {
|
|
1000
|
+
version: number;
|
|
1001
|
+
}> | {
|
|
1002
|
+
status: "saved";
|
|
1003
|
+
row: RowOf<T>;
|
|
1004
|
+
} | {
|
|
1005
|
+
status: "not_found";
|
|
1006
|
+
}>;
|
|
1007
|
+
/**
|
|
1008
|
+
* Remove one row the way the table removes: soft delete (`deleted_at`) where it
|
|
1009
|
+
* has one; an archive-only table refuses (use `archive`); a hard delete only where
|
|
1010
|
+
* the table has neither.
|
|
1011
|
+
*/
|
|
1012
|
+
declare function remove<T extends TableDescriptor<any, any, any>>(db: Db, table: T, id: unknown): Promise<"soft_deleted" | "deleted" | "not_found">;
|
|
1013
|
+
/** Archive (or, with `restore`, bring back) one row by the table's archive column. */
|
|
1014
|
+
declare function archive<T extends TableDescriptor<any, any, any>>(db: Db, table: T, id: unknown, opts?: {
|
|
1015
|
+
restore?: boolean;
|
|
1016
|
+
}): Promise<RowOf<T> | null>;
|
|
1017
|
+
/** Bring back an archived or soft-deleted row. */
|
|
1018
|
+
declare const restore: <T extends TableDescriptor<any, any, any>>(db: Db, table: T, id: unknown) => Promise<RowOf<T> | null>;
|
|
1019
|
+
/** Call a database function through its generated door (PostgREST chooses an overload by argument names). */
|
|
1020
|
+
declare function call<F extends FunctionDescriptor<any, any>>(db: Db, fn: F, args: ArgsOf<F>): Promise<ResultOf<F>>;
|
|
1021
|
+
/** The compact flags index `matrx-data generate --flags-index` writes: "schema.table" → descriptor. */
|
|
1022
|
+
type FlagsIndex = Readonly<Record<string, TableDescriptor>>;
|
|
1023
|
+
/** The door for a table whose name arrives at runtime — loosely typed, the same rules. */
|
|
1024
|
+
declare function tableByName(index: FlagsIndex, name: string): TableDescriptor;
|
|
1025
|
+
/** The `postgres_changes` filter for a table — only published tables have one (an unpublished table delivers nothing). */
|
|
1026
|
+
declare function changesOf(table: TableDescriptor<any, any, any>): {
|
|
1027
|
+
schema: string;
|
|
1028
|
+
table: string;
|
|
1029
|
+
};
|
|
1030
|
+
|
|
1031
|
+
export { type ActiveOrgCookie, type ActiveOrgCookieOptions, type ActiveOrgSelection, type ArgsOf, type AuthRetryableResult, BulkWriteError, type CreateActiveOrgCookieArgs, type CreateDbOptions, type CreateOAuthRefreshFetchArgs, type CreateSessionRetryOptions, DEFAULT_ACTIVE_ORG_COOKIE_MAX_AGE, DEFAULT_ACTIVE_ORG_COOKIE_NAME, DatabaseError, type Db, DoorRefusedError, 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, 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, archive, asJsonObject, assertDoor, assertWriteLanded, call, changesOf, count, createActiveOrgCookie, createDb, createOAuthRefreshFetch, createServiceDb, createSessionRetry, decodeActiveOrgSelection, decodeOAuthRefreshClaims, describeWriteFailure, encodeActiveOrgSelection, guardedUpdate, insert, isMissingSessionError, isRecordUnavailableError, listAll, mergeJsonColumn, page, personSentence, read, readAllRows, readAllRowsRest, recordUnavailableMessage, remove, restore, serverMessageFromBody, tableByName, tryReadAllRows, tryReadAllRowsRest, tryWriteOne, update, writeFailureStatus, writeOne, writeOneRow };
|