Store
Defined in: packages/@livestore/livestore/src/store/store.ts:57
Extends
Section titled “Extends”Class
Type Parameters
Section titled “Type Parameters”TSchema
Section titled “TSchema”TSchema
extends LiveStoreSchema
= LiveStoreSchema
TContext
Section titled “TContext”TContext
= { }
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new Store<
TSchema
,TContext
>(__namedParameters
):Store
<TSchema
,TContext
>
Defined in: packages/@livestore/livestore/src/store/store.ts:86
Parameters
Section titled “Parameters”__namedParameters
Section titled “__namedParameters”StoreOptions
<TSchema
, TContext
>
Returns
Section titled “Returns”Store
<TSchema
, TContext
>
Overrides
Section titled “Overrides”Inspectable.Class.constructor
Properties
Section titled “Properties”__eventSchema
Section titled “__eventSchema”
readonly
__eventSchema:ForEventDefRecord
<TSchema
["_EventDefMapType"
]>
Defined in: packages/@livestore/livestore/src/store/store.ts:80
activeQueries
Section titled “activeQueries”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
Section titled “clientSession”clientSession:
ClientSession
Defined in: packages/@livestore/livestore/src/store/store.ts:61
commit()
Section titled “commit()”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
).
Call Signature
Section titled “Call Signature”<
TCommitArg
>(…list
):void
Type Parameters
Section titled “Type Parameters”TCommitArg
Section titled “TCommitArg”TCommitArg
extends readonly PartialForSchema
<TSchema
>[]
Parameters
Section titled “Parameters”…TCommitArg
Returns
Section titled “Returns”void
Call Signature
Section titled “Call Signature”(
txn
):void
Parameters
Section titled “Parameters”<TCommitArg
>(…list
) => void
Returns
Section titled “Returns”void
Call Signature
Section titled “Call Signature”<
TCommitArg
>(options
, …list
):void
Type Parameters
Section titled “Type Parameters”TCommitArg
Section titled “TCommitArg”TCommitArg
extends readonly PartialForSchema
<TSchema
>[]
Parameters
Section titled “Parameters”options
Section titled “options”StoreCommitOptions
…TCommitArg
Returns
Section titled “Returns”void
Call Signature
Section titled “Call Signature”(
options
,txn
):void
Parameters
Section titled “Parameters”options
Section titled “options”StoreCommitOptions
<TCommitArg
>(…list
) => void
Returns
Section titled “Returns”void
Examples
Section titled “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
Section titled “context”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
Section titled “reactivityGraph”reactivityGraph:
ReactivityGraph
Defined in: packages/@livestore/livestore/src/store/store.ts:59
schema
Section titled “schema”schema:
LiveStoreSchema
Defined in: packages/@livestore/livestore/src/store/store.ts:62
sqliteDbWrapper
Section titled “sqliteDbWrapper”sqliteDbWrapper:
SqliteDbWrapper
Defined in: packages/@livestore/livestore/src/store/store.ts:60
storeId
Section titled “storeId”
readonly
storeId:string
Defined in: packages/@livestore/livestore/src/store/store.ts:58
syncProcessor
Section titled “syncProcessor”
readonly
syncProcessor:ClientSessionSyncProcessor
Defined in: packages/@livestore/livestore/src/store/store.ts:81
tableRefs
Section titled “tableRefs”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.
Index Signature
Section titled “Index Signature”[key
: string
]: Ref
<null
, ReactivityGraphContext
, RefreshReason
>
Accessors
Section titled “Accessors”clientId
Section titled “clientId”Get Signature
Section titled “Get Signature”get clientId():
string
Defined in: packages/@livestore/livestore/src/store/store.ts:253
Returns
Section titled “Returns”string
sessionId
Section titled “sessionId”Get Signature
Section titled “Get Signature”get sessionId():
string
Defined in: packages/@livestore/livestore/src/store/store.ts:249
Returns
Section titled “Returns”string
Methods
Section titled “Methods”[NodeInspectSymbol]()
Section titled “[NodeInspectSymbol]()”[NodeInspectSymbol]():
unknown
Defined in: node_modules/.pnpm/effect@3.15.2/node_modules/effect/dist/dts/Inspectable.d.ts:47
Returns
Section titled “Returns”unknown
2.0.0
Inherited from
Section titled “Inherited from”Inspectable.Class.[NodeInspectSymbol]
events()
Section titled “events()”events(
_options?
):AsyncIterable
<ForSchema
<TSchema
>>
Defined in: packages/@livestore/livestore/src/store/store.ts:629
Returns an async iterable of events.
Parameters
Section titled “Parameters”_options?
Section titled “_options?”StoreEventsOptions
<TSchema
>
Returns
Section titled “Returns”AsyncIterable
<ForSchema
<TSchema
>>
Examples
Section titled “Examples”for await (const event of store.events()) { console.log(event)}
// Get all events from the beginning of timefor await (const event of store.events({ cursor: EventSequenceNumber.ROOT })) { console.log(event)}
eventsStream()
Section titled “eventsStream()”eventsStream(
_options?
):Stream
<ForSchema
<TSchema
>>
Defined in: packages/@livestore/livestore/src/store/store.ts:633
Parameters
Section titled “Parameters”_options?
Section titled “_options?”StoreEventsOptions
<TSchema
>
Returns
Section titled “Returns”Stream
<ForSchema
<TSchema
>>
manualRefresh()
Section titled “manualRefresh()”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.
Parameters
Section titled “Parameters”options?
Section titled “options?”label?
Section titled “label?”string
Returns
Section titled “Returns”void
query()
Section titled “query()”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: {} })
Type Parameters
Section titled “Type Parameters”TResult
Section titled “TResult”TResult
Parameters
Section titled “Parameters”{ bindValues
: ParamsObject
; query
: string
; } | QueryBuilder
<TResult
, any
, any
> | LiveQuery
<TResult
> | LiveQueryDef
<TResult
, "def"
> | SignalDef
<TResult
>
options?
Section titled “options?”debugRefreshReason?
Section titled “debugRefreshReason?”otelContext?
Section titled “otelContext?”Context
Returns
Section titled “Returns”TResult
setSignal()
Section titled “setSignal()”setSignal<
T
>(signalDef
,value
):void
Defined in: packages/@livestore/livestore/src/store/store.ts:442
Set the value of a signal
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”signalDef
Section titled “signalDef”SignalDef
<T
>
T
| (prev
) => T
Returns
Section titled “Returns”void
Examples
Section titled “Examples”const count$ = signal(0, { label: 'count$' })store.setSignal(count$, 2)
const count$ = signal(0, { label: 'count$' })store.setSignal(count$, (prev) => prev + 1)
shutdown()
Section titled “shutdown()”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.
Parameters
Section titled “Parameters”cause?
Section titled “cause?”Cause
<UnexpectedError
>
Returns
Section titled “Returns”Promise
<void
>
subscribe()
Section titled “subscribe()”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.
Type Parameters
Section titled “Type Parameters”TResult
Section titled “TResult”TResult
Parameters
Section titled “Parameters”LiveQueryDef
<TResult
, "def"
| "signal-def"
> | LiveQuery
<TResult
>
options
Section titled “options”label?
Section titled “label?”string
onSubscribe?
Section titled “onSubscribe?”(query$
) => void
onUnsubsubscribe?
Section titled “onUnsubsubscribe?”() => void
Gets called after the query subscription has been removed
onUpdate
Section titled “onUpdate”(value
) => void
Called when the query result has changed
otelContext?
Section titled “otelContext?”Context
skipInitialRun?
Section titled “skipInitialRun?”boolean
Skips the initial onUpdate
callback
Default
false
stackInfo?
Section titled “stackInfo?”If provided, the stack info will be added to the activeSubscriptions
set of the query
Returns
Section titled “Returns”Unsubscribe
Example
Section titled “Example”const unsubscribe = store.subscribe(query$, { onUpdate: (result) => console.log(result) })
subscribeStream()
Section titled “subscribeStream()”subscribeStream<
TResult
>(query$
,options?
):Stream
<TResult
>
Defined in: packages/@livestore/livestore/src/store/store.ts:344
Type Parameters
Section titled “Type Parameters”TResult
Section titled “TResult”TResult
Parameters
Section titled “Parameters”query$
Section titled “query$”LiveQueryDef
<TResult
>
options?
Section titled “options?”label?
Section titled “label?”string
skipInitialRun?
Section titled “skipInitialRun?”boolean
Returns
Section titled “Returns”Stream
<TResult
>
toJSON()
Section titled “toJSON()”toJSON():
object
Defined in: packages/@livestore/livestore/src/store/store.ts:726
Returns
Section titled “Returns”object
_tag:
string
='livestore.Store'
reactivityGraph
Section titled “reactivityGraph”reactivityGraph:
ReactiveGraphSnapshot
Overrides
Section titled “Overrides”Inspectable.Class.toJSON
toString()
Section titled “toString()”toString():
string
Defined in: node_modules/.pnpm/effect@3.15.2/node_modules/effect/dist/dts/Inspectable.d.ts:51
Returns
Section titled “Returns”string
2.0.0
Inherited from
Section titled “Inherited from”Inspectable.Class.toString