import type { StorageTable } from '@prisma-next/sql-contract/types'; import type { TableProxy } from './table-proxy'; export type CapabilitiesBase = Record>; type NamespaceEntries = Readonly>>>; // The application-domain models the table-proxy helpers read to map a storage // table -> model and column -> field within a namespace coordinate. The index // signature lets the helpers index `C['domain']['namespaces'][NsId]` by a // generic `NsId` directly, so `FindModelForTable` / `FindFieldForColumn` no // longer need a `C extends Contract` guard to reach the per-namespace // models; concrete emitted contracts (whose models are richer) satisfy it. type NamespaceDomain = Readonly< Record> }> >; export type TableProxyContract = { readonly domain: { readonly namespaces: NamespaceDomain; }; readonly storage: { readonly namespaces: Readonly>; }; readonly capabilities: CapabilitiesBase; }; type TablesInNamespace = NS['entries']['table'] extends Readonly> ? NS['entries']['table'] : Readonly>; // Union of every table name declared in any namespace of `C`. Replaces // the prior `UnboundTables` indexing (which only saw `__unbound__`). export type UnboundTables = { readonly [Name in TableNamesAcrossNamespaces]: TableInAnyNamespace; }; export type TableNamesAcrossNamespaces = { [NSId in keyof C['storage']['namespaces']]: keyof TablesInNamespace< C['storage']['namespaces'][NSId] > & string; }[keyof C['storage']['namespaces']]; export type TableInAnyNamespace = { [NSId in keyof C['storage']['namespaces']]: Name extends keyof TablesInNamespace< C['storage']['namespaces'][NSId] > ? TablesInNamespace[Name] : never; }[keyof C['storage']['namespaces']]; // The exact storage table at a single namespace coordinate. Resolving through // the coordinate (rather than the cross-namespace `UnboundTables` union) keeps // a bare table name shared across namespaces resolving to each namespace's own // table — no per-namespace column intersection. `TablesInNamespace` narrows the // open-dict `entries['table']` (`Record`) back to the typed // `StorageTable` map before indexing by the bare name. export type NamespaceTable< C extends TableProxyContract, NsId extends string, Name extends string, > = TablesInNamespace[Name]; // The tables of a single storage namespace, keyed by bare table name. Lets // callers reach a table by its namespace coordinate (`db..`) when // the same bare name is declared in more than one namespace. The `NsId` // coordinate is threaded into each `TableProxy` so its column/field resolution // is a function of `(NsId, Name)`, not `Name` alone. export type Namespace< C extends TableProxyContract, NsId extends string & keyof C['storage']['namespaces'], > = { readonly [Name in keyof TablesInNamespace & string]: TableProxy< C, NsId, Name >; }; export type Db = { readonly [Ns in keyof C['storage']['namespaces'] & string]: Namespace; };