{"version":3,"file":"querying.mjs","names":["executorRequest","emptyGroups","FieldGroupError","createQueryEngine","config","settings","options","planner","createPlanner","relations","schema","policies","table","definitions","name","policy","cursorCodec","createCursorCodec","maxPageSize","engineVersion","policyVersion","legacyDigest","security","createManagedSecurity","querying","request","ctx","decisions","resolveGroups","fieldGroups","mode","groups","key","decision","error","ManagedReadError","nested","selection","tableQuery","conditions","constraints","route","ordering","search","SearchError","z","index","value","spec","selected","requested","path","declaredPath","target","edge","codec","createEnvironmentCursorCodec","assertRelationshipDepth","searchPlan","planSearch","input","plan","resolved","groupAccess","node","field","fields","checkField","checkClient","ast","child","sortIndex","condition","registryVersion","groupBinding","hasGroups","serverFilters","binding","executionRequest","legacy","opener","token","cursorBinding","execute","executor","relationshipExecutor","executeSearch","document","row","executeQuery","permitted","result","digest","scope","createScopedCursorCodec","CursorError","queryFailureContract"],"sources":["../src/querying.ts"],"sourcesContent":["import type { GenericSchema, SchemaDefinition, DataModelFromSchemaDefinition } from 'convex/server'\nimport type { GenericId } from 'convex/values'\nimport type { EnginePaginationResult } from './zod-table'\nimport { z } from 'zod'\nimport type {\n\tRelationTables,\n\tRelationDeclarations,\n\tRelationRegistry,\n\tRelationQueryRow,\n\tRelationSelection,\n\tRelationFilters,\n\tRelationFilterSelection,\n\tFieldGroupRequest,\n\tRelationGroupSelection,\n\tGroupSelection,\n\tExactGroupSelection,\n} from './relations'\nimport {\n\tcreatePlanner,\n\tdigest,\n\ttype QueryInput,\n\ttype StoredConstraint,\n} from './querying/planner'\nimport { executeQuery, type ExecutionRequest } from './querying/execution'\nimport {\n\trelationshipExecutor,\n\tassertRelationshipDepth,\n\ttype OutputSelection,\n\ttype RelationDocument,\n\ttype GroupAccess,\n\ttype ResolvedGroups,\n} from './querying/relationships'\nimport {\n\tcreateCursorCodec,\n\tcreateScopedCursorCodec,\n\ttype ScopedCursorOptions,\n\ttype ScopedCursorCodec,\n\tcreateEnvironmentCursorCodec,\n\ttype CursorCodecConfig,\n\ttype CursorBinding,\n} from './querying/cursors'\nimport {\n\tcreateManagedSecurity,\n\ttype ManagedSecurityConfig,\n\ttype ManagedQueryContext,\n\ttype ManagedBudgets,\n\ttype ManagedScope,\n\tManagedReadError,\n} from './querying/security'\nimport type { FilterAst, IndexCondition, NativePagination } from './querying/filters'\nimport type { GenericDatabaseReader } from 'convex/server'\nimport type { Value } from 'convex/values'\n\nimport { planSearch, executeSearch, SearchError } from './querying/search'\nexport { SearchError } from './querying/search'\nexport type { SearchPaginationResult } from './querying/search'\nexport { queryFailureContract } from './querying/failures'\nexport { CursorError } from './querying/cursors'\nexport type { ScopedCursorOptions, ScopedCursorCodec } from './querying/cursors'\n\nconst emptyGroups: ResolvedGroups = { fieldGroups: {}, relations: {} }\n\ntype FullMask<S> = {\n\t[K in keyof S]-?: NonNullable<S[K]> extends true\n\t\t? true\n\t\t: FullMask<Exclude<NonNullable<S[K]>, true>>\n}\ntype Paths<\n\tD extends RelationDeclarations,\n\tT extends string,\n\tL extends readonly 1[] = [1, 1, 1, 1],\n> = L extends readonly [1, ...infer Rest extends 1[]]\n\t? {\n\t\t\t[K in keyof D[T] & string]:\n\t\t\t\t| (D[T][K]['cardinality'] extends 'many' ? K : never)\n\t\t\t\t| `${K}.${Paths<D, D[T][K]['target'], Rest>}`\n\t\t}[keyof D[T] & string]\n\t: never\ntype PathTarget<\n\tD extends RelationDeclarations,\n\tT extends string,\n\tP extends string,\n> = P extends `${infer Head}.${infer Rest}`\n\t? PathTarget<D, D[T][Head]['target'], Rest>\n\t: D[T][P]['target']\ntype PathScope<\n\tD extends RelationDeclarations,\n\tT extends string,\n\tP extends string,\n> = P extends `${infer Head}.${infer Rest}`\n\t? PathScope<D, D[T][Head]['target'], Rest>\n\t: Extract<NonNullable<D[T][P]['targetGuard']>['tag'], string>\ntype RemainingDepth<P extends string, L extends readonly 1[]> = L extends readonly [\n\t1,\n\t...infer Rest extends 1[],\n]\n\t? P extends `${string}.${infer Tail}`\n\t\t? RemainingDepth<Tail, Rest>\n\t\t: Rest\n\t: []\ntype EngineArgs<\n\tTables extends RelationTables,\n\tD extends RelationDeclarations,\n\tT extends keyof Tables,\n> = NativePagination & {\n\tfilters?: RelationFilters<Tables, D, T, FullMask<RelationFilterSelection<Tables, D, T>>>\n\torder?: 'asc' | 'desc'\n}\n/** Typed declarative chain returned by the engine; named for consumer declaration emission. */\nexport interface TableQuery<\n\tTables extends RelationTables,\n\tD extends RelationDeclarations,\n\tT extends keyof Tables,\n\tS = {},\n\tL extends readonly 1[] = [1, 1, 1, 1],\n\tScope extends string = never,\n\tSchema extends SchemaDefinition<GenericSchema, boolean> = SchemaDefinition<\n\t\tGenericSchema,\n\t\tboolean\n\t>,\n\tG = {},\n\tRoot extends boolean = true,\n> {\n\tsearch: Root extends true\n\t\t? (\n\t\t\t\tindex: Extract<\n\t\t\t\t\tkeyof DataModelFromSchemaDefinition<Schema>[Extract<T, string>]['searchIndexes'],\n\t\t\t\t\tstring\n\t\t\t\t>,\n\t\t\t\ttext: string,\n\t\t\t) => SearchQuery<Tables, D, T, S, L, Scope, Schema, G>\n\t\t: never\n\tinclude<const Groups extends FieldGroupRequest<Tables[T]>>(\n\t\tgroups: Groups & Record<Exclude<keyof Groups, keyof FieldGroupRequest<Tables[T]>>, never>,\n\t): TableQuery<\n\t\tTables,\n\t\tD,\n\t\tT,\n\t\tS,\n\t\tL,\n\t\tScope,\n\t\tSchema,\n\t\tOmit<G, 'fieldGroups'> & { fieldGroups: Groups },\n\t\tRoot\n\t>\n\torderBy(\n\t\tindex: Exclude<\n\t\t\tExtract<keyof DataModelFromSchemaDefinition<Schema>[Extract<T, string>]['indexes'], string>,\n\t\t\t'by_id' | 'by_creation_time'\n\t\t>,\n\t): TableQuery<Tables, D, T, S, L, Scope, Schema, G, Root>\n\twhen(\n\t\tvalue: IndexCondition['value'],\n\t\tindex: string,\n\t): TableQuery<Tables, D, T, S, L, Scope, Schema, G, Root>\n\twhere(\n\t\tspec: NonNullable<EngineArgs<Tables, D, T>['filters']>,\n\t): TableQuery<Tables, D, T, S, L, Scope, Schema, G, Root>\n\twith<\n\t\tconst Selection extends RelationSelection<Tables, D, T, L>,\n\t\tconst Groups extends RelationGroupSelection<Tables, D, T, L> = {},\n\t>(\n\t\tselection: Selection & {\n\t\t\t[K in keyof Selection]: K extends keyof RelationSelection<Tables, D, T, L>\n\t\t\t\t? Selection[K]\n\t\t\t\t: never\n\t\t},\n\t\tgroups?: ExactGroupSelection<Tables, D, T, Groups>,\n\t): TableQuery<Tables, D, T, Selection, L, Scope, Schema, Omit<G, keyof Groups> & Groups, Root>\n\trelation<const P extends Paths<D, Extract<T, string>>>(\n\t\tparent: GenericId<Tables[T]['tableName']>,\n\t\tpath: P,\n\t): Omit<\n\t\tTableQuery<\n\t\t\tTables,\n\t\t\tD,\n\t\t\tExtract<PathTarget<D, Extract<T, string>, P>, keyof Tables>,\n\t\t\t{},\n\t\t\tRemainingDepth<P, L>,\n\t\t\tPathScope<D, Extract<T, string>, P>,\n\t\t\tSchema,\n\t\t\t{},\n\t\t\tfalse\n\t\t>,\n\t\t'relation'\n\t>\n\tpaginate(\n\t\targs: EngineArgs<Tables, D, T>,\n\t): Promise<EnginePaginationResult<RelationQueryRow<Tables, D, T, S, Scope, G>>>\n}\n\nexport interface SearchQuery<\n\tTables extends RelationTables,\n\tD extends RelationDeclarations,\n\tT extends keyof Tables,\n\tS = {},\n\tL extends readonly 1[] = [1, 1, 1, 1],\n\tScope extends string = never,\n\tSchema extends SchemaDefinition<GenericSchema, boolean> = SchemaDefinition<\n\t\tGenericSchema,\n\t\tboolean\n\t>,\n\tG = {},\n> {\n\tinclude<const Groups extends FieldGroupRequest<Tables[T]>>(\n\t\tgroups: Groups & Record<Exclude<keyof Groups, keyof FieldGroupRequest<Tables[T]>>, never>,\n\t): SearchQuery<\n\t\tTables,\n\t\tD,\n\t\tT,\n\t\tS,\n\t\tL,\n\t\tScope,\n\t\tSchema,\n\t\tOmit<G, 'fieldGroups'> & { fieldGroups: Groups }\n\t>\n\twhere(\n\t\tspec: NonNullable<EngineArgs<Tables, D, T>['filters']>,\n\t): SearchQuery<Tables, D, T, S, L, Scope, Schema, G>\n\twith<\n\t\tconst Selection extends RelationSelection<Tables, D, T, L>,\n\t\tconst Groups extends RelationGroupSelection<Tables, D, T, L> = {},\n\t>(\n\t\tselection: Selection & {\n\t\t\t[K in keyof Selection]: K extends keyof RelationSelection<Tables, D, T, L>\n\t\t\t\t? Selection[K]\n\t\t\t\t: never\n\t\t},\n\t\tgroups?: ExactGroupSelection<Tables, D, T, Groups>,\n\t): SearchQuery<Tables, D, T, Selection, L, Scope, Schema, Omit<G, keyof Groups> & Groups>\n\tpaginate(\n\t\targs: Omit<\n\t\t\tEngineArgs<Tables, D, T>,\n\t\t\t'order' | 'endCursor' | 'maximumRowsRead' | 'maximumBytesRead' | 'onEmptyBudget'\n\t\t> & { scanLimit?: number },\n\t): Promise<\n\t\timport('./querying/search').SearchPaginationResult<RelationQueryRow<Tables, D, T, S, Scope, G>>\n\t>\n}\nexport interface QueryEngineDeclaration<\n\tSchema extends SchemaDefinition<GenericSchema, boolean>,\n\tTables extends RelationTables,\n\tD extends RelationDeclarations,\n> {\n\tschema: Schema\n\trelations: RelationRegistry<Tables, D>\n}\n/** Advanced server configuration, separate from the normal schema/relations declaration. */\nexport interface QueryEngineOptions<\n\tSchema extends SchemaDefinition<GenericSchema, boolean>,\n\tTables extends RelationTables = RelationTables,\n> {\n\t/** Explicit codecs validate at creation. Otherwise environment keys load at first paginate. */\n\tcursor?: CursorCodecConfig\n\tbudgets?: Partial<ManagedBudgets>\n\tmaxPageSize?: number\n\tpolicyVersion?: string\n\tengineVersion?: string\n\ttrustedAdapter?: ManagedSecurityConfig<DataModelFromSchemaDefinition<Schema>>['trustedAdapter']\n\tfieldGroups?: {\n\t\t[T in keyof Tables]?: {\n\t\t\t[G in keyof NonNullable<Tables[T]['fieldGroups']>]?: (\n\t\t\t\tctx: FieldGroupPolicyContext<Schema>,\n\t\t\t) => boolean | Promise<boolean>\n\t\t}\n\t}\n}\nexport interface FieldGroupPolicyContext<Schema extends SchemaDefinition<GenericSchema, boolean>> {\n\treadonly db: GenericDatabaseReader<DataModelFromSchemaDefinition<Schema>>\n\treadonly scope: ManagedScope\n}\nexport class FieldGroupError extends Error {\n\treadonly code = 'FIELD_GROUP_DENIED'\n\tconstructor() {\n\t\tsuper('FIELD_GROUP_DENIED')\n\t\tthis.name = 'FieldGroupError'\n\t}\n}\n\n/** @deprecated Use QueryEngineDeclaration and optional QueryEngineOptions separately. */\nexport interface QueryEngineConfig<\n\tSchema extends SchemaDefinition<GenericSchema, boolean>,\n\tTables extends RelationTables,\n\tD extends RelationDeclarations,\n>\n\textends QueryEngineDeclaration<Schema, Tables, D>, QueryEngineOptions<Schema, Tables> {\n\tcursor: CursorCodecConfig\n}\n\ntype QueryEngine<\n\tSchema extends SchemaDefinition<GenericSchema, boolean>,\n\tTables extends RelationTables,\n\tD extends RelationDeclarations,\n> = {\n\t(ctx: ManagedQueryContext<DataModelFromSchemaDefinition<Schema>>): {\n\t\t[T in keyof Tables]: TableQuery<Tables, D, T, {}, [1, 1, 1, 1], never, Schema>\n\t}\n\t/** Opens prove integrity only; native consumers must enforce current row authorization. */\n\tcursorCodec<S extends z.ZodType<Value>>(\n\t\tctx: ManagedQueryContext<DataModelFromSchemaDefinition<Schema>>,\n\t\toptions: ScopedCursorOptions<S>,\n\t): ScopedCursorCodec<S>\n\ttables: RelationRegistry<Tables, D>['tables']\n}\n\n/** Central facade. Endpoint chains carry declarative constraints and explicit relationships. */\nexport function createQueryEngine<\n\tSchema extends SchemaDefinition<GenericSchema, boolean>,\n\tconst Tables extends RelationTables,\n\tconst D extends RelationDeclarations,\n>(\n\tconfig: QueryEngineDeclaration<Schema, Tables, D>,\n\toptions?: QueryEngineOptions<Schema, Tables>,\n): QueryEngine<Schema, Tables, D>\n/** @deprecated Pass advanced fields in the optional second argument. */\nexport function createQueryEngine<\n\tSchema extends SchemaDefinition<GenericSchema, boolean>,\n\tconst Tables extends RelationTables,\n\tconst D extends RelationDeclarations,\n>(\n\tconfig: QueryEngineDeclaration<Schema, Tables, D> &\n\t\tQueryEngineOptions<Schema, Tables> & { cursor: CursorCodecConfig },\n): QueryEngine<Schema, Tables, D>\nexport function createQueryEngine<\n\tSchema extends SchemaDefinition<GenericSchema, boolean>,\n\tconst Tables extends RelationTables,\n\tconst D extends RelationDeclarations,\n>(\n\tconfig: QueryEngineDeclaration<Schema, Tables, D> & QueryEngineOptions<Schema, Tables>,\n\toptions?: QueryEngineOptions<Schema, Tables>,\n) {\n\tconst { relations } = config\n\t// Retain released one-argument calls; a second argument overrides legacy fields.\n\tconst settings = { ...config, ...options }\n\tconst planner = createPlanner(config.schema, relations)\n\tconst schema = planner.streamSchema\n\t// Copy callback references once: later option mutation cannot replace engine authority.\n\tconst policies = new Map<\n\t\tstring,\n\t\t(ctx: FieldGroupPolicyContext<Schema>) => boolean | Promise<boolean>\n\t>()\n\tfor (const [table, definitions] of Object.entries(settings.fieldGroups ?? {})) {\n\t\tif (!Object.hasOwn(planner.metadata.tables, table))\n\t\t\tthrow new Error('Unknown field group policy table')\n\t\tfor (const [name, policy] of Object.entries(definitions ?? {})) {\n\t\t\tif (\n\t\t\t\t!Object.hasOwn(planner.metadata.tables[table].fieldGroups, name) ||\n\t\t\t\t!(policy instanceof Function)\n\t\t\t)\n\t\t\t\tthrow new Error('Unknown field group policy')\n\t\t\t// SAFETY: the public mapped options require this policy signature; the function check also rejects malformed JS callers.\n\t\t\tpolicies.set(\n\t\t\t\tJSON.stringify([table, name]),\n\t\t\t\tpolicy as (ctx: FieldGroupPolicyContext<Schema>) => boolean | Promise<boolean>,\n\t\t\t)\n\t\t}\n\t}\n\tlet cursorCodec = settings.cursor === undefined ? undefined : createCursorCodec(settings.cursor)\n\tconst maxPageSize = settings.maxPageSize ?? 100\n\tif (!Number.isSafeInteger(maxPageSize) || maxPageSize <= 0 || maxPageSize > 100)\n\t\tthrow new Error('Invalid central page size')\n\tconst engineVersion = settings.engineVersion ?? 'scalar-1'\n\tconst policyVersion = settings.policyVersion ?? '1'\n\t// Cursors from 0.1.24 and earlier bound the full registry identity, not the\n\t// scope of their query. They still open: the first one in an engine pays for\n\t// the full digest once, and every continuation reseals under its scope.\n\tlet legacyDigest: string | undefined\n\tconst security = createManagedSecurity<DataModelFromSchemaDefinition<Schema>>({\n\t\ttables: Object.keys(relations.tables),\n\t\tpolicyVersion,\n\t\tbudgets: settings.budgets,\n\t\ttrustedAdapter: settings.trustedAdapter,\n\t})\n\tfunction querying(ctx: ManagedQueryContext<DataModelFromSchemaDefinition<Schema>>) {\n\t\tconst request = security(ctx)\n\t\tconst decisions = new Map<string, Promise<boolean>>()\n\t\tasync function resolveGroups(table: string, groups: GroupSelection): Promise<ResolvedGroups> {\n\t\t\tconst fieldGroups: ResolvedGroups['fieldGroups'] = {}\n\t\t\tfor (const [name, mode] of Object.entries(groups.fieldGroups ?? {})) {\n\t\t\t\tconst key = JSON.stringify([table, name])\n\t\t\t\tlet decision = decisions.get(key)\n\t\t\t\tif (!decision) {\n\t\t\t\t\tdecision = Promise.resolve().then(async () => {\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\treturn (\n\t\t\t\t\t\t\t\t(await policies.get(key)?.(\n\t\t\t\t\t\t\t\t\tObject.freeze({ db: request.db, scope: request.scope }),\n\t\t\t\t\t\t\t\t)) === true\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t} catch (error) {\n\t\t\t\t\t\t\tif (error instanceof ManagedReadError) throw error\n\t\t\t\t\t\t\tthrow new FieldGroupError()\n\t\t\t\t\t\t}\n\t\t\t\t\t})\n\t\t\t\t\tdecisions.set(key, decision)\n\t\t\t\t}\n\t\t\t\tfieldGroups[name] = { mode, allowed: await decision }\n\t\t\t\tif (mode === 'required' && !fieldGroups[name].allowed) throw new FieldGroupError()\n\t\t\t}\n\t\t\tconst nested: ResolvedGroups['relations'] = {}\n\t\t\tfor (const [name, selection] of Object.entries(groups.relations ?? {}))\n\t\t\t\tnested[name] = await resolveGroups(\n\t\t\t\t\tplanner.metadata.declarations[table][name].target,\n\t\t\t\t\tselection,\n\t\t\t\t)\n\t\t\treturn { fieldGroups, relations: nested }\n\t\t}\n\t\tfunction tableQuery(\n\t\t\ttable: string,\n\t\t\tconditions: readonly IndexCondition[] = [],\n\t\t\tconstraints: readonly StoredConstraint[] = [],\n\t\t\tselection: OutputSelection = {},\n\t\t\troute?: { table: string; parent: string; path: string[] },\n\t\t\tordering?: string,\n\t\t\tgroups: GroupSelection = {},\n\t\t\tsearch?: { index: string; text: string },\n\t\t) {\n\t\t\treturn Object.freeze({\n\t\t\t\tinclude: (fieldGroups: GroupSelection['fieldGroups']) =>\n\t\t\t\t\ttableQuery(\n\t\t\t\t\t\ttable,\n\t\t\t\t\t\tconditions,\n\t\t\t\t\t\tconstraints,\n\t\t\t\t\t\tselection,\n\t\t\t\t\t\troute,\n\t\t\t\t\t\tordering,\n\t\t\t\t\t\tplanner.groups(table, { ...groups, fieldGroups }),\n\t\t\t\t\t\tsearch,\n\t\t\t\t\t),\n\t\t\t\torderBy: (index: string) => {\n\t\t\t\t\tif (search) throw new SearchError('SEARCH_ORDER_UNSUPPORTED')\n\t\t\t\t\tif (!z.string().safeParse(index).success) throw new Error('Unknown ordering index')\n\t\t\t\t\treturn tableQuery(table, conditions, constraints, selection, route, index, groups, search)\n\t\t\t\t},\n\t\t\t\twhen: (value: IndexCondition['value'], index: string) => {\n\t\t\t\t\tif (search && value != null) throw new SearchError('SEARCH_ORDER_UNSUPPORTED')\n\t\t\t\t\treturn tableQuery(\n\t\t\t\t\t\ttable,\n\t\t\t\t\t\t[...conditions, { value, index }],\n\t\t\t\t\t\tconstraints,\n\t\t\t\t\t\tselection,\n\t\t\t\t\t\troute,\n\t\t\t\t\t\tordering,\n\t\t\t\t\t\tgroups,\n\t\t\t\t\t\tsearch,\n\t\t\t\t\t)\n\t\t\t\t},\n\t\t\t\twhere: (spec: StoredConstraint) =>\n\t\t\t\t\ttableQuery(\n\t\t\t\t\t\ttable,\n\t\t\t\t\t\tconditions,\n\t\t\t\t\t\t[...constraints, spec],\n\t\t\t\t\t\tselection,\n\t\t\t\t\t\troute,\n\t\t\t\t\t\tordering,\n\t\t\t\t\t\tgroups,\n\t\t\t\t\t\tsearch,\n\t\t\t\t\t),\n\t\t\t\twith: (selected: OutputSelection, requested: GroupSelection = {}) =>\n\t\t\t\t\ttableQuery(\n\t\t\t\t\t\ttable,\n\t\t\t\t\t\tconditions,\n\t\t\t\t\t\tconstraints,\n\t\t\t\t\t\tselected,\n\t\t\t\t\t\troute,\n\t\t\t\t\t\tordering,\n\t\t\t\t\t\tplanner.groups(table, { ...groups, ...requested }),\n\t\t\t\t\t\tsearch,\n\t\t\t\t\t),\n\t\t\t\tsearch: (index: string, text: string) => {\n\t\t\t\t\tif (route) throw new SearchError('SEARCH_RELATION_UNSUPPORTED')\n\t\t\t\t\tif (ordering !== undefined || conditions.some(({ value }) => value != null))\n\t\t\t\t\t\tthrow new SearchError('SEARCH_ORDER_UNSUPPORTED')\n\t\t\t\t\tif (search) throw new SearchError('SEARCH_OPTIONS_UNSUPPORTED')\n\t\t\t\t\treturn tableQuery(table, conditions, constraints, selection, route, ordering, groups, {\n\t\t\t\t\t\tindex,\n\t\t\t\t\t\ttext,\n\t\t\t\t\t})\n\t\t\t\t},\n\t\t\t\trelation: (parent: string, declaredPath: string) => {\n\t\t\t\t\tif (search) throw new SearchError('SEARCH_RELATION_UNSUPPORTED')\n\t\t\t\t\tconst path = declaredPath.split('.')\n\t\t\t\t\tif (!path.length || path.length > 4) throw new Error('INVALID_RELATION')\n\t\t\t\t\tlet target = table\n\t\t\t\t\tfor (const name of path) {\n\t\t\t\t\t\tconst edge = planner.metadata.declarations[target]?.[name]\n\t\t\t\t\t\tif (!edge) throw new Error('INVALID_RELATION')\n\t\t\t\t\t\ttarget = edge.target\n\t\t\t\t\t}\n\t\t\t\t\treturn tableQuery(target, conditions, constraints, {}, { table, parent, path }, ordering)\n\t\t\t\t},\n\t\t\t\tasync paginate(input: QueryInput) {\n\t\t\t\t\t// Resolve once per global engine, before any native query or policy reads.\n\t\t\t\t\tconst codec = (cursorCodec ??= createEnvironmentCursorCodec())\n\t\t\t\t\tplanner.select(table, selection)\n\t\t\t\t\tassertRelationshipDepth(selection, route?.path.length ?? 0)\n\t\t\t\t\tconst searchPlan = search\n\t\t\t\t\t\t? planSearch(\n\t\t\t\t\t\t\t\tplanner,\n\t\t\t\t\t\t\t\ttable,\n\t\t\t\t\t\t\t\tsearch.index,\n\t\t\t\t\t\t\t\tsearch.text,\n\t\t\t\t\t\t\t\tinput,\n\t\t\t\t\t\t\t\tconstraints,\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t: undefined\n\t\t\t\t\tconst plan =\n\t\t\t\t\t\tsearchPlan?.plan ??\n\t\t\t\t\t\tplanner.plan(\n\t\t\t\t\t\t\ttable,\n\t\t\t\t\t\t\tinput,\n\t\t\t\t\t\t\troute ? [] : conditions,\n\t\t\t\t\t\t\tconstraints,\n\t\t\t\t\t\t\tordering,\n\t\t\t\t\t\t\troute ? 'relation' : 'root',\n\t\t\t\t\t\t)\n\t\t\t\t\tif (!searchPlan && plan.args.numItems > maxPageSize)\n\t\t\t\t\t\tthrow new Error('Page size exceeds central ceiling')\n\t\t\t\t\tconst resolved = await resolveGroups(table, planner.groups(table, groups))\n\t\t\t\t\tconst groupAccess: GroupAccess = {\n\t\t\t\t\t\tat(path) {\n\t\t\t\t\t\t\treturn path\n\t\t\t\t\t\t\t\t.slice(route?.path.length ?? 0)\n\t\t\t\t\t\t\t\t.reduce((node, name) => node.relations[name] ?? emptyGroups, resolved)\n\t\t\t\t\t\t},\n\t\t\t\t\t\tallowed(path) {\n\t\t\t\t\t\t\treturn Object.entries(this.at(path).fieldGroups)\n\t\t\t\t\t\t\t\t.filter(([, decision]) => decision.allowed)\n\t\t\t\t\t\t\t\t.map(([name]) => name)\n\t\t\t\t\t\t},\n\t\t\t\t\t\tcheckOrder(table, fields, path) {\n\t\t\t\t\t\t\tfor (const field of fields)\n\t\t\t\t\t\t\t\tcheckField(table, field, path ? this.at(path) : emptyGroups)\n\t\t\t\t\t\t},\n\t\t\t\t\t}\n\t\t\t\t\tfunction checkField(table: string, field: string, groups: ResolvedGroups) {\n\t\t\t\t\t\tfor (const [name, fields] of Object.entries(planner.metadata.tables[table].fieldGroups))\n\t\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t\tfields.includes(field.split('.')[0]) &&\n\t\t\t\t\t\t\t\tgroups.fieldGroups[name]?.allowed !== true\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\tthrow new FieldGroupError()\n\t\t\t\t\t}\n\t\t\t\t\tfunction checkClient(table: string, ast: FilterAst, groups: ResolvedGroups): void {\n\t\t\t\t\t\tif (ast.kind === 'scalar') checkField(table, ast.field, groups)\n\t\t\t\t\t\telse if (ast.kind === 'variant') {\n\t\t\t\t\t\t\tcheckField(table, ast.discriminator, groups)\n\t\t\t\t\t\t\tcheckClient(table, ast.child, groups)\n\t\t\t\t\t\t} else if (ast.kind === 'not') checkClient(table, ast.child, groups)\n\t\t\t\t\t\telse if (ast.kind === 'and' || ast.kind === 'or')\n\t\t\t\t\t\t\tfor (const child of ast.children) checkClient(table, child, groups)\n\t\t\t\t\t\telse if (ast.kind === 'relation' && ast.predicate)\n\t\t\t\t\t\t\tcheckClient(\n\t\t\t\t\t\t\t\tplanner.metadata.declarations[table][ast.name].target,\n\t\t\t\t\t\t\t\tast.predicate,\n\t\t\t\t\t\t\t\tgroups.relations[ast.name] ?? emptyGroups,\n\t\t\t\t\t\t\t)\n\t\t\t\t\t}\n\t\t\t\t\t// Inspect the whole client tree independently of execution/contradiction short circuits.\n\t\t\t\t\tif (plan.residual.kind !== 'and') throw new Error('Invalid compiled filter root')\n\t\t\t\t\tcheckClient(table, plan.residual.children[0], resolved)\n\t\t\t\t\tif (searchPlan) checkField(table, searchPlan.metadata.searchField, resolved)\n\t\t\t\t\tconst sortIndex =\n\t\t\t\t\t\tordering ?? conditions.find((condition) => condition.value != null)?.index\n\t\t\t\t\tif (!searchPlan && sortIndex !== undefined)\n\t\t\t\t\t\tfor (const field of planner.metadata.tables[table].indexes.find(\n\t\t\t\t\t\t\t(index) => index.name === sortIndex,\n\t\t\t\t\t\t)?.fields ?? [])\n\t\t\t\t\t\t\tcheckField(table, field, resolved)\n\t\t\t\t\telse if (!searchPlan && !route && plan.index)\n\t\t\t\t\t\tfor (const field of plan.index.fields.slice(plan.prefix.length))\n\t\t\t\t\t\t\tcheckField(table, field, resolved)\n\t\t\t\t\t// Native root limits stay native; lowering those limits cannot raise\n\t\t\t\t\t// the independent shared request meter.\n\t\t\t\t\tplan.args.maximumRowsRead = Math.min(\n\t\t\t\t\t\tplan.args.maximumRowsRead ?? request.budgets.rawCandidates,\n\t\t\t\t\t\trequest.budgets.rawCandidates,\n\t\t\t\t\t)\n\t\t\t\t\tplan.args.maximumBytesRead = Math.min(\n\t\t\t\t\t\tplan.args.maximumBytesRead ?? request.budgets.documentBytes,\n\t\t\t\t\t\trequest.budgets.documentBytes,\n\t\t\t\t\t)\n\t\t\t\t\t// A relation route binds its cursors per child path, in the executor.\n\t\t\t\t\tconst registryVersion = await planner.registryVersion(\n\t\t\t\t\t\troute\n\t\t\t\t\t\t\t? { root: route.table, path: [], selection: {}, groups: emptyGroups, filters: [] }\n\t\t\t\t\t\t\t: { root: table, path: [], selection, groups: resolved, filters: [plan.residual] },\n\t\t\t\t\t)\n\t\t\t\t\tconst groupBinding: Record<string, Value> = {}\n\t\t\t\t\tif (hasGroups(resolved)) groupBinding.fieldGroups = resolved\n\t\t\t\t\t// SAFETY: selection has passed registry admission before cursor binding.\n\t\t\t\t\tconst serverFilters: CursorBinding['serverFilters'] = {\n\t\t\t\t\t\tpredicates: plan.serverIdentity,\n\t\t\t\t\t\tselection: selection as import('convex/values').Value,\n\t\t\t\t\t\t...groupBinding,\n\t\t\t\t\t\tscope: { ...request.scope },\n\t\t\t\t\t}\n\t\t\t\t\tif (searchPlan) serverFilters.search = {\n\t\t\t\t\t\tmode: 'search', text: searchPlan.text,\n\t\t\t\t\t\tindex: {...searchPlan.metadata, filterFields: [...searchPlan.metadata.filterFields]},\n\t\t\t\t\t\tequalities: searchPlan.equalities, scanLimit: searchPlan.scanLimit ?? null,\n\t\t\t\t\t}\n\t\t\t\t\tconst binding: CursorBinding = {\n\t\t\t\t\t\ttable: route?.table ?? table,\n\t\t\t\t\t\tindex: searchPlan?.metadata.name ?? plan.index?.name ?? 'by_creation_time',\n\t\t\t\t\t\torder: plan.args.order ?? 'asc',\n\t\t\t\t\t\tclientFilters: plan.clientIdentity,\n\t\t\t\t\t\t// Include output selection and the complete immutable scope in trusted identity.\n\t\t\t\t\t\tserverFilters,\n\t\t\t\t\t\trelationPath: [],\n\t\t\t\t\t\tprincipal: JSON.stringify(request.scope),\n\t\t\t\t\t\ttenant: request.scope.tenant,\n\t\t\t\t\t\tpolicyVersion,\n\t\t\t\t\t\tengineVersion,\n\t\t\t\t\t\tregistryVersion,\n\t\t\t\t\t}\n\t\t\t\t\t// The executor accepts registry-selected native tables, rather than a static DM key.\n\t\t\t\t\tconst executorRequest: unknown = request\n\t\t\t\t\t// SAFETY: only native DM types are erased at this private executor boundary.\n\t\t\t\t\tconst executionRequest = executorRequest as ExecutionRequest\n\t\t\t\t\tlet legacy = false\n\t\t\t\t\tconst opener: typeof codec = Object.freeze({\n\t\t\t\t\t\t...codec,\n\t\t\t\t\t\topen(token: string, cursorBinding: CursorBinding) {\n\t\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t\treturn codec.open(token, cursorBinding)\n\t\t\t\t\t\t\t} catch (error) {\n\t\t\t\t\t\t\t\tif (legacyDigest === undefined) {\n\t\t\t\t\t\t\t\t\tlegacy = true\n\t\t\t\t\t\t\t\t\tthrow error\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\treturn codec.open(token, { ...cursorBinding, registryVersion: legacyDigest })\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t},\n\t\t\t\t\t})\n\t\t\t\t\tconst execute = async () => {\n\t\t\t\t\t\tconst executor = relationshipExecutor(\n\t\t\t\t\t\t\tschema,\n\t\t\t\t\t\t\texecutionRequest,\n\t\t\t\t\t\t\tplanner,\n\t\t\t\t\t\t\topener,\n\t\t\t\t\t\t\tbinding,\n\t\t\t\t\t\t\tgroupAccess,\n\t\t\t\t\t\t)\n\t\t\t\t\t\texecutor.preflight(table, selection, plan.args, route?.path ?? [])\n\t\t\t\t\t\treturn searchPlan\n\t\t\t\t\t\t\t? await executeSearch(\n\t\t\t\t\t\t\t\t\texecutionRequest,\n\t\t\t\t\t\t\t\t\ttable,\n\t\t\t\t\t\t\t\t\tsearchPlan,\n\t\t\t\t\t\t\t\t\topener,\n\t\t\t\t\t\t\t\t\tbinding,\n\t\t\t\t\t\t\t\t\tmaxPageSize,\n\t\t\t\t\t\t\t\t\tasync (row) => {\n\t\t\t\t\t\t\t\t\t\t// SAFETY: native search returns system fields from the admitted registry table.\n\t\t\t\t\t\t\t\t\t\tconst document = row as RelationDocument\n\t\t\t\t\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t\t\t\t\t!(await request.authorize(table, row)) ||\n\t\t\t\t\t\t\t\t\t\t\t!(await executor.matches(table, document, plan.residual))\n\t\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t\t\t\treturn null\n\t\t\t\t\t\t\t\t\t\treturn executor.output(table, document, selection, plan.args)\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t: route\n\t\t\t\t\t\t\t\t? await executor.continuation(\n\t\t\t\t\t\t\t\t\t\troute.table,\n\t\t\t\t\t\t\t\t\t\troute.parent,\n\t\t\t\t\t\t\t\t\t\troute.path,\n\t\t\t\t\t\t\t\t\t\tselection,\n\t\t\t\t\t\t\t\t\t\tplan.args,\n\t\t\t\t\t\t\t\t\t\tconditions,\n\t\t\t\t\t\t\t\t\t\tconstraints,\n\t\t\t\t\t\t\t\t\t\tordering,\n\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t: await executeQuery(\n\t\t\t\t\t\t\t\t\t\tschema,\n\t\t\t\t\t\t\t\t\t\trequest,\n\t\t\t\t\t\t\t\t\t\ttable,\n\t\t\t\t\t\t\t\t\t\tplan,\n\t\t\t\t\t\t\t\t\t\tplanner,\n\t\t\t\t\t\t\t\t\t\topener,\n\t\t\t\t\t\t\t\t\t\tbinding,\n\t\t\t\t\t\t\t\t\t\tasync (row, permitted) => {\n\t\t\t\t\t\t\t\t\t\t\t// SAFETY: metered native stream rows contain the validated system fields.\n\t\t\t\t\t\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t\t\t\t\t\t!permitted ||\n\t\t\t\t\t\t\t\t\t\t\t\t!(await executor.matches(table, row as RelationDocument, plan.residual))\n\t\t\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t\t\t\t\treturn null\n\t\t\t\t\t\t\t\t\t\t\t// SAFETY: the same native row is authorized and matches the declared predicates.\n\t\t\t\t\t\t\t\t\t\t\treturn executor.output(table, row as RelationDocument, selection, plan.args)\n\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t}\n\t\t\t\t\tlet result: Awaited<ReturnType<typeof execute>>\n\t\t\t\t\ttry {\n\t\t\t\t\t\tresult = await execute()\n\t\t\t\t\t} catch (error) {\n\t\t\t\t\t\tif (!legacy) throw error\n\t\t\t\t\t\t// Every cursor opens before the first read, so the retry repeats no reads.\n\t\t\t\t\t\tlegacyDigest ??= await digest(relations.identity)\n\t\t\t\t\t\tresult = await execute()\n\t\t\t\t\t}\n\t\t\t\t\t// SAFETY: the private executor validated and projected every row against\n\t\t\t\t\t// this registry's DTO and selected edges before assembling the page.\n\t\t\t\t\treturn result as EnginePaginationResult<\n\t\t\t\t\t\tRelationQueryRow<Tables, D, keyof Tables, OutputSelection>\n\t\t\t\t\t>\n\t\t\t\t},\n\t\t\t})\n\t\t}\n\t\t// SAFETY: each key comes from the exact validated registry; the planner\n\t\t// validates all input and projection while mapped types retain table brands.\n\t\treturn Object.freeze(\n\t\t\tObject.fromEntries(Object.keys(relations.tables).map((table) => [table, tableQuery(table)])),\n\t\t) as { [T in keyof Tables]: TableQuery<Tables, D, T, {}, [1, 1, 1, 1], never, Schema> }\n\t}\n\treturn Object.assign(querying, {\n\t\ttables: relations.tables,\n\t\tcursorCodec<S extends z.ZodType<Value>>(\n\t\t\tctx: ManagedQueryContext<DataModelFromSchemaDefinition<Schema>>,\n\t\t\toptions: ScopedCursorOptions<S>,\n\t\t): ScopedCursorCodec<S> {\n\t\t\t// Admission precedes lazy key loading and all schema/codec operations.\n\t\t\tconst scope = security.cursorScope(ctx)\n\t\t\tcursorCodec ??= createEnvironmentCursorCodec()\n\t\t\treturn createScopedCursorCodec(cursorCodec.custom, scope, options)\n\t\t},\n\t})\n}\n\nfunction hasGroups(groups: ResolvedGroups): boolean {\n\treturn (\n\t\tObject.keys(groups.fieldGroups).length > 0 || Object.values(groups.relations).some(hasGroups)\n\t)\n}\n"],"mappings":"2mBA4DA,MAAMC,EAA8B,CAAE,YAAa,CAAC,EAAG,UAAW,CAAC,CAAE,EAmNrE,IAAaC,EAAb,cAAqC,KAAM,CAC1C,KAAgB,qBAChB,aAAc,CACb,MAAM,oBAAoB,EAC1B,KAAK,KAAO,iBACb,CACD,EA8CA,SAAgBC,EAKf,EACA,EACC,CACD,GAAM,CAAE,aAAcC,EAEhBC,EAAW,CAAE,GAAGD,EAAQ,GAAGE,CAAQ,EACnCC,EAAUC,EAAcJ,EAAO,OAAQK,CAAS,EAChDC,EAASH,EAAQ,aAEjBI,kBAAW,IAAI,IAIrB,IAAK,GAAM,CAACC,EAAOC,KAAgB,OAAO,QAAQR,EAAS,aAAe,CAAC,CAAC,EAAG,CAC9E,GAAI,CAAC,OAAO,OAAOE,EAAQ,SAAS,OAAQK,CAAK,EAChD,MAAU,MAAM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