Skip to content
Source
Sign inSign up
TanStack/db

fix(db): preserve explicit gcTime of 0 on live query collections #1660

Mergedspokodev(GitHub profile, opens in a new tab)merged 2 commits intomain fromrefs/pull/1660/headmerged by KyleAMathews(GitHub profile, opens in a new tab)

The live query config builder built its collection options with gcTime: this.config.gcTime || 5000. A gcTime of 0 disables garbage collection (the core lifecycle treats gcTime <= 0 as "never GC"), but 0 || 5000 evaluates to 5000, so an explicit gcTime: 0 on a live query collection is silently replaced by the 5s default and the collection is garbage collected 5s after its last subscriber unsubscribes.

Use ?? so only undefined falls back to the default; an explicit 0 is forwarded and GC is disabled as documented.

Repro: liveQueryCollectionOptions({ query, gcTime: 0 }).gcTime returns 5000 instead of 0. Added a regression test.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed live query settings so an explicitly set gcTime: 0 is preserved instead of being replaced with the default timeout.
    • Live query behavior now respects disabled garbage collection as intended.
    • Added coverage to verify that gcTime: 0 continues to work correctly.
    1. Review Change Stack

      No actionable comments were generated in the recent review. 🎉

      ℹ️ Recent review info
      ⚙️ Run configuration

      Configuration used: defaults

      Review profile: CHILL

      Plan: Pro

      Run ID: 09101176-28b1-45f3-b1b1-f6284f180fd3

      📥 Commits

      Reviewing files that changed from the base of the PR and between a93abf66b1d6e2a5dcbab1f4686890eec7d41436 and f9be1a63f6b817a87aa4525f6d87f19700d3184b.

      📒 Files selected for processing (3)
      • .changeset/live-query-gctime-zero.md
      • packages/db/src/query/live/collection-config-builder.ts
      • packages/db/tests/query/live-query-collection.test.ts

      📝 Walkthrough

      Walkthrough

      This PR fixes a bug in @tanstack/db's live query collection config where an explicit gcTime: 0 was incorrectly replaced with a 5000ms default due to falsy-value coercion via ||. The fix switches to ?? so only undefined triggers the default.

      Changes

      gcTime Default Fix

      Layer / File(s)Summary
      Nullish-coalescing fix for gcTime default
      packages/db/src/query/live/collection-config-builder.ts
      Changes this.config.gcTime || 5000 to this.config.gcTime ?? 5000 so gcTime: 0 is preserved instead of falling back to the default.
      Test coverage and changeset
      packages/db/tests/query/live-query-collection.test.ts, .changeset/live-query-gctime-zero.md
      Adds a test asserting liveQueryCollectionOptions returns gcTime: 0 when explicitly configured, and adds a changeset documenting the patch.

      Estimated code review effort: 1 (Trivial) | ~3 minutes

      Possibly related PRs

      • TanStack/db#1568: Also touches gcTime plumbing in the live/query collection configuration path.

      Suggested reviewers: kevin-dp

      🚥 Pre-merge checks | ✅ 5
      ✅ Passed checks (5 passed)
      Check nameStatusExplanation
      Title check✅ PassedThe title clearly and concisely describes the main fix: preserving an explicit gcTime of 0 for live query collections.
      Description check✅ PassedThe description explains the bug, fix, and regression test, but it does not follow the repository template sections exactly.
      Docstring Coverage✅ PassedNo functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
      Linked Issues check✅ PassedCheck skipped because no linked issues were found for this pull request.
      Out of Scope Changes check✅ PassedCheck skipped because no linked issues were found for this pull request.
      ✨ Finishing Touches
      🧪 Generate unit tests (beta)
      • Create PR with unit tests

      Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

      ❤️ Share
      Comment `@coderabbitai help` to get the list of available commands.
    2. More templates

      @tanstack/angular-db

      npm i https://pkg.pr.new/@tanstack/angular-db@1660

      @tanstack/browser-db-sqlite-persistence

      npm i https://pkg.pr.new/@tanstack/browser-db-sqlite-persistence@1660

      @tanstack/capacitor-db-sqlite-persistence

      npm i https://pkg.pr.new/@tanstack/capacitor-db-sqlite-persistence@1660

      @tanstack/cloudflare-durable-objects-db-sqlite-persistence

      npm i https://pkg.pr.new/@tanstack/cloudflare-durable-objects-db-sqlite-persistence@1660

      @tanstack/db

      npm i https://pkg.pr.new/@tanstack/db@1660

      @tanstack/db-ivm

      npm i https://pkg.pr.new/@tanstack/db-ivm@1660

      @tanstack/db-sqlite-persistence-core

      npm i https://pkg.pr.new/@tanstack/db-sqlite-persistence-core@1660

      @tanstack/electric-db-collection

      npm i https://pkg.pr.new/@tanstack/electric-db-collection@1660

      @tanstack/electron-db-sqlite-persistence

      npm i https://pkg.pr.new/@tanstack/electron-db-sqlite-persistence@1660

      @tanstack/expo-db-sqlite-persistence

      npm i https://pkg.pr.new/@tanstack/expo-db-sqlite-persistence@1660

      @tanstack/node-db-sqlite-persistence

      npm i https://pkg.pr.new/@tanstack/node-db-sqlite-persistence@1660

      @tanstack/offline-transactions

      npm i https://pkg.pr.new/@tanstack/offline-transactions@1660

      @tanstack/powersync-db-collection

      npm i https://pkg.pr.new/@tanstack/powersync-db-collection@1660

      @tanstack/query-db-collection

      npm i https://pkg.pr.new/@tanstack/query-db-collection@1660

      @tanstack/react-db

      npm i https://pkg.pr.new/@tanstack/react-db@1660

      @tanstack/react-native-db-sqlite-persistence

      npm i https://pkg.pr.new/@tanstack/react-native-db-sqlite-persistence@1660

      @tanstack/rxdb-db-collection

      npm i https://pkg.pr.new/@tanstack/rxdb-db-collection@1660

      @tanstack/solid-db

      npm i https://pkg.pr.new/@tanstack/solid-db@1660

      @tanstack/svelte-db

      npm i https://pkg.pr.new/@tanstack/svelte-db@1660

      @tanstack/tauri-db-sqlite-persistence

      npm i https://pkg.pr.new/@tanstack/tauri-db-sqlite-persistence@1660

      @tanstack/trailbase-db-collection

      npm i https://pkg.pr.new/@tanstack/trailbase-db-collection@1660

      @tanstack/vue-db

      npm i https://pkg.pr.new/@tanstack/vue-db@1660

      commit: 82c4b13

Sign in to join this conversation.