Skip to content

Store

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

  • Class

TSchema extends LiveStoreSchema = LiveStoreSchema

TContext = { }

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

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

StoreOptions<TSchema, TContext>

Store<TSchema, TContext>

Inspectable.Class.constructor

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

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


activeQueries: ReferenceCountedSet<LiveQuery<any>>

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

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:83


clientSession: ClientSession

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


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

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

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:63


otel: StoreOtel

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


reactivityGraph: ReactivityGraph

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


schema: LiveStoreSchema

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


sqliteDbWrapper: SqliteDbWrapper

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


readonly storeId: string

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


readonly syncProcessor: ClientSessionSyncProcessor

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


tableRefs: object

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

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:253

string


get sessionId(): string

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

string

[NodeInspectSymbol](): unknown

Defined in: node_modules/.pnpm/effect@3.15.2/node_modules/effect/dist/dts/Inspectable.d.ts:47

unknown

2.0.0

Inspectable.Class.[NodeInspectSymbol]


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

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

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:633

StoreEventsOptions<TSchema>

Stream<ForSchema<TSchema>>


manualRefresh(options?): void

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

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:382

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

{ bindValues: ParamsObject; query: string; } | QueryBuilder<TResult, any, any> | LiveQuery<TResult> | LiveQueryDef<TResult, "def"> | SignalDef<TResult>

RefreshReason

Context

TResult


setSignal<T>(signalDef, value): void

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

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?): Promise<void>

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

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>

Promise<void>


subscribe<TResult>(query, options): Unsubscribe

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

Subscribe to the results of a query Returns a function to cancel the subscription.

TResult

LiveQueryDef<TResult, "def" | "signal-def"> | LiveQuery<TResult>

string

(query$) => void

() => void

Gets called after the query subscription has been removed

(value) => void

Called when the query result has changed

Context

boolean

Skips the initial onUpdate callback

Default

false

StackInfo

If provided, the stack info will be added to the activeSubscriptions set of the query

Unsubscribe

const unsubscribe = store.subscribe(query$, { onUpdate: (result) => console.log(result) })

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

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

TResult

LiveQueryDef<TResult>

string

boolean

Stream<TResult>


toJSON(): object

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

object

_tag: string = 'livestore.Store'

reactivityGraph: ReactiveGraphSnapshot

Inspectable.Class.toJSON


toString(): string

Defined in: node_modules/.pnpm/effect@3.15.2/node_modules/effect/dist/dts/Inspectable.d.ts:51

string

2.0.0

Inspectable.Class.toString