Repository navigation
Support declaring columns in fromSql to get a typed query usable as a table (#902) - #912
Merged
IlyaSemenov merged 1 commit intoNov 17, 2026
Conversation
👷 Deploy request for orchid-orm pending review.Visit the deploys page to approve it
|
IlyaSemenov
force-pushed
the
IlyaSemenov/typed-raw-sql-relation-usable-as-a-query-source
branch
from
November 17, 2026 07:13
165d12c to
c46f36d
Compare
IlyaSemenov
force-pushed
the
IlyaSemenov/typed-raw-sql-relation-usable-as-a-query-source
branch
2 times, most recently
from
November 17, 2026 07:28
5708b83 to
93a0960
Compare
This was referenced Nov 17, 2026
IlyaSemenov
force-pushed
the
IlyaSemenov/typed-raw-sql-relation-usable-as-a-query-source
branch
from
November 17, 2026 07:40
93a0960 to
a1fbb0c
Compare
Collaborator
Author
|
I won't claim I fully understand the new code. I checked that it looks reasonable, it's isolated, it's covered by tests, and I had it reviewed multiple times by different AIs. I also measured instantiations, which surfaced a problem: originally I put the new code into The surrounding code is buggy, see #914, #915, #916, and the PR deliberately leaves holes, e.g.:
|
IlyaSemenov
deleted the
IlyaSemenov/typed-raw-sql-relation-usable-as-a-query-source
branch
November 17, 2026 07:49
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.
Summary
Fixes #902.
fromSqlaccepts a column shape, likewithSql, and returns a typed query that works as a table. The keys of the declared shape name the SQL result columns by position:Two fixes along the way:
Motivation
Another follow-up to #904, #905, and #909, for the same ACL optimization. One relation, a
UNION ALLof several sources, backs many access filters across tables asEXISTSandINconditions, some of them correlated with an outer row. Without a typed standalone query, all of it had to be written as raw SQL with hand-picked aliases.Verification
Added database-backed tests for
fromSqlwith a shape: filtering, ordering, and parsing, and use injoin,whereExists(with arguments and with a callback),whereIn, and aselectsub-query.Called on a table, the tests cover a renamed column (
group, a qualifiedget, the default alias) and a decimal column (JSON of a sub-query, also after a laterfrom). Other edge cases are a column withname(), a quoted column name, and an empty shape.@ts-expect-errorcovers an undeclared column andfromSqlafterselect.The shared
whereExiststests for a sub-query with selected aliases expected"as"."one", a column the table doesn't have; they now expect the underlying column.pnpm verifypasses. Tests ran with postgres-js; other adapters and Bun were not checked.TypeScript instantiations,
tsc --noEmit --extendedDiagnosticsin the package directory, the base commit vs. this PR without its test changes:pqb: 2,333,680 → 2,335,888 (+2,208, +0.09%), check time 0.69 s → 0.69 s.orm: 2,439,373 → 2,442,632 (+3,259, +0.13%), check time 0.73–0.80 s → 0.73–0.78 s, within run-to-run noise.The new overload and its result type are evaluated only where
fromSqlis called with a shape.Checklist
pnpm changeset) if this PR includes a feature or bug fix in a published package.