Skip to content

Store

The Store is the most common way to interact with LiveStore from your application code. It provides a way to query data, commit events, and subscribe to data changes.

For how to create a store in React, see the React integration docs. The following example shows how to create a store manually:

import { createStorePromise } from '@livestore/livestore'
import { schema } from './livestore/schema.js'
const adapter = // ...
const store = await createStorePromise({
schema,
adapter,
storeId: 'some-store-id',
})
const todos = store.query(tables.todos)
const unsubscribe = store.subscribe(tables.todos, (todos) => {
console.log(todos)
})
store.commit(events.todoCreated({ id: '1', text: 'Buy milk' }))
await store.shutdown()

You can create and use multiple stores in the same app. This can be useful when breaking up your data model into smaller pieces.

A store instance also exposes a _dev property that contains some helpful methods for development. For convenience you can access a store on globalThis/window like via __debugLiveStore.default._dev (default is the store id):

// Download the SQLite database
__debugLiveStore.default._dev.downloadDb()
// Download the eventlog database
__debugLiveStore.default._dev.downloadEventlogDb()
// Reset the store
__debugLiveStore.default._dev.hardReset()
// See the current sync state
__debugLiveStore.default._dev.syncStates()