Version Requirements
Live Queries are available with the following minimum package versions:modelence >= 0.15.1(requiresStore.watch(), introduced in0.15.1)@modelence/react-query >= 1.2.1
Overview
The live query system consists of three parts:- Server-side
LiveData- Defines how to fetch data and watch for changes ModelenceQueryProvider(orconnectModelenceQueryClient) - Connects Modelence’s live query system to TanStack QuerymodelenceLiveQuery- Creates live query options foruseQuery
Client Setup
1. Provide a connected QueryClient
modelenceLiveQuery needs a TanStack Query QueryClient connected to Modelence’s
live-query layer. The simplest way is to let renderApp handle it: if you don’t
mount your own provider, renderApp automatically wraps your app in
ModelenceQueryProvider, which creates a QueryClient and connects it for you.
QueryClient (for example, to configure defaults or share it
with other libraries), connect it yourself with connectModelenceQueryClient and
mount your own QueryClientProvider. renderApp detects the already-connected
client and will not inject a second provider.
2. Use modelenceLiveQuery in Components
UsemodelenceLiveQuery with TanStack Query’s useQuery hook. It works just like modelenceQuery, but data updates automatically when the server detects changes:
Server Setup
Returning LiveData from Query Handlers
Live query handlers must return aLiveData object, not plain data. LiveData tells Modelence how to fetch the data and how to watch for changes.
LiveData Configuration
LiveData accepts two functions:
Using MongoDB Change Streams
The most common pattern for thewatch function is MongoDB change streams, which notify you when documents in a collection are inserted, updated, or deleted:
MongoDB change streams require a replica set or sharded cluster. If you’re using MongoDB Atlas, this is enabled by default. For local development, you need to configure a replica set.
Complete Example
Here’s a full example of a live todo list:Server
Client
Live Queries vs WebSockets
Live queries are built on top of WebSockets internally, but provide a higher-level abstraction for the common pattern of keeping query data in sync.
Common Pitfalls
Missing QueryClient connection
If you seeModelence: connect a QueryClient before using modelenceLiveQuery(),
you mounted your own QueryClientProvider without connecting it. Either drop your
custom provider and let renderApp inject ModelenceQueryProvider, or connect
your client explicitly:
Returning plain data instead of LiveData
If you seeLive query handler for 'X' must return a LiveData object, your query handler is returning data directly. Wrap it in LiveData:
Forgetting to close change streams
Always return a cleanup function fromwatch to close change streams. Without this, streams accumulate and may exhaust database connections:
Migrating from @modelence/react-query
Sincemodelence@0.15.0, the query helpers live in modelence/client, and
renderApp connects a QueryClient for you. Existing apps using
@modelence/react-query keep working — this migration is optional — but new code
should use the core imports.
1. Swap the import source. The helpers are drop-in identical:
renderApp inject
ModelenceQueryProvider and remove the boilerplate:
QueryClient, replace
new ModelenceQueryClient().connect(queryClient) with
connectModelenceQueryClient(queryClient) (the ModelenceQueryClient class is
deprecated but still exported for compatibility).
API Reference
- modelenceQuery - Standard (non-live) query helper
- modelenceLiveQuery - Live query helper
- ModelenceQueryProvider - Auto-connecting provider
- Store - Database store with
watch()support