@tanstack/preact-db#
1.0.0-rc.1#
Major Changes#
Release the first 1.0 candidate. Remove deprecated pre-1.0 APIs and give (#2171) published row metadata precedence over colliding selected fields. Structured source spreads and direct source-row returns keep source attribution; new functional objects and other projections receive output Collection metadata. Keep the two packages accidentally released as stable 1.x on their stable release lines. Both stable minor releases require
@tanstack/db@^1.0.0-rc.0for the RC health and restoration settlement APIs; upgrade DB with them.Remove Query Collection’s automatic refetch after mutation handlers. Handlers return
Promise<void>and must awaitcollection.utils.refetch()or write confirmed data with thewrite*utilities;{ refetch: false }is removed.
Minor Changes#
Add
@tanstack/preact-db, a Preact adapter withuseLiveQueryanduseLiveInfiniteQuery. Both compare query identity derived from the query IR on each render, require aqueryKeyfor functional query variants, and pass the shared live-query and infinite-query conformance suites. Queries read concrete source Collections;DbProviderand collection descriptors are not supported yet. (#2235)Collections now answer two questions separately: can the data be shown (
status), and is it still kept up to date (health). Once a query has shown data, a network or backend failure never takes it off the screen. (#2168)statusgainscrashed: local storage broke (a closed IndexedDB connection, a failed SQLite persistence write or restore, a throwing sync function, or an invariant failure). Nothing recovers it in place, andreload()rejects. To recover, callcleanup({ dependents: true }), then start the Collection again (for example withpreload()); it starts with no rows. It reaches every live query and Effect above it.errornow means only "the first load failed".- Every Collection has
collection.health, a health node withstatus,failureReason(the adapter is still trying),error(the adapter stopped, or the first load failed),reloading, andhasErrors. A live query'shealthhas one node per source alias, in query order, and keeps object identity while unchanged.health:changefires when it changes.findProblems(health)lists the nodes that have a problem, in query order. Each problem (HealthProblem) shows its newest failure inmessage,cause, andat, and lists every failure still open inerrors, oldest first: eachreportError()of the sync run untilreportHealthy(), or each failed request for more data on a live query's node. One error value is one failure: reporting it again changes nothing, andreportHealthy(error)removes just that failure, so a query Collection whose failing Query recovers removes only that Query's failures. After a live query is ready, a failed request for more data (a page, a joined row, or a source reload's replay of its data) is anerroron the query's own node; the source stays healthy. collection.reload()starts a new sync run and keeps the rows, status, and error on screen until the reload completes: when the new run's snapshot (begin({ snapshot: true })) ortruncate()applies, or at itsmarkReady(), which keeps the rows when no snapshot came. On a live query it reloads every Collection below it that shows anerror, through nested live queries, then the query itself when its own node has anerror, including a failed request for more data. It rejects on acrashedCollection. When the cleanup of the sync run that a reload retired rejects, the Collection crashes, also if the rejection comes later. A query inerrorstays there until itsreload().cleanup()rejects withCollectionHasSubscribersErrorwhile a live query with subscribers or an Effect reads the Collection.cleanup({ dependents: true })ends them first.collection.subscriberslists every dependent.- A local-only Collection's
reload()keeps its rows, including the user's writes:initialDataloads only when the Collection starts, or starts again after cleanup. - Persisted Collections have one status. With
initialRender: { strategy: 'network-first' }, restored rows make the Collectionreadyonce the deadline passes or the remote fails; an empty restore staysloading. Only a local storage failure crashes a persisted Collection: a failed remote subset request is a failure reason, which the next successful remote request clears.
Breaking changes and migration:
Before After hook isErrorisLoadingError(first load failed) orisCrashed— isSyncError:readyand the health tree has a problemstatus: 'error'after a crashstatus: 'crashed'adapter markError(error)fail(error). After readiness it sets the node'serrorand keeps the rows. Later calls throw.cleanup()thenpreload()to recoverreload()adapter begin({ direct: true })begin(): every write applies on the rows the Collection holds, also during a reloada reload's first commit replaced every row begin({ snapshot: true })ortruncate()in the new run replaces themadapter markReady()afterfail()threwit does nothing cleanup()while live queries or Effects read the Collectionrejects; unmount first or pass { dependents: true }persistedStatus,isPersistedReady,persistedError,preloadForInitialRender(),PERSISTED_READINESSstatusandhealthquery-db utils.lastError,utils.isError,utils.errorCount,utils.clearError()collection.health,reload()infinite-query hooks' error(React, Preact, Vue, Svelte)the root node's errorinhealth;fetchNextPage()still resolvesCustom sync adapters: keep the run's readiness in your
syncfunction. A reload callssyncagain whilecollection.statusstill shows the previous run's status and the Collection still holds the previous run's rows. The new run's writes apply on those rows; it replaces every row only with a snapshot, a transaction begun withbegin({ snapshot: true }), or with its owntruncate(). The reload completes when that replacement applies, or at the new run'smarkReady(), which keeps the rows when no snapshot came. So send the new run's full state as a snapshot, or write against the kept rows: aninsertof a key the Collection holds throwsDuplicateKeySyncError, during a reload as at any time, so writeupdatefor a kept row, also from a replayed load after the reload completed. A reload keeps every row its new run does not rewrite: core never deletes kept rows on its own, so removing a row the backend deleted is the adapter's job, through a snapshot, adelete, or row ownership (#2269). An on-demandloadSubsetresult is partial and is never a snapshot. A live query's own reload keeps its rows and its window until its new run's result is complete.reportError()andreportHealthy()throw before the firstmarkReady()and afterfail().markReady()after the run is ready, or after itsfail(), does nothing, so an adapter needs no flag of its own to guard it. A secondfail()in the same sync run does nothing. A first-ready callback that throws whilesyncruns is reported uncaught, and the Collection stays ready.reload()called from insidesyncor aloadSubsetcall rejects, unless a reload is already in flight, whose promise it returns. Calls after a crash do nothing. For an on-demand Collection, a reload aborts the retired run'sloadSubsetcalls and asks the new run'sloadSubsetfor the current demand as soon assyncreturns. Honor thesignal: when core aborts a load, that load is over for core, and its later resolve or reject decides nothing, holds no readiness, and no replay waits for it. AloadSubsetthat throws synchronously is a rejection: a failed load, whichrequestSnapshot()andsetWindow()report without throwing. A truncate's or reload's replay decides every request error of the query's demand: its success clears them all, and its failure replaces them with the replay's failures.useLiveSuspenseQuerynow throws an Error for a failed first load or a crash: the cause itself when it is an Error, otherwise an Error with the problem's message whosecauseholds the reported value.
Patch Changes#
Breaking: grouping, includes, and pooling are now opt-in. An app bundles an operator's code only when it imports the value for that operator. Joins do not change. A typical app is about 8 kB smaller after gzip. This app has a query collection, a filtered live query, and React
useLiveQuery. (#2232)- An include needs
include(),toArray(), ormaterialize(). Changeissues: q.from(...)inselect()toissues: include(q.from(...)). A subquery inselect()without one of these throwsIncludeOperatorRequiredError. - Aggregates such as
count()need no change. AgroupBywith no aggregate takesgroupKeys():.groupBy(groupKeys(({ todo }) => todo.status)). Without it, the query throwsGroupingOperatorRequiredError. - Hooks do not pool live queries by default. To share one partition among many equality-filtered queries, pass
pool: pooledLiveQueriesin the hook's query config:useLiveQuery({ query, pool: pooledLiveQueries }). Results do not change. Angular's{ params, query }form takespoolthe same way. Any otherpoolvalue, such as the old boolean, throwsInvalidPoolOptionError, also for a disabled query. For adapter authors: thepooloption ofresolveLiveQueryValueis nowpooledLiveQueriesorfalse, not a boolean. An adapter that readspoolfrom its own options checks it withconfigPoolfirst. - Code that builds query IR by hand must supply the operator code.
IR.AggregatetakesgroupingCompileras its third argument.IR.IncludesSubquerytakesincludesRuntimeas its fifth argument, beforeparentFilters. Import both from@tanstack/db. IR without them throwsGroupingOperatorRequiredErrororIncludeOperatorRequiredErrorwhen it compiles, unless another aggregate orgroupKeys()in the query supplies the grouping compiler. Queries that the query builder builds need no change. - Operator code is part of query identity. A hook that re-renders a grouped query without
groupKeys()now throwsGroupingOperatorRequiredError. Before, React and Preact kept the grouped query. - Vue: when a changed query fails to compile,
useLiveQueryno longer shows the rows of the previous query.
- An include needs
Updated dependencies [
5649a07,43d4ef6,7538afd,a07cd73,6c1ca8f,30175be,4c39add,1640de8,367ea7c,7d9ed02,45eeef8]:- @tanstack/db@1.0.0-rc.1