Repository navigation
TML-3544: ORM queries search a weighted full-text index by naming it - #30676
Merged
Merged
Conversation
Functions, BuiltinFunctions, BooleanCodecType, Subquery, createFunctions and ExpressionImpl move from @internal/sql-builder to @internal/sql-relational-core/functions. IndexReference, the table index reference type and the code that builds index references move to @internal/sql-relational-core/index-reference. The SQL builder imports them from there and its public exports are unchanged. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
…indexes Copied from the collection scopes design branch. Status stays Proposed until the slice lands. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
The build regenerates the export maps of the public SQL packages for @internal/sql-relational-core/functions and /index-reference. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
Every ORM callback that builds a condition or an order now receives a second argument { fns, indexes }: where and orderBy on a collection, the filter of first and firstOrThrow, include refinements, fragment bodies and relation filters. fns is the SQL query builder's function surface for the contract; a value it returns has asc() and desc(), and a condition it returns goes to where and to and, or and not. indexes holds the indexes of the model's table, bound to the table reference the query uses, so a relation filter or an include refinement on a self-relation searches the aliased table. The body of a fragment for any model gets fns and no index.
orm() takes the adapter's raw codec inferer for fns.raw, and the Postgres, SQLite and Supabase clients pass it. The ORM test fixture's users table gains a weighted full-text index, users_search, over name and email.
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
Only a condition from fns is an expression to unwrap. A field operation such as fullTextMatches returns an AST node of a kind the where-expression check does not list, and the callback result is now unwrapped only when it is an expression. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
The test names documents_search in where and orderBy on the root collection, in a fragment, in a custom collection method, and in a relation filter and an include refinement on the documents self-relation, which read the table under an alias. It checks the rows, that a title match ranks above a body match, and that EXPLAIN uses the index with sequential scans disabled, with a negative control that restates the fields in one group. The fixture's Document model gains parentId and the parent and children relations. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
…name-an-index Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]> # Conflicts: # docs/architecture docs/ADR-INDEX.md
…n index Post gains an optional body and a weighted full-text index, post_search, over the title (weight A) and the body (weight B), with a migration planned by migration plan. src/orm-client/search-posts.ts names the index in where and orderBy, the repositories test checks that a title match comes before a body match, and the declaration-emit fixtures cover the second callback argument in a fragment and a custom collection method. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
Regenerated with PLANNER_GOLDEN_WRITE=1 after the demo's post gained a body and the post_search index. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
The ORM client README describes the second callback argument. The Postgres query reference leads with naming the index, in the ORM first. ADR 236 says both query lanes hand a query the index. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
The examples match the shipped API: a search method builds its tsquery from a string, and the second argument's type is ModelCallbackTools with OrmFunctions. A type test checks the ADR's fragment inside an include refinement. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
The change is additive: one-parameter callbacks keep compiling, and orm() takes the raw codec inferer as an optional option. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
…ences Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
…nd one type maps a storage column to a scope field
ExpressionImpl moves from @internal/sql-relational-core/functions to /expression, where the other expression types live. StorageColumnScopeField in /expression is the one mapping from a storage column to { codecId, nullable, many }; the SQL query builder's StorageTableToScopeTable and the shared index reference type both use it.
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…del gets no indexes at run time A value from fns is now the operation's own result with asc() and desc() added, so ParadeDB's proximity chain keeps within(). The result of fns.raw also has asc() and desc(). The ORM maps up to four overloads of an operation; a type test in the integration package checks that the ORM's fns keeps the SQL query builder's signature for every operation the Postgres target, pgvector, ParadeDB and PostGIS register, and records that a generic operation keeps its parameters at their constraints. The body of a fragment for any model now receives an indexes with no members at run time, as its type says. One FunctionCondition and Condition type and one converter in filters.ts serve where, and, or, not and relation predicates. The raw codec inferer rides on the model accessor scope instead of a parameter on six functions. ModelCallbackTools, OrmFunctions, OrmFunctionsOf, ModelIndexReferences and the condition types are exported. New tests cover a refused non-full-text index and a refused shared index name through the ORM, the base table's indexes on a collection narrowed by variant, a reusable condition typed with ModelCallbackTools, and the whole SQL of the include-refinement alias. The ORM fixture's users table gains an index that shares its name with a foreign key's backing index. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
… comment describes each search command The declaration-emit fixtures write a condition whose second parameter is ModelCallbackTools from @prisma/orm-postgres/orm-client. test/callback-tools-uses.types.test-d.ts holds ten uses of the second argument, one per site; ADR 270's cost figures use it. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
Moved to test/integration/test/sql-orm-client; it still uses the SQL query builder's fixture and setup. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
ADR 270's cost figures for the client package use it. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
… the limit for generic operations Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
An assertion over the ten collections cost more to check than the uses themselves, which would inflate the measured cost of the feature. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
The paragraph names the two commits, the command, the TypeScript version, the probe files and the source state of each run. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
…erloads in one step The value proxy returns the result's own properties as they are and binds only inherited methods, so a frozen operation result no longer throws when buildAst is read. OrmOperation matches four signatures once and keeps a single signature as one; the parity test covers one to four overloads and records that an operation with five keeps only its last four, as ADR 270 now says. The fragment body's list of chain methods is typed from DeclaredFieldsFragmentCollection, and the relational-core README says ExpressionImpl comes from the expression entry. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
Contributor
@prisma/orm-extension-arktype-json
@prisma/orm-extension-middleware-cache
@prisma/orm-extension-paradedb
@prisma/orm-extension-pgvector
@prisma/orm-extension-postgis
@prisma/orm-extension-supabase
@prisma/orm-family-mongo
@prisma/orm-family-sql
@prisma/orm-framework
@prisma/orm-mongo
@prisma/orm-postgres
@prisma/orm-sqlite
@prisma/orm-target-mongo
@prisma/orm-target-postgres
@prisma/orm-target-sqlite
@prisma/orm-toolchain
commit: |
Contributor
size-limit report 📦
|
…demo's Post has two Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
… values in fns.raw The execution context now carries the adapter's raw codec inferer: createExecutionContext sets it from the execution stack, and ExecutionContext declares it as optional, so hand-built contexts keep compiling and fall back to a refusal. The ORM reads it from the context, so the client the README and the demo build, with db.context, has it. The orm() option and the facades' arguments for it are gone; the option was never released. fns.and and fns.or in the ORM also take ORM conditions such as p.title.ilike(...). The README says how conditions combine in both directions, and that a registered collection class's methods are not typed inside an include refinement, where a fragment applied with with does the job. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
wmadden
approved these changes
Oct 9, 2026
wmadden
enabled auto-merge
October 9, 2026 15:50
…al-core createFunctions moved to @internal/sql-relational-core, but its tests stayed in sql-builder and reached it through the built package, so relational-core's coverage counted none of them. The createFunctions cases move to relational-core/test/functions.test.ts with local column expressions; sql-builder keeps the aggregate cases. New cases cover ne with a null left operand, a boolean literal in and, an unknown function name, and an expression index's empty columns. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
github-merge-queue
Bot
removed this pull request from the merge queue due to failed status checks
Oct 9, 2026
github-merge-queue
Bot
removed this pull request from the merge queue due to failed status checks
Oct 12, 2026
… without a foreign key in the SQL builder fixture The fixture declares its relations without foreign keys on purpose. Its new self-relation Document.parent sorts before Post.author, so contract print refuses it first. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
github-merge-queue
Bot
removed this pull request from the merge queue due to failed status checks
Oct 12, 2026
wmadden
enabled auto-merge
October 12, 2026 12:36
3 of 5 tasks
This was referenced Oct 12, 2026
Closed
opensource-fork-sync Bot
pushed a commit
to cla7aye15I4nd/opensource-prisma__orm
that referenced
this pull request
Oct 16, 2026
… the project files removed (prisma#30739) ```ts // examples/prisma-8-demo/src/orm-client/get-filtered-posts.ts db.Post .with((posts) => (input.userId !== undefined ? posts.where({ userId: input.userId }) : posts)) .with((posts) => (input.before !== undefined ? posts.where(createdBefore(input.before)) : posts)) ``` This closes the collection scopes project. The demo gains its last two examples: a list query whose filters apply only when the request gives them, written with `with`, and `createdBefore`, a row fragment for any model with a `createdAt` timestamp, typed with `CodecField`. The project's spike notes leave main, and its retro lessons land in `drive/retro/README.md`. ## What changes - **Demo:** `ormClientGetFilteredPosts` and the command `repo-list-posts [userId|-] [before|-] [limit]`; `createdBefore` in `src/orm-client/fragments.ts`; an integration test covering each combination of inputs, the exclusive `before` bound and the limit; type tests that `createdBefore` is accepted on `User` and `Post` and refused on `Tag`; README text. - **ADR 259:** two sentences that pointed at the project's records now state the limitation directly, and `CodecField` is described as checking values against the codec's input type, which is what the code does (the ORM client README had the same error). - **Removed:** `projects/collection-scopes/` (three spike notes). Nothing else on main linked to it. - **Retro:** the final retro entry in `drive/retro/README.md`. ## Project Definition of Done | Item | Status | Evidence | | --- | --- | --- | | ADRs 259, 265, 270 Accepted and matching what shipped | Met | All Accepted on main; ADR 272 also Accepted | | Chaining keeps a custom class (TML-3403) | Met | Done; type tests in the ORM client | | A ternary cannot unlock `deleteAll` (TML-3397) | Met | Done; test in the ORM client | | Lock methods keep the class; `fragment` refuses a narrowed receiver (TML-3543) | Met | prisma#30673, Done | | Demo chains custom methods, sorts by a request field, searches by naming the index | Met | On main | | Demo has a conditional list query written with `with` and a shared filter typed with `CodecField` | Met | This PR | | An ORM query that names a weighted full-text index works at every site, title above body, `EXPLAIN` uses the index | Met | prisma#30676 integration tests | | Built packages type chaining, fragments and the callback tools | Met | `examples/prisma-8-demo/test/declaration-emit.test.ts`, through the published facade | | A model with a full-text index offers `using.<index name>(q)` with no application code, at every site, `EXPLAIN` uses the index | Met | prisma#30717 | | A second, test-only contribution adds its own method with no ORM client change | Met, changed form | Under ADR 272 contributions read the whole model, so the proof is two test-only contributions, one from a field's type and one with a name of its own, instead of a test-only index type | | No type arguments in the application; demo type-checking cost within ADR 272 | Met | +1.17% unused, recorded in ADR 272 | ## Deferred work Every open item on the project's deferred list is a Linear ticket: TML-3587, TML-3588, TML-3589, TML-3590, TML-3591, TML-3592, TML-3593, TML-3594, and the existing TML-3449. ## Project files Everything the project wrote was coordination material: specs, plans, reviews, handover and spikes. The lasting decisions are in ADRs 259, 265, 270 and 272 and in `docs/architecture docs/patterns/declaration-merging-registries.md`, all already on main. The project's working files were never merged; they stay on the branch `model-scopes-design` until this PR merges, then the branch is deleted. ## Checks Demo typecheck, lint and tests; root typecheck; `lint:docs`; `lint:deps`; `fixtures:check`; build. Reviewed with `/drive-code-review` (system design and code); every finding is fixed. Agent: daedalus-71 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * The Prisma 8 demo adds a command to list posts, optionally filtered by author and creation time. * Results are ordered from newest to oldest, with a default limit of 10. The command also supports limiting the number of results. * **Documentation** * Added usage examples for the filtered-post command. * Clarified that fragment comparisons use a codec’s input type and distinguished fragments used to filter rows from those used to extend query context. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
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.
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):fnsis 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'sfullTextMatchesandfullTextRank. ORM fields are accepted as arguments. A condition goes towhereand composes withand,orandnot; a value getsasc()anddesc()fororderBy, and keeps its own methods.indexesis 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:
whereandorderBy(including arrays of order callbacks), the filters offirstandfirstOrThrow,thisin 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)) getsfnsand anindexeswith 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
sql-builderinto@internal/sql-relational-core, under the new entry points./functionsand./index-reference, so both query lanes build them the same way.sql-builder's public exports and behaviour are unchanged.callback-tools.tsbuilds the argument;ModelCallbackTools,OrmFunctions,ModelIndexReferences,Conditionand related types are exported so applications can type a reusable condition.OrmOperationkeeps up to four overloads of an operation; a fifth would be dropped, which the parity test and ADR 270 record.fns.rawneeds a codec inferer for interpolated values.ExecutionContextgains an optionalrawCodecInferer, whichcreateExecutionContexttakes from the execution stack's adapter, so every client built from a facade's context, includingorm({ runtime, context: db.context, collections }), has it. A context built by hand without it gets a clear error naming the fix.and,orandnot, andfns.andandfns.or, accept both ORM conditions (p.title.ilike(...)) andfnsconditions.Evidence
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;EXPLAINwith sequential scans disabled shows the index used, and a query that restates different weight groups does not use it.languagepassed 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.fns.rawwith an interpolated value works in a client built withorm({ runtime, context, collections }), tested in the package and through the demo's own client.RUNTIME.ARGUMENT_INVALID) and an index name two indexes share (ORM.ARGUMENT_INVALID).Postgains an optionalbodyand a weightedpost_searchindex (migration20261009T1354_add_post_body_search, made withmigration planand the regen script), and a new ORM search,src/orm-client/search-posts.ts, with a test that a title match comes first.fixtures:check, the lint suite,check:upgrade-coverage, and seven integration files run one at a time. The planner goldens were regenerated withPLANNER_GOLDEN_WRITE=1for the new fixture indexes./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
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.indexes.post_search.matches(q). It needs packages to attach operations to index types, which belongs to collection scopes.indexesandfnsas members of the model accessor. They would collide with fields of the same names.Linear: TML-3544. Design: #30428.
Agent: daedalus-71
Summary by CodeRabbit