Repository navigation
Conversation
The expo-sqlite useLiveQuery hook only re-ran its query when the main table changed, so views built on a join or a relational `with` were silently stale until the main table was touched. Added SQLiteRelationalQuery#getReferencedTables and reuse extractUsedTable so the hook now subscribes to every table the query actually reads. Closes drizzle-team#2660.
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 #2660.
useLiveQueryonexpo-sqliteonly re-ran a query when the root table changed. If the query joined other tables — either throughinnerJoin/leftJoinon a plain select or through nestedwithondb.query.*— mutations to those joined tables did not retrigger the hook and the UI showed stale data.That has bitten enough users that the thread already contains two working community prototypes (@doughsay 2024-10, @jsteiner 2024-11). This PR lands the same idea upstream, using existing drizzle-orm primitives, so no copy-pasting into user codebases.
What changed
drizzle-orm/src/sqlite-core/query-builders/query.ts— new/** @internal */ getReferencedTables()method onSQLiteRelationalQuery. It starts from the root table name (Table.Symbol.BaseName) and walks thewithtree, resolving each enabled relation to its referenced table via the schema's relational config. The walk is cycle-guarded (visited: Set<TableRelationalConfig>) so a self-referencing relation can't loop.drizzle-orm/src/expo-sqlite/query.ts— theuseLiveQueryhook now builds aSet<string>of listening tables:SQLiteRelationalQuery(thedb.query.*API) it folds inquery.getReferencedTables().AnySQLiteSelect(the SQL-like builder) it iteratesconfig.joinsand reuses the existing publicextractUsedTablefrom~/sqlite-core/utils.ts.addDatabaseChangeListenercallback usestableSet.has(tableName)instead of a single equality check.Tests
Added
drizzle-orm/tests/sqlite/use-live-query-tables.test.ts, five cases built on thesqlite-proxydriver (no native deps, so it runs anywhere):The existing 507 unit tests in the sibling directories still pass locally. The two files that fail locally (
casing/sqlite-to-camel.test.ts,casing/sqlite-to-snake.test.ts) do so becausebetter-sqlite3's native binary doesn't build on my toolchain; both are untouched by this PR.Rationale notes
Table.Symbol.BaseNamewas chosen (overTable.Symbol.Name) because that is whatextractUsedTablealready uses and whatexpo-sqlite'saddDatabaseChangeListeneremits — avoids an alias mismatch.withis handled, not just top-level — the community prototypes only did one level.SQLiteSelectConfig.joinsis already/** @internal */ public, so no visibility change is needed on the SQL-like path.expo-sqlitedriver is outside the kit rewrite, so there's no risk of this PR colliding with that work.Changelog
Added
changelogs/drizzle-orm/0.45.5.md.Checklist
ed25519).node_modules/vitest/dist/cli.js run tests/sqlite/use-live-query-tables.test.ts).Credit to @doughsay for the first community diagnosis and to @jsteiner for the two-pass prototype that proved both the SQL-like-join and relational-
withpaths. This PR is the same shape, lifted ontogetReferencedTables+extractUsedTableso it only touches drizzle-orm, not every user's codebase.🤖 Used Claude Code for research + review; all code and reasoning signed off by me on content.