Repository navigation
Design: ORM queries use the query builder's functions and a model's indexes (ADR 270) - #30428
Closed
wmadden-electric wants to merge 86 commits into
Closed
wmadden-electric wants to merge 86 commits into
wmadden-electric wants to merge 86 commits into
Conversation
…for weighted full-text search ADR 256 (Proposed) records how an ORM collection offers ways into a model that an index makes possible, such as searching posts across weighted fields. The ORM client derives a scope from each index, the owner of the index kind supplies the operations by extending the ORM's collection base class, and the contract declares nothing about scopes and carries no ORM types. The project spec under projects/collection-scopes states the purpose, non-goals, cross-cutting requirements, done conditions and open questions for delivering it with weighted full-text search as the first use. Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
Contributor
|
Important Draft PR not reviewedDraft PRs are not automatically reviewed by default.
To automatically review draft PRs, update your CodeRabbit configuration: reviews:
auto_review:
drafts: true
Comment |
Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
The Postgres target may reference the ORM client's interface type, and the full-text index has no consumers yet, so its representation changes without a migration path. Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
… by index kind The ADR and spec record the result of the type spike: a registry interface keyed by index kind, no type arguments at client construction, and no scopes placed directly on the collection. The spike findings and probes are added under the project. Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
…xtensions Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
… and its index ADR 256 is renamed to match. The findings of the second type spike are added. Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
…s its own supplier interface Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
… Search Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
…copes that are not searches Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
…names an existing index Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
…ySQL, SQLite and MongoDB Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
…c query operations Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
…index definitions Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
…a subtype of an unfiltered one A query fragment is a function. Row fragments go to where and orderBy; collection fragments are applied with pipe. A conditional inside pipe is sound because the collection type state is a declared property and unknown flags default to boolean, so TypeScript reduces a filtered-or-not union to the unfiltered type. FieldExpression, rowFragment, RowOf and sortField cover the shared-filter, shared-select and request-sort idioms. ADR 257 is rewritten on top of it: a scope is a fragment a package builds from an index definition, made with fulltextSearchScopes<Contract, Model>() and applied with pipe. The project spec and plan match the two ADRs, and the write-ups of the four later spikes are added. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
…and query fragments are functions ADR 258: the type state and row are declared properties of the collection, unknown flags default to boolean, where and orderBy return this intersected with the flag they set, include returns this with the row widened, select returns a plain Collection. A custom class keeps its methods through the chain, and a conditional between a filtered and an unfiltered collection reduces to the unfiltered one, so update, delete and cursor stay refused. ADR 259: a query fragment is a function. where and orderBy take row fragments; a pipe method applies collection fragments; any function body inside pipe is sound by ADR 258. FieldExpression, rowFragment with RowOf, and sortField cover the shared-filter, shared-select and request-sort idioms. The three spike write-ups behind the decisions are included. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
db.orm.scope and db.orm.public.Post.scope, DeclaredField with many for a list, the per-receiver check and its run-time checks, orderByField with a required allowed list, the write refusals, the shared builder interface, and the cost tables in ADR 259. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
Base automatically changed from
collection-chaining-and-fragments-design
to
main
October 6, 2026 08:26
Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
5 tasks done
github-merge-queue Bot
pushed a commit
that referenced
this pull request
Oct 6, 2026
…30619) ## Linked issue [TML-3483](https://linear.app/prisma-company/issue/TML-3483). Related: [TML-3020](https://linear.app/prisma-company/issue/TML-3020) (the eleven older duplicated numbers, not touched here). ## Summary `main` had three files named `ADR 258 - …`, so "see ADR 258" did not say which decision it meant: ```text ADR 258 - List cardinality has independent container and element nullability.md ADR 258 - A model names its storage verbatim, and a rename is an operation.md ADR 258 - A collection keeps its class through the chain.md ``` The same happened to 207, 255 and 259 in the last two weeks. The ADR that merged first keeps the number. Each ADR that merged later moves to a new number: | Was | Now | ADR | | --- | --- | --- | | 255 | 262 | Block specs bind top-level block values | | 207 | 263 | A serverless Postgres connection has the same query interface as a `postgres()` client | | 258 | 264 | A model names its storage verbatim, and a rename is an operation | | 258 | 265 | A collection keeps its class through the chain | | 259 | 266 | The cache middleware passes data to its store, and the store decides how to cache | The new numbers start at 262 because `main` now has ADR 260 and open pull requests already use 256 and 261. The query fragments ADR (259) cited "ADR 260" for the collection-scopes ADR in #30428, which has not merged. ADR 260 on `main` is now the `afterTransaction` stage, so that citation is removed. #30428 can add it back with its final number. Every link and every mention of a moved ADR in docs, package READMEs and project files now uses the new number. Mentions in project research notes that describe the numbering at an earlier point in time are left as they were. `ADR-INDEX.md` gains rows for three ADRs it was missing: 259 (query fragments), 264 (storage names) and 265 (collection class). ## Open pull requests this affects These pull requests change a file that moves here. Git should carry their changes to the new file name, but links they add to the old name need updating: - #30550 and #30613 change ADR 255 (block specs), now ADR 262. - #30428 changes ADR 258 (collection class), now ADR 265. These open pull requests add an ADR whose number is already taken, so they clash again when they merge: - ADR 256: #30535 (language-server file watching). `main` already has ADR 256 (mutation-default generators). - ADR 260: #30428 (collection scopes) and #30550 / #30558 (raw SQL is a value of `sql-expression`). `main` already has ADR 260 (`afterTransaction` stage). ## Testing performed - Checked every Markdown link in the changed files that points to an ADR. All resolve. Two links that were already broken on `main` (ADR 009 and ADR 015) are unrelated and unchanged. - Searched the repository for the old file names and the old numbers. The only remaining matches are the earlier-point-in-time research notes described above. ## Skill update n/a — internal only. ## Upgrade instructions [`upgrade-instructions/pending/adr-numbers-unique/extension/`](upgrade-instructions/pending/adr-numbers-unique/extension/instructions.md) declares `changes: []`. The three package READMEs change only their ADR links, so extension authors have nothing to do. ## Checklist - [x] All commits are signed off (`git commit -s`) per the [DCO](../CONTRIBUTING.md#developer-certificate-of-origin-dco). - [x] I read [CONTRIBUTING.md](../CONTRIBUTING.md) and the change is scoped to one logical concern. - [x] Tests are updated (or `n/a` if the change is doc-only / refactor with no behavioural delta). n/a, docs only. - [x] The PR title is in `TML-NNNN: <sentence-case title>` form. - [x] The **Skill update** section above is filled in. ## Notes for the reviewer Alternatives considered: - **Move whichever ADR has fewer references.** For 255 that would move relation ordering instead of block specs, and no open pull request touches it. Rejected because a rule based on merge order is simple to apply the next time, and an earlier fix (ADR 255 to ADR 256 for mutation-default generators) already moved the later ADR. - **Also renumber the eleven older duplicates (159 to 224).** They have been cited by number for months, and deciding which ADR each bare mention means needs reading each citation. That stays with TML-3020. Agent: etain-65 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Documentation** * Updated ADR references across guides, architecture documentation, and package READMEs to reflect current numbering. * Added ADRs to the architecture index and corrected links covering serverless PostgreSQL connections, block specifications, storage naming, query collections, and cache middleware. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
wmadden-electric
added a commit
that referenced
this pull request
Oct 6, 2026
ADR 259 keeps this branch's text, cites the collection ADR as ADR 265 as main now numbers it, and names #30428 for the scope builder instead of ADR 260, which main now gives to another decision. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
ADR 260 and the collection-scopes spec, plan, slice specs and handover call the method that runs a scope with, not apply. The formal reviews keep the name they reviewed. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
…ey answered, and the scope-or-fragment discussion doc Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
Main's ADR 259 and ADR index win. The branch's copy of the collection-class ADR is removed; main carries it as ADR 265. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
ORM where and orderBy callbacks receive { fns, indexes }. The package-helper
draft (ADR 260 on this branch) is removed; collection scopes built from indexes
are recorded as a later decision. Spec, plan, slice 3 spec, deferred list and
handover brought up to date.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
…he project Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
…x a package understands Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
github-merge-queue Bot
pushed a commit
that referenced
this pull request
Oct 12, 2026
…30676) ```prisma model Post { id Int @id title String body String? @@fullTextIndex([[title], body], name: "post_search") } ``` ```ts const q = websearchToTsquery(input.search); const posts = await db.Post .where((p, { fns, indexes }) => fns.fullTextMatches(indexes.post_search, q)) .orderBy((p, { fns, indexes }) => fns.fullTextRank(indexes.post_search, q).desc()) .limit(20) .all(); ``` An ORM query can now search a weighted full-text index over several fields by naming the index. Until now the ORM could search one field at a time (`p.title.fullTextMatches(q)`), and a weighted search over the title and the body could only be written in the SQL query builder. ## The decision Every ORM callback that builds a condition or an order receives a second argument, `{ fns, indexes }` ([ADR 270](https://github.com/prisma/orm/blob/tml-3544-orm-queries-name-an-index/docs/architecture%20docs/adrs/ADR%20270%20-%20ORM%20queries%20use%20the%20query%20builder's%20functions%20and%20a%20model's%20indexes.md)): - **`fns`** is the SQL query builder's function surface for the contract: the built-in functions (`eq`, `and`, `or`, `raw`, …) and every operation the target and extensions register, such as Postgres's `fullTextMatches` and `fullTextRank`. ORM fields are accepted as arguments. A condition goes to `where` and composes with `and`, `or` and `not`; a value gets `asc()` and `desc()` for `orderBy`, and keeps its own methods. - **`indexes`** is the model's table's indexes, keyed by the name the schema gave each. Each is an index reference: the index's columns bound to the table as the query reads it, its type and its options. The Postgres search operations read the weights and language from it, so the query renders the same search document the index was built over, and Postgres uses the index. The second argument reaches every site: `where` and `orderBy` (including arrays of order callbacks), the filters of `first` and `firstOrThrow`, `this` in a custom collection class, fragments for one model, include refinements and relation filters (`some`, `every`, `none`, `count`). Inside an include refinement or a relation filter, an index's columns are bound to the related table's alias. The body of a fragment for any model (`db.orm.fragment(fields, body)`) gets `fns` and an `indexes` with no members, because it does not know its model. Existing callbacks that take one argument keep working, and no upgrade action is needed. ## How it is built - **Shared code.** The function surface and the index reference move from `sql-builder` into `@internal/sql-relational-core`, under the new entry points `./functions` and `./index-reference`, so both query lanes build them the same way. `sql-builder`'s public exports and behaviour are unchanged. - **ORM client.** A new `callback-tools.ts` builds the argument; `ModelCallbackTools`, `OrmFunctions`, `ModelIndexReferences`, `Condition` and related types are exported so applications can type a reusable condition. `OrmOperation` keeps up to four overloads of an operation; a fifth would be dropped, which the parity test and ADR 270 record. - **Raw SQL.** `fns.raw` needs a codec inferer for interpolated values. `ExecutionContext` gains an optional `rawCodecInferer`, which `createExecutionContext` takes from the execution stack's adapter, so every client built from a facade's context, including `orm({ runtime, context: db.context, collections })`, has it. A context built by hand without it gets a clear error naming the fix. - **Conditions combine both ways.** The package's `and`, `or` and `not`, and `fns.and` and `fns.or`, accept both ORM conditions (`p.title.ilike(...)`) and `fns` conditions. ## Evidence - **Integration test** (`test/integration/test/sql-orm-client/weighted-full-text-index-usage.test.ts`, against Postgres): whole-shape results through a root collection, a fragment, a custom collection method, and a relation filter with an include in the same statement; a title match ranks above a body match; `EXPLAIN` with sequential scans disabled shows the index used, and a query that restates different weight groups does not use it. - **Type tests** at every site, including compile errors for a misspelled index, a non-full-text index, and `language` passed with an index. A parity test checks that every operation registered by Postgres, pgvector, ParadeDB and PostGIS has the same signature in the ORM as in the SQL query builder. - **Clients with custom collection classes**: `fns.raw` with an interpolated value works in a client built with `orm({ runtime, context, collections })`, tested in the package and through the demo's own client. - **Run-time refusals through the ORM**: a non-full-text index (`RUNTIME.ARGUMENT_INVALID`) and an index name two indexes share (`ORM.ARGUMENT_INVALID`). - **Demo.** `Post` gains an optional `body` and a weighted `post_search` index (migration `20261009T1354_add_post_body_search`, made with `migration plan` and the regen script), and a new ORM search, `src/orm-client/search-posts.ts`, with a test that a title match comes first. - **Type-checking cost** (TypeScript 5.9.3, recorded in ADR 270 with the commits and command): the demo is 0.6% cheaper when unused, and ten uses add 0.17%. - **Checks:** build, root typecheck, typecheck, lint, tests and coverage of every touched package, demo typecheck, lint and tests (including declaration emit), `fixtures:check`, the lint suite, `check:upgrade-coverage`, and seven integration files run one at a time. The planner goldens were regenerated with `PLANNER_GOLDEN_WRITE=1` for the new fixture indexes. - Reviewed with `/drive-code-review` (system design and code) and a verification pass; every finding is fixed. ## Later decision The ORM offering each search index as a scope on the model's collection, `db.Post.scopes.search.fulltext(q).where(...)`, so the application writes no query. It builds on this change and is recorded in ADR 270 under "Later decision". ## Alternatives considered - **A helper exported by the Postgres package**, `fulltextSearchScopes(db.Post).post_search(q)`. It needs a generic builder in the ORM and one helper per package; naming the index in an ordinary query needs neither. - **Methods on the index reference**, `indexes.post_search.matches(q)`. It needs packages to attach operations to index types, which belongs to collection scopes. - **Restating the weight groups in the query.** Any difference from the index silently turns the search into a sequential scan. - **`indexes` and `fns` as members of the model accessor.** They would collide with fields of the same names. Linear: [TML-3544](https://linear.app/prisma-company/issue/TML-3544). Design: #30428. Agent: daedalus-71 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * ORM query callbacks now provide SQL functions and model index references for filtering, sorting, relation queries, and reusable fragments, including full-text matching and ranking. * Added a Prisma demo of weighted full-text search across post titles and bodies, ranked by relevance. * Relational query functions and index references are available through public package exports. * **Documentation** * Expanded guidance and examples for using named full-text indexes through the ORM and SQL builder. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
…om a model Replaces the index-only design rejected on 2026-10-13. A contribution reads one whole model and names its own methods; types load through the package's own import, never contract.d.ts; name clashes are compile errors. Slice 5 spec, project spec and plan updated. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
…heir methods Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
Grounding example with the SQL it runs, a short decision, a narrative that builds from the problem to the contribution, naming, using, type loading, clashes and order; no references to tickets, spikes or earlier versions. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
Contributor
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This draft carries the design for the last slice of the collection scopes project, ADR 270, and the project's spec and plan. It is not meant to merge: ADR 270 lands with the slice that implements it (TML-3544), and the project folder is removed at close-out.
The decision
An ORM query can search a weighted full-text index by naming it, as a SQL query builder query already can. Every ORM callback that builds a condition or an order (
where,orderBy, relation filters, fragment bodies, include refinements) receives a second argument{ fns, indexes }:fnsis the SQL query builder's function surface for the contract: the built-in functions and every registered query operation, such as Postgres'sfullTextMatchesandfullTextRank.indexesis the model's table's indexes, keyed by the name the schema gave each, as index references bound to the table as the query reads it. The query reads the index's weights and language from the contract, so it cannot drift from the index, and Postgres uses it.Packages need no new mechanism: the Postgres operations already accept an index reference, and the ORM's execution context already holds the operation registry
fnsis built from.Where to read
projects/collection-scopes/spec.md; plan:projects/collection-scopes/plan.md; slice spec:projects/collection-scopes/slices/4-orm-queries-name-an-index/spec.md.Later decision
Collection scopes built from indexes,
db.Post.scopes.search.fulltext(q).where(...), where the ORM offers each search index as a scope and the application writes no query. It was designed and spiked in September 2026 and is recorded in ADR 270 under "Later decision".Alternatives considered
fulltextSearchScopes(db.Post).post_search(q): this draft's earlier proposal. It needs a generic builder in the ORM and one helper per package; naming the index in an ordinary query does the same with neither.indexes.post_search.matches(q): needs packages to attach operations to index types, which belongs to collection scopes.Agent: daedalus-71