[codex] Add MSSQL RQBv2 support#5886
Draft
quesurifn wants to merge 9 commits into
Draft
Conversation
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.
What changed
Adds Relational Queries v2 support for the MSSQL dialect so
db.query.*works withdefineRelations, matching the RQBv2 surface already available in pg/mysql/sqlite/singlestore.Key pieces:
_querypath intact.OUTER APPLYplusFOR JSON PATH/JSON_QUERY.JSON_QUERY, returns empty many-relations as[], and parses top-levelFOR JSONchunks through the shared RQBv2 row mapper.offsetwithoutorderBy: PK columns first, then all exposed columns for views/no-PK sources, and1only when no columns exist.db.$count, rawdb.execute(),withReplicas, and full query-result caching support includingdrizzle({ cache }),$withCache(), cache-aware prepared query execution, and mutation invalidation metadata.uniqueidentifier,xml,money,smallmoney,rowversion,geography, andgeometry..asc()/.desc(),INCLUDE, andWITH (FILLFACTOR = ..., ONLINE = ...)SQL emission.FOR XMLandFOR BROWSEselect modes in the dialect.Root cause
MSSQL still only exposed the legacy
_relations.ts/RQBv1 path, so libraries that require RQBv2, including@pothos/plugin-drizzle, could not use SQL Server. The dialect also needed SQL Server-specific JSON aggregation and lateral-query equivalents instead of the pg/mysql primitives used by other RQBv2 dialects.One live SQL Server issue found during validation: SQL Server rejects
ORDER BYinside derived tables unless paired withTOP,OFFSET, or XML/JSON context. Nested ordered relation subqueries without a limit now emitOFFSET 0 ROWSto preserve order legally.Validation
Passed during the RQBv2/cache/indexed-view work:
pnpm --filter drizzle-orm buildpnpm --filter drizzle-orm test:typespnpm exec vitest run tests/mssql-rqbv2.test.tspnpm --filter integration-tests test:typesMSSQL_CONNECTION_STRING=... pnpm --filter integration-tests exec vitest run tests/mssql/mssql-rqbv2.test.ts --reporter=verbose --silent=false(9 tests)pnpm --filter drizzle-kit test:typesMSSQL_CONNECTION_STRING=... pnpm --filter drizzle-kit exec vitest run tests/mssql/views.test.ts --reporter=verbose(36 tests)$countgit diff --checkpnpm format:check --allow-no-filesandpnpm lint:checkPassed after the latest MSSQL native-type/index/FOR-mode follow-up:
pnpm --filter drizzle-orm test:typesgit diff --checkpnpm oxlint --max-warnings=0 ...over touched ORM/kit filespnpm format:check --allow-no-filesandpnpm lint:checkCurrent local verification blockers after the latest follow-up:
pnpm --filter drizzle-orm buildhangs locally inbun --bun run scripts/build.ts, sodrizzle-orm/distcould not be refreshed in this checkout.drizzle-ormthroughdrizzle-orm/dist,pnpm --filter drizzle-kit test:typescurrently reports stale-package missing exports/methods from before the latest follow-up.drizzle-kitMSSQL tests for the new native type/index snapshots collect, but fail during setup before assertions because the local SQL Server config uses127.0.0.1with TLS SNI and tedious rejects IP-addressservername.Notes
Minimum target remains SQL Server 2016+ / Azure SQL because this relies on
FOR JSON,JSON_QUERY, andOFFSET/FETCH.When a query uses
offsetwithout an explicitorderBy, MSSQL injects a fallbackORDER BYbecause SQL Server requires it. PK-backed tables use aliased primary-key columns for deterministic pagination. Views and no-PK sources fall back to all exposed columns only as a best-effort SQL Server compatibility path; production pagination should still pass an explicitorderBywith the desired business ordering.ONLINE = ONis supported for SQL emission as an index create/rebuild execution option. SQL Server does not expose it as durable index metadata, so pull round trips can preservefill_factorbut cannot prove a historicalONLINEchoice.Fulltext and columnstore indexes are not modeled by the normal MSSQL b-tree index builder in this PR. They are separate SQL Server index families with different required clauses and should get dedicated APIs if maintainers want them.
I did not add a refresh-materialized-view builder for MSSQL. SQL Server does not have PostgreSQL/Cockroach-style materialized views or a
REFRESH MATERIALIZED VIEWstatement. This PR adds SQL Server indexed-view support instead, which is the closest SQL Server-specific analogue.The committed Pothos smoke uses
t.relatedFieldwith an explicit list type because the integration test environment can load mixed ESM/CJS Drizzle module identities, while the plugin's defaultt.relationlist inference usesinstanceof Many. The test still exercises Pothos' Drizzle selection hook resolving an MSSQL RQBv2 relation throughdb.query, and also asserts apostsCountfield backed by the real MSSQLdb.$count.The 1.0.0 beta/RC type surface is moving quickly. This should land upstream rather than being carried as a patched
node_modulesfork.