Skip to content
Source
Sign inSign up
TanStack/db

feat(svelte-db): add infinite query binding #1723

Mergedtannerlinsleymerged 3 commits intomain fromrefs/pull/1723/headmerged by KyleAMathews(GitHub profile, opens in a new tab)

tannerlinsleycommented

🎯 Changes

  • Add useLiveInfiniteQuery as a Svelte 5 binding over the shared live-query window controller.
  • Preserve query-function, reactive collection getter, page, pagination-state, and typed pre-created collection APIs from #1447.
  • Cover exact peek-ahead windows, live row movement, dependency resets, pre-created collections, promise completion, and pagination boundaries.
  • Retain Reijhanniel Jearl Campos's contribution through commit co-authorship.

Reworks #1447 without closing it.

✅ Checklist

  • I have tested this code locally with pnpm test.

Validation:

  • pnpm --filter @tanstack/svelte-db test (64 tests)
  • pnpm --filter @tanstack/svelte-db test:types
  • pnpm --filter @tanstack/svelte-db build
  • ESLint and Prettier

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Summary by CodeRabbit

  • New Features

    • Added useLiveInfiniteQuery for reactive infinite queries in Svelte.
    • Supports paginated data, live updates, query state, and fetching additional pages.
    • Works with pre-created live-query collections or query functions.
    • Exposed the new hook through the Svelte DB package.
  • Bug Fixes

    • Added validation and warnings for unsupported pagination configurations.
  • Tests

    • Added coverage for pagination boundaries, live updates, dependency changes, and reactive collections.
    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 Plus

      Run ID: 29ffe0eb-39f9-49e4-b090-1addc0e24524

      📥 Commits

      Reviewing files that changed from the base of the PR and between f1c24cd96daca0b6b66db78684b8a327324076a3 and 6d3395bfd3e551d1394853d7acd74e5c4799089e.

      📒 Files selected for processing (1)
      • packages/svelte-db/src/useLiveInfiniteQuery.svelte.ts
      🚧 Files skipped from review as they are similar to previous changes (1)
      • packages/svelte-db/src/useLiveInfiniteQuery.svelte.ts

      📝 Walkthrough

      Walkthrough

      Changes

      Adds the Svelte useLiveInfiniteQuery binding over the shared live-query window controller. It supports query functions and pre-created ordered collections, exposes reactive pagination state, and includes tests and release metadata.

      Svelte live infinite query

      Layer / File(s)Summary
      API contract and input resolution
      packages/svelte-db/src/useLiveInfiniteQuery.svelte.ts, packages/svelte-db/src/index.ts
      Defines configuration and return types, supports collection and query-function inputs, validates inputs, and exports the binding.
      Controller and reactive state
      packages/svelte-db/src/useLiveInfiniteQuery.svelte.ts
      Creates or validates ordered collections, configures the window controller, subscribes to snapshots, handles cleanup and fetch errors, and exposes reactive query state.
      Behavior validation and release metadata
      packages/svelte-db/tests/useLiveInfiniteQuery.svelte.test.ts, .changeset/spicy-roses-hide.md
      Tests pagination, live updates, dependency changes, reactive collections, warnings, boundary cases, and the package release entry.

      Estimated code review effort: 4 (Complex) | ~45 minutes

      Mergeability Score: ⚪ Minimal · up to 6d339

      This PR adds the Svelte infinite-query binding and associated tests and release metadata; no actionable merge-blocking risk remains beyond normal checks and review.

      Sequence Diagram(s)

      sequenceDiagram
        participant SvelteComponent
        participant useLiveInfiniteQuery
        participant createLiveQueryWindowController
        participant LiveQueryCollection
        SvelteComponent->>useLiveInfiniteQuery: invoke query binding
        useLiveInfiniteQuery->>LiveQueryCollection: resolve or create ordered collection
        useLiveInfiniteQuery->>createLiveQueryWindowController: initialize controller
        createLiveQueryWindowController->>LiveQueryCollection: configure collection window
        createLiveQueryWindowController-->>useLiveInfiniteQuery: publish snapshot
        useLiveInfiniteQuery-->>SvelteComponent: expose reactive query state
        SvelteComponent->>useLiveInfiniteQuery: fetchNextPage()
        useLiveInfiniteQuery->>createLiveQueryWindowController: fetch next page
        createLiveQueryWindowController-->>useLiveInfiniteQuery: publish updated snapshot

      Possibly related PRs

      • TanStack/db#1642: Introduces the shared live-query observer and window-controller infrastructure used by this binding.
      • TanStack/db#1669: Introduces shared reactive snapshot and pagination-window behavior used by the binding.
      • TanStack/db#1675: Introduces the shared createLiveQueryWindowController used by this binding.

      Suggested reviewers: kyleamathews

      🚥 Pre-merge checks | ✅ 4 | ❌ 1

      ❌ Failed checks (1 warning)

      Check nameStatusExplanationResolution
      Docstring Coverage⚠️ WarningDocstring coverage is 0.00% which is insufficient. The required threshold is 80.00%.Write docstrings for the functions missing them to satisfy the coverage threshold.
      ✅ Passed checks (4 passed)
      Check nameStatusExplanation
      Title check✅ PassedThe title clearly identifies the addition of the Svelte DB infinite query binding, which is the main change.
      Description check✅ PassedThe description covers the required changes, checklist, validation results, and release impact, including the generated changeset.
      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 💡 2
      📝 Generate docstrings 💡
      • Create stacked PR
      • Commit on current branch
      🛠️ Fix failing CI checks 💡
      • Create stacked PR
      • Commit on current branch
      🧪 Generate unit tests (beta)
      • Create PR with unit tests
      • Commit unit tests in branch `taren/svelte-infinite-query-controller`

      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. Actionable comments posted: 4

      🤖 Prompt for all review comments with AI agents
      Verify each finding against current code. Fix only still-valid issues, skip the
      rest with a brief reason, keep changes minimal, and validate.
      
      Inline comments:
      In `@packages/svelte-db/src/useLiveInfiniteQuery.svelte.ts`:
      - Around line 141-145: Update the pageSize derivation in useLiveInfiniteQuery so
      only positive integers from config.pageSize are accepted; fractional, zero,
      negative, and otherwise invalid values must use DEFAULT_PAGE_SIZE. Ensure the
      validated integer is the value passed to limit() and the window controller.
      - Around line 117-126: Update useLiveInfiniteQuery so its configuration and
      return types are parameterized by the inferred TResult, TKey, and TUtils instead
      of any. Introduce or reuse collection-specific generic types for
      UseLiveInfiniteQueryConfig and UseLiveInfiniteQueryReturn, preserving those
      inferred types through getNextPageParam, data, pages, and related values.
      - Line 103: Update the fetchNextPage binding in useLiveInfiniteQuery to return
      the promise from LiveQueryWindowController.fetchNextPage instead of discarding
      it, preserving its resolve/reject behavior. Adjust the related tests to await
      fetchNextPage before making assertions.
      
      In `@packages/svelte-db/tests/useLiveInfiniteQuery.svelte.test.ts`:
      - Around line 45-182: Extend the useLiveInfiniteQuery pagination tests with
      empty-collection, single-row, and pageSize: 0 cases, asserting hasNextPage,
      pages, and effective window limits. In the existing multi-page tests, await each
      fetchNextPage() promise before flushSync and subsequent assertions so
      asynchronous completion order is covered.
      🪄 Autofix

      Fix all unresolved CodeRabbit comments on this PR:

      • Push a commit to this branch (recommended)
      • Create a new PR with the fixes

      ℹ️ Review info
      ⚙️ Run configuration

      Configuration used: defaults

      Review profile: CHILL

      Plan: Pro Plus

      Run ID: 492335f2-05fa-4c8b-9453-e15bad8d0f11

      📥 Commits

      Reviewing files that changed from the base of the PR and between dc53f0ecbc38e173af68d829ff2de97531494722 and 35b813dadf6cd033b252ea286c552fcdf94b4d19.

      📒 Files selected for processing (4)
      • .changeset/spicy-roses-hide.md
      • packages/svelte-db/src/index.ts
      • packages/svelte-db/src/useLiveInfiniteQuery.svelte.ts
      • packages/svelte-db/tests/useLiveInfiniteQuery.svelte.test.ts
      OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)
      OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)
      OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)
      OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)
    3. More templates

      @tanstack/angular-db

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

      @tanstack/browser-db-sqlite-persistence

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

      @tanstack/capacitor-db-sqlite-persistence

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

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

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

      @tanstack/db

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

      @tanstack/db-ivm

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

      @tanstack/db-sqlite-persistence-core

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

      @tanstack/electric-db-collection

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

      @tanstack/electron-db-sqlite-persistence

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

      @tanstack/expo-db-sqlite-persistence

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

      @tanstack/node-db-sqlite-persistence

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

      @tanstack/offline-transactions

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

      @tanstack/powersync-db-collection

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

      @tanstack/query-db-collection

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

      @tanstack/react-db

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

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

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

      @tanstack/rxdb-db-collection

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

      @tanstack/solid-db

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

      @tanstack/svelte-db

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

      @tanstack/tauri-db-sqlite-persistence

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

      @tanstack/trailbase-db-collection

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

      @tanstack/vue-db

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

      commit: 6d3395b

    4. Size Change: 0 B

      Total Size: 132 kB

      ℹ️ View Unchanged
      FilenameSize
      packages/db/dist/esm/collection/change-events.js1.44 kB
      packages/db/dist/esm/collection/changes.js1.51 kB
      packages/db/dist/esm/collection/cleanup-queue.js810 B
      packages/db/dist/esm/collection/events.js434 B
      packages/db/dist/esm/collection/index.js3.86 kB
      packages/db/dist/esm/collection/indexes.js1.99 kB
      packages/db/dist/esm/collection/lifecycle.js1.7 kB
      packages/db/dist/esm/collection/mutations.js2.47 kB
      packages/db/dist/esm/collection/state.js5.51 kB
      packages/db/dist/esm/collection/subscription.js3.77 kB
      packages/db/dist/esm/collection/sync.js3.05 kB
      packages/db/dist/esm/collection/transaction-metadata.js144 B
      packages/db/dist/esm/deferred.js207 B
      packages/db/dist/esm/errors.js5.16 kB
      packages/db/dist/esm/event-emitter.js748 B
      packages/db/dist/esm/index.js3.26 kB
      packages/db/dist/esm/indexes/auto-index.js829 B
      packages/db/dist/esm/indexes/base-index.js784 B
      packages/db/dist/esm/indexes/basic-index.js2.17 kB
      packages/db/dist/esm/indexes/btree-index.js2.29 kB
      packages/db/dist/esm/indexes/index-registry.js820 B
      packages/db/dist/esm/indexes/reverse-index.js557 B
      packages/db/dist/esm/live-query-adapter.js318 B
      packages/db/dist/esm/live-query-observer.js2.35 kB
      packages/db/dist/esm/live-query-window-controller.js3.04 kB
      packages/db/dist/esm/local-only.js916 B
      packages/db/dist/esm/local-storage.js2.12 kB
      packages/db/dist/esm/optimistic-action.js359 B
      packages/db/dist/esm/paced-mutations.js496 B
      packages/db/dist/esm/proxy.js3.75 kB
      packages/db/dist/esm/query/builder/functions.js1.47 kB
      packages/db/dist/esm/query/builder/index.js5.84 kB
      packages/db/dist/esm/query/builder/ref-proxy.js1.24 kB
      packages/db/dist/esm/query/compiler/evaluators.js1.89 kB
      packages/db/dist/esm/query/compiler/expressions.js430 B
      packages/db/dist/esm/query/compiler/group-by.js3.56 kB
      packages/db/dist/esm/query/compiler/index.js6.67 kB
      packages/db/dist/esm/query/compiler/joins.js2.5 kB
      packages/db/dist/esm/query/compiler/lazy-targets.js923 B
      packages/db/dist/esm/query/compiler/order-by.js1.74 kB
      packages/db/dist/esm/query/compiler/select.js1.53 kB
      packages/db/dist/esm/query/effect.js4.77 kB
      packages/db/dist/esm/query/expression-helpers.js1.43 kB
      packages/db/dist/esm/query/ir.js1.25 kB
      packages/db/dist/esm/query/live-query-collection.js360 B
      packages/db/dist/esm/query/live/collection-config-builder.js9.32 kB
      packages/db/dist/esm/query/live/collection-registry.js264 B
      packages/db/dist/esm/query/live/collection-subscriber.js1.95 kB
      packages/db/dist/esm/query/live/internal.js145 B
      packages/db/dist/esm/query/live/utils.js1.81 kB
      packages/db/dist/esm/query/optimizer.js2.92 kB
      packages/db/dist/esm/query/predicate-utils.js2.97 kB
      packages/db/dist/esm/query/query-once.js359 B
      packages/db/dist/esm/query/subset-dedupe.js960 B
      packages/db/dist/esm/scheduler.js1.3 kB
      packages/db/dist/esm/SortedMap.js1.3 kB
      packages/db/dist/esm/strategies/debounceStrategy.js247 B
      packages/db/dist/esm/strategies/queueStrategy.js428 B
      packages/db/dist/esm/strategies/throttleStrategy.js246 B
      packages/db/dist/esm/transactions.js3.04 kB
      packages/db/dist/esm/utils.js927 B
      packages/db/dist/esm/utils/array-utils.js273 B
      packages/db/dist/esm/utils/browser-polyfills.js304 B
      packages/db/dist/esm/utils/btree.js5.61 kB
      packages/db/dist/esm/utils/comparison.js1.15 kB
      packages/db/dist/esm/utils/cursor.js457 B
      packages/db/dist/esm/utils/index-optimization.js2.39 kB
      packages/db/dist/esm/utils/type-guards.js157 B
      packages/db/dist/esm/utils/uuid.js449 B
      packages/db/dist/esm/virtual-props.js360 B
      compressed-size-action::db-package-size
    5. Size Change: 0 B

      Total Size: 3.79 kB

      ℹ️ View Unchanged
      FilenameSize
      packages/react-db/dist/esm/index.js249 B
      packages/react-db/dist/esm/useLiveInfiniteQuery.js1.29 kB
      packages/react-db/dist/esm/useLiveQuery.js920 B
      packages/react-db/dist/esm/useLiveQueryEffect.js355 B
      packages/react-db/dist/esm/useLiveSuspenseQuery.js567 B
      packages/react-db/dist/esm/usePacedMutations.js401 B
      compressed-size-action::react-db-package-size
    6. 🧹 Nitpick comments (1)
      packages/svelte-db/src/useLiveInfiniteQuery.svelte.ts (1)

      50-52: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

      Replace the local any placeholders with shared generic bounds.

      Use object, string | number, and UtilsRecord for the internal collection, snapshot, and controller types, or propagate the actual generics. Keep TUtils extends Record<string, any> because the shared Collection and UseLiveQueryReturnWithCollection contracts currently require that bound.

      🤖 Prompt for AI Agents
      Verify each finding against current code. Fix only still-valid issues, skip the
      rest with a brief reason, keep changes minimal, and validate.
      
      In `@packages/svelte-db/src/useLiveInfiniteQuery.svelte.ts` around lines 50 - 52,
      Update hasSetWindow and its related collection, snapshot, and controller type
      annotations to use the shared bounds object, string | number, and UtilsRecord
      instead of local any placeholders, or propagate the actual generics where
      available. Preserve TUtils extends Record<string, any> to satisfy the existing
      Collection and UseLiveQueryReturnWithCollection contracts.

      Source: Coding guidelines

      🤖 Prompt for all review comments with AI agents
      Verify each finding against current code. Fix only still-valid issues, skip the
      rest with a brief reason, keep changes minimal, and validate.
      
      Nitpick comments:
      In `@packages/svelte-db/src/useLiveInfiniteQuery.svelte.ts`:
      - Around line 50-52: Update hasSetWindow and its related collection, snapshot,
      and controller type annotations to use the shared bounds object, string |
      number, and UtilsRecord instead of local any placeholders, or propagate the
      actual generics where available. Preserve TUtils extends Record<string, any> to
      satisfy the existing Collection and UseLiveQueryReturnWithCollection contracts.

      ℹ️ Review info
      ⚙️ Run configuration

      Configuration used: defaults

      Review profile: CHILL

      Plan: Pro Plus

      Run ID: 6520ccca-06b9-41d2-a8de-bc4b4e77c655

      📥 Commits

      Reviewing files that changed from the base of the PR and between 35b813dadf6cd033b252ea286c552fcdf94b4d19 and f1c24cd96daca0b6b66db78684b8a327324076a3.

      📒 Files selected for processing (2)
      • packages/svelte-db/src/useLiveInfiniteQuery.svelte.ts
      • packages/svelte-db/tests/useLiveInfiniteQuery.svelte.test.ts
      🚧 Files skipped from review as they are similar to previous changes (1)
      • packages/svelte-db/tests/useLiveInfiniteQuery.svelte.test.ts
    7. KyleAMathews(GitHub profile, opens in a new tab) approved these changes

Sign in to join this conversation.