import { FilterContract, SharedFilters } from "./querying/shared/contract.mjs"; import "./filters.mjs"; import { FieldSchemas, PaginationValidatorOptions } from "./querying/shared/filters.mjs"; import { PaginationOptionsSchema, ScalarPredicate, ScalarSelection } from "./querying/filters.mjs"; import { EnginePaginationResult } from "./zod-table.mjs"; import { ConvexValidatorFromZod } from "convex-helpers/server/zod4"; import { TableDefinition } from "convex/server"; import { z } from "zod"; import { GenericId, GenericValidator, ValidatorJSON } from "convex/values"; //#region src/relations.d.ts export type PaginationFiltersInput = z.input extends { filters?: infer Filters; } ? Exclude : never; /** Generated object or discriminated-union boundaries. */ export interface RelationTable { tableName: string; schema: z.ZodObject | z.ZodDiscriminatedUnion; publicDto: z.ZodObject | z.ZodDiscriminatedUnion; table: TableDefinition; dto(includeId: true): z.ZodType; /** Whole stored fields; schemas are captured from the native table, never a DTO transform. */ fieldGroups?: Readonly>; } export type RelationTables = Record; export type FieldGroupMode = 'if-permitted' | 'required'; export type FieldGroupRequest = { [K in keyof NonNullable]?: FieldGroupMode; }; /** Separate from relation keys so existing selection syntax remains unambiguous. */ export type RelationGroupSelection = { fieldGroups?: FieldGroupRequest; relations?: L extends readonly [] ? never : { [K in keyof Edges]?: RelationGroupSelection[K]>, keyof Tables>, Tail>; }; }; /** Internal admitted runtime tree, independent of user relation names. */ export interface GroupSelection { fieldGroups?: Readonly>; relations?: Readonly>; } export type ExactGroupSelection = G & { [K in keyof G]: K extends 'fieldGroups' ? G[K] & { [N in keyof G[K]]: N extends keyof FieldGroupRequest ? FieldGroupMode : never; } : K extends 'relations' ? G[K] & { [N in keyof G[K]]: N extends keyof Edges ? ExactGroupSelection[N]>, keyof Tables>, G[K][N]> : never; } : never; }; type StoredValue = GenericValidator extends V ? V['type'] : V extends { kind: 'object'; fields: infer F extends Record; } ? { [K in keyof F as F[K]['isOptional'] extends 'optional' ? never : K]: StoredValue; } & { [K in keyof F as F[K]['isOptional'] extends 'optional' ? K : never]?: StoredValue; } : V extends { kind: 'array'; element: infer E extends GenericValidator; } ? StoredValue[] : V extends { kind: 'union'; members: infer M extends GenericValidator[]; } ? StoredValue : V extends { kind: 'record'; key: infer K extends GenericValidator; value: infer E extends GenericValidator; } ? Record, StoredValue> : V['type']; type GroupStoredRow>> = [Tags] extends [never] ? Stored : Extract, keyof Row>>>; type GroupFields = T extends { fieldGroups: infer Definitions extends Readonly>; } ? { [K in keyof G & keyof Definitions]: Pick, Extract>>; } : {}; type UnionToIntersection = (U extends unknown ? (x: U) => void : never) extends ((x: infer I) => void) ? I : never; type IncludedFields = G extends { fieldGroups: infer Requests; } ? UnionToIntersection<{ [K in keyof Requests & keyof GroupFields]: Requests[K] extends 'required' ? GroupFields[K] : Partial[K]>; }[keyof Requests & keyof GroupFields]> : {}; type NestedGroups = G extends { relations: infer R; } ? K extends keyof R ? R[K] : {} : {}; /** Internal named declaration dependency; excluded from the supported relations facade. */ export interface SelectionMask { [key: string]: true | SelectionMask; } type DocumentFields = S extends z.ZodObject ? F : S extends z.ZodDiscriminatedUnion ? O[number] extends (infer B) ? B extends z.ZodObject ? F : never : never : never; type AllKeys = F extends unknown ? keyof F : never; type FieldAt = F extends unknown ? K extends keyof F ? F[K] : never : never; type NativeDomain = V extends { kind: 'union'; members: infer M extends GenericValidator[]; } ? NativeDomain : V extends { kind: 'null'; } ? never : V extends { kind: infer K; type: infer Value; } ? readonly [K, Exclude] : never; type DomainAt = F extends unknown ? K extends keyof F ? F[K] extends z.ZodType ? NativeDomain> : never : never : never; type CompatibleBranches> = F extends unknown ? Same, Domain> : never; type Discriminator = T['schema'] extends z.ZodDiscriminatedUnion ? T['schema']['_zod']['def']['discriminator'] : never; type Fields> = string extends T['tableName'] ? FieldSchemas : T['schema'] extends z.ZodObject ? { [K in keyof ObjectFields]: Extract; } : { [K in Extract as K extends Discriminator ? K : false extends CompatibleBranches ? never : K]: Extract, z.ZodType>; }; type BranchFields = DocumentFields extends (infer F) ? F extends Record, z.ZodType> ? Tag extends z.output]> ? F : never : never : never; type Tags = T['schema'] extends z.ZodDiscriminatedUnion ? Extract[Discriminator], string> : never; type Native> = ConvexValidatorFromZod[K], 'required'>['type']; declare const fieldBrand: unique symbol; declare const edgeBrand: unique symbol; type Comparable = Exclude; type IdMembers = [Comparable] extends [never] ? never : [Comparable] extends [GenericId] ? Extract, GenericId>['__tableName'] & Registered : never; type Same = [Comparable | Comparable] extends [never] ? false : [Comparable] extends [Comparable] ? [Comparable] extends [Comparable] ? true : false : false; export interface FieldReference { readonly guard?: RelationGuard; readonly [fieldBrand]: Value; readonly table: Table; readonly field: Field; /** Select a registered native-ID member, retaining this field's nullish wrappers. */ forTable : never)>(table: T): FieldReference | null | undefined>, Scope, Registered, T>; through(junction: J & ([Member] extends [never] ? {} : never) & (J extends FieldReference ? [M] extends [never] ? {} : never : never) & (Same extends true ? {} : never)): ThroughReference; } export interface RelationGuard { readonly discriminator: string; readonly tag: Tag; } type AnyField = FieldReference; export interface ThroughReference
{ readonly base: FieldReference; readonly junction: Junction; } type AnyThrough = ThroughReference; type Leg = AnyField | AnyThrough; type Base = L extends AnyThrough ? L['base'] : L; type JunctionTable = L extends AnyThrough ? L['junction']['table'] : never; type CompatibleLegs = F extends AnyThrough ? T extends AnyThrough ? Same, JunctionTable> extends true ? {} : never : never : T extends AnyThrough ? never : Same[typeof fieldBrand], Base[typeof fieldBrand]> extends true ? {} : never; export interface RelationEdge { readonly sourceGuard?: RelationGuard; readonly targetGuard?: RelationGuard; readonly junctionGuard?: RelationGuard; readonly sourceMember?: RelationMember; readonly targetMember?: RelationMember; readonly [edgeBrand]: true; readonly source: Source; readonly target: Target; readonly cardinality: Cardinality; readonly from: Leg; readonly to: Leg; } interface RelationMember { readonly table: string; } type AnyEdge = RelationEdge; type FieldReferences = { [T in keyof Tables]: { [K in Extract, string>]: FieldReference, K, Native, never, Extract>; }; }; export type BranchBuilder = >(table: T, tag: Tag) => { [K in keyof BranchFields & string]: FieldReference[K], z.ZodType>, 'required'>['type'], Tag>; }; type Scope = NonNullable['guard']>['tag']; type AllReferences = { [T in keyof Tables]: FieldReferences[T][Extract[T], string>]; }[keyof Tables]; type Builders = { [T in keyof Tables]: | AnyField | AnyThrough, To extends FieldReferences[T][Extract[T], string>] | AnyField | AnyThrough>(spec: { from: F; to: To; } & CompatibleLegs & (Base['table'] extends T ? {} : never)) => RelationEdge['table'], Extract, C, Scope, Scope>; }; export type RelationBuilder = FieldReferences & { one: Builders; many: Builders; }; export type RelationDeclarations = Record>; type ExactDeclarations = { [T in keyof D]: T extends keyof Tables ? { [K in keyof D[T]]: K extends AllKeys> | 'and' | 'or' | 'not' | 'fields' | 'then' | ([Tags] extends [never] ? never : 'variant' | 'variants') | '__proto__' | 'constructor' | 'prototype' ? never : D[T][K]['source'] extends T ? D[T][K] : never; } : never; }; type Depth = readonly [1, 1, 1, 1]; type Tail = D extends readonly [1, ...infer R extends 1[]] ? R : []; type Edges = T extends keyof D ? D[T] : {}; type Target = E extends AnyEdge ? E['target'] : never; export type RelationSelection = Level extends readonly [] ? Record : { [K in keyof Edges]?: true | RelationSelection[K]>, keyof Tables>, Tail>; }; type BranchTables = { [K in keyof Tables]: K extends T ? Omit & { schema: z.ZodObject>; } : Tables[K]; }; type VariantSelections = { [Tag in Tags]?: RelationFilterSelection, D, T, Level>; }; export type RelationFilterSelection = ([Tags] extends [never] ? ScalarSelection> : Omit>, 'variants'> & { variants?: ('variants' extends keyof ScalarSelection> ? true : never) | VariantSelections; }) & (Level extends readonly [] ? {} : { [K in keyof Edges]?: RelationFilterSelection[K]>, keyof Tables>, Tail>; }); type ExactOutputMask = S & { [K in keyof S]: L extends readonly [] ? never : K extends keyof Edges ? S[K] extends true ? true : ExactOutputMask[K]>, keyof Tables>, S[K], Tail> : never; }; type ExactFilterMask = S & { [K in keyof S]: K extends ([Tags] extends [never] ? never : 'variants') ? S[K] extends true ? K extends keyof ScalarSelection> ? true : never : S[K] & { [Tag in keyof S[K]]: Tag extends Tags ? ExactFilterMask>, D, T, S[K][Tag], Tail> : never; } : K extends keyof ScalarSelection> ? S[K] extends true ? true : never : L extends readonly [] ? never : K extends keyof Edges ? S[K] extends true ? never : ExactFilterMask[K]>, keyof Tables>, S[K], Tail> : never; }; type ScalarValues = { [K in Extract>]?: ScalarPredicate[K], Side>; }; export type RelationFilters = Omit, 'and' | 'or' | 'not' | 'fields' | ([Tags] extends [never] ? never : 'variant')> & { fields?: ScalarValues; } & VariantFilters & (Level extends readonly [] ? {} : { [K in Extract>]?: Edges[K] extends RelationEdge ? C extends 'one' ? RelationFilters, S[K], Side, Tail> | null : { some: RelationFilters, S[K], Side, Tail>; } | { none: RelationFilters, S[K], Side, Tail>; } : never; } & { and?: readonly RelationFilters>[]; or?: readonly RelationFilters>[]; not?: RelationFilters>; }); type VariantFilters = L extends readonly [] ? {} : S extends { variants: infer V; } ? V extends true ? {} : { variant?: { [Tag in keyof V]: Tag extends Tags ? { tag: Tag; filters: RelationFilters>, D, T, V[Tag] & Omit, Side, Tail>; } : never; }[keyof V]; } : {}; type PublicRow = z.output & { _id: GenericId; }; export type ScopedRelationRow = [Scope] extends [never] ? Row : Extract>; type ComposeRelationRow = Row extends unknown ? Row & IncludedFields & (Level extends readonly [] ? {} : { [K in keyof S]: K extends keyof Edges ? Edges[K] extends RelationEdge ? C extends 'one' ? ([SourceScope] extends [never] ? true : Row extends ScopedRelationRow> ? true : false) extends true ? ScopedRelationRow, S[K] extends true ? {} : S[K], Tail, NestedGroups>, TargetScope, Discriminator]>> | null : null : EnginePaginationResult, S[K] extends true ? {} : S[K], Tail, NestedGroups>, TargetScope, Discriminator]>>> : never : never; }) : never; export type RelationRow = ComposeRelationRow, G>; export type RelationQueryRow = ScopedRelationRow, Scope, Discriminator>; export interface RelationTableHelpers { filters>(contract: FilterContract): PaginationOptionsSchema, SharedFilters>; paginationOpts>(selection: ExactFilterMask, options?: PaginationValidatorOptions): PaginationOptionsSchema, RelationFilters>; pageDto, const G extends RelationGroupSelection = {}>(selection: ExactOutputMask, groups?: ExactGroupSelection): z.ZodType>>; } export interface RelationRegistryIdentity { readonly identity: string; readonly version: 1; } export interface RelationRegistry { readonly identity: string; readonly version: 1; readonly tables: { [T in keyof Tables]: RelationTableHelpers; }; } export interface RelationFieldMetadata { readonly kind: string; readonly optional: boolean; readonly nullable: boolean; readonly idTables: readonly string[]; readonly native: Readonly; } export interface SearchIndexMetadata { readonly name: string; readonly searchField: string; readonly filterFields: readonly string[]; readonly staged: boolean; } export interface RelationTableMetadata { readonly searchIndexes: readonly SearchIndexMetadata[]; readonly fieldGroups: Readonly>; readonly fields: Readonly>; readonly variant?: { readonly discriminator: string; readonly tags: readonly string[]; }; readonly indexes: readonly { readonly name: string; readonly fields: readonly string[]; }[]; } export interface RelationMetadata { readonly tables: Readonly>; readonly declarations: Readonly>>>; } /** No ID-field inference and no readers: this registry owns declarations/schema helpers only. * Registration checks table names, native table identity and edge structure. Every other * per-table check (preflight, schema capture, indexes, field groups, edge field types and * indexes) runs once, on the table's first use. `verifyRelations` runs all of them eagerly. */ export declare function defineRelations(tables: Tables, declare: (r: RelationBuilder, branch: BranchBuilder) => D & ExactDeclarations): RelationRegistry; /** Runs every deferred registration check now, for example in a test that should fail CI. */ export declare function verifyRelations(registry: RelationRegistryIdentity): void; /** Internal owned executor bridge. Source ownership is checked once by each engine. */ export declare function getRelationExecutor(registry: RelationRegistryIdentity): Readonly<{ streamSchema: import("convex/server").SchemaDefinition; scopedIdentity: (tables: readonly string[], edges: readonly (readonly [source: string, name: string])[]) => string; groups(table: string, groups: GroupSelection): GroupSelection; validateDocument: (table: string, row: Record) => void; assertSchema: (tables: Readonly>) => void; pagination(table: string): z.ZodObject<{ cursor: z.ZodUnion; endCursor: z.ZodOptional>; id: z.ZodOptional; onEmptyBudget: z.ZodOptional>; numItems: z.ZodNumber; maximumRowsRead: z.ZodOptional; maximumBytesRead: z.ZodOptional; filters: z.ZodOptional>>; order: z.ZodOptional>; }, z.core.$strict>; select(table: string, selection: SelectionMask): void; project(table: string, row: Record, eligible: () => boolean, groups?: readonly string[]): any; }>; /** Internal compiler/engine adapter. Registry identity alone cannot forge a registry. */ export declare function getRelationMetadata(registry: RelationRegistryIdentity): RelationMetadata; //#endregion //# sourceMappingURL=relations.d.mts.map