@prisma-next-idb/client-idb 0.1.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.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"idb-orm-NHIZ3oNt.mjs","names":["getKeyPath","#contract","#modelName","#executor","#storeName","#state","#newGroupingKey","#includeRefinementMode","#clone","#resolveIncludeEntry","#buildScanPlan","#applyIncludes","#projectRows","#combinedFilterExpr","#materialize","#planMeta","getKeyPath","#countTerminal"],"sources":["../src/core/types.ts","../src/core/model-accessor.ts","../src/core/store-state.ts","../src/core/query-shaping.ts","../src/core/aggregate-builder.ts","../src/core/grouped-accessor.ts","../src/core/relation-loader.ts","../src/core/mutation-scope.ts","../src/core/relation-mutator.ts","../src/core/mutation-executor.ts","../src/core/store-accessor.ts","../src/core/idb-orm.ts"],"sourcesContent":["import type { Contract, ContractModelBase, ContractReferenceRelation } from \"@prisma-next/contract/types\";\nimport { domainModelsAtDefaultNamespace } from \"@prisma-next/contract/types\";\nimport type {\n ExtractIdbFieldInputTypes,\n ExtractIdbFieldOutputTypes,\n IdbModelStorage,\n IdbStorage,\n} from \"@prisma-next-idb/target-idb/pack\";\n\n// Re-export for consumers who only import from client-idb\nexport type { IdbStorage };\n\n// ── Model-map extraction (v0.12.0 domain plane) ───────────────────────────────\n\n/**\n * Extract the model map from a contract by traversing the domain namespace.\n *\n * 0.14.0: `Contract` dropped the 2nd `TModels` type parameter; models are now\n * accessed via `domain.namespaces[ns].models`. For a single-namespace contract\n * (the only IDB case) this resolves to the typed model map; falls back to\n * `Record<string, ContractModelBase>` for un-typed/multi-namespace inputs.\n */\ntype ModelsOf<TContract> = TContract extends { readonly domain: { readonly namespaces: infer NS } }\n ? NS[keyof NS] extends { readonly models: infer M }\n ? M extends Record<string, ContractModelBase>\n ? M\n : Record<string, ContractModelBase>\n : Record<string, ContractModelBase>\n : Record<string, ContractModelBase>;\n\n// ── IdbContract convenience alias ─────────────────────────────────────────────\n\n/**\n * A `Contract` narrowed to the IDB storage shape. Accepts any contract whose\n * `storage` block includes an `stores` record, regardless of the model set or\n * whether type maps are attached.\n */\nexport type IdbContract = Contract<IdbStorage>;\n\n// ── Row type resolution (full type-maps path) ─────────────────────────────────\n\n/**\n * Resolve the output row type for a model from the contract's type maps.\n *\n * When `fieldOutputTypes` is parameterised (the emitted contract carries\n * `IdbContractWithTypeMaps<Base, IdbTypeMaps<Codecs, FieldOutputTypes, ...>>`),\n * each field gets the exact TypeScript type emitted by the family (e.g. `Date`\n * for `idb/date@1`).\n *\n * Falls back to `Record<string, unknown>` when type maps are absent (plain\n * `IdbContract` or when the model name is not in the type maps).\n */\n// FieldOutputTypes = { __unbound__: { User: {...} } } — union over namespace values\n// to get the flat model map, then look up ModelName.\ntype NsFieldOutputTypes<TContract> = ExtractIdbFieldOutputTypes<TContract>[keyof ExtractIdbFieldOutputTypes<TContract>];\n\ntype ResolvedOutputRow<TContract, ModelName extends string> = string extends keyof ExtractIdbFieldOutputTypes<TContract>\n ? Record<string, unknown>\n : NsFieldOutputTypes<TContract> extends Record<string, unknown>\n ? ModelName extends keyof NsFieldOutputTypes<TContract>\n ? { -readonly [K in keyof NsFieldOutputTypes<TContract>[ModelName]]: NsFieldOutputTypes<TContract>[ModelName][K] }\n : Record<string, unknown>\n : Record<string, unknown>;\n\n/**\n * Resolve the input row type for a model from the contract's type maps.\n *\n * Mirrors `ResolvedOutputRow` but uses `fieldInputTypes` — the input types\n * used for `create()`, `where()`, and mutation payloads.\n */\ntype NsFieldInputTypes<TContract> = ExtractIdbFieldInputTypes<TContract>[keyof ExtractIdbFieldInputTypes<TContract>];\n\ntype ResolvedInputRow<TContract, ModelName extends string> = string extends keyof ExtractIdbFieldInputTypes<TContract>\n ? Record<string, unknown>\n : NsFieldInputTypes<TContract> extends Record<string, unknown>\n ? ModelName extends keyof NsFieldInputTypes<TContract>\n ? { -readonly [K in keyof NsFieldInputTypes<TContract>[ModelName]]: NsFieldInputTypes<TContract>[ModelName][K] }\n : Record<string, unknown>\n : Record<string, unknown>;\n\n// ── Public row types ──────────────────────────────────────────────────────────\n\n/** The full TypeScript row shape returned by `all()`, `first()`, and `create()`. */\nexport type DefaultModelRow<TContract, ModelName extends string> = ResolvedOutputRow<TContract, ModelName>;\n\n// ── Where filter ──────────────────────────────────────────────────────────────\n\n/**\n * Partial equality filter applied as an in-memory predicate during cursor scans.\n *\n * All fields are optional — only provided fields are checked. Values use the\n * output types (from `fieldOutputTypes`) since the values are compared against\n * decoded row data.\n */\nexport type WhereFilter<TContract, ModelName extends string> = {\n readonly [K in keyof DefaultModelRow<TContract, ModelName>]?: DefaultModelRow<TContract, ModelName>[K];\n};\n\n// ── KeyPath / KeyType ─────────────────────────────────────────────────────────\n\n/**\n * The literal `keyPath` string for a model, extracted from\n * `contract.models[ModelName].storage.keyPath`.\n *\n * Used at the type level to exclude the key field from `CreateInput` and to\n * narrow the `findUnique` / `delete` key parameter type.\n */\nexport type ModelKeyPath<TContract, ModelName extends string> = ModelName extends keyof ModelsOf<TContract>\n ? ModelsOf<TContract>[ModelName] extends { storage: { keyPath: infer P } }\n ? P extends string\n ? P\n : never\n : never\n : never;\n\n/**\n * TypeScript type of the primary key field for a given model.\n *\n * When the output row type has a field matching `ModelKeyPath`, that field's\n * type is used. Otherwise falls back to `IDBValidKey` (the DOM union of valid\n * IDB key types).\n */\nexport type KeyType<TContract, ModelName extends string> =\n ModelKeyPath<TContract, ModelName> extends keyof ResolvedOutputRow<TContract, ModelName>\n ? ResolvedOutputRow<TContract, ModelName>[ModelKeyPath<TContract, ModelName>]\n : IDBValidKey;\n\n// ── Create input ──────────────────────────────────────────────────────────────\n\n/** The keyPath field when it exists in the resolved input row (may be absent for models with no typed maps). */\ntype KeyPathField<TContract, ModelName extends string> = ModelKeyPath<TContract, ModelName> &\n keyof ResolvedInputRow<TContract, ModelName>;\n\n/**\n * Input shape for `create()`: the full input row with the primary key field\n * made optional (IDB can generate keys via `autoIncrement`, or clients may\n * omit the key when using `cuid()` / `uuid()` at the application layer).\n */\nexport type CreateInput<TContract, ModelName extends string> = Omit<\n ResolvedInputRow<TContract, ModelName>,\n ModelKeyPath<TContract, ModelName>\n> &\n Partial<Pick<ResolvedInputRow<TContract, ModelName>, KeyPathField<TContract, ModelName>>>;\n\n// ── Relations ─────────────────────────────────────────────────────────────────\n\n/** Extract the relations record for a model. */\ntype ModelRelations<TContract, ModelName extends string> = ModelName extends keyof ModelsOf<TContract>\n ? ModelsOf<TContract>[ModelName] extends { relations: infer R }\n ? R extends Record<string, unknown>\n ? R\n : Record<string, never>\n : Record<string, never>\n : Record<string, never>;\n\n/**\n * Union of relation keys on a model that are `ContractReferenceRelation`s\n * (i.e. cross-store joins, not embedded documents).\n *\n * Used to constrain the `include()` method's `relation` parameter to only\n * valid reference relation names.\n */\nexport type ReferenceRelKeys<TContract, ModelName extends string> = {\n [K in keyof ModelRelations<TContract, ModelName>]: ModelRelations<\n TContract,\n ModelName\n >[K] extends ContractReferenceRelation\n ? K\n : never;\n}[keyof ModelRelations<TContract, ModelName>] &\n string;\n\n/**\n * TypeScript row type for an included relation.\n *\n * - `1:N` cardinality → `RelatedRow[]`\n * - `N:1` or `1:1` cardinality → `RelatedRow | null`\n */\ntype RelationRowType<TContract, ModelName extends string, RelKey extends string> = RelKey extends keyof ModelRelations<\n TContract,\n ModelName\n>\n ? ModelRelations<TContract, ModelName>[RelKey] extends ContractReferenceRelation\n ? ModelRelations<TContract, ModelName>[RelKey] extends {\n to: { model: infer To extends string };\n cardinality: infer C;\n }\n ? C extends \"1:N\"\n ? DefaultModelRow<TContract, To>[]\n : DefaultModelRow<TContract, To> | null\n : never\n : never\n : never;\n\n/**\n * Per-relation include marker tracked at the type level.\n *\n * - `true` — the relation is loaded as rows (array for `1:N`, single/null\n * otherwise), optionally refined by a `where`/`orderBy`/`take` callback.\n * - `\"scalar\"` — the relation is reduced to a `count()` (Phase 6.5), so the\n * row field becomes a `number` instead of related rows.\n */\nexport type IncludeMarker = true | \"scalar\";\n\n/** Which relations are included in the current accessor chain. */\nexport type IncludeSpec<TContract, ModelName extends string> = Partial<\n Record<ReferenceRelKeys<TContract, ModelName>, IncludeMarker>\n>;\n\n/** Empty include spec — no relations included. */\nexport type NoIncludes = Record<never, never>;\n\n/**\n * The relation fields contributed by a set of `.include()` calls.\n *\n * A key is added only when its `TIncludes` marker is set; the field type is\n * `number` for a scalar `count()` include and the cardinality-shaped related\n * row(s) otherwise. Split out from {@link IncludedRow} so {@link SelectedRow}\n * can re-use it on top of a projected (picked) scalar base.\n */\nexport type IncludeFields<TContract, ModelName extends string, TIncludes extends IncludeSpec<TContract, ModelName>> = {\n -readonly [K in keyof TIncludes & string as TIncludes[K] extends IncludeMarker\n ? K\n : never]: TIncludes[K] extends \"scalar\" ? number : RelationRowType<TContract, ModelName, K>;\n};\n\n/**\n * A row type that merges the base model row with any included relation fields.\n *\n * The extra fields are only added when the corresponding key in `TIncludes` is\n * set, so the type stays narrow until `.include()` is called.\n */\nexport type IncludedRow<\n TContract,\n ModelName extends string,\n TIncludes extends IncludeSpec<TContract, ModelName>,\n> = DefaultModelRow<TContract, ModelName> & IncludeFields<TContract, ModelName, TIncludes>;\n\n/**\n * The row type after an optional `.select()` projection.\n *\n * When `TSelected` is `never` (no `.select()` call) the full {@link IncludedRow}\n * is returned. Otherwise the scalar base is narrowed to the picked fields, with\n * any included relation fields preserved (mirrors the vendor `select()` which\n * keeps relations and narrows only scalar columns).\n */\nexport type SelectedRow<\n TContract,\n ModelName extends string,\n TIncludes extends IncludeSpec<TContract, ModelName>,\n TSelected extends string,\n> = [TSelected] extends [never]\n ? IncludedRow<TContract, ModelName, TIncludes>\n : Pick<DefaultModelRow<TContract, ModelName>, TSelected & keyof DefaultModelRow<TContract, ModelName>> &\n IncludeFields<TContract, ModelName, TIncludes>;\n\n// ── Patch input ───────────────────────────────────────────────────────────────\n\n/**\n * Partial update shape for `update()`, `updateAll()`, `updateCount()`, and the\n * `update` arm of `upsert()`. All fields are optional — only provided fields\n * are shallow-merged onto the existing record.\n */\nexport type PatchInput<TContract, ModelName extends string> = Partial<DefaultModelRow<TContract, ModelName>>;\n\n// ── Relation mutation types ───────────────────────────────────────────────────\n\n/** Extracts the `to` model name for a named relation on a model. */\nexport type RelatedModelOf<\n TContract,\n ModelName extends string,\n RelName extends string,\n> = ModelName extends keyof ModelsOf<TContract>\n ? ModelsOf<TContract>[ModelName] extends {\n relations: Record<RelName, { to: { model: infer To extends string } }>;\n }\n ? To\n : string\n : string;\n\n/** Nested-create descriptor: insert one or more related records. */\nexport interface RelationMutationCreate<TContract, ModelName extends string> {\n readonly kind: \"create\";\n readonly data: readonly CreateInput<TContract, ModelName>[];\n}\n\n/** Nested-connect descriptor: link existing records to the parent via FK update. */\nexport interface RelationMutationConnect {\n readonly kind: \"connect\";\n readonly criteria: readonly Record<string, unknown>[];\n}\n\n/**\n * Nested-disconnect descriptor: unlink related records by setting FK to null.\n * With no criteria, disconnects all child records from this parent.\n */\nexport interface RelationMutationDisconnect {\n readonly kind: \"disconnect\";\n readonly criteria?: readonly Record<string, unknown>[];\n}\n\n/** Discriminated union of all nested relation mutation descriptors. */\nexport type IdbRelationMutation<TContract, ModelName extends string> =\n | RelationMutationCreate<TContract, ModelName>\n | RelationMutationConnect\n | RelationMutationDisconnect;\n\n/** Relation mutator object passed to the user's relation callback. */\nexport interface IdbRelationMutator<TContract, ModelName extends string> {\n create(\n data: CreateInput<TContract, ModelName> | readonly CreateInput<TContract, ModelName>[]\n ): RelationMutationCreate<TContract, ModelName>;\n connect(criteria: Record<string, unknown> | readonly Record<string, unknown>[]): RelationMutationConnect;\n disconnect(criteria?: readonly Record<string, unknown>[]): RelationMutationDisconnect;\n}\n\n/**\n * Maps each reference relation key to an optional mutation callback.\n *\n * When `ReferenceRelKeys` widens to `string` (a loosely-typed `IdbContract`\n * with no emitted type maps), a naive mapped type would become an index\n * signature `{ [k: string]: callback }` that incorrectly forces *every* field —\n * scalars included — to be a relation callback, so even `create({ name: \"x\" })`\n * would fail to type-check. The `string extends …` guard detects that case and\n * contributes no constraint (`& unknown` is identity), leaving plain scalar\n * payloads valid. Precisely-typed contracts (the emitted `contract.d.ts`)\n * resolve `ReferenceRelKeys` to a finite union and get the full callback typing.\n */\ntype RelationMutationFields<TContract, ModelName extends string> =\n string extends ReferenceRelKeys<TContract, ModelName>\n ? unknown\n : Partial<{\n [K in ReferenceRelKeys<TContract, ModelName>]: (\n mutator: IdbRelationMutator<TContract, RelatedModelOf<TContract, ModelName, K>>\n ) => IdbRelationMutation<TContract, RelatedModelOf<TContract, ModelName, K>>;\n }>;\n\n/**\n * The `localFields` of all N:1 relations on a model — the FK fields that are\n * owned by this model and can be supplied via a relation callback instead of\n * as a scalar value. Resolves to `never` for loosely-typed contracts where\n * cardinality is not preserved as a literal (e.g. `defineContract` in tests).\n *\n * Mirrors `ChildForeignKeyFieldNames` from `sql-orm-client`, simplified: instead\n * of crawling all models for relations pointing *to* this one, we look at this\n * model's own N:1 relations directly — same field set, one model.\n */\ntype N1LocalFieldNames<TContract, ModelName extends string> = {\n [K in keyof ModelRelations<TContract, ModelName>]: ModelRelations<TContract, ModelName>[K] extends {\n readonly cardinality: \"N:1\";\n readonly on: { readonly localFields: infer Fields extends readonly string[] };\n }\n ? Fields[number]\n : never;\n}[keyof ModelRelations<TContract, ModelName>] &\n string;\n\n/**\n * Like `CreateInput` but with N:1 FK fields made optional.\n *\n * An N:1 FK field (e.g. `authorId` on `Post`) can be supplied either as a\n * scalar value or via a relation callback (`author: (rel) => rel.connect({id})`).\n * Making it optional here lets callers omit it when using the callback form —\n * the executor populates it from the related record before inserting.\n *\n * Mirrors `NestedCreateInput` from `sql-orm-client`.\n */\ntype NestedCreateInput<TContract, ModelName extends string> = Omit<\n CreateInput<TContract, ModelName>,\n N1LocalFieldNames<TContract, ModelName>\n> &\n Partial<\n Pick<\n CreateInput<TContract, ModelName>,\n N1LocalFieldNames<TContract, ModelName> & keyof CreateInput<TContract, ModelName>\n >\n >;\n\n/**\n * Input shape for `create()` with optional relation callbacks.\n * N:1 FK fields (e.g. `authorId`) are optional when using a relation callback.\n * Relation fields accept a callback `(rel) => rel.create([...])` / `rel.connect(...)`.\n */\nexport type MutationCreateInput<TContract, ModelName extends string> = NestedCreateInput<TContract, ModelName> &\n RelationMutationFields<TContract, ModelName>;\n\n/**\n * Input shape for `update()` with optional relation callbacks.\n * All scalar fields are optional (shallow merge); relation fields accept\n * `connect` or `disconnect` callbacks.\n */\nexport type MutationUpdateInput<TContract, ModelName extends string> = PatchInput<TContract, ModelName> &\n RelationMutationFields<TContract, ModelName>;\n\n// ── OrderBy spec ─────────────────────────────────────────────────────────────\n\n/** Sort direction for `orderBy()`. */\nexport type SortDirection = \"asc\" | \"desc\";\n\n/** Partial sort spec: field name → direction. */\nexport type OrderBySpec<TContract, ModelName extends string> = Partial<\n Record<string & keyof DefaultModelRow<TContract, ModelName>, SortDirection>\n>;\n\n// ── Aggregate / groupBy ───────────────────────────────────────────────────────\n\n/** The five aggregation functions, matching the vendor `AggregateFn`. */\nexport type AggregateFn = \"count\" | \"sum\" | \"avg\" | \"min\" | \"max\";\n\n/**\n * Fields eligible for numeric aggregation (`sum`/`avg`/`min`/`max`).\n *\n * For an emitted (precisely-typed) contract this narrows to the fields whose\n * output type is assignable to `number`. For a loosely-typed `IdbContract`\n * (no type maps — `DefaultModelRow` is `Record<string, unknown>`) the row key\n * set widens to `string`, so we allow any field name rather than collapsing to\n * `never`. Mirrors `NumericFieldNames` from `sql-orm-client`, trait-free.\n */\nexport type NumericFieldNames<TContract, ModelName extends string> = string extends keyof DefaultModelRow<\n TContract,\n ModelName\n>\n ? string\n : {\n [K in keyof DefaultModelRow<TContract, ModelName> & string]: NonNullable<\n DefaultModelRow<TContract, ModelName>[K]\n > extends number\n ? K\n : never;\n }[keyof DefaultModelRow<TContract, ModelName> & string];\n\ndeclare const idbAggregateResultBrand: unique symbol;\n\n/**\n * A single aggregation selector produced by the {@link IdbAggregateBuilder}.\n *\n * The phantom `Result` brand carries the per-selector result type so\n * {@link IdbAggregateResult} can map each alias back to its value type.\n * Mirrors the vendor `AggregateSelector`.\n */\nexport interface IdbAggregateSelector<Result> {\n readonly kind: \"aggregate\";\n readonly fn: AggregateFn;\n readonly field?: string;\n readonly [idbAggregateResultBrand]?: Result;\n}\n\n/** A map of result aliases → aggregation selectors (the `aggregate()` spec). */\nexport type IdbAggregateSpec = Record<string, IdbAggregateSelector<unknown>>;\n\n/** The result row shape for an {@link IdbAggregateSpec}: alias → value type. */\nexport type IdbAggregateResult<Spec extends IdbAggregateSpec> = {\n [K in keyof Spec]: Spec[K] extends IdbAggregateSelector<infer Result> ? Result : never;\n};\n\n/**\n * The builder handed to an `.aggregate(agg => …)` callback. `count()` is always\n * available; the field reducers are constrained to {@link NumericFieldNames}.\n * Mirrors the vendor `AggregateBuilder`, minus the SQL column mapping.\n */\nexport interface IdbAggregateBuilder<TContract, ModelName extends string> {\n count(): IdbAggregateSelector<number>;\n sum<F extends NumericFieldNames<TContract, ModelName>>(field: F): IdbAggregateSelector<number | null>;\n avg<F extends NumericFieldNames<TContract, ModelName>>(field: F): IdbAggregateSelector<number | null>;\n min<F extends NumericFieldNames<TContract, ModelName>>(field: F): IdbAggregateSelector<number | null>;\n max<F extends NumericFieldNames<TContract, ModelName>>(field: F): IdbAggregateSelector<number | null>;\n}\n\n// ── Model storage helpers ─────────────────────────────────────────────────────\n\n/**\n * Extract the `storeName` from a model's storage metadata at runtime.\n * Falls back to the model name if `storeName` is absent.\n */\nexport function getStoreName(contract: IdbContract, modelName: string): string {\n const model = domainModelsAtDefaultNamespace(contract.domain)[modelName];\n return (model?.storage as IdbModelStorage | undefined)?.storeName ?? modelName;\n}\n\n/**\n * Extract the `keyPath` from a model's storage metadata at runtime.\n * Falls back to `\"id\"` (the invariant key name for all syncable IDB models).\n */\nexport function getKeyPath(contract: IdbContract, modelName: string): string {\n const model = domainModelsAtDefaultNamespace(contract.domain)[modelName];\n return (model?.storage as IdbModelStorage | undefined)?.keyPath ?? \"id\";\n}\n\n/**\n * Resolve a model's named relation to a {@link ContractReferenceRelation} at\n * runtime, or `undefined` when the relation is absent or an embedded relation\n * (no `on` join block). Used by `include()` to find the related model name and\n * cardinality before building the child-accessor refinement.\n */\nexport function getRelation(\n contract: IdbContract,\n modelName: string,\n relName: string\n): ContractReferenceRelation | undefined {\n const relation = domainModelsAtDefaultNamespace(contract.domain)[modelName]?.relations?.[relName];\n if (relation === undefined || !(\"on\" in relation)) return undefined;\n return relation as ContractReferenceRelation;\n}\n","/**\n * Proxy-based typed accessor handed to `where(fn)` callbacks.\n *\n * Mirrors `createModelAccessor()` from\n * `vendor/prisma-next/packages/3-extensions/sql-orm-client` but without\n * the codec-trait gating — IDB stores native JS values, so every operator\n * is available on every field. The runtime accessor object is a Proxy\n * that materialises an `IdbFieldAccessor` on demand for whichever field\n * name is read; the type-level surface narrows that to the model's\n * actual field set so callbacks get autocomplete.\n */\n\nimport {\n type IdbFieldFilter,\n type IdbFilterExpr,\n type IdbNullCheckExpr,\n fieldFilter,\n nullCheckExpr,\n} from \"@prisma-next-idb/adapter-idb/runtime\";\nimport type { DefaultModelRow, IdbContract } from \"./types\";\n\n// ── Typed surface ─────────────────────────────────────────────────────────────\n\n/**\n * Operator surface available on every field. All return frozen AST nodes\n * — they are values, not statements, so chaining and logical combinators\n * compose naturally.\n */\nexport interface IdbFieldAccessor<T> {\n eq(value: T): IdbFieldFilter;\n neq(value: T): IdbFieldFilter;\n gt(value: T): IdbFieldFilter;\n lt(value: T): IdbFieldFilter;\n gte(value: T): IdbFieldFilter;\n lte(value: T): IdbFieldFilter;\n in(values: ReadonlyArray<T>): IdbFieldFilter;\n notIn(values: ReadonlyArray<T>): IdbFieldFilter;\n contains(sub: string): IdbFieldFilter;\n startsWith(sub: string): IdbFieldFilter;\n endsWith(sub: string): IdbFieldFilter;\n isNull(): IdbNullCheckExpr;\n isNotNull(): IdbNullCheckExpr;\n}\n\n/**\n * The accessor object handed to a `where(fn)` callback. Each key resolves\n * to an {@link IdbFieldAccessor} typed against the field's output type.\n *\n * The Proxy makes every string key resolve to an accessor at runtime; the\n * type narrows the visible surface to the model's declared fields so the\n * developer gets autocomplete.\n */\nexport type IdbModelAccessor<TContract, ModelName extends string> = {\n readonly [K in keyof DefaultModelRow<TContract, ModelName>]-?: IdbFieldAccessor<\n DefaultModelRow<TContract, ModelName>[K]\n >;\n};\n\n// ── Runtime construction ──────────────────────────────────────────────────────\n\n/**\n * Build an {@link IdbFieldAccessor} for a single field. The accessor is\n * shared via the proxy below, but each accessor instance is bound to its\n * own field name so the produced AST nodes carry the right field.\n */\nfunction createFieldAccessor(field: string): IdbFieldAccessor<unknown> {\n return {\n eq: (value) => fieldFilter(field, \"eq\", value),\n neq: (value) => fieldFilter(field, \"neq\", value),\n gt: (value) => fieldFilter(field, \"gt\", value),\n lt: (value) => fieldFilter(field, \"lt\", value),\n gte: (value) => fieldFilter(field, \"gte\", value),\n lte: (value) => fieldFilter(field, \"lte\", value),\n in: (values) => fieldFilter(field, \"in\", values),\n notIn: (values) => fieldFilter(field, \"notIn\", values),\n contains: (sub) => fieldFilter(field, \"contains\", sub),\n startsWith: (sub) => fieldFilter(field, \"startsWith\", sub),\n endsWith: (sub) => fieldFilter(field, \"endsWith\", sub),\n isNull: () => nullCheckExpr(field, true),\n isNotNull: () => nullCheckExpr(field, false),\n };\n}\n\n/**\n * Create the typed model accessor used by `where(fn)` callbacks.\n *\n * Implemented as a Proxy keyed on field names — every read returns an\n * {@link IdbFieldAccessor} bound to that field. The accessor cache means\n * `m.name.eq(...)` and `m.name.startsWith(...)` reuse the same per-field\n * accessor object across the same `where()` invocation.\n *\n * The type parameter is structural — no contract is consulted at\n * runtime, because TS already gates the visible field set at compile\n * time. (Misspelled fields surface as accessor calls on names that\n * don't exist in stored rows, which is consistent with how the\n * shorthand form behaves today.)\n */\nexport function createModelAccessor<TContract extends IdbContract, ModelName extends string>(): IdbModelAccessor<\n TContract,\n ModelName\n> {\n const cache = new Map<string, IdbFieldAccessor<unknown>>();\n return new Proxy(\n {},\n {\n get(_target, prop: string | symbol) {\n if (typeof prop !== \"string\") return undefined;\n let acc = cache.get(prop);\n if (acc === undefined) {\n acc = createFieldAccessor(prop);\n cache.set(prop, acc);\n }\n return acc;\n },\n }\n ) as IdbModelAccessor<TContract, ModelName>;\n}\n\n// ── Re-export the AST type the callback returns ───────────────────────────────\n\nexport type { IdbFilterExpr };\n","import type { IdbFilterExpr } from \"@prisma-next-idb/adapter-idb/runtime\";\n\n/**\n * One entry in {@link IdbAccessorState.includes} — describes how a single\n * `.include()`d relation should be materialised.\n *\n * - `collection`: load the related rows, applying the refined `state`\n * (its `filters` / `orderBy` / `skip` / `take`) to the child scan.\n * `state` is `emptyAccessorState()` for an unrefined `.include(rel)`.\n * - `scalar`: reduce the related rows to a single number (Phase 6.5 supports\n * `count`). The `state.filters` still constrain which children are counted.\n *\n * Mirrors the `IncludeExpr` / `IncludeScalar` split from\n * `vendor/prisma-next/.../sql-orm-client`, collapsed to the two shapes IDB\n * needs (no SQL column mapping, no `combine()` branches).\n */\nexport type IncludeEntry =\n | { readonly kind: \"collection\"; readonly state: IdbAccessorState }\n | { readonly kind: \"scalar\"; readonly fn: \"count\"; readonly state: IdbAccessorState };\n\n/**\n * Marker returned by the child accessor's `.count()` terminal when it is\n * called inside an `include()` refinement callback. Carries the refined\n * child `state` so the relation loader counts only the matching children.\n *\n * Detected by {@link isIncludeScalar}; mirrors `IncludeScalar` from the\n * vendor `sql-orm-client/include-descriptors.ts`.\n */\nexport interface IdbIncludeScalar {\n readonly kind: \"includeScalar\";\n readonly fn: \"count\";\n readonly state: IdbAccessorState;\n}\n\n/** Build an {@link IdbIncludeScalar} marker for a `count()` reducer. */\nexport function createIncludeScalar(state: IdbAccessorState): IdbIncludeScalar {\n return { kind: \"includeScalar\", fn: \"count\", state };\n}\n\n/** Type guard: is `value` an {@link IdbIncludeScalar} marker? */\nexport function isIncludeScalar(value: unknown): value is IdbIncludeScalar {\n if (typeof value !== \"object\" || value === null) return false;\n const candidate = value as { kind?: unknown; fn?: unknown };\n return candidate.kind === \"includeScalar\" && candidate.fn === \"count\";\n}\n\n/**\n * Immutable state carried by each {@link IdbStoreAccessorImpl} node in the\n * builder chain.\n *\n * Every mutating method (`.where()`, `.take()`, etc.) returns a cloned\n * instance with an updated state rather than mutating in place, making the\n * accessor safe to reuse across multiple query branches.\n */\nexport interface IdbAccessorState {\n /**\n * Accumulated filter expressions. Multiple `.where()` calls compose\n * with AND semantics — the planner wraps them in an `andExpr` when\n * lowering. Each entry is already either an `IdbFieldFilter`, a\n * combinator, or a `null-check` node — never raw shorthand records.\n */\n readonly filters: ReadonlyArray<IdbFilterExpr>;\n /** Sort spec: field → direction. Applied as an in-memory comparator. */\n readonly orderBy?: Record<string, \"asc\" | \"desc\">;\n /** OFFSET (number of rows to skip). */\n readonly skip?: number;\n /** LIMIT (maximum number of rows to return). */\n readonly take?: number;\n /**\n * Relations that have been `.include()`d — keys are relation names, values\n * describe how to materialise each (collection vs scalar count, plus any\n * refinement state). See {@link IncludeEntry}.\n */\n readonly includes: Record<string, IncludeEntry>;\n /**\n * Fields kept by `.select()`. `undefined` means \"all fields\". When set,\n * the materialised rows are projected down to these fields (plus any\n * included relation keys) after the scan and relation loads complete.\n */\n readonly selectedFields?: ReadonlyArray<string>;\n}\n\n/** Create a fresh, empty accessor state (no filters, no ordering, no includes). */\nexport function emptyAccessorState(): IdbAccessorState {\n return {\n filters: [],\n includes: {},\n };\n}\n\n/** Return a shallow-merged copy of `state` with the given overrides applied. */\nexport function mergeAccessorState(state: IdbAccessorState, overrides: Partial<IdbAccessorState>): IdbAccessorState {\n return { ...state, ...overrides };\n}\n","import type { IdbFilterExpr } from \"@prisma-next-idb/adapter-idb/runtime\";\nimport { andExpr } from \"@prisma-next-idb/adapter-idb/runtime\";\nimport type { IdbRowComparator } from \"@prisma-next-idb/driver-idb/runtime\";\n\n/**\n * Combine accumulated filter expressions with AND.\n *\n * Returns `undefined` when no filter is present so callers can skip building a\n * row-filter closure. Shared by {@link IdbStoreAccessorImpl} (top-level scans)\n * and the relation loader (refined `include()` child scans).\n */\nexport function combineFilterExprs(filters: ReadonlyArray<IdbFilterExpr>): IdbFilterExpr | undefined {\n if (filters.length === 0) return undefined;\n if (filters.length === 1) return filters[0];\n return andExpr(filters);\n}\n\n/**\n * Build an in-memory comparator from an `orderBy` spec (field → direction).\n *\n * Returns `undefined` when there is nothing to sort by. Compares fields in\n * declaration order; values are primitives (strings, numbers, dates) in\n * practice, so JS relational comparison is sufficient.\n */\nexport function buildRowComparator(orderBy: Record<string, \"asc\" | \"desc\"> | undefined): IdbRowComparator | undefined {\n if (orderBy === undefined) return undefined;\n return (a: Record<string, unknown>, b: Record<string, unknown>): number => {\n for (const [field, dir] of Object.entries(orderBy)) {\n const av = a[field];\n const bv = b[field];\n if (av === bv) continue;\n const cmp = (av as string | number) < (bv as string | number) ? -1 : 1;\n return dir === \"desc\" ? -cmp : cmp;\n }\n return 0;\n };\n}\n","import type { AggregateFn, IdbAggregateBuilder, IdbAggregateSelector } from \"./types\";\n\n/**\n * Build the aggregate selector factory handed to `.aggregate(agg => …)` and\n * `groupBy(...).aggregate(agg => …)`.\n *\n * Each method returns a frozen {@link IdbAggregateSelector} marker; the actual\n * reduction happens in {@link reduceAggregate} once the matching rows are\n * materialised in memory. Mirrors `createAggregateBuilder` from the vendor\n * `sql-orm-client/aggregate-builder.ts`, minus the field→column mapping (IDB\n * stores native field names).\n */\nexport function createAggregateBuilder<TContract, ModelName extends string>(): IdbAggregateBuilder<\n TContract,\n ModelName\n> {\n return {\n count: () => ({ kind: \"aggregate\", fn: \"count\" }),\n sum: (field) => ({ kind: \"aggregate\", fn: \"sum\", field: field as string }),\n avg: (field) => ({ kind: \"aggregate\", fn: \"avg\", field: field as string }),\n min: (field) => ({ kind: \"aggregate\", fn: \"min\", field: field as string }),\n max: (field) => ({ kind: \"aggregate\", fn: \"max\", field: field as string }),\n };\n}\n\n/** Type guard: is `value` an {@link IdbAggregateSelector}? */\nexport function isAggregateSelector(value: unknown): value is IdbAggregateSelector<unknown> {\n if (typeof value !== \"object\" || value === null) return false;\n const candidate = value as { kind?: unknown; fn?: unknown };\n return (\n candidate.kind === \"aggregate\" &&\n (candidate.fn === \"count\" ||\n candidate.fn === \"sum\" ||\n candidate.fn === \"avg\" ||\n candidate.fn === \"min\" ||\n candidate.fn === \"max\")\n );\n}\n\n/**\n * Reduce a set of materialised rows to a single aggregate value.\n *\n * - `count` → the number of rows (always a number, never null).\n * - `sum`/`avg`/`min`/`max` → computed over the non-null numeric values of\n * `field`; `null` when no row has a non-null value (matching Prisma's\n * \"aggregate of an empty set is null\" and the vendor `coerceAggregateValue`\n * null handling). String-encoded numbers are coerced via `Number()`.\n */\nexport function reduceAggregate(\n fn: AggregateFn,\n field: string | undefined,\n rows: readonly Record<string, unknown>[]\n): number | null {\n if (fn === \"count\") return rows.length;\n if (field === undefined) return null;\n\n const values: number[] = [];\n for (const row of rows) {\n const raw = row[field];\n if (raw === null || raw === undefined) continue;\n const n = typeof raw === \"bigint\" ? Number(raw) : Number(raw as number);\n if (!Number.isNaN(n)) values.push(n);\n }\n\n if (values.length === 0) return null;\n\n switch (fn) {\n case \"sum\":\n return values.reduce((acc, n) => acc + n, 0);\n case \"avg\":\n return values.reduce((acc, n) => acc + n, 0) / values.length;\n case \"min\":\n return Math.min(...values);\n case \"max\":\n return Math.max(...values);\n }\n}\n\n/**\n * Run every selector in an aggregate spec against `rows`, producing the\n * result object keyed by the spec's aliases.\n */\nexport function computeAggregateSpec(\n spec: Record<string, IdbAggregateSelector<unknown>>,\n rows: readonly Record<string, unknown>[]\n): Record<string, number | null> {\n const result: Record<string, number | null> = {};\n for (const [alias, selector] of Object.entries(spec)) {\n result[alias] = reduceAggregate(selector.fn, selector.field, rows);\n }\n return result;\n}\n\n/**\n * Project an aggregate spec down to the plain `{ fn, field? }` shape carried by\n * the query AST (`IdbAggregateAst.aggregates` / `IdbGroupByAst.aggregates`), so\n * middleware can observe the requested aggregations.\n */\nexport function toAggregateRequests(\n spec: Record<string, IdbAggregateSelector<unknown>>\n): Record<string, { readonly fn: string; readonly field?: string }> {\n const out: Record<string, { fn: string; field?: string }> = {};\n for (const [alias, selector] of Object.entries(spec)) {\n out[alias] = selector.field !== undefined ? { fn: selector.fn, field: selector.field } : { fn: selector.fn };\n }\n return out;\n}\n\n/**\n * Validate a user-supplied aggregate spec: it must be non-empty and every\n * value must be a real selector. Throws a descriptive error otherwise, so a\n * typo (e.g. returning a plain object) fails loudly rather than silently\n * producing `NaN`/`undefined` results.\n */\nexport function assertValidAggregateSpec(\n spec: Record<string, unknown>,\n context: \"aggregate()\" | \"groupBy().aggregate()\"\n): void {\n const entries = Object.entries(spec);\n if (entries.length === 0) {\n throw new Error(`${context} requires at least one aggregation selector`);\n }\n for (const [alias, selector] of entries) {\n if (!isAggregateSelector(selector)) {\n throw new Error(`${context} selector \"${alias}\" is invalid`);\n }\n }\n}\n","import type { IdbFilterExpr, IdbGroupByAst, IdbQueryAst } from \"@prisma-next-idb/adapter-idb/runtime\";\nimport {\n assertValidAggregateSpec,\n computeAggregateSpec,\n createAggregateBuilder,\n toAggregateRequests,\n} from \"./aggregate-builder\";\nimport type { DefaultModelRow, IdbAggregateBuilder, IdbAggregateResult, IdbAggregateSpec } from \"./types\";\n\n/**\n * One grouped-aggregate result row: the group-key fields picked from the model\n * row, intersected with the aggregate aliases.\n */\nexport type GroupedResultRow<\n TContract,\n ModelName extends string,\n Fields extends readonly string[],\n Spec extends IdbAggregateSpec,\n> = Pick<DefaultModelRow<TContract, ModelName>, Fields[number] & keyof DefaultModelRow<TContract, ModelName>> &\n IdbAggregateResult<Spec>;\n\n/**\n * Grouped-aggregate builder returned by `accessor.groupBy(...)`.\n *\n * Port of the vendor `GroupedCollection` (`sql-orm-client/grouped-collection.ts`),\n * but purely in-memory: there is no SQL compilation, so `aggregate()`\n * materialises the matching rows, partitions them by the group-key fields, and\n * reduces each partition with the requested selectors.\n */\nexport interface IdbGroupedAccessor<TContract, ModelName extends string, Fields extends readonly string[]> {\n /**\n * Reduce each group to one row of `{ ...groupKeyFields, ...aggregates }`.\n *\n * @example\n * ```ts\n * const byUser = await db.posts\n * .where({ published: true })\n * .groupBy(\"authorId\")\n * .aggregate((agg) => ({ count: agg.count(), totalViews: agg.sum(\"views\") }));\n * // [{ authorId: \"u1\", count: 3, totalViews: 120 }, ...]\n * ```\n */\n aggregate<Spec extends IdbAggregateSpec>(\n fn: (agg: IdbAggregateBuilder<TContract, ModelName>) => Spec\n ): Promise<Array<GroupedResultRow<TContract, ModelName, Fields, Spec>>>;\n}\n\n/** Runtime wiring handed to {@link createGroupedAccessor} by the store accessor. */\nexport interface GroupedAccessorInit {\n readonly modelName: string;\n /** Group-key fields, in declaration order. */\n readonly by: readonly string[];\n /** Combined `where` expression carried over from the source accessor, if any. */\n readonly where: IdbFilterExpr | undefined;\n /**\n * Materialise all rows matching the source filters. The store accessor owns\n * plan-building; the grouped accessor only supplies the AST it wants attached\n * (for middleware visibility) and does the in-memory grouping.\n */\n readonly materialize: (ast: IdbQueryAst) => Promise<Record<string, unknown>[]>;\n}\n\n/**\n * Build the composite group key for a row. Uses a JSON encoding of the ordered\n * key-field values so multi-field groups and primitive value types (string,\n * number, boolean, null) partition correctly.\n */\nfunction groupKeyOf(by: readonly string[], row: Record<string, unknown>): string {\n return JSON.stringify(by.map((field) => row[field] ?? null));\n}\n\nexport function createGroupedAccessor<TContract, ModelName extends string, Fields extends readonly string[]>(\n init: GroupedAccessorInit\n): IdbGroupedAccessor<TContract, ModelName, Fields> {\n return {\n async aggregate<Spec extends IdbAggregateSpec>(\n fn: (agg: IdbAggregateBuilder<TContract, ModelName>) => Spec\n ): Promise<Array<GroupedResultRow<TContract, ModelName, Fields, Spec>>> {\n const spec = fn(createAggregateBuilder<TContract, ModelName>());\n assertValidAggregateSpec(spec, \"groupBy().aggregate()\");\n\n const ast: IdbGroupByAst = {\n kind: \"groupBy\",\n modelName: init.modelName,\n by: init.by,\n aggregates: toAggregateRequests(spec),\n ...(init.where !== undefined ? { where: init.where } : {}),\n };\n\n const rows = await init.materialize(ast);\n\n // Partition rows by their composite group key, preserving first-seen order.\n const groups = new Map<string, { key: Record<string, unknown>; rows: Record<string, unknown>[] }>();\n for (const row of rows) {\n const gk = groupKeyOf(init.by, row);\n let group = groups.get(gk);\n if (group === undefined) {\n const key: Record<string, unknown> = {};\n for (const field of init.by) key[field] = row[field];\n group = { key, rows: [] };\n groups.set(gk, group);\n }\n group.rows.push(row);\n }\n\n return Array.from(groups.values()).map(\n (group) =>\n ({ ...group.key, ...computeAggregateSpec(spec, group.rows) }) as GroupedResultRow<\n TContract,\n ModelName,\n Fields,\n Spec\n >\n );\n },\n };\n}\n","import type { ContractReferenceRelation } from \"@prisma-next/contract/types\";\nimport { domainModelsAtDefaultNamespace } from \"@prisma-next/contract/types\";\nimport type { IdbQueryPlan } from \"@prisma-next-idb/adapter-idb/runtime\";\nimport { evaluateFilter } from \"@prisma-next-idb/adapter-idb/runtime\";\nimport type { IdbRowFilter } from \"@prisma-next-idb/driver-idb/runtime\";\nimport type { IdbQueryExecutor } from \"./executor\";\nimport { buildRowComparator, combineFilterExprs } from \"./query-shaping\";\nimport type { IncludeEntry } from \"./store-state\";\nimport type { IdbContract } from \"./types\";\n\n/**\n * Batch-load a single named relation for all rows in `rows` and attach the\n * result to each row under the `relName` key.\n *\n * The join is done with one cursor scan over the related store (with an\n * in-memory filter), then grouped/indexed in memory — avoiding N+1 queries.\n *\n * The `entry` carries any `include()` refinement:\n *\n * - `collection` — the refined `where` further filters the child scan;\n * `orderBy` / `skip` / `take` are applied **per parent group** for `1:N`\n * relations (each parent's children are independently sorted and paginated).\n * - `scalar` — the relation field becomes the `count` of matching children\n * (to-many only; `include()` rejects scalar refinements on to-one relations).\n *\n * @param relName - The relation key to load (e.g. `\"posts\"`, `\"author\"`).\n * @param entry - How to materialise the relation (collection vs scalar + refinement state).\n * @param rows - The parent rows to attach related data to.\n * @param contract - The resolved IDB contract.\n * @param modelName - The source model name (owner of the relation).\n * @param executor - The query executor used to run the related-store scan.\n */\nexport async function loadRelation(\n relName: string,\n entry: IncludeEntry,\n rows: Record<string, unknown>[],\n contract: IdbContract,\n modelName: string,\n executor: IdbQueryExecutor,\n groupingKey: string\n): Promise<Record<string, unknown>[]> {\n if (rows.length === 0) return rows;\n\n const models = domainModelsAtDefaultNamespace(contract.domain);\n const model = models[modelName];\n if (model === undefined) return rows;\n\n const rawRelation = model.relations[relName];\n if (rawRelation === undefined) return rows;\n\n // Only handle reference relations (cross-store joins). Embed relations don't\n // have an `on` block and are stored inline — nothing to load.\n if (!(\"on\" in rawRelation)) return rows;\n\n const relation = rawRelation as ContractReferenceRelation;\n const { cardinality, on } = relation;\n // `relation.to` is a CrossReference `{ namespace, model }`.\n const relatedModelName = relation.to.model;\n\n const localField = on.localFields[0];\n const foreignField = on.targetFields[0];\n if (localField === undefined || foreignField === undefined) return rows;\n\n const relatedModel = models[relatedModelName];\n if (relatedModel === undefined) return rows;\n\n // Resolve the related object store name from the model's storage metadata.\n const relatedStoreName =\n typeof relatedModel.storage === \"object\" && relatedModel.storage !== null && \"storeName\" in relatedModel.storage\n ? String((relatedModel.storage as { storeName: unknown })[\"storeName\"])\n : relatedModelName;\n\n const isScalar = entry.kind === \"scalar\";\n\n // Collect all distinct local-field values to drive the in-memory filter.\n const localValues = new Set<unknown>();\n for (const row of rows) {\n const v = row[localField];\n if (v !== undefined && v !== null) localValues.add(v);\n }\n\n // Short-circuit: if all local values are null/undefined, attach empties.\n // (Scalar counts are 0; to-many is [], to-one is null.)\n if (localValues.size === 0) {\n return rows.map((row) => ({\n ...row,\n [relName]: isScalar ? 0 : cardinality === \"1:N\" ? [] : null,\n }));\n }\n\n // One scan: load related rows whose foreignField is in localValues AND that\n // satisfy any refined `where` carried on the include entry.\n const capturedForeignField = foreignField;\n const refinedWhere = combineFilterExprs(entry.state.filters);\n const filter: IdbRowFilter = (row: Record<string, unknown>): boolean =>\n localValues.has(row[capturedForeignField]) && (refinedWhere === undefined || evaluateFilter(refinedWhere, row));\n\n const storageHash = contract.storage.storageHash;\n const planMeta = { target: \"idb\", storageHash, lane: \"idb-orm\", annotations: { groupingKey } } as const;\n const plan: IdbQueryPlan<Record<string, unknown>> = {\n meta: planMeta,\n idbPlan: { meta: planMeta, kind: \"cursor-scan\", storeName: relatedStoreName, filter },\n };\n\n const relatedRows: Record<string, unknown>[] = [];\n for await (const row of executor.execute(plan)) {\n relatedRows.push(row);\n }\n\n // ── Merge ──────────────────────────────────────────────────────────────────\n\n if (cardinality === \"1:N\") {\n // Group related rows by their foreignField value.\n const grouped = new Map<unknown, Record<string, unknown>[]>();\n for (const rrow of relatedRows) {\n const gk = rrow[capturedForeignField];\n const group = grouped.get(gk) ?? [];\n group.push(rrow);\n grouped.set(gk, group);\n }\n\n if (isScalar) {\n // Scalar reducer (Phase 6.5: count) — attach the per-parent child count.\n return rows.map((row) => ({\n ...row,\n [relName]: (grouped.get(row[localField]) ?? []).length,\n }));\n }\n\n // Collection: apply refined orderBy / skip / take per parent group.\n const comparator = buildRowComparator(entry.state.orderBy);\n const skip = entry.state.skip ?? 0;\n const take = entry.state.take;\n return rows.map((row) => {\n let group = grouped.get(row[localField]) ?? [];\n if (comparator !== undefined) group = [...group].sort(comparator);\n if (skip > 0 || take !== undefined) {\n group = group.slice(skip, take !== undefined ? skip + take : undefined);\n }\n return { ...row, [relName]: group };\n });\n }\n\n // N:1 / 1:1: index related rows by their foreignField value, attach singles.\n // A refined `where` that excludes the related row yields `null` here.\n const indexed = new Map<unknown, Record<string, unknown>>();\n for (const rrow of relatedRows) {\n indexed.set(rrow[capturedForeignField], rrow);\n }\n return rows.map((row) => ({\n ...row,\n [relName]: indexed.get(row[localField]) ?? null,\n }));\n}\n","/**\n * Multi-store mutation scope for the IDB ORM.\n *\n * `withMutationScope` opens one IDB transaction spanning the named stores,\n * runs the callback with an `IdbTransactionScope` (raw driver-level execute),\n * then commits or rolls back depending on whether the callback throws.\n *\n * Pattern is a direct port of `withMutationScope` from\n * `sql-orm-client/mutation-executor.ts`, adapted for IDB's explicit\n * transaction scope API.\n *\n * @example\n * ```ts\n * await withMutationScope(runtime, [\"users\", \"posts\"], async (scope) => {\n * const userRow = await scope.execute({ kind: \"put\", storeName: \"users\", ... });\n * await scope.execute({ kind: \"put\", storeName: \"posts\", ... });\n * });\n * ```\n */\nimport type { IdbQueryExecutor } from \"./executor\";\nimport type { IdbTransactionScope } from \"@prisma-next-idb/driver-idb/runtime\";\n\n// ── Interface ─────────────────────────────────────────────────────────────────\n\n/**\n * Extends the basic {@link IdbQueryExecutor} with multi-store transaction\n * support. `IdbRuntime` satisfies this interface — `runtime.transaction()`\n * delegates to `driver.transaction()` which opens the IDB transaction.\n */\nexport interface IdbQueryExecutorWithTransaction extends IdbQueryExecutor {\n /**\n * Open a multi-store IDB transaction and return a scope object.\n *\n * The returned scope's `execute()` runs `IdbAtomicPlan`s directly inside\n * the transaction — no new transaction is opened per call, and the\n * middleware chain is bypassed. `commit()` resolves when `tx.oncomplete`\n * fires; `rollback()` calls `tx.abort()`.\n */\n transaction(storeNames: string[], mode?: IDBTransactionMode): Promise<IdbTransactionScope>;\n}\n\n// ── withMutationScope ─────────────────────────────────────────────────────────\n\n/**\n * Run a callback inside a single multi-store IDB readwrite transaction.\n *\n * Opens the transaction, passes the `IdbTransactionScope` to `run`, then:\n * - On success: awaits `scope.commit()` (waits for `tx.oncomplete`).\n * - On error: calls `scope.rollback()` and rethrows.\n *\n * The callback receives the low-level scope, not the ORM accessor.\n * Use this for multi-store atomic writes that span several object stores.\n */\nexport async function withMutationScope<T>(\n executor: IdbQueryExecutorWithTransaction,\n storeNames: string[],\n run: (scope: IdbTransactionScope) => Promise<T>\n): Promise<T> {\n const tx = await executor.transaction(storeNames, \"readwrite\");\n try {\n const result = await run(tx);\n await tx.commit();\n return result;\n } catch (err) {\n tx.rollback();\n throw err;\n }\n}\n","/**\n * IDB relation mutator factory and type guards.\n *\n * Port of `sql-orm-client/relation-mutator.ts` — same pattern, IDB types only.\n * The mutator object is passed to user-supplied relation callbacks so they can\n * express nested writes in a type-safe, intent-revealing way.\n *\n * @example\n * ```ts\n * orm.users.create({\n * id: \"u1\",\n * name: \"Alice\",\n * posts: (rel) => rel.create([{ id: \"p1\", title: \"Hello\" }]),\n * });\n * ```\n */\nimport type {\n CreateInput,\n IdbContract,\n IdbRelationMutation,\n IdbRelationMutator,\n RelationMutationConnect,\n RelationMutationCreate,\n RelationMutationDisconnect,\n} from \"./types\";\n\nexport function createRelationMutator<TContract extends IdbContract, ModelName extends string>(): IdbRelationMutator<\n TContract,\n ModelName\n> {\n return {\n create(\n data: CreateInput<TContract, ModelName> | readonly CreateInput<TContract, ModelName>[]\n ): RelationMutationCreate<TContract, ModelName> {\n const rows = Array.isArray(data) ? [...data] : [data];\n return { kind: \"create\", data: rows as readonly CreateInput<TContract, ModelName>[] };\n },\n\n connect(criteria: Record<string, unknown> | readonly Record<string, unknown>[]): RelationMutationConnect {\n const values = Array.isArray(criteria) ? [...criteria] : [criteria];\n return { kind: \"connect\", criteria: values };\n },\n\n disconnect(criteria?: readonly Record<string, unknown>[]): RelationMutationDisconnect {\n if (!criteria) return { kind: \"disconnect\" };\n return { kind: \"disconnect\", criteria: [...criteria] };\n },\n };\n}\n\nexport function isRelationMutationDescriptor(value: unknown): value is IdbRelationMutation<IdbContract, string> {\n if (!value || typeof value !== \"object\") return false;\n const candidate = value as { kind?: unknown };\n return candidate.kind === \"create\" || candidate.kind === \"connect\" || candidate.kind === \"disconnect\";\n}\n\nexport function isRelationMutationCallback(\n value: unknown\n): value is (mutator: IdbRelationMutator<IdbContract, string>) => IdbRelationMutation<IdbContract, string> {\n return typeof value === \"function\";\n}\n","/**\n * IDB nested-write executor.\n *\n * IDB adaptation of `sql-orm-client/mutation-executor.ts`. Key differences\n * from the SQL vendor:\n *\n * - No column/field mapping — IDB field names ARE the storage keys.\n * - No `applyMutationDefaults` — IDB has no server-side defaults.\n * - `insertSingleRow` → `scope.execute({ kind: \"put\", ... })`.\n * - `findRowByCriterion` / `findFirstByFilters` → `scope.execute({ kind: \"cursor-scan\", ... })`.\n * IDB allows reads inside a readwrite transaction; the transaction scope accepts\n * all `IdbAtomicPlan` types including `cursor-scan`.\n * - Child-owned `connect` → `scope.execute({ kind: \"scan-write\", write: \"put-merged\", ... })`.\n * IDB has no UPDATE SET WHERE, so we use the scan-write plan with a filter closure.\n * - `connect()` for parent-owned (N:1) relations throws if the referenced row is not\n * found — this is Phase 6.4's implicit FK validation for the connect case.\n * - Recursive nesting (nested writes inside nested writes) is not supported and throws.\n *\n * All multi-store writes are wrapped in a single `withMutationScope` call that opens\n * one IDB transaction spanning all required stores, per ADR 007.\n */\n\nimport type { PlanMeta } from \"@prisma-next/contract/types\";\nimport type { ContractReferenceRelation } from \"@prisma-next/contract/types\";\nimport { domainModelsAtDefaultNamespace } from \"@prisma-next/contract/types\";\nimport type { IdbAtomicPlan, IdbCursorScanPlan } from \"@prisma-next-idb/driver-idb/runtime\";\nimport { evaluateFilter, shorthandToFilterExpr } from \"@prisma-next-idb/adapter-idb/runtime\";\nimport type { IdbFilterExpr } from \"@prisma-next-idb/adapter-idb/runtime\";\nimport type { IdbReferentialAction } from \"@prisma-next-idb/target-idb/pack\";\nimport type { IdbQueryExecutor } from \"./executor\";\nimport { withMutationScope, type IdbQueryExecutorWithTransaction } from \"./mutation-scope\";\nimport { createRelationMutator, isRelationMutationCallback, isRelationMutationDescriptor } from \"./relation-mutator\";\nimport {\n type IdbContract,\n type IdbRelationMutation,\n type IdbRelationMutator,\n type MutationCreateInput,\n type MutationUpdateInput,\n getStoreName,\n} from \"./types\";\nimport type { IdbTransactionScope } from \"@prisma-next-idb/driver-idb/runtime\";\n\n// ── Internal types ─────────────────────────────────────────────────────────────\n\ninterface RelationDefinition {\n readonly relationName: string;\n readonly relatedModelName: string;\n readonly relatedStoreName: string;\n readonly cardinality: string | undefined;\n readonly localFields: readonly string[];\n readonly targetFields: readonly string[];\n}\n\ninterface ParsedRelationMutation {\n readonly relation: RelationDefinition;\n readonly mutation: IdbRelationMutation<IdbContract, string>;\n}\n\ninterface ParsedMutationInput {\n readonly scalarData: Record<string, unknown>;\n readonly relationMutations: readonly ParsedRelationMutation[];\n}\n\n// ── Plan meta helpers ─────────────────────────────────────────────────────────\n\nfunction makePlanMeta(contract: IdbContract): PlanMeta {\n return {\n target: \"idb\",\n storageHash: contract.storage.storageHash,\n lane: \"idb-mutation-executor\",\n annotations: { groupingKey: \"nested\" },\n };\n}\n\n// ── Relation definition resolution (cached) ──────────────────────────────────\n\nconst relationDefsCache = new WeakMap<object, Map<string, RelationDefinition[]>>();\n\nfunction getRelationDefinitions(contract: IdbContract, modelName: string): RelationDefinition[] {\n let perContract = relationDefsCache.get(contract);\n if (!perContract) {\n perContract = new Map();\n relationDefsCache.set(contract, perContract);\n }\n\n const cached = perContract.get(modelName);\n if (cached) return cached;\n\n const model = domainModelsAtDefaultNamespace(contract.domain)[modelName];\n if (!model) {\n perContract.set(modelName, []);\n return [];\n }\n\n const defs: RelationDefinition[] = [];\n for (const [relationName, rawRelation] of Object.entries(model.relations)) {\n if (!rawRelation || typeof rawRelation !== \"object\" || !(\"on\" in rawRelation)) continue;\n\n const relation = rawRelation as ContractReferenceRelation;\n // `relation.to` is a CrossReference `{ namespace, model }`.\n const relatedModelName = relation.to.model;\n const relatedStoreName = getStoreName(contract, relatedModelName);\n defs.push({\n relationName,\n relatedModelName,\n relatedStoreName,\n cardinality: relation.cardinality,\n localFields: relation.on.localFields,\n targetFields: relation.on.targetFields,\n });\n }\n\n perContract.set(modelName, defs);\n return defs;\n}\n\n// ── Public helpers ────────────────────────────────────────────────────────────\n\n/**\n * Returns true if `data` contains at least one field that is both a known\n * relation name for `modelName` and a function (a mutation callback).\n */\nexport function hasNestedMutationCallbacks(\n contract: IdbContract,\n modelName: string,\n data: Record<string, unknown>\n): boolean {\n const relationNames = new Set(getRelationDefinitions(contract, modelName).map((r) => r.relationName));\n for (const [fieldName, value] of Object.entries(data)) {\n if (relationNames.has(fieldName) && isRelationMutationCallback(value)) return true;\n }\n return false;\n}\n\n/**\n * Guards that the executor supports multi-store transactions.\n * Throws a clear error if `transaction()` is not available — the user must\n * use IdbRuntime (createIdbRuntime / createAutoMigratingIdbClient) rather than\n * a plain IdbQueryExecutor stub.\n */\nexport function requireTransactionExecutor(executor: IdbQueryExecutor): IdbQueryExecutorWithTransaction {\n if (typeof (executor as IdbQueryExecutorWithTransaction).transaction !== \"function\") {\n throw new Error(\n \"Nested relation writes require an executor with transaction support. \" +\n \"Use IdbRuntime (createIdbRuntime or createAutoMigratingIdbClient) instead of a plain IdbQueryExecutor.\"\n );\n }\n return executor as IdbQueryExecutorWithTransaction;\n}\n\n// ── Entry points ──────────────────────────────────────────────────────────────\n\nexport async function executeNestedCreateMutation(options: {\n executor: IdbQueryExecutorWithTransaction;\n contract: IdbContract;\n modelName: string;\n data: MutationCreateInput<IdbContract, string>;\n}): Promise<Record<string, unknown>> {\n const { executor, contract, modelName, data } = options;\n const record = data as Record<string, unknown>;\n const storeNames = collectStoreNames(contract, modelName, record);\n return withMutationScope(executor, storeNames, (scope) => createGraph(scope, contract, modelName, record));\n}\n\nexport async function executeNestedUpdateMutation(options: {\n executor: IdbQueryExecutorWithTransaction;\n contract: IdbContract;\n modelName: string;\n filters: readonly IdbFilterExpr[];\n data: MutationUpdateInput<IdbContract, string>;\n}): Promise<Record<string, unknown> | null> {\n const { executor, contract, modelName, filters, data } = options;\n const record = data as Record<string, unknown>;\n const storeNames = collectStoreNames(contract, modelName, record);\n return withMutationScope(executor, storeNames, (scope) =>\n updateFirstGraph(scope, contract, modelName, filters, record)\n );\n}\n\n// ── Store name collection ─────────────────────────────────────────────────────\n\nfunction collectStoreNames(contract: IdbContract, modelName: string, data: Record<string, unknown>): string[] {\n const stores = new Set([getStoreName(contract, modelName)]);\n for (const def of getRelationDefinitions(contract, modelName)) {\n if (def.relationName in data && isRelationMutationCallback(data[def.relationName])) {\n stores.add(def.relatedStoreName);\n }\n }\n return [...stores];\n}\n\n// ── Graph operations ──────────────────────────────────────────────────────────\n\nasync function createGraph(\n scope: IdbTransactionScope,\n contract: IdbContract,\n modelName: string,\n input: Record<string, unknown>\n): Promise<Record<string, unknown>> {\n const parsed = parseMutationInput(contract, modelName, input);\n const { parentOwned, childOwned } = partitionByOwnership(parsed.relationMutations);\n\n const scalarData = { ...parsed.scalarData };\n\n for (const item of parentOwned) {\n if (item.mutation.kind === \"disconnect\") {\n throw new Error(\"disconnect() is only supported in update() nested mutations\");\n }\n await applyParentOwnedMutation(scope, contract, modelName, scalarData, item.relation, item.mutation);\n }\n\n const parentRow = await insertSingleRow(scope, contract, modelName, scalarData);\n\n for (const item of childOwned) {\n if (item.mutation.kind === \"disconnect\") {\n throw new Error(\"disconnect() is only supported in update() nested mutations\");\n }\n await applyChildOwnedMutation(scope, contract, modelName, parentRow, item.relation, item.mutation);\n }\n\n return parentRow;\n}\n\nasync function updateFirstGraph(\n scope: IdbTransactionScope,\n contract: IdbContract,\n modelName: string,\n filters: readonly IdbFilterExpr[],\n input: Record<string, unknown>\n): Promise<Record<string, unknown> | null> {\n const existingRow = await findFirstByFilters(scope, contract, modelName, filters);\n if (!existingRow) return null;\n\n const parsed = parseMutationInput(contract, modelName, input);\n const { parentOwned, childOwned } = partitionByOwnership(parsed.relationMutations);\n\n const scalarData = { ...parsed.scalarData };\n\n for (const item of parentOwned) {\n await applyParentOwnedMutation(scope, contract, modelName, scalarData, item.relation, item.mutation);\n }\n\n let parentRow = existingRow;\n\n if (Object.keys(scalarData).length > 0) {\n const storeName = getStoreName(contract, modelName);\n const keyPath = getKeyPath(contract, modelName);\n const key = existingRow[keyPath] as IDBValidKey;\n const meta = makePlanMeta(contract);\n const rows = await scope.execute({ meta, kind: \"update\", storeName, key, patch: scalarData });\n const updated = rows[0];\n if (updated) parentRow = updated;\n }\n\n for (const item of childOwned) {\n await applyChildOwnedMutation(scope, contract, modelName, parentRow, item.relation, item.mutation);\n }\n\n return parentRow;\n}\n\n// ── Input parsing ─────────────────────────────────────────────────────────────\n\nfunction parseMutationInput(\n contract: IdbContract,\n modelName: string,\n input: Record<string, unknown>\n): ParsedMutationInput {\n const scalarData: Record<string, unknown> = {};\n const relationDefs = new Map(getRelationDefinitions(contract, modelName).map((r) => [r.relationName, r]));\n const relationMutations: ParsedRelationMutation[] = [];\n\n for (const [fieldName, value] of Object.entries(input)) {\n const relation = relationDefs.get(fieldName);\n if (!relation) {\n scalarData[fieldName] = value;\n continue;\n }\n\n if (!isRelationMutationCallback(value)) {\n throw new Error(`Relation field \"${fieldName}\" on model \"${modelName}\" expects a mutator callback`);\n }\n\n const mutator = createRelationMutator<IdbContract, string>();\n const mutation = value(mutator as IdbRelationMutator<IdbContract, string>);\n if (!isRelationMutationDescriptor(mutation)) {\n throw new Error(`Relation field \"${fieldName}\" on model \"${modelName}\" returned an invalid mutation descriptor`);\n }\n\n relationMutations.push({ relation, mutation });\n }\n\n return { scalarData, relationMutations };\n}\n\n// ── Ownership partitioning ────────────────────────────────────────────────────\n\nfunction partitionByOwnership(mutations: readonly ParsedRelationMutation[]): {\n parentOwned: ParsedRelationMutation[];\n childOwned: ParsedRelationMutation[];\n} {\n const parentOwned: ParsedRelationMutation[] = [];\n const childOwned: ParsedRelationMutation[] = [];\n\n for (const item of mutations) {\n if (item.relation.cardinality === \"N:1\") {\n parentOwned.push(item);\n continue;\n }\n if (item.relation.cardinality === \"M:N\") {\n throw new Error(\"M:N nested mutations are not supported\");\n }\n childOwned.push(item);\n }\n\n return { parentOwned, childOwned };\n}\n\n// ── Parent-owned (N:1) mutations ──────────────────────────────────────────────\n\nasync function applyParentOwnedMutation(\n scope: IdbTransactionScope,\n contract: IdbContract,\n parentModelName: string,\n scalarData: Record<string, unknown>,\n relation: RelationDefinition,\n mutation: IdbRelationMutation<IdbContract, string>\n): Promise<void> {\n if (mutation.kind === \"disconnect\") {\n for (const localField of relation.localFields) {\n scalarData[localField] = null;\n }\n return;\n }\n\n if (mutation.kind === \"create\") {\n const row = mutation.data[0] as Record<string, unknown> | undefined;\n if (!row) {\n throw new Error(`create() nested mutation for relation \"${relation.relationName}\" requires data`);\n }\n // Recursive nesting is not supported in Phase 6.4 — the nested record must\n // be a plain scalar create, not itself a nested mutation.\n const relatedRow = await insertSingleRow(scope, contract, relation.relatedModelName, row);\n copyRelatedValuesToParent(relation, scalarData, relatedRow, parentModelName, contract);\n return;\n }\n\n // connect()\n const criterion = mutation.criteria[0] as Record<string, unknown> | undefined;\n if (!criterion) {\n throw new Error(`connect() nested mutation for relation \"${relation.relationName}\" requires a criterion`);\n }\n const relatedRow = await findRowByCriterion(scope, contract, relation.relatedModelName, criterion);\n if (!relatedRow) {\n throw new Error(`connect() nested mutation for relation \"${relation.relationName}\" did not find a matching row`);\n }\n copyRelatedValuesToParent(relation, scalarData, relatedRow, parentModelName, contract);\n}\n\nfunction copyRelatedValuesToParent(\n relation: RelationDefinition,\n scalarData: Record<string, unknown>,\n relatedRow: Record<string, unknown>,\n _parentModelName: string,\n _contract: IdbContract\n): void {\n // localFields = parent's FK fields; targetFields = related model's PK/unique fields\n for (let i = 0; i < relation.localFields.length; i++) {\n const localField = relation.localFields[i];\n const targetField = relation.targetFields[i];\n if (!localField || !targetField) continue;\n scalarData[localField] = relatedRow[targetField];\n }\n}\n\n// ── Child-owned (1:N / 1:1) mutations ────────────────────────────────────────\n\nasync function applyChildOwnedMutation(\n scope: IdbTransactionScope,\n contract: IdbContract,\n parentModelName: string,\n parentRow: Record<string, unknown>,\n relation: RelationDefinition,\n mutation: IdbRelationMutation<IdbContract, string>\n): Promise<void> {\n // parentValues: childFkField → parentPkValue (e.g. \"authorId\" → \"u1\")\n const parentValues = readParentColumnValues(parentModelName, relation, parentRow);\n\n if (mutation.kind === \"create\") {\n for (const childInput of mutation.data) {\n const payload: Record<string, unknown> = { ...(childInput as Record<string, unknown>) };\n for (const [childField, parentValue] of parentValues.entries()) {\n payload[childField] = parentValue;\n }\n await insertSingleRow(scope, contract, relation.relatedModelName, payload);\n }\n return;\n }\n\n if (mutation.kind === \"connect\") {\n for (const criterion of mutation.criteria) {\n const setValues: Record<string, unknown> = {};\n for (const [childField, parentValue] of parentValues.entries()) {\n setValues[childField] = parentValue;\n }\n const filter = buildCriterionFilter(criterion as Record<string, unknown>);\n const meta = makePlanMeta(contract);\n // scan-write + put-merged: set the FK fields on every child row matching\n // the criterion. No `take` cap — the vendor's relational connect\n // (`executeUpdateCount`) connects all matching rows; for the normal\n // unique-key criterion that is exactly one row anyway. (PLAN Issue #24.)\n await scope.execute({\n meta,\n kind: \"scan-write\",\n storeName: relation.relatedStoreName,\n write: \"put-merged\",\n patch: setValues,\n filter,\n });\n }\n return;\n }\n\n // disconnect()\n const setValues: Record<string, unknown> = {};\n for (const childField of parentValues.keys()) {\n setValues[childField] = null;\n }\n const meta = makePlanMeta(contract);\n\n if (!mutation.criteria || mutation.criteria.length === 0) {\n // Disconnect all children of this parent.\n const parentJoinFilter = buildParentJoinFilter(parentValues);\n await scope.execute({\n meta,\n kind: \"scan-write\",\n storeName: relation.relatedStoreName,\n write: \"put-merged\",\n patch: setValues,\n filter: parentJoinFilter,\n });\n return;\n }\n\n // Disconnect specific children matching each criterion AND the parent join.\n for (const criterion of mutation.criteria) {\n const criterionFilter = buildCriterionFilter(criterion as Record<string, unknown>);\n const parentJoinFilter = buildParentJoinFilter(parentValues);\n const combinedFilter = (row: Record<string, unknown>): boolean => parentJoinFilter(row) && criterionFilter(row);\n await scope.execute({\n meta,\n kind: \"scan-write\",\n storeName: relation.relatedStoreName,\n write: \"put-merged\",\n patch: setValues,\n filter: combinedFilter,\n });\n }\n}\n\nfunction readParentColumnValues(\n parentModelName: string,\n relation: RelationDefinition,\n parentRow: Record<string, unknown>\n): Map<string, unknown> {\n const values = new Map<string, unknown>();\n // For 1:N: localFields = parent PK fields; targetFields = child FK fields\n for (let i = 0; i < relation.localFields.length; i++) {\n const localField = relation.localFields[i];\n const targetField = relation.targetFields[i];\n if (!localField || !targetField) continue;\n const parentValue = parentRow[localField];\n if (parentValue === undefined) {\n throw new Error(\n `Nested mutation requires parent field \"${localField}\" to be present in \"${parentModelName}\" row`\n );\n }\n // targetField is the child's FK column name; map it to the parent's value.\n values.set(targetField, parentValue);\n }\n return values;\n}\n\n// ── Row operations ────────────────────────────────────────────────────────────\n\nasync function insertSingleRow(\n scope: IdbTransactionScope,\n contract: IdbContract,\n modelName: string,\n data: Record<string, unknown>\n): Promise<Record<string, unknown>> {\n assertNoNestedCallbacks(modelName, data);\n const storeName = getStoreName(contract, modelName);\n const meta = makePlanMeta(contract);\n const rows = await scope.execute({ meta, kind: \"put\", storeName, record: data });\n return rows[0] ?? data;\n}\n\n/**\n * Recursive nesting (a relation callback inside an already-nested create) is\n * not supported in Phase 6.4. Without this guard the callback function would be\n * handed to `store.put(...)`, where IDB's structured-clone throws an opaque\n * `DataCloneError` (\"could not be cloned\") that gives the developer no hint\n * about the real cause. Surface a precise error instead. (PLAN Issue #22.)\n */\nfunction assertNoNestedCallbacks(modelName: string, data: Record<string, unknown>): void {\n for (const [field, value] of Object.entries(data)) {\n if (typeof value === \"function\") {\n throw new Error(\n `Recursive nested writes are not supported: field \"${field}\" on a nested \"${modelName}\" ` +\n \"record is a relation callback. Only one level of relation nesting is supported — \" +\n \"flatten the inner relation into a separate create/connect call.\"\n );\n }\n }\n}\n\nasync function findRowByCriterion(\n scope: IdbTransactionScope,\n contract: IdbContract,\n modelName: string,\n criterion: Record<string, unknown>\n): Promise<Record<string, unknown> | null> {\n const expr = shorthandToFilterExpr(criterion);\n if (!expr) {\n throw new Error(`Nested connect for model \"${modelName}\" requires a non-empty criterion`);\n }\n const filter = (row: Record<string, unknown>): boolean => evaluateFilter(expr, row);\n return scanOneRow(scope, contract, modelName, filter);\n}\n\nasync function findFirstByFilters(\n scope: IdbTransactionScope,\n contract: IdbContract,\n modelName: string,\n filters: readonly IdbFilterExpr[]\n): Promise<Record<string, unknown> | null> {\n if (filters.length === 0) return null;\n const combined = filters.length === 1 ? filters[0]! : { kind: \"and\" as const, exprs: filters };\n const filter = (row: Record<string, unknown>): boolean => evaluateFilter(combined, row);\n return scanOneRow(scope, contract, modelName, filter);\n}\n\nasync function scanOneRow(\n scope: IdbTransactionScope,\n contract: IdbContract,\n modelName: string,\n filter: (row: Record<string, unknown>) => boolean\n): Promise<Record<string, unknown> | null> {\n const storeName = getStoreName(contract, modelName);\n const meta = makePlanMeta(contract);\n const plan: IdbCursorScanPlan = { meta, kind: \"cursor-scan\", storeName, filter, take: 1 };\n const rows = await scope.execute(plan as IdbAtomicPlan);\n return rows[0] ?? null;\n}\n\n// ── Filter helpers ────────────────────────────────────────────────────────────\n\nfunction buildCriterionFilter(criterion: Record<string, unknown>): (row: Record<string, unknown>) => boolean {\n const expr = shorthandToFilterExpr(criterion);\n if (!expr) return () => true;\n return (row) => evaluateFilter(expr, row);\n}\n\nfunction buildParentJoinFilter(parentValues: Map<string, unknown>): (row: Record<string, unknown>) => boolean {\n const pairs = [...parentValues.entries()];\n return (row: Record<string, unknown>): boolean =>\n pairs.every(([childField, parentValue]) => row[childField] === parentValue);\n}\n\n// ── Key path helper ───────────────────────────────────────────────────────────\n\nfunction getKeyPath(contract: IdbContract, modelName: string): string {\n const model = domainModelsAtDefaultNamespace(contract.domain)[modelName];\n return (model?.storage as { keyPath?: string } | undefined)?.keyPath ?? \"id\";\n}\n\n// ── Referential action helpers ────────────────────────────────────────────────\n\nfunction getOnDelete(contract: IdbContract, modelName: string, relationName: string): IdbReferentialAction {\n const model = domainModelsAtDefaultNamespace(contract.domain)[modelName];\n const storage = model?.storage as { relations?: Record<string, { onDelete?: string }> } | undefined;\n return (storage?.relations?.[relationName]?.onDelete ?? \"restrict\") as IdbReferentialAction;\n}\n\nfunction isDeleteEnforcementRelation(contract: IdbContract, modelName: string, def: RelationDefinition): boolean {\n if (def.cardinality === \"1:N\") return true;\n if (def.cardinality === \"1:1\") {\n const keyPath = getKeyPath(contract, modelName);\n return def.localFields.length > 0 && def.localFields[0] === keyPath;\n }\n return false;\n}\n\n// ── Scalar FK validation ──────────────────────────────────────────────────────\n\n/**\n * Returns true if `data` contains at least one non-null value for a localField\n * of a N:1 relation — indicating scalar FK fields that need existence validation.\n */\nexport function hasScalarFkFields(contract: IdbContract, modelName: string, data: Record<string, unknown>): boolean {\n for (const def of getRelationDefinitions(contract, modelName)) {\n if (def.cardinality !== \"N:1\") continue;\n for (const localField of def.localFields) {\n if (localField in data && data[localField] !== null && data[localField] !== undefined) return true;\n }\n }\n return false;\n}\n\nfunction collectScalarFkStoreNames(contract: IdbContract, modelName: string, data: Record<string, unknown>): string[] {\n const stores = new Set([getStoreName(contract, modelName)]);\n for (const def of getRelationDefinitions(contract, modelName)) {\n if (def.cardinality !== \"N:1\") continue;\n const hasFkField = def.localFields.some((f) => f in data && data[f] !== null && data[f] !== undefined);\n if (hasFkField) stores.add(def.relatedStoreName);\n }\n return [...stores];\n}\n\nasync function validateScalarFks(\n scope: IdbTransactionScope,\n contract: IdbContract,\n modelName: string,\n data: Record<string, unknown>\n): Promise<void> {\n const meta = makePlanMeta(contract);\n for (const def of getRelationDefinitions(contract, modelName)) {\n if (def.cardinality !== \"N:1\") continue;\n for (let i = 0; i < def.localFields.length; i++) {\n const localField = def.localFields[i]!;\n const targetField = def.targetFields[i]!;\n const value = data[localField];\n if (!(localField in data) || value === null || value === undefined) continue;\n const filter = (row: Record<string, unknown>): boolean => row[targetField] === value;\n const plan: IdbCursorScanPlan = {\n meta,\n kind: \"cursor-scan\",\n storeName: def.relatedStoreName,\n filter,\n take: 1,\n };\n const rows = await scope.execute(plan as IdbAtomicPlan);\n if (rows.length === 0) {\n throw new Error(\n `FK violation on relation '${def.relationName}': no ${def.relatedModelName} with ${targetField}='${String(value)}'`\n );\n }\n }\n }\n}\n\nexport async function executeScalarCreateWithFkValidation(options: {\n executor: IdbQueryExecutorWithTransaction;\n contract: IdbContract;\n modelName: string;\n data: Record<string, unknown>;\n}): Promise<Record<string, unknown>> {\n const { executor, contract, modelName, data } = options;\n const storeNames = collectScalarFkStoreNames(contract, modelName, data);\n return withMutationScope(executor, storeNames, async (scope) => {\n await validateScalarFks(scope, contract, modelName, data);\n return insertSingleRow(scope, contract, modelName, data);\n });\n}\n\nexport async function executeScalarUpdateWithFkValidation(options: {\n executor: IdbQueryExecutorWithTransaction;\n contract: IdbContract;\n modelName: string;\n filters: readonly IdbFilterExpr[];\n data: Record<string, unknown>;\n}): Promise<Record<string, unknown> | null> {\n const { executor, contract, modelName, filters, data } = options;\n const storeNames = collectScalarFkStoreNames(contract, modelName, data);\n return withMutationScope(executor, storeNames, async (scope) => {\n await validateScalarFks(scope, contract, modelName, data);\n const storeName = getStoreName(contract, modelName);\n const meta = makePlanMeta(contract);\n const combined =\n filters.length === 0 ? undefined : filters.length === 1 ? filters[0]! : { kind: \"and\" as const, exprs: filters };\n const filter =\n combined !== undefined ? (row: Record<string, unknown>): boolean => evaluateFilter(combined, row) : undefined;\n const rows = await scope.execute({\n meta,\n kind: \"scan-write\",\n storeName,\n write: \"put-merged\",\n patch: data,\n take: 1,\n ...(filter !== undefined ? { filter } : {}),\n } as IdbAtomicPlan);\n return rows[0] ?? null;\n });\n}\n\n// ── Delete referential action enforcement ─────────────────────────────────────\n\n/**\n * Returns true if the model has at least one child relation (1:N or parent-side\n * 1:1) whose `onDelete` action requires enforcement (anything except `noAction`).\n * Since the default is `restrict`, any model with 1:N/1:1 relations that do not\n * explicitly set `noAction` returns true.\n */\nexport function hasEnforceableChildRelations(contract: IdbContract, modelName: string): boolean {\n for (const def of getRelationDefinitions(contract, modelName)) {\n if (!isDeleteEnforcementRelation(contract, modelName, def)) continue;\n if (getOnDelete(contract, modelName, def.relationName) !== \"noAction\") return true;\n }\n return false;\n}\n\nfunction collectDeleteStoreNames(contract: IdbContract, modelName: string): string[] {\n const stores = new Set([getStoreName(contract, modelName)]);\n for (const def of getRelationDefinitions(contract, modelName)) {\n if (!isDeleteEnforcementRelation(contract, modelName, def)) continue;\n if (getOnDelete(contract, modelName, def.relationName) !== \"noAction\") {\n stores.add(def.relatedStoreName);\n }\n }\n return [...stores];\n}\n\nasync function applyReferentialActionsForRow(\n scope: IdbTransactionScope,\n contract: IdbContract,\n modelName: string,\n row: Record<string, unknown>\n): Promise<void> {\n const meta = makePlanMeta(contract);\n for (const def of getRelationDefinitions(contract, modelName)) {\n if (!isDeleteEnforcementRelation(contract, modelName, def)) continue;\n const action = getOnDelete(contract, modelName, def.relationName);\n if (action === \"noAction\") continue;\n\n // Build the filter matching children of this parent row.\n const pairs = def.localFields.map((lf, i) => ({ childField: def.targetFields[i]!, parentValue: row[lf] }));\n const childFilter = (child: Record<string, unknown>): boolean =>\n pairs.every(({ childField, parentValue }) => child[childField] === parentValue);\n\n if (action === \"restrict\") {\n const found = await scope.execute({\n meta,\n kind: \"cursor-scan\",\n storeName: def.relatedStoreName,\n filter: childFilter,\n take: 1,\n } as IdbAtomicPlan);\n if (found.length > 0) {\n const keyPath = getKeyPath(contract, modelName);\n throw new Error(\n `Cannot delete ${modelName} '${String(row[keyPath])}': child records exist on relation '${def.relationName}'. ` +\n \"Use onDelete: 'cascade', 'setNull', or 'noAction'.\"\n );\n }\n continue;\n }\n\n if (action === \"cascade\") {\n await scope.execute({\n meta,\n kind: \"scan-write\",\n storeName: def.relatedStoreName,\n write: \"delete\",\n filter: childFilter,\n } as IdbAtomicPlan);\n continue;\n }\n\n if (action === \"setNull\") {\n const patch: Record<string, unknown> = {};\n for (const targetField of def.targetFields) patch[targetField] = null;\n await scope.execute({\n meta,\n kind: \"scan-write\",\n storeName: def.relatedStoreName,\n write: \"put-merged\",\n patch,\n filter: childFilter,\n } as IdbAtomicPlan);\n continue;\n }\n\n if (action === \"setDefault\") {\n throw new Error(\n `setDefault referential action is not supported on relation '${def.relationName}': ` +\n \"IDB contracts do not track field defaults. Use 'cascade', 'setNull', or 'noAction' instead.\"\n );\n }\n }\n}\n\nexport async function executeDeleteWithReferentialActions(options: {\n executor: IdbQueryExecutorWithTransaction;\n contract: IdbContract;\n modelName: string;\n key: IDBValidKey;\n}): Promise<void> {\n const { executor, contract, modelName, key } = options;\n const storeNames = collectDeleteStoreNames(contract, modelName);\n await withMutationScope(executor, storeNames, async (scope) => {\n const storeName = getStoreName(contract, modelName);\n const meta = makePlanMeta(contract);\n const rows = await scope.execute({ meta, kind: \"key-get\", storeName, key } as IdbAtomicPlan);\n const row = rows[0];\n if (!row) return [];\n await applyReferentialActionsForRow(scope, contract, modelName, row);\n await scope.execute({ meta, kind: \"delete\", storeName, key } as IdbAtomicPlan);\n return [];\n });\n}\n\nexport async function executeDeleteAllWithReferentialActions(options: {\n executor: IdbQueryExecutorWithTransaction;\n contract: IdbContract;\n modelName: string;\n filter?: (row: Record<string, unknown>) => boolean;\n}): Promise<Record<string, unknown>[]> {\n const { executor, contract, modelName, filter } = options;\n const storeNames = collectDeleteStoreNames(contract, modelName);\n return withMutationScope(executor, storeNames, async (scope) => {\n const storeName = getStoreName(contract, modelName);\n const meta = makePlanMeta(contract);\n const keyPath = getKeyPath(contract, modelName);\n const rows = await scope.execute({\n meta,\n kind: \"cursor-scan\",\n storeName,\n ...(filter !== undefined ? { filter } : {}),\n } as IdbAtomicPlan);\n for (const row of rows) {\n await applyReferentialActionsForRow(scope, contract, modelName, row);\n const key = row[keyPath] as IDBValidKey;\n await scope.execute({ meta, kind: \"delete\", storeName, key } as IdbAtomicPlan);\n }\n return rows;\n });\n}\n","import { AsyncIterableResult } from \"@prisma-next/framework-components/runtime\";\nimport type { PlanMeta } from \"@prisma-next/contract/types\";\nimport type { IdbFilterExpr, IdbQueryPlan } from \"@prisma-next-idb/adapter-idb/runtime\";\nimport type {\n IdbAggregateAst,\n IdbCountAst,\n IdbCreateAst,\n IdbCreateAllAst,\n IdbDeleteAst,\n IdbDeleteAllAst,\n IdbFindManyAst,\n IdbFindUniqueAst,\n IdbQueryAst,\n IdbUpdateAst,\n IdbUpdateAllAst,\n IdbUpsertAst,\n} from \"@prisma-next-idb/adapter-idb/runtime\";\nimport { evaluateFilter, shorthandToFilterExpr } from \"@prisma-next-idb/adapter-idb/runtime\";\nimport {\n type CreateInput,\n type DefaultModelRow,\n type IdbAggregateBuilder,\n type IdbAggregateResult,\n type IdbAggregateSpec,\n type IdbContract,\n type IncludeSpec,\n type KeyType,\n type MutationCreateInput,\n type MutationUpdateInput,\n type NoIncludes,\n type OrderBySpec,\n type PatchInput,\n type ReferenceRelKeys,\n type RelatedModelOf,\n type SelectedRow,\n type WhereFilter,\n getKeyPath,\n getRelation,\n getStoreName,\n} from \"./types\";\nimport { createModelAccessor, type IdbModelAccessor } from \"./model-accessor\";\nimport {\n type IdbAccessorState,\n type IdbIncludeScalar,\n type IncludeEntry,\n createIncludeScalar,\n emptyAccessorState,\n isIncludeScalar,\n mergeAccessorState,\n} from \"./store-state\";\nimport { buildRowComparator, combineFilterExprs } from \"./query-shaping\";\nimport {\n assertValidAggregateSpec,\n computeAggregateSpec,\n createAggregateBuilder,\n toAggregateRequests,\n} from \"./aggregate-builder\";\nimport { type IdbGroupedAccessor, createGroupedAccessor } from \"./grouped-accessor\";\nimport type { IdbQueryExecutor } from \"./executor\";\nimport { loadRelation } from \"./relation-loader\";\nimport {\n executeDeleteAllWithReferentialActions,\n executeDeleteWithReferentialActions,\n executeNestedCreateMutation,\n executeNestedUpdateMutation,\n executeScalarCreateWithFkValidation,\n executeScalarUpdateWithFkValidation,\n hasEnforceableChildRelations,\n hasNestedMutationCallbacks,\n hasScalarFkFields,\n requireTransactionExecutor,\n} from \"./mutation-executor\";\nimport { withMutationScope, type IdbQueryExecutorWithTransaction } from \"./mutation-scope\";\n\n/** Callback form of `.where(fn)` — receives the typed model accessor proxy. */\nexport type WhereCallback<TContract, ModelName extends string> = (\n m: IdbModelAccessor<TContract, ModelName>\n) => IdbFilterExpr;\n\n/** Tuple of one-or-more field names of a model (for `select()` / `groupBy()`). */\ntype FieldTuple<TContract, ModelName extends string> = readonly [\n keyof DefaultModelRow<TContract, ModelName> & string,\n ...(keyof DefaultModelRow<TContract, ModelName> & string)[],\n];\n\n/**\n * The child accessor handed to an `include()` refinement callback.\n *\n * Exposes the chainable narrowing methods (`where` / `orderBy` / `take` /\n * `skip`) plus the scalar `count()` reducer. Mirrors the vendor\n * `IncludeRefinementCollection`: chainable methods return the same refinement\n * surface so `count()` stays reachable after a `where()`, and `count()` returns\n * an {@link IdbIncludeScalar} marker rather than the async terminal `count()`\n * found on the top-level accessor.\n */\nexport interface IdbIncludeRefinementAccessor<TContract, ModelName extends string> {\n where(\n filter: WhereFilter<TContract, ModelName> | WhereCallback<TContract, ModelName>\n ): IdbIncludeRefinementAccessor<TContract, ModelName>;\n orderBy(spec: OrderBySpec<TContract, ModelName>): IdbIncludeRefinementAccessor<TContract, ModelName>;\n take(n: number): IdbIncludeRefinementAccessor<TContract, ModelName>;\n skip(n: number): IdbIncludeRefinementAccessor<TContract, ModelName>;\n count(): IdbIncludeScalar;\n}\n\n/** Refinement callback type for a given relation key `K`. */\ntype IncludeRefineFn<TContract, ModelName extends string, K extends string, R> = (\n collection: IdbIncludeRefinementAccessor<TContract, RelatedModelOf<TContract, ModelName, K>>\n) => R;\n\n// ── Interface ─────────────────────────────────────────────────────────────────\n\n/**\n * Immutable query-builder for a single IDB object store.\n *\n * Each method that narrows the query (`.where()`, `.take()`, etc.) returns a\n * new, independent accessor instance — the original is never mutated. This\n * mirrors the `MongoCollection` pattern from `@prisma-next/2-mongo-family`.\n *\n * @template TContract - The full IDB contract (with or without type maps).\n * @template ModelName - The model (store) this accessor targets.\n * @template TIncludes - Tracks which relations have been included via\n * `.include()` calls, so the return type widens progressively.\n * @template TSelected - Field names kept by `.select()`. `never` (the\n * default) means \"all fields\"; otherwise the row narrows to these fields\n * (plus any included relations).\n */\nexport interface IdbStoreAccessor<\n TContract,\n ModelName extends string,\n TIncludes extends IncludeSpec<TContract, ModelName> = NoIncludes,\n TSelected extends string = never,\n> {\n /**\n * Add a filter (ANDed with any previous `.where()` calls).\n *\n * Two forms:\n *\n * - **Shorthand**: `where({ field: value })` — multi-key shorthand\n * objects compose as AND. `null` values become null-checks rather\n * than literal-null equalities so absent fields match.\n * - **Callback**: `where((m) => m.field.op(value))` — receives the\n * typed model accessor proxy and returns an `IdbFilterExpr` built\n * via the operator surface. Combinators (`and`, `or`, `not` from\n * `@prisma-next-idb/client-idb/orm`) compose nodes.\n */\n where(\n filter: WhereFilter<TContract, ModelName> | WhereCallback<TContract, ModelName>\n ): IdbStoreAccessor<TContract, ModelName, TIncludes, TSelected>;\n\n /** Set the sort order. Replaces any previous `.orderBy()` call. */\n orderBy(spec: OrderBySpec<TContract, ModelName>): IdbStoreAccessor<TContract, ModelName, TIncludes, TSelected>;\n\n /** Limit the number of rows returned. */\n take(n: number): IdbStoreAccessor<TContract, ModelName, TIncludes, TSelected>;\n\n /** Skip the first `n` rows (OFFSET). */\n skip(n: number): IdbStoreAccessor<TContract, ModelName, TIncludes, TSelected>;\n\n /**\n * Include a reference relation in the returned rows.\n *\n * The relation is loaded via a single batch cursor scan after the main\n * query — O(1) round trips to IDB per included relation regardless of\n * the number of parent rows. The return type gains the relation field\n * automatically.\n *\n * An optional refinement callback narrows the loaded relation:\n *\n * - return the (chained) collection to apply `where` / `orderBy` /\n * `take` / `skip` to the related rows (per-parent for `1:N`);\n * - return `collection.count()` to reduce a to-many relation to the\n * number of matching children (the field becomes a `number`).\n *\n * @example\n * ```ts\n * db.users.include(\"posts\", (posts) => posts.where({ published: true }).take(5))\n * db.users.include(\"posts\", (posts) => posts.count())\n * ```\n */\n include<K extends ReferenceRelKeys<TContract, ModelName>>(\n relation: K,\n refineFn?: IncludeRefineFn<\n TContract,\n ModelName,\n K,\n IdbIncludeRefinementAccessor<TContract, RelatedModelOf<TContract, ModelName, K>>\n >\n ): IdbStoreAccessor<TContract, ModelName, TIncludes & Record<K, true>, TSelected>;\n include<K extends ReferenceRelKeys<TContract, ModelName>>(\n relation: K,\n refineFn: IncludeRefineFn<TContract, ModelName, K, IdbIncludeScalar>\n ): IdbStoreAccessor<TContract, ModelName, TIncludes & Record<K, \"scalar\">, TSelected>;\n\n /**\n * Project the row down to a subset of scalar fields. Any previously\n * `.include()`d relations are preserved on the result; only the scalar\n * fields are narrowed.\n *\n * @example\n * ```ts\n * const summaries = await db.users.select(\"id\", \"email\").all();\n * // typeof summaries[number] === { id: string; email: string }\n * ```\n */\n select<Fields extends FieldTuple<TContract, ModelName>>(\n ...fields: Fields\n ): IdbStoreAccessor<TContract, ModelName, TIncludes, Fields[number]>;\n\n /** Return all matching rows as an async iterable (also awaitable as `Row[]`). */\n all(): AsyncIterableResult<SelectedRow<TContract, ModelName, TIncludes, TSelected>>;\n\n /** Return the first matching row, or `null` if none match. */\n first(): Promise<SelectedRow<TContract, ModelName, TIncludes, TSelected> | null>;\n\n /**\n * Run an in-memory aggregate (count/sum/avg/min/max) over the rows matching\n * the accumulated `.where()` filter. Returns one result object keyed by the\n * aliases supplied in the spec.\n *\n * @example\n * ```ts\n * const stats = await db.posts.where({ published: true }).aggregate((agg) => ({\n * total: agg.count(),\n * avgViews: agg.avg(\"views\"),\n * }));\n * ```\n */\n aggregate<Spec extends IdbAggregateSpec>(\n fn: (agg: IdbAggregateBuilder<TContract, ModelName>) => Spec\n ): Promise<IdbAggregateResult<Spec>>;\n\n /**\n * Switch to grouped-aggregate mode. The returned {@link IdbGroupedAccessor}'s\n * `.aggregate(...)` terminal produces one row per group with the chosen key\n * fields plus the requested aggregates.\n *\n * @example\n * ```ts\n * const byUser = await db.posts\n * .where({ published: true })\n * .groupBy(\"authorId\")\n * .aggregate((agg) => ({ count: agg.count(), totalViews: agg.sum(\"views\") }));\n * ```\n */\n groupBy<Fields extends FieldTuple<TContract, ModelName>>(\n ...fields: Fields\n ): IdbGroupedAccessor<TContract, ModelName, Fields>;\n\n /**\n * Insert a record into the store and return the stored row.\n *\n * The primary key field is optional in `data` — pass it to use a\n * client-generated ID (`cuid`, `uuid`) or omit it for auto-increment stores.\n *\n * Relation fields accept a mutation callback:\n * `posts: (rel) => rel.create([...])` or `author: (rel) => rel.connect({ id })`.\n * When any relation callback is present, all writes are wrapped in a single\n * multi-store IDB transaction (requires IdbRuntime, not a plain executor).\n */\n create(data: MutationCreateInput<TContract, ModelName>): Promise<DefaultModelRow<TContract, ModelName>>;\n\n /** Look up a single row by primary key. Returns `null` if not found. */\n findUnique(key: KeyType<TContract, ModelName>): Promise<DefaultModelRow<TContract, ModelName> | null>;\n\n /** Delete the row with the given primary key. */\n delete(key: KeyType<TContract, ModelName>): Promise<void>;\n\n /**\n * Update the first row matching the accumulated `.where()` filter.\n * Returns the merged row, or `null` if no row matches.\n *\n * Relation fields accept `connect` or `disconnect` callbacks. When any\n * relation callback is present, all writes run in a single multi-store\n * IDB transaction (requires IdbRuntime).\n */\n update(patch: MutationUpdateInput<TContract, ModelName>): Promise<DefaultModelRow<TContract, ModelName> | null>;\n\n /**\n * Update all rows matching the accumulated `.where()` filter and return\n * them as an `AsyncIterableResult` (also awaitable as `Row[]`).\n */\n updateAll(patch: PatchInput<TContract, ModelName>): AsyncIterableResult<DefaultModelRow<TContract, ModelName>>;\n\n /**\n * Update all rows matching the accumulated `.where()` filter.\n * Returns the count of updated rows.\n */\n updateCount(patch: PatchInput<TContract, ModelName>): Promise<number>;\n\n /**\n * Insert or update a single record.\n *\n * - If a row matching `where` exists: shallow-merge `update` onto it and\n * return the merged row.\n * - If no matching row exists: insert `create` and return it.\n */\n upsert(args: {\n create: CreateInput<TContract, ModelName>;\n update: PatchInput<TContract, ModelName>;\n where: WhereFilter<TContract, ModelName>;\n }): Promise<DefaultModelRow<TContract, ModelName>>;\n\n /**\n * Insert multiple records in a single atomic transaction.\n * Returns all inserted rows as an `AsyncIterableResult`.\n */\n createAll(data: CreateInput<TContract, ModelName>[]): AsyncIterableResult<DefaultModelRow<TContract, ModelName>>;\n\n /**\n * Insert multiple records in a single atomic transaction.\n * Returns the count of inserted rows.\n */\n createCount(data: CreateInput<TContract, ModelName>[]): Promise<number>;\n\n /**\n * Delete all rows matching the accumulated `.where()` filter.\n * Returns the deleted rows as an `AsyncIterableResult`.\n */\n deleteAll(): AsyncIterableResult<DefaultModelRow<TContract, ModelName>>;\n\n /**\n * Delete all rows matching the accumulated `.where()` filter.\n * Returns the count of deleted rows.\n */\n deleteCount(): Promise<number>;\n\n /**\n * Count all rows matching the accumulated `.where()` filter.\n * With no filter, counts all rows in the store.\n *\n * **Note — `skip`/`take` are respected**: unlike Prisma's SQL `count()`,\n * which ignores pagination, this implementation reuses the same cursor-scan\n * plan as `all()`. That means `where(...).take(5).count()` returns at most 5,\n * not the total number of matching rows. Use `where(...).count()` without\n * `take`/`skip` when you need an unbounded total.\n */\n count(): Promise<number>;\n}\n\n// ── Implementation ────────────────────────────────────────────────────────────\n\n/**\n * Concrete immutable query builder.\n *\n * Internal details:\n * - All state is in `#state` (filters, orderBy, skip, take, includes, selectedFields).\n * - Builder methods clone via `#clone()` — O(1) copies since state is\n * structurally shared.\n * - `all()` materialises the main rows first, then batch-loads each included\n * relation, then applies any `.select()` projection before yielding.\n * - `#includeRefinementMode` flips `count()` from an async terminal to an\n * {@link IdbIncludeScalar} marker so it can be used inside `include()`.\n */\nexport class IdbStoreAccessorImpl<\n TContract extends IdbContract,\n ModelName extends string,\n TIncludes extends IncludeSpec<TContract, ModelName> = NoIncludes,\n TSelected extends string = never,\n> implements IdbStoreAccessor<TContract, ModelName, TIncludes, TSelected> {\n readonly #contract: TContract;\n readonly #modelName: ModelName;\n readonly #executor: IdbQueryExecutor;\n readonly #storeName: string;\n readonly #state: IdbAccessorState;\n readonly #newGroupingKey: () => string;\n readonly #includeRefinementMode: boolean;\n\n constructor(\n contract: TContract,\n modelName: ModelName,\n executor: IdbQueryExecutor,\n state?: IdbAccessorState,\n newGroupingKey?: () => string,\n includeRefinementMode = false\n ) {\n this.#contract = contract;\n this.#modelName = modelName;\n this.#executor = executor;\n this.#storeName = getStoreName(contract, modelName);\n this.#state = state ?? emptyAccessorState();\n // Default: per-instance counter (single client; avoids module-level interleaving).\n let _key = 0;\n this.#newGroupingKey = newGroupingKey ?? (() => `idb-op-${++_key}`);\n this.#includeRefinementMode = includeRefinementMode;\n }\n\n // ── Builder methods ───────────────────────────────────────────────────────\n\n where(\n filter: WhereFilter<TContract, ModelName> | WhereCallback<TContract, ModelName>\n ): IdbStoreAccessor<TContract, ModelName, TIncludes, TSelected> {\n const expr =\n typeof filter === \"function\"\n ? filter(createModelAccessor<TContract, ModelName>())\n : shorthandToFilterExpr(filter as Record<string, unknown>);\n // An empty shorthand object (or one with only undefined values) lifts\n // to `undefined` — keep the existing filter list untouched so chained\n // `.where({})` calls don't produce noisy AND nodes.\n if (expr === undefined) return this.#clone({});\n return this.#clone({ filters: [...this.#state.filters, expr] });\n }\n\n orderBy(spec: OrderBySpec<TContract, ModelName>): IdbStoreAccessor<TContract, ModelName, TIncludes, TSelected> {\n return this.#clone({ orderBy: spec as Record<string, \"asc\" | \"desc\"> });\n }\n\n take(n: number): IdbStoreAccessor<TContract, ModelName, TIncludes, TSelected> {\n return this.#clone({ take: n });\n }\n\n skip(n: number): IdbStoreAccessor<TContract, ModelName, TIncludes, TSelected> {\n return this.#clone({ skip: n });\n }\n\n include<K extends ReferenceRelKeys<TContract, ModelName>>(\n relation: K,\n refineFn?: IncludeRefineFn<\n TContract,\n ModelName,\n K,\n IdbIncludeRefinementAccessor<TContract, RelatedModelOf<TContract, ModelName, K>>\n >\n ): IdbStoreAccessor<TContract, ModelName, TIncludes & Record<K, true>, TSelected>;\n include<K extends ReferenceRelKeys<TContract, ModelName>>(\n relation: K,\n refineFn: IncludeRefineFn<TContract, ModelName, K, IdbIncludeScalar>\n ): IdbStoreAccessor<TContract, ModelName, TIncludes & Record<K, \"scalar\">, TSelected>;\n include<K extends ReferenceRelKeys<TContract, ModelName>>(\n relation: K,\n refineFn?: IncludeRefineFn<\n TContract,\n ModelName,\n K,\n IdbIncludeRefinementAccessor<TContract, RelatedModelOf<TContract, ModelName, K>> | IdbIncludeScalar\n >\n ): IdbStoreAccessor<TContract, ModelName, IncludeSpec<TContract, ModelName>, TSelected> {\n const entry = this.#resolveIncludeEntry(relation, refineFn);\n const newState = mergeAccessorState(this.#state, {\n includes: { ...this.#state.includes, [relation]: entry },\n });\n // The new instance is identical at runtime; the narrowed TIncludes type is\n // only a compile-time distinction — so an `as unknown as` cast is safe.\n return new IdbStoreAccessorImpl(\n this.#contract,\n this.#modelName,\n this.#executor,\n newState,\n this.#newGroupingKey,\n this.#includeRefinementMode\n ) as unknown as IdbStoreAccessor<TContract, ModelName, IncludeSpec<TContract, ModelName>, TSelected>;\n }\n\n select<Fields extends FieldTuple<TContract, ModelName>>(\n ...fields: Fields\n ): IdbStoreAccessor<TContract, ModelName, TIncludes, Fields[number]> {\n // Runtime is identical; only TSelected narrows. Cast bridges the type-level\n // projection (the clone preserves TIncludes / contract / executor).\n return this.#clone({ selectedFields: fields as readonly string[] }) as unknown as IdbStoreAccessor<\n TContract,\n ModelName,\n TIncludes,\n Fields[number]\n >;\n }\n\n // ── Execution methods ─────────────────────────────────────────────────────\n\n all(): AsyncIterableResult<SelectedRow<TContract, ModelName, TIncludes, TSelected>> {\n const groupingKey = this.#newGroupingKey();\n // Capture the private fields needed inside the generator. Private names\n // must be accessed on `this`, so we bind the methods to keep them callable\n // without aliasing `this` (no-this-alias).\n const buildScanPlan = this.#buildScanPlan.bind(this);\n const executorExecute = this.#executor.execute.bind(this.#executor);\n const applyIncludes = this.#applyIncludes.bind(this);\n const projectRows = this.#projectRows.bind(this);\n return new AsyncIterableResult(\n (async function* (): AsyncGenerator<SelectedRow<TContract, ModelName, TIncludes, TSelected>, void, unknown> {\n // 1. Run the main cursor scan and materialise rows.\n const scanPlan = buildScanPlan<Record<string, unknown>>(groupingKey);\n const rows: Record<string, unknown>[] = [];\n for await (const row of executorExecute(scanPlan)) {\n rows.push(row);\n }\n\n // 2. Batch-load any included relations (uses full rows — FK fields intact).\n const withIncludes = await applyIncludes(rows, groupingKey);\n\n // 3. Apply any `.select()` projection, then yield.\n for (const row of projectRows(withIncludes)) {\n yield row as SelectedRow<TContract, ModelName, TIncludes, TSelected>;\n }\n })()\n );\n }\n\n async first(): Promise<SelectedRow<TContract, ModelName, TIncludes, TSelected> | null> {\n return this.take(1).all().first();\n }\n\n async aggregate<Spec extends IdbAggregateSpec>(\n fn: (agg: IdbAggregateBuilder<TContract, ModelName>) => Spec\n ): Promise<IdbAggregateResult<Spec>> {\n const spec = fn(createAggregateBuilder<TContract, ModelName>());\n assertValidAggregateSpec(spec, \"aggregate()\");\n const combined = this.#combinedFilterExpr();\n const ast: IdbAggregateAst = {\n kind: \"aggregate\",\n modelName: this.#modelName,\n aggregates: toAggregateRequests(spec),\n ...(combined !== undefined ? { where: combined } : {}),\n };\n const rows = await this.#materialize(this.#newGroupingKey(), ast);\n return computeAggregateSpec(spec, rows) as IdbAggregateResult<Spec>;\n }\n\n groupBy<Fields extends FieldTuple<TContract, ModelName>>(\n ...fields: Fields\n ): IdbGroupedAccessor<TContract, ModelName, Fields> {\n const combined = this.#combinedFilterExpr();\n const materialize = (ast: IdbQueryAst): Promise<Record<string, unknown>[]> =>\n this.#materialize(this.#newGroupingKey(), ast);\n return createGroupedAccessor<TContract, ModelName, Fields>({\n modelName: this.#modelName,\n by: fields as readonly string[],\n where: combined,\n materialize,\n });\n }\n\n async create(data: MutationCreateInput<TContract, ModelName>): Promise<DefaultModelRow<TContract, ModelName>> {\n const record = data as Record<string, unknown>;\n\n if (hasNestedMutationCallbacks(this.#contract, this.#modelName, record)) {\n const row = await executeNestedCreateMutation({\n executor: requireTransactionExecutor(this.#executor),\n contract: this.#contract,\n modelName: this.#modelName,\n data: record,\n });\n return row as DefaultModelRow<TContract, ModelName>;\n }\n\n if (hasScalarFkFields(this.#contract, this.#modelName, record)) {\n const row = await executeScalarCreateWithFkValidation({\n executor: requireTransactionExecutor(this.#executor),\n contract: this.#contract,\n modelName: this.#modelName,\n data: record,\n });\n return row as DefaultModelRow<TContract, ModelName>;\n }\n\n const groupingKey = this.#newGroupingKey();\n const meta = this.#planMeta(groupingKey);\n const ast: IdbCreateAst = { kind: \"create\", modelName: this.#modelName, data: record };\n const plan: IdbQueryPlan<Record<string, unknown>> = {\n meta,\n ast,\n idbPlan: { meta, kind: \"put\", storeName: this.#storeName, record },\n };\n // The IDB driver echoes the stored record back as the single result row.\n for await (const row of this.#executor.execute(plan)) {\n return row as DefaultModelRow<TContract, ModelName>;\n }\n return record as DefaultModelRow<TContract, ModelName>;\n }\n\n async findUnique(key: KeyType<TContract, ModelName>): Promise<DefaultModelRow<TContract, ModelName> | null> {\n const groupingKey = this.#newGroupingKey();\n const meta = this.#planMeta(groupingKey);\n const ast: IdbFindUniqueAst = { kind: \"findUnique\", modelName: this.#modelName, key };\n const plan: IdbQueryPlan<Record<string, unknown>> = {\n meta,\n ast,\n idbPlan: { meta, kind: \"key-get\", storeName: this.#storeName, key: key as IDBValidKey },\n };\n for await (const row of this.#executor.execute(plan)) {\n return row as DefaultModelRow<TContract, ModelName>;\n }\n return null;\n }\n\n async delete(key: KeyType<TContract, ModelName>): Promise<void> {\n if (hasEnforceableChildRelations(this.#contract, this.#modelName)) {\n await executeDeleteWithReferentialActions({\n executor: requireTransactionExecutor(this.#executor),\n contract: this.#contract,\n modelName: this.#modelName,\n key: key as IDBValidKey,\n });\n return;\n }\n const groupingKey = this.#newGroupingKey();\n const meta = this.#planMeta(groupingKey);\n const ast: IdbDeleteAst = { kind: \"delete\", modelName: this.#modelName, key };\n const plan: IdbQueryPlan<Record<string, unknown>> = {\n meta,\n ast,\n idbPlan: { meta, kind: \"delete\", storeName: this.#storeName, key: key as IDBValidKey },\n };\n // `delete` yields no rows; drain via toArray() to execute the plan.\n await this.#executor.execute(plan).toArray();\n }\n\n async update(\n patch: MutationUpdateInput<TContract, ModelName>\n ): Promise<DefaultModelRow<TContract, ModelName> | null> {\n const patchRecord = patch as Record<string, unknown>;\n\n if (hasNestedMutationCallbacks(this.#contract, this.#modelName, patchRecord)) {\n const row = await executeNestedUpdateMutation({\n executor: requireTransactionExecutor(this.#executor),\n contract: this.#contract,\n modelName: this.#modelName,\n filters: this.#state.filters,\n data: patchRecord,\n });\n return row as DefaultModelRow<TContract, ModelName> | null;\n }\n\n if (hasScalarFkFields(this.#contract, this.#modelName, patchRecord)) {\n const row = await executeScalarUpdateWithFkValidation({\n executor: requireTransactionExecutor(this.#executor),\n contract: this.#contract,\n modelName: this.#modelName,\n filters: this.#state.filters,\n data: patchRecord,\n });\n return row as DefaultModelRow<TContract, ModelName> | null;\n }\n\n const groupingKey = this.#newGroupingKey();\n const combined = this.#combinedFilterExpr();\n const filter = combined !== undefined ? (row: Record<string, unknown>) => evaluateFilter(combined, row) : undefined;\n const meta = this.#planMeta(groupingKey);\n const ast: IdbUpdateAst = {\n kind: \"update\",\n modelName: this.#modelName,\n patch: patchRecord,\n ...(combined !== undefined ? { where: combined } : {}),\n };\n const plan: IdbQueryPlan<Record<string, unknown>> = {\n meta,\n ast,\n idbPlan: {\n meta,\n kind: \"scan-write\",\n storeName: this.#storeName,\n write: \"put-merged\",\n patch: patchRecord,\n take: 1,\n ...(filter !== undefined ? { filter } : {}),\n },\n };\n for await (const row of this.#executor.execute(plan)) {\n return row as DefaultModelRow<TContract, ModelName>;\n }\n return null;\n }\n\n updateAll(patch: PatchInput<TContract, ModelName>): AsyncIterableResult<DefaultModelRow<TContract, ModelName>> {\n const groupingKey = this.#newGroupingKey();\n const combined = this.#combinedFilterExpr();\n const filter = combined !== undefined ? (row: Record<string, unknown>) => evaluateFilter(combined, row) : undefined;\n const meta = this.#planMeta(groupingKey);\n const storeName = this.#storeName;\n const modelName = this.#modelName;\n const patchRecord = patch as Record<string, unknown>;\n const executorExecute = this.#executor.execute.bind(this.#executor);\n return new AsyncIterableResult(\n (async function* (): AsyncGenerator<DefaultModelRow<TContract, ModelName>, void, unknown> {\n const ast: IdbUpdateAllAst = {\n kind: \"updateAll\",\n modelName,\n patch: patchRecord,\n ...(combined !== undefined ? { where: combined } : {}),\n };\n const plan: IdbQueryPlan<Record<string, unknown>> = {\n meta,\n ast,\n idbPlan: {\n meta,\n kind: \"scan-write\",\n storeName,\n write: \"put-merged\",\n patch: patchRecord,\n ...(filter !== undefined ? { filter } : {}),\n },\n };\n for await (const row of executorExecute(plan)) {\n yield row as DefaultModelRow<TContract, ModelName>;\n }\n })()\n );\n }\n\n async updateCount(patch: PatchInput<TContract, ModelName>): Promise<number> {\n return (await this.updateAll(patch).toArray()).length;\n }\n\n async upsert(args: {\n create: CreateInput<TContract, ModelName>;\n update: PatchInput<TContract, ModelName>;\n where: WhereFilter<TContract, ModelName>;\n }): Promise<DefaultModelRow<TContract, ModelName>> {\n const keyPath = getKeyPath(this.#contract, this.#modelName);\n const whereExpr = shorthandToFilterExpr(args.where as Record<string, unknown>);\n const matches = (row: Record<string, unknown>): boolean =>\n whereExpr === undefined || evaluateFilter(whereExpr, row);\n const meta = this.#planMeta(this.#newGroupingKey());\n const createRecord = args.create as Record<string, unknown>;\n const patchRecord = args.update as Record<string, unknown>;\n const storeName = this.#storeName;\n\n // Atomic path: when the executor supports transactions (always true for\n // IdbRuntime, i.e. createIdbClient / createAutoMigratingIdbClient), run the\n // find-then-write in a single readwrite transaction so there is no\n // check-then-act race window. Mirrors the vendor's single-statement upsert.\n const exec = this.#executor as IdbQueryExecutor & Partial<Pick<IdbQueryExecutorWithTransaction, \"transaction\">>;\n if (typeof exec.transaction === \"function\") {\n return withMutationScope(exec as IdbQueryExecutorWithTransaction, [storeName], async (scope) => {\n const found = await scope.execute({ meta, kind: \"cursor-scan\", storeName, filter: matches, take: 1 });\n const existing = found[0];\n if (existing === undefined) {\n const rows = await scope.execute({ meta, kind: \"put\", storeName, record: createRecord });\n return (rows[0] ?? createRecord) as DefaultModelRow<TContract, ModelName>;\n }\n const key = existing[keyPath] as IDBValidKey;\n const rows = await scope.execute({ meta, kind: \"update\", storeName, key, patch: patchRecord });\n return (rows[0] ?? existing) as DefaultModelRow<TContract, ModelName>;\n });\n }\n\n // Fallback (non-atomic): a bare bring-your-own `IdbQueryExecutor` with no\n // transaction support. A check-then-act window exists here, but it is the\n // only path available without `transaction()`. Real clients never hit it.\n const existing = await this.where(args.where).first();\n if (!existing) {\n // A bare CreateInput (no relation callbacks) is always a valid\n // MutationCreateInput; the generic intersection can't be proven here.\n return this.create(args.create as MutationCreateInput<TContract, ModelName>);\n }\n const key = (existing as Record<string, unknown>)[keyPath] as IDBValidKey;\n const ast: IdbUpsertAst = {\n kind: \"upsert\",\n modelName: this.#modelName,\n create: createRecord,\n update: patchRecord,\n where: args.where as Record<string, unknown>,\n };\n const plan: IdbQueryPlan<Record<string, unknown>> = {\n meta,\n ast,\n idbPlan: { meta, kind: \"update\", storeName, key, patch: patchRecord },\n };\n for await (const row of this.#executor.execute(plan)) {\n return row as DefaultModelRow<TContract, ModelName>;\n }\n return existing as DefaultModelRow<TContract, ModelName>;\n }\n\n createAll(data: CreateInput<TContract, ModelName>[]): AsyncIterableResult<DefaultModelRow<TContract, ModelName>> {\n const groupingKey = this.#newGroupingKey();\n const meta = this.#planMeta(groupingKey);\n const records = data.map((d) => d as Record<string, unknown>);\n const ast: IdbCreateAllAst = { kind: \"createAll\", modelName: this.#modelName, data: records };\n const plan: IdbQueryPlan<Record<string, unknown>> = {\n meta,\n ast,\n idbPlan: {\n meta,\n kind: \"batch\",\n storeNames: [this.#storeName],\n ops: records.map((record) => ({ meta, kind: \"put\" as const, storeName: this.#storeName, record })),\n },\n };\n const executorExecute = this.#executor.execute.bind(this.#executor);\n return new AsyncIterableResult(\n (async function* (): AsyncGenerator<DefaultModelRow<TContract, ModelName>, void, unknown> {\n for await (const row of executorExecute(plan)) {\n yield row as DefaultModelRow<TContract, ModelName>;\n }\n })()\n );\n }\n\n async createCount(data: CreateInput<TContract, ModelName>[]): Promise<number> {\n return (await this.createAll(data).toArray()).length;\n }\n\n deleteAll(): AsyncIterableResult<DefaultModelRow<TContract, ModelName>> {\n const combined = this.#combinedFilterExpr();\n const filter = combined !== undefined ? (row: Record<string, unknown>) => evaluateFilter(combined, row) : undefined;\n\n if (hasEnforceableChildRelations(this.#contract, this.#modelName)) {\n const contract = this.#contract;\n const modelName = this.#modelName;\n const executor = requireTransactionExecutor(this.#executor);\n return new AsyncIterableResult(\n (async function* (): AsyncGenerator<DefaultModelRow<TContract, ModelName>, void, unknown> {\n const rows = await executeDeleteAllWithReferentialActions({\n executor,\n contract,\n modelName,\n ...(filter !== undefined ? { filter } : {}),\n });\n for (const row of rows) yield row as DefaultModelRow<TContract, ModelName>;\n })()\n );\n }\n\n const groupingKey = this.#newGroupingKey();\n const meta = this.#planMeta(groupingKey);\n const ast: IdbDeleteAllAst = {\n kind: \"deleteAll\",\n modelName: this.#modelName,\n ...(combined !== undefined ? { where: combined } : {}),\n };\n const storeName = this.#storeName;\n const executorExecute = this.#executor.execute.bind(this.#executor);\n return new AsyncIterableResult(\n (async function* (): AsyncGenerator<DefaultModelRow<TContract, ModelName>, void, unknown> {\n const plan: IdbQueryPlan<Record<string, unknown>> = {\n meta,\n ast,\n idbPlan: {\n meta,\n kind: \"scan-write\",\n storeName,\n write: \"delete\",\n ...(filter !== undefined ? { filter } : {}),\n },\n };\n for await (const row of executorExecute(plan)) {\n yield row as DefaultModelRow<TContract, ModelName>;\n }\n })()\n );\n }\n\n async deleteCount(): Promise<number> {\n return (await this.deleteAll().toArray()).length;\n }\n\n count(): Promise<number> {\n if (this.#includeRefinementMode) {\n // Inside an include() refinement, count() is a scalar-include marker that\n // include() consumes synchronously — not the async terminal below. The\n // IdbIncludeRefinementAccessor type surfaces the IdbIncludeScalar return;\n // this cast bridges count()'s dual runtime role.\n return createIncludeScalar(this.#state) as unknown as Promise<number>;\n }\n return this.#countTerminal();\n }\n\n // ── Private helpers ───────────────────────────────────────────────────────\n\n async #countTerminal(): Promise<number> {\n const groupingKey = this.#newGroupingKey();\n const scanPlan = this.#buildScanPlan<Record<string, unknown>>(groupingKey);\n // Override the AST kind for middleware introspection — the idbPlan stays cursor-scan.\n const scanAst = scanPlan.ast;\n const ast: IdbCountAst = {\n kind: \"count\",\n modelName: this.#modelName,\n ...(scanAst?.kind === \"findMany\" && scanAst.where !== undefined ? { where: scanAst.where } : {}),\n };\n const plan: IdbQueryPlan<Record<string, unknown>> = { ...scanPlan, ast };\n let n = 0;\n for await (const _ of this.#executor.execute(plan)) {\n n++;\n }\n return n;\n }\n\n /**\n * Resolve an `include()` argument pair into an {@link IncludeEntry}: run the\n * optional refinement against a fresh refinement-mode child accessor, then\n * classify the result as a scalar count or a refined collection.\n */\n #resolveIncludeEntry(relation: string, refineFn: ((collection: never) => unknown) | undefined): IncludeEntry {\n if (refineFn === undefined) {\n return { kind: \"collection\", state: emptyAccessorState() };\n }\n\n const rel = getRelation(this.#contract, this.#modelName, relation);\n // v0.12.0: `relation.to` is a CrossReference `{ namespace, model }`.\n const relatedModelName = rel?.to.model ?? relation;\n const child = new IdbStoreAccessorImpl(\n this.#contract,\n relatedModelName,\n this.#executor,\n emptyAccessorState(),\n this.#newGroupingKey,\n /* includeRefinementMode */ true\n );\n\n const refined = (refineFn as (c: unknown) => unknown)(child);\n\n if (isIncludeScalar(refined)) {\n if (rel !== undefined && rel.cardinality !== \"1:N\") {\n throw new Error(`include('${relation}'): count() is only supported for to-many (1:N) relations`);\n }\n return { kind: \"scalar\", fn: refined.fn, state: refined.state };\n }\n\n // Cross-instance private access is allowed within the class body.\n if (refined instanceof IdbStoreAccessorImpl) {\n return { kind: \"collection\", state: refined.#state };\n }\n\n throw new Error(\n `include('${relation}') refinement must return the collection (for where/orderBy/take/skip) or a count() selector`\n );\n }\n\n /**\n * Materialise all rows matching the accumulated filters with no pagination —\n * used by `aggregate()` / `groupBy()`. The supplied `ast` is attached to the\n * scan plan so middleware can observe the aggregate intent.\n */\n async #materialize(groupingKey: string, ast: IdbQueryAst): Promise<Record<string, unknown>[]> {\n const combined = this.#combinedFilterExpr();\n const filter = combined !== undefined ? (row: Record<string, unknown>) => evaluateFilter(combined, row) : undefined;\n const meta = this.#planMeta(groupingKey);\n const plan: IdbQueryPlan<Record<string, unknown>> = {\n meta,\n ast,\n idbPlan: {\n meta,\n kind: \"cursor-scan\",\n storeName: this.#storeName,\n ...(filter !== undefined ? { filter } : {}),\n },\n };\n const rows: Record<string, unknown>[] = [];\n for await (const row of this.#executor.execute(plan)) {\n rows.push(row);\n }\n return rows;\n }\n\n #buildScanPlan<Row>(groupingKey: string): IdbQueryPlan<Row> {\n const combined = this.#combinedFilterExpr();\n const filter = combined !== undefined ? (row: Record<string, unknown>) => evaluateFilter(combined, row) : undefined;\n const comparator = buildRowComparator(this.#state.orderBy);\n const meta = this.#planMeta(groupingKey);\n const ast: IdbFindManyAst = {\n kind: \"findMany\",\n modelName: this.#modelName,\n ...(combined !== undefined ? { where: combined } : {}),\n ...(this.#state.orderBy !== undefined ? { orderBy: this.#state.orderBy as Record<string, \"asc\" | \"desc\"> } : {}),\n ...(this.#state.skip !== undefined ? { skip: this.#state.skip } : {}),\n ...(this.#state.take !== undefined ? { take: this.#state.take } : {}),\n };\n // exactOptionalPropertyTypes: spread conditionally to avoid `undefined`\n // values in optional fields.\n return {\n meta,\n ast,\n idbPlan: {\n meta,\n kind: \"cursor-scan\" as const,\n storeName: this.#storeName,\n ...(filter !== undefined ? { filter } : {}),\n ...(comparator !== undefined ? { comparator } : {}),\n ...(this.#state.skip !== undefined ? { skip: this.#state.skip } : {}),\n ...(this.#state.take !== undefined ? { take: this.#state.take } : {}),\n },\n } as IdbQueryPlan<Row>;\n }\n\n /**\n * Combine all accumulated filter expressions with AND.\n *\n * Returns `undefined` when no filter has been installed so the driver can\n * skip building a row filter closure (a small perf and readability win on\n * `.all()` paths). Delegates to the shared {@link combineFilterExprs}.\n */\n #combinedFilterExpr(): IdbFilterExpr | undefined {\n return combineFilterExprs(this.#state.filters);\n }\n\n async #applyIncludes(rows: Record<string, unknown>[], groupingKey: string): Promise<Record<string, unknown>[]> {\n const relNames = Object.keys(this.#state.includes);\n if (relNames.length === 0) return rows;\n let result = rows;\n for (const relName of relNames) {\n const entry = this.#state.includes[relName]!;\n result = await loadRelation(relName, entry, result, this.#contract, this.#modelName, this.#executor, groupingKey);\n }\n return result;\n }\n\n /**\n * Apply a `.select()` projection (if any) to materialised rows. Keeps the\n * selected scalar fields plus every included relation key (which `include()`\n * attached during {@link #applyIncludes}); a no-op when nothing is selected.\n */\n #projectRows(rows: Record<string, unknown>[]): Record<string, unknown>[] {\n const selected = this.#state.selectedFields;\n if (selected === undefined) return rows;\n const keep = [...selected, ...Object.keys(this.#state.includes)];\n return rows.map((row) => {\n const out: Record<string, unknown> = {};\n for (const field of keep) {\n if (field in row) out[field] = row[field];\n }\n return out;\n });\n }\n\n #planMeta(groupingKey: string): PlanMeta {\n return {\n target: \"idb\",\n storageHash: this.#contract.storage.storageHash,\n lane: \"idb-orm\",\n annotations: { groupingKey },\n };\n }\n\n #clone(overrides: Partial<IdbAccessorState>): IdbStoreAccessorImpl<TContract, ModelName, TIncludes, TSelected> {\n return new IdbStoreAccessorImpl(\n this.#contract,\n this.#modelName,\n this.#executor,\n mergeAccessorState(this.#state, overrides),\n this.#newGroupingKey,\n this.#includeRefinementMode\n );\n }\n}\n","import type { Contract } from \"@prisma-next/contract/types\";\nimport type { IdbContract, IdbStorage } from \"./types\";\nimport type { IdbQueryExecutor } from \"./executor\";\nimport { type IdbStoreAccessor, IdbStoreAccessorImpl } from \"./store-accessor\";\n\n// ── Public types ──────────────────────────────────────────────────────────────\n\n/**\n * The ORM client object returned by {@link idbOrm}.\n *\n * Keys are the root keys from `contract.roots` (e.g. `\"users\"`, `\"posts\"`).\n * Each value is an {@link IdbStoreAccessor} targeting the corresponding model.\n *\n * @example\n * ```ts\n * const db = idbOrm({ contract, executor: runtime });\n * const users = await db.users.all(); // IdbStoreAccessor<..., \"User\">\n * ```\n */\nexport type IdbOrmClient<TContract extends Contract<IdbStorage>> = {\n // v0.12.0: `roots` values are CrossReference `{ namespace, model }`, so the\n // target model name is `roots[K].model` (was a bare model-name string).\n readonly [K in string & keyof TContract[\"roots\"]]: TContract[\"roots\"][K] extends {\n model: infer ModelName extends string;\n }\n ? IdbStoreAccessor<TContract, ModelName>\n : never;\n};\n\n/**\n * Options for {@link idbOrm}.\n */\nexport interface IdbOrmOptions<TContract extends IdbContract> {\n /** The resolved IDB contract (with or without attached type maps). */\n readonly contract: TContract;\n /**\n * The query executor.\n *\n * Any object with a compatible `execute()` signature satisfies this —\n * most commonly an `IdbRuntime` created via `createIdbRuntime()`.\n */\n readonly executor: IdbQueryExecutor;\n}\n\n// ── Factory function ──────────────────────────────────────────────────────────\n\n/**\n * Create a typed IDB ORM client from a contract and executor.\n *\n * The client exposes one `IdbStoreAccessor` per entry in `contract.roots`.\n * Only roots-declared stores are accessible at the top level — other stores\n * can be reached via `.include()` on any accessor.\n *\n * @example\n * ```ts\n * import { idbOrm } from \"@prisma-next-idb/client-idb/orm\";\n * import { createIdbRuntime } from \"@prisma-next-idb/runtime-idb/runtime\";\n * import contract from \"./prisma/idb-contract\";\n *\n * const runtime = createIdbRuntime({ adapter, driver });\n * const db = idbOrm({ contract, executor: runtime });\n *\n * // Typed query builder:\n * const alice = await db.users.where({ email: \"alice@example.com\" }).first();\n * const postsWithAuthor = await db.posts.include(\"author\").all();\n * ```\n */\nexport function idbOrm<TContract extends IdbContract>(options: IdbOrmOptions<TContract>): IdbOrmClient<TContract> {\n const { contract, executor } = options;\n\n // One counter per idbOrm() call — two clients in the same realm get\n // independent key sequences and can't interleave (ADR 160).\n let _key = 0;\n const newGroupingKey = () => `idb-op-${++_key}`;\n\n const client: Record<string, IdbStoreAccessor<TContract, string>> = {};\n for (const [rootKey, ref] of Object.entries(contract.roots)) {\n // v0.12.0: `roots` values are CrossReference `{ namespace, model }`.\n client[rootKey] = new IdbStoreAccessorImpl(contract, ref.model, executor, undefined, newGroupingKey);\n }\n\n return client as unknown as IdbOrmClient<TContract>;\n}\n"],"mappings":";;;;;;;;AA0dA,SAAgB,aA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ACF;AACF;;;;;;;;;;;;;;;;;;;;;;;;ACt8BA,SAAgB,OAAsC,SAA4D;CAChH,MAAM,EAAE,UAAU,aAAa;CAI/B,IAAI,OAAO;CACX,MAAM,uBAAuB,UAAU,EAAE;CAEzC,MAAM,SAA8D,CAAC;CACrE,KAAK,MAAM,CAAC,SAAS,QAAQ,OAAO,QAAQ,SAAS,KAAK,GAExD,OAAO,WAAW,IAAI,qBAAqB,UAAU,IAAI,OAAO,UAAU,KAAA,GAAW,cAAc;CAGrG,OAAO;AACT"}
package/dist/orm.d.mts ADDED
@@ -0,0 +1,55 @@
1
+ import { A as MutationUpdateInput, B as SelectedRow, C as IncludeFields, D as KeyType, E as IncludedRow, F as ReferenceRelKeys, H as WhereFilter, I as RelatedModelOf, L as RelationMutationConnect, M as NumericFieldNames, N as OrderBySpec, O as ModelKeyPath, P as PatchInput, R as RelationMutationCreate, S as IdbRelationMutator, T as IncludeSpec, V as SortDirection, _ as IdbAggregateResult, a as IdbStoreAccessor, b as IdbContract, c as IdbGroupedAccessor, d as IdbModelAccessor, f as IdbQueryExecutor, g as IdbAggregateBuilder, h as DefaultModelRow, i as IdbIncludeRefinementAccessor, j as NoIncludes, k as MutationCreateInput, l as IdbIncludeScalar, m as CreateInput, n as IdbOrmOptions, o as WhereCallback, p as AggregateFn, r as idbOrm, s as GroupedResultRow, t as IdbOrmClient, u as IdbFieldAccessor, v as IdbAggregateSelector, w as IncludeMarker, x as IdbRelationMutation, y as IdbAggregateSpec, z as RelationMutationDisconnect } from "./idb-orm-B5H6xka-.mjs";
2
+ import { IdbAndExpr, IdbAndExpr as IdbAndExpr$1, IdbFieldFilter, IdbFilterExpr, IdbFilterExpr as IdbFilterExpr$1, IdbFilterOp, IdbNotExpr, IdbNotExpr as IdbNotExpr$1, IdbNullCheckExpr, IdbOrExpr, IdbOrExpr as IdbOrExpr$1 } from "@prisma-next-idb/adapter-idb/runtime";
3
+ import { IdbTransactionScope } from "@prisma-next-idb/driver-idb/runtime";
4
+
5
+ //#region src/core/filters.d.ts
6
+ /** Variadic AND. `and()` (no args) is vacuously true. */
7
+ declare function and(...exprs: ReadonlyArray<IdbFilterExpr$1>): IdbAndExpr$1;
8
+ /** Variadic OR. `or()` (no args) is vacuously false. */
9
+ declare function or(...exprs: ReadonlyArray<IdbFilterExpr$1>): IdbOrExpr$1;
10
+ /** Logical NOT. */
11
+ declare function not(expr: IdbFilterExpr$1): IdbNotExpr$1;
12
+ //#endregion
13
+ //#region src/core/mutation-scope.d.ts
14
+ /**
15
+ * Extends the basic {@link IdbQueryExecutor} with multi-store transaction
16
+ * support. `IdbRuntime` satisfies this interface — `runtime.transaction()`
17
+ * delegates to `driver.transaction()` which opens the IDB transaction.
18
+ */
19
+ interface IdbQueryExecutorWithTransaction extends IdbQueryExecutor {
20
+ /**
21
+ * Open a multi-store IDB transaction and return a scope object.
22
+ *
23
+ * The returned scope's `execute()` runs `IdbAtomicPlan`s directly inside
24
+ * the transaction — no new transaction is opened per call, and the
25
+ * middleware chain is bypassed. `commit()` resolves when `tx.oncomplete`
26
+ * fires; `rollback()` calls `tx.abort()`.
27
+ */
28
+ transaction(storeNames: string[], mode?: IDBTransactionMode): Promise<IdbTransactionScope>;
29
+ }
30
+ /**
31
+ * Run a callback inside a single multi-store IDB readwrite transaction.
32
+ *
33
+ * Opens the transaction, passes the `IdbTransactionScope` to `run`, then:
34
+ * - On success: awaits `scope.commit()` (waits for `tx.oncomplete`).
35
+ * - On error: calls `scope.rollback()` and rethrows.
36
+ *
37
+ * The callback receives the low-level scope, not the ORM accessor.
38
+ * Use this for multi-store atomic writes that span several object stores.
39
+ */
40
+ declare function withMutationScope<T>(executor: IdbQueryExecutorWithTransaction, storeNames: string[], run: (scope: IdbTransactionScope) => Promise<T>): Promise<T>;
41
+ //#endregion
42
+ //#region src/core/relation-mutator.d.ts
43
+ declare function createRelationMutator<TContract extends IdbContract, ModelName extends string>(): IdbRelationMutator<TContract, ModelName>;
44
+ declare function isRelationMutationDescriptor(value: unknown): value is IdbRelationMutation<IdbContract, string>;
45
+ declare function isRelationMutationCallback(value: unknown): value is (mutator: IdbRelationMutator<IdbContract, string>) => IdbRelationMutation<IdbContract, string>;
46
+ //#endregion
47
+ //#region src/core/mutation-executor.d.ts
48
+ /**
49
+ * Returns true if `data` contains at least one field that is both a known
50
+ * relation name for `modelName` and a function (a mutation callback).
51
+ */
52
+ declare function hasNestedMutationCallbacks(contract: IdbContract, modelName: string, data: Record<string, unknown>): boolean;
53
+ //#endregion
54
+ export { type AggregateFn, type CreateInput, type DefaultModelRow, type GroupedResultRow, type IdbAggregateBuilder, type IdbAggregateResult, type IdbAggregateSelector, type IdbAggregateSpec, type IdbAndExpr, type IdbContract, type IdbFieldAccessor, type IdbFieldFilter, type IdbFilterExpr, type IdbFilterOp, type IdbGroupedAccessor, type IdbIncludeRefinementAccessor, type IdbIncludeScalar, type IdbModelAccessor, type IdbNotExpr, type IdbNullCheckExpr, type IdbOrExpr, type IdbOrmClient, type IdbOrmOptions, type IdbQueryExecutor, type IdbQueryExecutorWithTransaction, type IdbRelationMutation, type IdbRelationMutator, type IdbStoreAccessor, type IncludeFields, type IncludeMarker, type IncludeSpec, type IncludedRow, type KeyType, type ModelKeyPath, type MutationCreateInput, type MutationUpdateInput, type NoIncludes, type NumericFieldNames, type OrderBySpec, type PatchInput, type ReferenceRelKeys, type RelatedModelOf, type RelationMutationConnect, type RelationMutationCreate, type RelationMutationDisconnect, type SelectedRow, type SortDirection, type WhereCallback, type WhereFilter, and, createRelationMutator, hasNestedMutationCallbacks, idbOrm, isRelationMutationCallback, isRelationMutationDescriptor, not, or, withMutationScope };
55
+ //# sourceMappingURL=orm.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"orm.d.mts","names":[],"sources":["../src/core/filters.ts","../src/core/mutation-scope.ts","../src/core/relation-mutator.ts","../src/core/mutation-executor.ts"],"mappings":";;;;;;iBAqBgB,GAAA,IAAO,KAAA,EAAO,aAAA,CAAc,eAAA,IAAiB,YAAA;;iBAK7C,EAAA,IAAM,KAAA,EAAO,aAAA,CAAc,eAAA,IAAiB,WAAA;;iBAK5C,GAAA,CAAI,IAAA,EAAM,eAAA,GAAgB,YAAU;;;;;;;;UCFnC,+BAAA,SAAwC,gBAAA;EDHnC;;;;;AAA+C;AAKrE;;ECOE,WAAA,CAAY,UAAA,YAAsB,IAAA,GAAO,kBAAA,GAAqB,OAAA,CAAQ,mBAAA;AAAA;;;;;ADPpB;;;;ACFpD;;iBAwBsB,iBAAA,IACpB,QAAA,EAAU,+BAAA,EACV,UAAA,YACA,GAAA,GAAM,KAAA,EAAO,mBAAA,KAAwB,OAAA,CAAQ,CAAA,IAC5C,OAAA,CAAQ,CAAA;;;iBC/BK,qBAAA,mBAAwC,WAAA,+BAA0C,kBAAA,CAChG,SAAA,EACA,SAAA;AAAA,iBAsBc,4BAAA,CAA6B,KAAA,YAAiB,KAAA,IAAS,mBAAmB,CAAC,WAAA;AAAA,iBAM3E,0BAAA,CACd,KAAA,YACC,KAAA,KAAU,OAAA,EAAS,kBAAA,CAAmB,WAAA,cAAyB,mBAAA,CAAoB,WAAA;;;;;;;iBCgEtE,0BAAA,CACd,QAAA,EAAU,WAAA,EACV,SAAA,UACA,IAAA,EAAM,MAAM"}
package/dist/orm.mjs ADDED
@@ -0,0 +1,28 @@
1
+ import { a as isRelationMutationDescriptor, i as isRelationMutationCallback, n as hasNestedMutationCallbacks, o as withMutationScope, r as createRelationMutator, t as idbOrm } from "./idb-orm-NHIZ3oNt.mjs";
2
+ import { andExpr, notExpr, orExpr } from "@prisma-next-idb/adapter-idb/runtime";
3
+ //#region src/core/filters.ts
4
+ /**
5
+ * User-facing logical combinators for the IDB ORM.
6
+ *
7
+ * `and(a, b, ...)`, `or(a, b, ...)`, `not(e)` — thin wrappers over the
8
+ * frozen factory helpers in `adapter-idb` that exist so callers don't
9
+ * have to import from the adapter package directly. Mirrors the
10
+ * `where { AND, OR, NOT }` shape Prisma users are already familiar with,
11
+ * exposed as plain functions to keep the AST construction explicit.
12
+ */
13
+ /** Variadic AND. `and()` (no args) is vacuously true. */
14
+ function and(...exprs) {
15
+ return andExpr(exprs);
16
+ }
17
+ /** Variadic OR. `or()` (no args) is vacuously false. */
18
+ function or(...exprs) {
19
+ return orExpr(exprs);
20
+ }
21
+ /** Logical NOT. */
22
+ function not(expr) {
23
+ return notExpr(expr);
24
+ }
25
+ //#endregion
26
+ export { and, createRelationMutator, hasNestedMutationCallbacks, idbOrm, isRelationMutationCallback, isRelationMutationDescriptor, not, or, withMutationScope };
27
+
28
+ //# sourceMappingURL=orm.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"orm.mjs","names":[],"sources":["../src/core/filters.ts"],"sourcesContent":["/**\n * User-facing logical combinators for the IDB ORM.\n *\n * `and(a, b, ...)`, `or(a, b, ...)`, `not(e)` — thin wrappers over the\n * frozen factory helpers in `adapter-idb` that exist so callers don't\n * have to import from the adapter package directly. Mirrors the\n * `where { AND, OR, NOT }` shape Prisma users are already familiar with,\n * exposed as plain functions to keep the AST construction explicit.\n */\n\nimport {\n andExpr,\n notExpr,\n orExpr,\n type IdbAndExpr,\n type IdbFilterExpr,\n type IdbNotExpr,\n type IdbOrExpr,\n} from \"@prisma-next-idb/adapter-idb/runtime\";\n\n/** Variadic AND. `and()` (no args) is vacuously true. */\nexport function and(...exprs: ReadonlyArray<IdbFilterExpr>): IdbAndExpr {\n return andExpr(exprs);\n}\n\n/** Variadic OR. `or()` (no args) is vacuously false. */\nexport function or(...exprs: ReadonlyArray<IdbFilterExpr>): IdbOrExpr {\n return orExpr(exprs);\n}\n\n/** Logical NOT. */\nexport function not(expr: IdbFilterExpr): IdbNotExpr {\n return notExpr(expr);\n}\n"],"mappings":";;;;;;;;;;;;;AAqBA,SAAgB,IAAI,GAAG,OAAiD;CACtE,OAAO,QAAQ,KAAK;AACtB;;AAGA,SAAgB,GAAG,GAAG,OAAgD;CACpE,OAAO,OAAO,KAAK;AACrB;;AAGA,SAAgB,IAAI,MAAiC;CACnD,OAAO,QAAQ,IAAI;AACrB"}
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "@prisma-next-idb/client-idb",
3
+ "version": "0.1.0",
4
+ "description": "Prisma Next IDB family — ORM client (idbOrm typed query builder)",
5
+ "license": "MIT",
6
+ "files": [
7
+ "dist"
8
+ ],
9
+ "keywords": [
10
+ "prisma",
11
+ "prisma-next",
12
+ "indexeddb",
13
+ "offline-first",
14
+ "idb",
15
+ "orm"
16
+ ],
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/prisma-idb/idb-client-generator.git",
20
+ "directory": "packages/prisma-next/client-idb"
21
+ },
22
+ "homepage": "https://prisma-idb.dev/",
23
+ "bugs": {
24
+ "url": "https://github.com/prisma-idb/idb-client-generator/issues"
25
+ },
26
+ "type": "module",
27
+ "exports": {
28
+ "./orm": "./dist/orm.mjs",
29
+ "./client": "./dist/client.mjs",
30
+ "./client-auto": "./dist/client-auto.mjs",
31
+ "./package.json": "./package.json"
32
+ },
33
+ "dependencies": {
34
+ "@prisma-next/contract": "^0.14.0",
35
+ "@prisma-next/framework-components": "^0.14.0",
36
+ "@prisma-next/migration-tools": "^0.14.0",
37
+ "@prisma-next-idb/adapter-idb": "0.1.0",
38
+ "@prisma-next-idb/target-idb": "0.1.0",
39
+ "@prisma-next-idb/driver-idb": "0.1.0",
40
+ "@prisma-next-idb/runtime-idb": "0.1.0"
41
+ },
42
+ "devDependencies": {
43
+ "fake-indexeddb": "^6.2.5",
44
+ "tsdown": "^0.22.3",
45
+ "typescript": "^6.0.3",
46
+ "vitest": "^4.1.9",
47
+ "@prisma-next-idb/family-idb": "0.1.0"
48
+ },
49
+ "scripts": {
50
+ "build": "tsdown",
51
+ "dev": "tsdown --watch",
52
+ "check": "tsc --noEmit",
53
+ "test": "vitest run",
54
+ "lint": "prettier --check . && eslint .",
55
+ "format": "prettier --write ."
56
+ }
57
+ }