Skip to content

Store

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

Extends

  • Class

Type Parameters

TSchema extends LiveStoreSchema = LiveStoreSchema

TContext = {}

Constructors

new Store()

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

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

Parameters

__namedParameters

StoreOptions<TSchema, TContext>

Returns

Store<TSchema, TContext>

Overrides

Inspectable.Class.constructor

Properties

__eventSchema

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

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


activeQueries

activeQueries: ReferenceCountedSet<LiveQuery<any>>

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

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


boot

readonly boot: Effect<void, UnexpectedError, Scope>

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


clientSession

clientSession: ClientSession

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


commit()

commit: <TCommitArg>(…list) => void(txn) => void<TCommitArg>(options, …list) => void(options, txn) => void

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

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).

Type Parameters

TCommitArg extends readonly PartialForSchema<TSchema>[]

Parameters

list

TCommitArg

Returns

void

Parameters

txn

<TCommitArg>(…list) => void

Returns

void

Type Parameters

TCommitArg extends readonly PartialForSchema<TSchema>[]

Parameters

options

StoreCommitOptions

list

TCommitArg

Returns

void

Parameters

options

StoreCommitOptions

txn

<TCommitArg>(…list) => void

Returns

void

Examples

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

context: TContext

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


otel

otel: StoreOtel

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


reactivityGraph

reactivityGraph: ReactivityGraph

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


schema

schema: LiveStoreSchema

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


sqliteDbWrapper

sqliteDbWrapper: SqliteDbWrapper

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


storeId

readonly storeId: string

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


syncProcessor

readonly syncProcessor: ClientSessionSyncProcessor

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


tableRefs

tableRefs: object

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

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.

Index Signature

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

Accessors

clientId

Get Signature

get clientId(): string

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

Returns

string


sessionId

Get Signature

get sessionId(): string

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

Returns

string

Methods

[NodeInspectSymbol]()

[NodeInspectSymbol](): unknown

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

Returns

unknown

Since

2.0.0

Inherited from

Inspectable.Class.[NodeInspectSymbol]


atom()

atom(): any

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

Returns

any


manualRefresh()

manualRefresh(options?): void

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

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

Parameters

options?
label?

string

Returns

void


query()

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

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

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: {} })

Type Parameters

TResult

Parameters

query

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

options?
debugRefreshReason?

RefreshReason

otelContext?

Context

Returns

TResult


setRef()

setRef<T>(refDef, value): void

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

Type Parameters

T

Parameters

refDef

ILiveQueryRefDef<T>

value

T

Returns

void


subscribe()

subscribe<TResult>(query, options): Unsubscribe

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

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

Type Parameters

TResult

Parameters

query

LiveQueryDef<TResult> | LiveQuery<TResult>

options
label?

string

onSubscribe?

(query$) => void

onUnsubsubscribe?

() => void

Gets called after the query subscription has been removed

onUpdate

(value) => void

Called when the query result has changed

otelContext?

Context

skipInitialRun?

boolean

Skips the initial onUpdate callback

Default

false
stackInfo?

StackInfo

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

Returns

Unsubscribe

Example

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

subscribeStream()

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

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

Type Parameters

TResult

Parameters

query$

LiveQueryDef<TResult>

options?
label?

string

skipInitialRun?

boolean

Returns

Stream<TResult>


toJSON()

toJSON(): object

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

Returns

object

_tag

_tag: string = 'livestore.Store'

reactivityGraph

reactivityGraph: ReactiveGraphSnapshot

Overrides

Inspectable.Class.toJSON


toString()

toString(): string

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

Returns

string

Since

2.0.0

Inherited from

Inspectable.Class.toString