materializers
constmaterializers: <TInputRecord>(_eventDefRecord,handlers) =>{ [TEventName in TInputRecord[keyof TInputRecord]["name"] as Extract<TInputRecord[keyof TInputRecord], { name: TEventName }>["options"]["derived"] extends true ? never : TEventName]: Materializer<Extract<TInputRecord[keyof TInputRecord], { name: TEventName }>> }
Defined in: packages/@livestore/common/dist/schema/EventDef/materializer.d.ts:146
Builder function for creating a type-safe materializer map.
This is the primary way to define materializers in LiveStore. It ensures:
- Every non-derived event has a corresponding materializer
- Materializer argument types match their event schemas
- Derived events are excluded from the required handlers
Type Parameters
Section titled “Type Parameters”TInputRecord
Section titled “TInputRecord”TInputRecord extends Record<string, AnyWithoutFn>
Parameters
Section titled “Parameters”_eventDefRecord
Section titled “_eventDefRecord”TInputRecord
handlers
Section titled “handlers”{ [TEventName in TInputRecord[keyof TInputRecord]["name"] as Extract<TInputRecord[keyof TInputRecord], { name: TEventName }>["options"]["derived"] extends true ? never : TEventName]: Materializer<Extract<TInputRecord[keyof TInputRecord], { name: TEventName }>> }
Returns
Section titled “Returns”{ [TEventName in TInputRecord[keyof TInputRecord]["name"] as Extract<TInputRecord[keyof TInputRecord], { name: TEventName }>["options"]["derived"] extends true ? never : TEventName]: Materializer<Extract<TInputRecord[keyof TInputRecord], { name: TEventName }>> }
Example
Section titled “Example”import { State } from '@livestore/livestore'
const handlers = State.SQLite.materializers(events, { // Handler for each event - argument types are inferred 'v1.TodoCreated': ({ id, text, completed }) => tables.todos.insert({ id, text, completed }),
'v1.TodoUpdated': ({ id, text }) => tables.todos.update({ text }).where({ id }),
// Can return multiple operations 'v1.UserCreatedWithDefaults': ({ userId, name }) => [ tables.users.insert({ id: userId, name }), tables.settings.insert({ userId, theme: 'light' }), ],
// Can query current state 'v1.TodoToggled': ({ id }, { query }) => { const todo = query(tables.todos.select().where({ id }).first()) return tables.todos.update({ completed: !todo?.completed }).where({ id }) },})