Skip to content

Store

Defined in: packages/@livestore/livestore/src/store/store.ts:72

  • Class

TSchema extends LiveStoreSchema = Any

TContext = { }

new Store<TSchema, TContext>(__namedParameters): Store<TSchema, TContext>

Defined in: packages/@livestore/livestore/src/store/store.ts:122

StoreOptions<TSchema, TContext>

Store<TSchema, TContext>

Inspectable.Class.constructor

readonly __eventSchema: ForEventDefRecord<TSchema["_EventDefMapType"]>

Defined in: packages/@livestore/livestore/src/store/store.ts:116


activeQueries: ReferenceCountedSet<LiveQuery<any>>

Defined in: packages/@livestore/livestore/src/store/store.ts:113

RC-based set to see which queries are currently subscribed to


readonly boot: Effect<void, UnexpectedError, Scope>

Defined in: packages/@livestore/livestore/src/store/store.ts:119


clientSession: ClientSession

Defined in: packages/@livestore/livestore/src/store/store.ts:76


commit: {<TCommitArg>(…list): void; (txn): void; <TCommitArg>(options, …list): void; (options, txn): void; }

Defined in: packages/@livestore/livestore/src/store/store.ts:644

Commit a list of events to the store which will immediately update the local database and sync the events across other clients (similar to a git commit).

<TCommitArg>(…list): void

TCommitArg extends readonly PartialForSchema<TSchema>[]

TCommitArg

void

(txn): void

<TCommitArg>(…list) => void

void

<TCommitArg>(options, …list): void

TCommitArg extends readonly PartialForSchema<TSchema>[]

StoreCommitOptions

TCommitArg

void

(options, txn): void

StoreCommitOptions

<TCommitArg>(…list) => void

void

store.commit(events.todoCreated({ id: nanoid(), text: 'Make coffee' }))

You can call commit with multiple events to apply them in a single database transaction.

const todoId = nanoid()
store.commit(
events.todoCreated({ id: todoId, text: 'Make coffee' }),
events.todoCompleted({ id: todoId }))

For more advanced transaction scenarios, you can pass a synchronous function to commit which will receive a callback to which you can pass multiple events to be committed in the same database transaction. Under the hood this will simply collect all events and apply them in a single database transaction.

store.commit((commit) => {
const todoId = nanoid()
if (Math.random() > 0.5) {
commit(events.todoCreated({ id: todoId, text: 'Make coffee' }))
} else {
commit(events.todoCompleted({ id: todoId }))
}
})

When committing a large batch of events, you can also skip the database refresh to improve performance and call store.manualRefresh() after all events have been committed.

const todos = [
{ id: nanoid(), text: 'Make coffee' },
{ id: nanoid(), text: 'Buy groceries' },
// ... 1000 more todos
]
for (const todo of todos) {
store.commit({ skipRefresh: true }, events.todoCreated({ id: todo.id, text: todo.text }))
}
store.manualRefresh()

context: TContext

Defined in: packages/@livestore/livestore/src/store/store.ts:78


readonly networkStatus: Subscribable<{ devtools: { latchClosed: boolean; }; isConnected: boolean; timestampMs: number; }, never, never>

Defined in: packages/@livestore/livestore/src/store/store.ts:97

Reactive connectivity updates emitted by the backing sync backend.

import { Effect, Stream } from 'effect'
const status = await store.networkStatus.pipe(Effect.runPromise)
await store.networkStatus.changes.pipe(
Stream.tap((next) => console.log('network status update', next)),
Stream.runDrain,
Effect.scoped,
Effect.runPromise,
)

otel: StoreOtel

Defined in: packages/@livestore/livestore/src/store/store.ts:79


reactivityGraph: ReactivityGraph

Defined in: packages/@livestore/livestore/src/store/store.ts:74


schema: LiveStoreSchema

Defined in: packages/@livestore/livestore/src/store/store.ts:77


sqliteDbWrapper: SqliteDbWrapper

Defined in: packages/@livestore/livestore/src/store/store.ts:75


readonly storeId: string

Defined in: packages/@livestore/livestore/src/store/store.ts:73


subscribe: SubscribeFn

Defined in: packages/@livestore/livestore/src/store/store.ts:372

Subscribe to the results of a query.

  • When providing an onUpdate callback it returns an Unsubscribe function.
  • Without a callback it returns an AsyncIterable that yields query results.
const unsubscribe = store.subscribe(query$, (result) => console.log(result))
for await (const result of store.subscribe(query$)) {
console.log(result)
}

readonly syncProcessor: ClientSessionSyncProcessor

Defined in: packages/@livestore/livestore/src/store/store.ts:117


tableRefs: object

Defined in: packages/@livestore/livestore/src/store/store.ts:102

Note we’re using Ref<null> here as we don’t care about the value but only about that something has changed. This only works in combination with equal: () => false which will always trigger a refresh.

[key: string]: Ref<null, ReactivityGraphContext, RefreshReason>

get clientId(): string

Defined in: packages/@livestore/livestore/src/store/store.ts:341

string


get sessionId(): string

Defined in: packages/@livestore/livestore/src/store/store.ts:337

string

events(_options?): AsyncIterable<ForSchema<TSchema>>

Defined in: packages/@livestore/livestore/src/store/store.ts:758

Returns an async iterable of events.

StoreEventsOptions<TSchema>

AsyncIterable<ForSchema<TSchema>>

for await (const event of store.events()) {
console.log(event)
}
// Get all events from the beginning of time
for await (const event of store.events({ cursor: EventSequenceNumber.ROOT })) {
console.log(event)
}

eventsStream(_options?): Stream<ForSchema<TSchema>>

Defined in: packages/@livestore/livestore/src/store/store.ts:764

StoreEventsOptions<TSchema>

Stream<ForSchema<TSchema>>


manualRefresh(options?): void

Defined in: packages/@livestore/livestore/src/store/store.ts:774

This can be used in combination with skipRefresh when committing events. We might need a better solution for this. Let’s see.

string

void


query<TResult>(query, options?): TResult

Defined in: packages/@livestore/livestore/src/store/store.ts:494

Synchronously queries the database without creating a LiveQuery. This is useful for queries that don’t need to be reactive.

Example: Query builder

const completedTodos = store.query(tables.todo.where({ complete: true }))

Example: Raw SQL query

const completedTodos = store.query({ query: 'SELECT * FROM todo WHERE complete = 1', bindValues: {} })

TResult

Queryable<TResult> | { bindValues: Bindable; query: string; schema?: Schema<TResult, TResult, never>; }

RefreshReason

Context

TResult


setSignal<T>(signalDef, value): void

Defined in: packages/@livestore/livestore/src/store/store.ts:575

Set the value of a signal

T

SignalDef<T>

T | (prev) => T

void

const count$ = signal(0, { label: 'count$' })
store.setSignal(count$, 2)
const count$ = signal(0, { label: 'count$' })
store.setSignal(count$, (prev) => prev + 1)

shutdown(cause?): Effect<void>

Defined in: packages/@livestore/livestore/src/store/store.ts:807

Shuts down the store and closes the client session.

This is called automatically when the store was created using the React or Effect API.

Cause<UnexpectedError | MaterializeError>

Effect<void>


shutdownPromise(cause?): Promise<void>

Defined in: packages/@livestore/livestore/src/store/store.ts:795

Shuts down the store and closes the client session.

This is called automatically when the store was created using the React or Effect API.

UnexpectedError

Promise<void>


subscribeStream<TResult>(query, options?): Stream<TResult>

Defined in: packages/@livestore/livestore/src/store/store.ts:460

TResult

Queryable<TResult>

SubscribeOptions<TResult>

Stream<TResult>


toJSON(): object

Defined in: packages/@livestore/livestore/src/store/store.ts:884

object

_tag: string = 'livestore.Store'

reactivityGraph: ReactiveGraphSnapshot

Inspectable.Class.toJSON