Skip to content

Materializers

Materializers are functions that allow you to write to your database in response to events. Materializers are executed in the order of the events in the eventlog.

import {
const defineMaterializer: <TEventDef extends State.SQLite.EventDef.AnyWithoutFn>(_eventDef: TEventDef, materializer: State.SQLite.Materializer<TEventDef>) => State.SQLite.Materializer<TEventDef>
defineMaterializer
,
import Events
Events
,
import Schema
Schema
,
import State
State
} from '@livestore/livestore'
export const
const todos: State.SQLite.TableDef<State.SQLite.SqliteTableDefForInput<"todos", {
readonly id: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: true;
autoIncrement: false;
};
readonly text: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: false;
autoIncrement: false;
};
readonly completed: {
columnType: "integer";
schema: Schema.Schema<boolean, number, never>;
default: Some<false>;
nullable: false;
primaryKey: false;
autoIncrement: false;
};
readonly previousIds: {
...;
};
}>, State.SQLite.WithDefaults<...>, Schema.Schema<...>>
todos
=
import State
State
.
import SQLite
SQLite
.
function table<"todos", {
readonly id: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: true;
autoIncrement: false;
};
readonly text: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: false;
autoIncrement: false;
};
readonly completed: {
columnType: "integer";
schema: Schema.Schema<boolean, number, never>;
default: Some<false>;
nullable: false;
primaryKey: false;
autoIncrement: false;
};
readonly previousIds: {
...;
};
}, Partial<...>>(args: {
...;
} & Partial<...>): State.SQLite.TableDef<...> (+2 overloads)

Creates a SQLite table definition from columns or an Effect Schema.

This function supports two main ways to define a table:

  1. Using explicit column definitions
  2. Using an Effect Schema (either the name property needs to be provided or the schema needs to have a title/identifier)
// Using explicit columns
const usersTable = State.SQLite.table({
name: 'users',
columns: {
id: State.SQLite.text({ primaryKey: true }),
name: State.SQLite.text({ nullable: false }),
email: State.SQLite.text({ nullable: false }),
age: State.SQLite.integer({ nullable: true }),
},
})
// Using Effect Schema with annotations
import { Schema } from '@livestore/utils/effect'
const UserSchema = Schema.Struct({
id: Schema.Int.pipe(State.SQLite.withPrimaryKey).pipe(State.SQLite.withAutoIncrement),
email: Schema.String.pipe(State.SQLite.withUnique),
name: Schema.String,
active: Schema.Boolean.pipe(State.SQLite.withDefault(true)),
createdAt: Schema.optional(Schema.Date),
})
// Option 1: With explicit name
const usersTable = State.SQLite.table({
name: 'users',
schema: UserSchema,
})
// Option 2: With name from schema annotation (title or identifier)
const AnnotatedUserSchema = UserSchema.annotations({ title: 'users' })
const usersTable2 = State.SQLite.table({
schema: AnnotatedUserSchema,
})
// Adding indexes
const PostSchema = Schema.Struct({
id: Schema.String.pipe(State.SQLite.withPrimaryKey),
title: Schema.String,
authorId: Schema.String,
createdAt: Schema.Date,
}).annotations({ identifier: 'posts' })
const postsTable = State.SQLite.table({
schema: PostSchema,
indexes: [
{ name: 'idx_posts_author', columns: ['authorId'] },
{ name: 'idx_posts_created', columns: ['createdAt'], isUnique: false },
],
})

table
({
name: "todos"
name
: 'todos',
columns: {
readonly id: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: true;
autoIncrement: false;
};
readonly text: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: false;
autoIncrement: false;
};
readonly completed: {
columnType: "integer";
schema: Schema.Schema<boolean, number, never>;
default: Some<false>;
nullable: false;
primaryKey: false;
autoIncrement: false;
};
readonly previousIds: {
...;
};
}
columns
: {
id: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: true;
autoIncrement: false;
}
id
:
import State
State
.
import SQLite
SQLite
.
const text: <string, string, false, typeof NoDefault, true, false>(args: {
schema?: Schema.Schema<string, string, never>;
default?: typeof NoDefault;
nullable?: false;
primaryKey?: true;
autoIncrement?: false;
}) => {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: true;
autoIncrement: false;
} (+1 overload)
text
({
primaryKey?: true
primaryKey
: true }),
text: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: false;
autoIncrement: false;
}
text
:
import State
State
.
import SQLite
SQLite
.
const text: () => {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: false;
autoIncrement: false;
} (+1 overload)
text
(),
completed: {
columnType: "integer";
schema: Schema.Schema<boolean, number, never>;
default: Some<false>;
nullable: false;
primaryKey: false;
autoIncrement: false;
}
completed
:
import State
State
.
import SQLite
SQLite
.
const boolean: <boolean, false, false, false, false>(args: {
default?: false;
nullable?: false;
primaryKey?: false;
autoIncrement?: false;
}) => {
columnType: "integer";
schema: Schema.Schema<boolean, number, never>;
default: Some<false>;
nullable: false;
primaryKey: false;
autoIncrement: false;
} (+1 overload)
boolean
({
default?: false
default
: false }),
previousIds: {
columnType: "text";
schema: Schema.Schema<readonly string[] | null, string | null, never>;
default: Some<any> | None<never>;
nullable: true;
primaryKey: false;
autoIncrement: false;
}
previousIds
:
import State
State
.
import SQLite
SQLite
.
const json: <readonly string[], true, any, false, false>(args: {
schema?: Schema.Schema<readonly string[], any, never>;
default?: any;
nullable?: true;
primaryKey?: false;
autoIncrement?: false;
}) => {
columnType: "text";
schema: Schema.Schema<readonly string[] | null, string | null, never>;
default: Some<any> | None<never>;
nullable: true;
primaryKey: false;
autoIncrement: false;
} (+1 overload)
json
({
schema?: Schema.Schema<readonly string[], any, never>
schema
:
import Schema
Schema
.
Array<typeof Schema.String>(value: typeof Schema.String): Schema.Array$<typeof Schema.String>
export Array

@since3.10.0

Array
(
import Schema
Schema
.
class String
export String

@since3.10.0

String
),
nullable?: true
nullable
: true,
}),
},
})
export const
const table1: State.SQLite.TableDef<State.SQLite.SqliteTableDefForInput<"settings", {
readonly id: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: true;
autoIncrement: false;
};
readonly someVal: {
columnType: "integer";
schema: Schema.Schema<number, number, never>;
default: Some<0>;
nullable: false;
primaryKey: false;
autoIncrement: false;
};
}>, State.SQLite.WithDefaults<{
readonly id: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: true;
autoIncrement: false;
};
readonly someVal: {
columnType: "integer";
schema: Schema.Schema<number, number, never>;
default: Some<0>;
nullable: false;
primaryKey: false;
autoIncrement: false;
};
}>, Schema.Schema<...>>
table1
=
import State
State
.
import SQLite
SQLite
.
function table<"settings", {
readonly id: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: true;
autoIncrement: false;
};
readonly someVal: {
columnType: "integer";
schema: Schema.Schema<number, number, never>;
default: Some<0>;
nullable: false;
primaryKey: false;
autoIncrement: false;
};
}, Partial<{
indexes: Index[];
}>>(args: {
name: "settings";
columns: {
readonly id: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: true;
autoIncrement: false;
};
readonly someVal: {
columnType: "integer";
schema: Schema.Schema<number, number, never>;
default: Some<0>;
nullable: false;
primaryKey: false;
autoIncrement: false;
};
};
} & Partial<...>): State.SQLite.TableDef<...> (+2 overloads)

Creates a SQLite table definition from columns or an Effect Schema.

This function supports two main ways to define a table:

  1. Using explicit column definitions
  2. Using an Effect Schema (either the name property needs to be provided or the schema needs to have a title/identifier)
// Using explicit columns
const usersTable = State.SQLite.table({
name: 'users',
columns: {
id: State.SQLite.text({ primaryKey: true }),
name: State.SQLite.text({ nullable: false }),
email: State.SQLite.text({ nullable: false }),
age: State.SQLite.integer({ nullable: true }),
},
})
// Using Effect Schema with annotations
import { Schema } from '@livestore/utils/effect'
const UserSchema = Schema.Struct({
id: Schema.Int.pipe(State.SQLite.withPrimaryKey).pipe(State.SQLite.withAutoIncrement),
email: Schema.String.pipe(State.SQLite.withUnique),
name: Schema.String,
active: Schema.Boolean.pipe(State.SQLite.withDefault(true)),
createdAt: Schema.optional(Schema.Date),
})
// Option 1: With explicit name
const usersTable = State.SQLite.table({
name: 'users',
schema: UserSchema,
})
// Option 2: With name from schema annotation (title or identifier)
const AnnotatedUserSchema = UserSchema.annotations({ title: 'users' })
const usersTable2 = State.SQLite.table({
schema: AnnotatedUserSchema,
})
// Adding indexes
const PostSchema = Schema.Struct({
id: Schema.String.pipe(State.SQLite.withPrimaryKey),
title: Schema.String,
authorId: Schema.String,
createdAt: Schema.Date,
}).annotations({ identifier: 'posts' })
const postsTable = State.SQLite.table({
schema: PostSchema,
indexes: [
{ name: 'idx_posts_author', columns: ['authorId'] },
{ name: 'idx_posts_created', columns: ['createdAt'], isUnique: false },
],
})

table
({
name: "settings"
name
: 'settings',
columns: {
readonly id: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: true;
autoIncrement: false;
};
readonly someVal: {
columnType: "integer";
schema: Schema.Schema<number, number, never>;
default: Some<0>;
nullable: false;
primaryKey: false;
autoIncrement: false;
};
}
columns
: {
id: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: true;
autoIncrement: false;
}
id
:
import State
State
.
import SQLite
SQLite
.
const text: <string, string, false, typeof NoDefault, true, false>(args: {
schema?: Schema.Schema<string, string, never>;
default?: typeof NoDefault;
nullable?: false;
primaryKey?: true;
autoIncrement?: false;
}) => {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: true;
autoIncrement: false;
} (+1 overload)
text
({
primaryKey?: true
primaryKey
: true }),
someVal: {
columnType: "integer";
schema: Schema.Schema<number, number, never>;
default: Some<0>;
nullable: false;
primaryKey: false;
autoIncrement: false;
}
someVal
:
import State
State
.
import SQLite
SQLite
.
const integer: <number, number, false, 0, false, false>(args: {
schema?: Schema.Schema<number, number, never>;
default?: 0;
nullable?: false;
primaryKey?: false;
autoIncrement?: false;
}) => {
columnType: "integer";
schema: Schema.Schema<number, number, never>;
default: Some<0>;
nullable: false;
primaryKey: false;
autoIncrement: false;
} (+1 overload)
integer
({
default?: 0
default
: 0 }),
},
})
export const
const table2: State.SQLite.TableDef<State.SQLite.SqliteTableDefForInput<"preferences", {
readonly id: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: true;
autoIncrement: false;
};
readonly otherVal: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: Some<"default">;
nullable: false;
primaryKey: false;
autoIncrement: false;
};
}>, State.SQLite.WithDefaults<{
readonly id: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: true;
autoIncrement: false;
};
readonly otherVal: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: Some<"default">;
nullable: false;
primaryKey: false;
autoIncrement: false;
};
}>, Schema.Schema<...>>
table2
=
import State
State
.
import SQLite
SQLite
.
function table<"preferences", {
readonly id: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: true;
autoIncrement: false;
};
readonly otherVal: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: Some<"default">;
nullable: false;
primaryKey: false;
autoIncrement: false;
};
}, Partial<{
indexes: Index[];
}>>(args: {
name: "preferences";
columns: {
readonly id: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: true;
autoIncrement: false;
};
readonly otherVal: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: Some<"default">;
nullable: false;
primaryKey: false;
autoIncrement: false;
};
};
} & Partial<...>): State.SQLite.TableDef<...> (+2 overloads)

Creates a SQLite table definition from columns or an Effect Schema.

This function supports two main ways to define a table:

  1. Using explicit column definitions
  2. Using an Effect Schema (either the name property needs to be provided or the schema needs to have a title/identifier)
// Using explicit columns
const usersTable = State.SQLite.table({
name: 'users',
columns: {
id: State.SQLite.text({ primaryKey: true }),
name: State.SQLite.text({ nullable: false }),
email: State.SQLite.text({ nullable: false }),
age: State.SQLite.integer({ nullable: true }),
},
})
// Using Effect Schema with annotations
import { Schema } from '@livestore/utils/effect'
const UserSchema = Schema.Struct({
id: Schema.Int.pipe(State.SQLite.withPrimaryKey).pipe(State.SQLite.withAutoIncrement),
email: Schema.String.pipe(State.SQLite.withUnique),
name: Schema.String,
active: Schema.Boolean.pipe(State.SQLite.withDefault(true)),
createdAt: Schema.optional(Schema.Date),
})
// Option 1: With explicit name
const usersTable = State.SQLite.table({
name: 'users',
schema: UserSchema,
})
// Option 2: With name from schema annotation (title or identifier)
const AnnotatedUserSchema = UserSchema.annotations({ title: 'users' })
const usersTable2 = State.SQLite.table({
schema: AnnotatedUserSchema,
})
// Adding indexes
const PostSchema = Schema.Struct({
id: Schema.String.pipe(State.SQLite.withPrimaryKey),
title: Schema.String,
authorId: Schema.String,
createdAt: Schema.Date,
}).annotations({ identifier: 'posts' })
const postsTable = State.SQLite.table({
schema: PostSchema,
indexes: [
{ name: 'idx_posts_author', columns: ['authorId'] },
{ name: 'idx_posts_created', columns: ['createdAt'], isUnique: false },
],
})

table
({
name: "preferences"
name
: 'preferences',
columns: {
readonly id: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: true;
autoIncrement: false;
};
readonly otherVal: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: Some<"default">;
nullable: false;
primaryKey: false;
autoIncrement: false;
};
}
columns
: {
id: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: true;
autoIncrement: false;
}
id
:
import State
State
.
import SQLite
SQLite
.
const text: <string, string, false, typeof NoDefault, true, false>(args: {
schema?: Schema.Schema<string, string, never>;
default?: typeof NoDefault;
nullable?: false;
primaryKey?: true;
autoIncrement?: false;
}) => {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: true;
autoIncrement: false;
} (+1 overload)
text
({
primaryKey?: true
primaryKey
: true }),
otherVal: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: Some<"default">;
nullable: false;
primaryKey: false;
autoIncrement: false;
}
otherVal
:
import State
State
.
import SQLite
SQLite
.
const text: <string, string, false, "default", false, false>(args: {
schema?: Schema.Schema<string, string, never>;
default?: "default";
nullable?: false;
primaryKey?: false;
autoIncrement?: false;
}) => {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: Some<"default">;
nullable: false;
primaryKey: false;
autoIncrement: false;
} (+1 overload)
text
({
default?: "default"
default
: 'default' }),
},
})
export const
const events: {
readonly todoCreated: State.SQLite.EventDef<"todoCreated", {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, false>;
readonly userPreferencesUpdated: State.SQLite.EventDef<"userPreferencesUpdated", {
readonly userId: string;
readonly theme: string;
}, {
readonly userId: string;
readonly theme: string;
}, false>;
readonly factoryResetApplied: State.SQLite.EventDef<"factoryResetApplied", {}, {}, false>;
}
events
= {
todoCreated: State.SQLite.EventDef<"todoCreated", {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, false>
todoCreated
:
import Events
Events
.
synced<"todoCreated", {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}>(args: {
name: "todoCreated";
schema: Schema.Schema<{
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, never>;
} & Omit<State.SQLite.DefineEventOptions<{
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, false>, "derived" | "clientOnly">): State.SQLite.EventDef<...>
export synced
synced
({
name: "todoCreated"
name
: 'todoCreated',
schema: Schema.Schema<{
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, never>
schema
:
import Schema
Schema
.
function Struct<{
id: typeof Schema.String;
text: typeof Schema.String;
completed: Schema.optional<typeof Schema.Boolean>;
}>(fields: {
id: typeof Schema.String;
text: typeof Schema.String;
completed: Schema.optional<typeof Schema.Boolean>;
}): Schema.Struct<{
id: typeof Schema.String;
text: typeof Schema.String;
completed: Schema.optional<typeof Schema.Boolean>;
}> (+1 overload)

@since3.10.0

Struct
({
id: typeof Schema.String
id
:
import Schema
Schema
.
class String
export String

@since3.10.0

String
,
text: typeof Schema.String
text
:
import Schema
Schema
.
class String
export String

@since3.10.0

String
,
completed: Schema.optional<typeof Schema.Boolean>
completed
:
import Schema
Schema
.
class Boolean
export Boolean

@since3.10.0

Boolean
.
Pipeable.pipe<typeof Schema.Boolean, Schema.optional<typeof Schema.Boolean>>(this: typeof Schema.Boolean, ab: (_: typeof Schema.Boolean) => Schema.optional<typeof Schema.Boolean>): Schema.optional<typeof Schema.Boolean> (+21 overloads)
pipe
(
import Schema
Schema
.
const optional: <S extends Schema.Schema.All>(self: S) => Schema.optional<S>

@since3.10.0

@since3.10.0

optional
),
}),
}),
userPreferencesUpdated: State.SQLite.EventDef<"userPreferencesUpdated", {
readonly userId: string;
readonly theme: string;
}, {
readonly userId: string;
readonly theme: string;
}, false>
userPreferencesUpdated
:
import Events
Events
.
synced<"userPreferencesUpdated", {
readonly userId: string;
readonly theme: string;
}, {
readonly userId: string;
readonly theme: string;
}>(args: {
name: "userPreferencesUpdated";
schema: Schema.Schema<{
readonly userId: string;
readonly theme: string;
}, {
readonly userId: string;
readonly theme: string;
}, never>;
} & Omit<State.SQLite.DefineEventOptions<{
readonly userId: string;
readonly theme: string;
}, false>, "derived" | "clientOnly">): State.SQLite.EventDef<"userPreferencesUpdated", {
readonly userId: string;
readonly theme: string;
}, {
readonly userId: string;
readonly theme: string;
}, false>
export synced
synced
({
name: "userPreferencesUpdated"
name
: 'userPreferencesUpdated',
schema: Schema.Schema<{
readonly userId: string;
readonly theme: string;
}, {
readonly userId: string;
readonly theme: string;
}, never>
schema
:
import Schema
Schema
.
function Struct<{
userId: typeof Schema.String;
theme: typeof Schema.String;
}>(fields: {
userId: typeof Schema.String;
theme: typeof Schema.String;
}): Schema.Struct<{
userId: typeof Schema.String;
theme: typeof Schema.String;
}> (+1 overload)

@since3.10.0

Struct
({
userId: typeof Schema.String
userId
:
import Schema
Schema
.
class String
export String

@since3.10.0

String
,
theme: typeof Schema.String
theme
:
import Schema
Schema
.
class String
export String

@since3.10.0

String
}),
}),
factoryResetApplied: State.SQLite.EventDef<"factoryResetApplied", {}, {}, false>
factoryResetApplied
:
import Events
Events
.
synced<"factoryResetApplied", {}, {}>(args: {
name: "factoryResetApplied";
schema: Schema.Schema<{}, {}, never>;
} & Omit<State.SQLite.DefineEventOptions<{}, false>, "derived" | "clientOnly">): State.SQLite.EventDef<"factoryResetApplied", {}, {}, false>
export synced
synced
({
name: "factoryResetApplied"
name
: 'factoryResetApplied',
schema: Schema.Schema<{}, {}, never>
schema
:
import Schema
Schema
.
function Struct<{}>(fields: {}): Schema.Struct<{}> (+1 overload)

@since3.10.0

Struct
({}),
}),
} as
type const = {
readonly todoCreated: State.SQLite.EventDef<"todoCreated", {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, false>;
readonly userPreferencesUpdated: State.SQLite.EventDef<"userPreferencesUpdated", {
readonly userId: string;
readonly theme: string;
}, {
readonly userId: string;
readonly theme: string;
}, false>;
readonly factoryResetApplied: State.SQLite.EventDef<"factoryResetApplied", {}, {}, false>;
}
const
export const
const materializers: {
todoCreated: State.SQLite.Materializer<State.SQLite.EventDef<"todoCreated", {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, false>>;
userPreferencesUpdated: State.SQLite.Materializer<State.SQLite.EventDef<"userPreferencesUpdated", {
readonly userId: string;
readonly theme: string;
}, {
readonly userId: string;
readonly theme: string;
}, false>>;
factoryResetApplied: State.SQLite.Materializer<State.SQLite.EventDef<"factoryResetApplied", {}, {}, false>>;
}
materializers
=
import State
State
.
import SQLite
SQLite
.
const materializers: <{
readonly todoCreated: State.SQLite.EventDef<"todoCreated", {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, false>;
readonly userPreferencesUpdated: State.SQLite.EventDef<"userPreferencesUpdated", {
readonly userId: string;
readonly theme: string;
}, {
readonly userId: string;
readonly theme: string;
}, false>;
readonly factoryResetApplied: State.SQLite.EventDef<"factoryResetApplied", {}, {}, false>;
}>(_eventDefRecord: {
readonly todoCreated: State.SQLite.EventDef<"todoCreated", {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, false>;
readonly userPreferencesUpdated: State.SQLite.EventDef<"userPreferencesUpdated", {
readonly userId: string;
readonly theme: string;
}, {
readonly userId: string;
readonly theme: string;
}, false>;
readonly factoryResetApplied: State.SQLite.EventDef<"factoryResetApplied", {}, {}, false>;
}, handlers: {
...;
}) => {
...;
}
materializers
(
const events: {
readonly todoCreated: State.SQLite.EventDef<"todoCreated", {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, false>;
readonly userPreferencesUpdated: State.SQLite.EventDef<"userPreferencesUpdated", {
readonly userId: string;
readonly theme: string;
}, {
readonly userId: string;
readonly theme: string;
}, false>;
readonly factoryResetApplied: State.SQLite.EventDef<"factoryResetApplied", {}, {}, false>;
}
events
, {
[
const events: {
readonly todoCreated: State.SQLite.EventDef<"todoCreated", {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, false>;
readonly userPreferencesUpdated: State.SQLite.EventDef<"userPreferencesUpdated", {
readonly userId: string;
readonly theme: string;
}, {
readonly userId: string;
readonly theme: string;
}, false>;
readonly factoryResetApplied: State.SQLite.EventDef<"factoryResetApplied", {}, {}, false>;
}
events
.
todoCreated: State.SQLite.EventDef<"todoCreated", {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, false>
todoCreated
.
name: "todoCreated"
name
]:
defineMaterializer<State.SQLite.EventDef<"todoCreated", {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, false>>(_eventDef: State.SQLite.EventDef<"todoCreated", {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, false>, materializer: State.SQLite.Materializer<State.SQLite.EventDef<"todoCreated", {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, false>>): State.SQLite.Materializer<...>
defineMaterializer
(
const events: {
readonly todoCreated: State.SQLite.EventDef<"todoCreated", {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, false>;
readonly userPreferencesUpdated: State.SQLite.EventDef<"userPreferencesUpdated", {
readonly userId: string;
readonly theme: string;
}, {
readonly userId: string;
readonly theme: string;
}, false>;
readonly factoryResetApplied: State.SQLite.EventDef<"factoryResetApplied", {}, {}, false>;
}
events
.
todoCreated: State.SQLite.EventDef<"todoCreated", {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, false>
todoCreated
, ({
id: string
id
,
text: string
text
,
completed: boolean | undefined
completed
}) =>
const todos: State.SQLite.TableDef<State.SQLite.SqliteTableDefForInput<"todos", {
readonly id: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: true;
autoIncrement: false;
};
readonly text: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: false;
autoIncrement: false;
};
readonly completed: {
columnType: "integer";
schema: Schema.Schema<boolean, number, never>;
default: Some<false>;
nullable: false;
primaryKey: false;
autoIncrement: false;
};
readonly previousIds: {
...;
};
}>, State.SQLite.WithDefaults<...>, Schema.Schema<...>>
todos
.
insert: (values: {
readonly text: string;
readonly id: string;
readonly completed?: boolean;
readonly previousIds?: readonly string[] | null;
}) => QueryBuilder<readonly {
readonly id: string;
readonly text: string;
readonly completed: boolean;
readonly previousIds: readonly string[] | null;
}[], State.SQLite.TableDefBase<State.SQLite.SqliteTableDefForInput<"todos", {
readonly id: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: true;
autoIncrement: false;
};
readonly text: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: false;
autoIncrement: false;
};
readonly completed: {
...;
};
readonly previousIds: {
...;
};
}>, State.SQLite.WithDefaults<...>>, "select" | ... 6 more ... | "row">

Insert a new row into the table

Example:

db.todos.insert({ id: '123', text: 'Buy milk', status: 'active' })

insert
({
id: string
id
,
text: string
text
,
completed?: boolean
completed
:
completed: boolean | undefined
completed
?? false }),
),
[
const events: {
readonly todoCreated: State.SQLite.EventDef<"todoCreated", {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, false>;
readonly userPreferencesUpdated: State.SQLite.EventDef<"userPreferencesUpdated", {
readonly userId: string;
readonly theme: string;
}, {
readonly userId: string;
readonly theme: string;
}, false>;
readonly factoryResetApplied: State.SQLite.EventDef<"factoryResetApplied", {}, {}, false>;
}
events
.
userPreferencesUpdated: State.SQLite.EventDef<"userPreferencesUpdated", {
readonly userId: string;
readonly theme: string;
}, {
readonly userId: string;
readonly theme: string;
}, false>
userPreferencesUpdated
.
name: "userPreferencesUpdated"
name
]:
defineMaterializer<State.SQLite.EventDef<"userPreferencesUpdated", {
readonly userId: string;
readonly theme: string;
}, {
readonly userId: string;
readonly theme: string;
}, false>>(_eventDef: State.SQLite.EventDef<"userPreferencesUpdated", {
readonly userId: string;
readonly theme: string;
}, {
readonly userId: string;
readonly theme: string;
}, false>, materializer: State.SQLite.Materializer<State.SQLite.EventDef<"userPreferencesUpdated", {
readonly userId: string;
readonly theme: string;
}, {
readonly userId: string;
readonly theme: string;
}, false>>): State.SQLite.Materializer<...>
defineMaterializer
(
const events: {
readonly todoCreated: State.SQLite.EventDef<"todoCreated", {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, false>;
readonly userPreferencesUpdated: State.SQLite.EventDef<"userPreferencesUpdated", {
readonly userId: string;
readonly theme: string;
}, {
readonly userId: string;
readonly theme: string;
}, false>;
readonly factoryResetApplied: State.SQLite.EventDef<"factoryResetApplied", {}, {}, false>;
}
events
.
userPreferencesUpdated: State.SQLite.EventDef<"userPreferencesUpdated", {
readonly userId: string;
readonly theme: string;
}, {
readonly userId: string;
readonly theme: string;
}, false>
userPreferencesUpdated
, ({
userId: string
userId
,
theme: string
theme
}) => {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void (+3 overloads)

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(`User ${
userId: string
userId
} updated theme to ${
theme: string
theme
}.`)
return []
}),
[
const events: {
readonly todoCreated: State.SQLite.EventDef<"todoCreated", {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, false>;
readonly userPreferencesUpdated: State.SQLite.EventDef<"userPreferencesUpdated", {
readonly userId: string;
readonly theme: string;
}, {
readonly userId: string;
readonly theme: string;
}, false>;
readonly factoryResetApplied: State.SQLite.EventDef<"factoryResetApplied", {}, {}, false>;
}
events
.
factoryResetApplied: State.SQLite.EventDef<"factoryResetApplied", {}, {}, false>
factoryResetApplied
.
name: "factoryResetApplied"
name
]:
defineMaterializer<State.SQLite.EventDef<"factoryResetApplied", {}, {}, false>>(_eventDef: State.SQLite.EventDef<"factoryResetApplied", {}, {}, false>, materializer: State.SQLite.Materializer<State.SQLite.EventDef<"factoryResetApplied", {}, {}, false>>): State.SQLite.Materializer<State.SQLite.EventDef<"factoryResetApplied", {}, {}, false>>
defineMaterializer
(
const events: {
readonly todoCreated: State.SQLite.EventDef<"todoCreated", {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, false>;
readonly userPreferencesUpdated: State.SQLite.EventDef<"userPreferencesUpdated", {
readonly userId: string;
readonly theme: string;
}, {
readonly userId: string;
readonly theme: string;
}, false>;
readonly factoryResetApplied: State.SQLite.EventDef<"factoryResetApplied", {}, {}, false>;
}
events
.
factoryResetApplied: State.SQLite.EventDef<"factoryResetApplied", {}, {}, false>
factoryResetApplied
, () => [
const table1: State.SQLite.TableDef<State.SQLite.SqliteTableDefForInput<"settings", {
readonly id: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: true;
autoIncrement: false;
};
readonly someVal: {
columnType: "integer";
schema: Schema.Schema<number, number, never>;
default: Some<0>;
nullable: false;
primaryKey: false;
autoIncrement: false;
};
}>, State.SQLite.WithDefaults<{
readonly id: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: true;
autoIncrement: false;
};
readonly someVal: {
columnType: "integer";
schema: Schema.Schema<number, number, never>;
default: Some<0>;
nullable: false;
primaryKey: false;
autoIncrement: false;
};
}>, Schema.Schema<...>>
table1
.
update: (values: Partial<{
readonly id: string;
readonly someVal: number;
}>) => QueryBuilder<readonly {
readonly id: string;
readonly someVal: number;
}[], State.SQLite.TableDefBase<State.SQLite.SqliteTableDefForInput<"settings", {
readonly id: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: true;
autoIncrement: false;
};
readonly someVal: {
columnType: "integer";
schema: Schema.Schema<number, number, never>;
default: Some<0>;
nullable: false;
primaryKey: false;
autoIncrement: false;
};
}>, State.SQLite.WithDefaults<{
readonly id: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: true;
autoIncrement: false;
};
readonly someVal: {
columnType: "integer";
schema: Schema.Schema<number, number, never>;
default: Some<0>;
nullable: false;
primaryKey: false;
autoIncrement: false;
};
}>>, "select" | ... 6 more ... | "row">

Update rows in the table that match the where clause

Example:

db.todos.update({ status: 'completed' }).where({ id: '123' })

update
({
someVal?: number
someVal
: 0 }),
const table2: State.SQLite.TableDef<State.SQLite.SqliteTableDefForInput<"preferences", {
readonly id: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: true;
autoIncrement: false;
};
readonly otherVal: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: Some<"default">;
nullable: false;
primaryKey: false;
autoIncrement: false;
};
}>, State.SQLite.WithDefaults<{
readonly id: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: true;
autoIncrement: false;
};
readonly otherVal: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: Some<"default">;
nullable: false;
primaryKey: false;
autoIncrement: false;
};
}>, Schema.Schema<...>>
table2
.
update: (values: Partial<{
readonly id: string;
readonly otherVal: string;
}>) => QueryBuilder<readonly {
readonly id: string;
readonly otherVal: string;
}[], State.SQLite.TableDefBase<State.SQLite.SqliteTableDefForInput<"preferences", {
readonly id: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: true;
autoIncrement: false;
};
readonly otherVal: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: Some<"default">;
nullable: false;
primaryKey: false;
autoIncrement: false;
};
}>, State.SQLite.WithDefaults<...>>, "select" | ... 6 more ... | "row">

Update rows in the table that match the where clause

Example:

db.todos.update({ status: 'completed' }).where({ id: '123' })

update
({
otherVal?: string
otherVal
: 'default' }),
]),
})

Reading from the database in materializers

Section titled “Reading from the database in materializers”

Sometimes it can be useful to query your current state when executing a materializer. This can be done by using ctx.query in your materializer function.

import {
const defineMaterializer: <TEventDef extends State.SQLite.EventDef.AnyWithoutFn>(_eventDef: TEventDef, materializer: State.SQLite.Materializer<TEventDef>) => State.SQLite.Materializer<TEventDef>
defineMaterializer
,
import Events
Events
,
import Schema
Schema
,
import State
State
} from '@livestore/livestore'
import {
const todos: State.SQLite.TableDef<State.SQLite.SqliteTableDefForInput<"todos", {
readonly id: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: true;
autoIncrement: false;
};
readonly text: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: false;
autoIncrement: false;
};
readonly completed: {
columnType: "integer";
schema: Schema.Schema<boolean, number, never>;
default: Some<false>;
nullable: false;
primaryKey: false;
autoIncrement: false;
};
readonly previousIds: {
...;
};
}>, State.SQLite.WithDefaults<...>, Schema.Schema<...>>
todos
} from './example.ts'
const
const events: {
readonly todoCreated: State.SQLite.EventDef<"todoCreated", {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, false>;
}
events
= {
todoCreated: State.SQLite.EventDef<"todoCreated", {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, false>
todoCreated
:
import Events
Events
.
synced<"todoCreated", {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}>(args: {
name: "todoCreated";
schema: Schema.Schema<{
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, never>;
} & Omit<State.SQLite.DefineEventOptions<{
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, false>, "derived" | "clientOnly">): State.SQLite.EventDef<...>
export synced
synced
({
name: "todoCreated"
name
: 'todoCreated',
schema: Schema.Schema<{
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, never>
schema
:
import Schema
Schema
.
function Struct<{
id: typeof Schema.String;
text: typeof Schema.String;
completed: Schema.optional<typeof Schema.Boolean>;
}>(fields: {
id: typeof Schema.String;
text: typeof Schema.String;
completed: Schema.optional<typeof Schema.Boolean>;
}): Schema.Struct<{
id: typeof Schema.String;
text: typeof Schema.String;
completed: Schema.optional<typeof Schema.Boolean>;
}> (+1 overload)

@since3.10.0

Struct
({
id: typeof Schema.String
id
:
import Schema
Schema
.
class String
export String

@since3.10.0

String
,
text: typeof Schema.String
text
:
import Schema
Schema
.
class String
export String

@since3.10.0

String
,
completed: Schema.optional<typeof Schema.Boolean>
completed
:
import Schema
Schema
.
class Boolean
export Boolean

@since3.10.0

Boolean
.
Pipeable.pipe<typeof Schema.Boolean, Schema.optional<typeof Schema.Boolean>>(this: typeof Schema.Boolean, ab: (_: typeof Schema.Boolean) => Schema.optional<typeof Schema.Boolean>): Schema.optional<typeof Schema.Boolean> (+21 overloads)
pipe
(
import Schema
Schema
.
const optional: <S extends Schema.Schema.All>(self: S) => Schema.optional<S>

@since3.10.0

@since3.10.0

optional
),
}),
}),
} as
type const = {
readonly todoCreated: State.SQLite.EventDef<"todoCreated", {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, false>;
}
const
export const
const materializers: {
todoCreated: State.SQLite.Materializer<State.SQLite.EventDef<"todoCreated", {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, false>>;
}
materializers
=
import State
State
.
import SQLite
SQLite
.
const materializers: <{
readonly todoCreated: State.SQLite.EventDef<"todoCreated", {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, false>;
}>(_eventDefRecord: {
readonly todoCreated: State.SQLite.EventDef<"todoCreated", {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, false>;
}, handlers: {
todoCreated: State.SQLite.Materializer<...>;
}) => {
todoCreated: State.SQLite.Materializer<...>;
}
materializers
(
const events: {
readonly todoCreated: State.SQLite.EventDef<"todoCreated", {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, false>;
}
events
, {
[
const events: {
readonly todoCreated: State.SQLite.EventDef<"todoCreated", {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, false>;
}
events
.
todoCreated: State.SQLite.EventDef<"todoCreated", {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, false>
todoCreated
.
name: "todoCreated"
name
]:
defineMaterializer<State.SQLite.EventDef<"todoCreated", {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, false>>(_eventDef: State.SQLite.EventDef<"todoCreated", {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, false>, materializer: State.SQLite.Materializer<State.SQLite.EventDef<"todoCreated", {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, false>>): State.SQLite.Materializer<...>
defineMaterializer
(
const events: {
readonly todoCreated: State.SQLite.EventDef<"todoCreated", {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, false>;
}
events
.
todoCreated: State.SQLite.EventDef<"todoCreated", {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, false>
todoCreated
, ({
id: string
id
,
text: string
text
,
completed: boolean | undefined
completed
},
ctx: {
currentFacts: State.SQLite.EventDefFacts;
eventDef: State.SQLite.EventDef<"todoCreated", {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, false>;
query: State.SQLite.MaterializerContextQuery;
event: AnyDecoded;
}
ctx
) => {
const
const previousIds: readonly string[]
previousIds
=
ctx: {
currentFacts: State.SQLite.EventDefFacts;
eventDef: State.SQLite.EventDef<"todoCreated", {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, {
readonly text: string;
readonly id: string;
readonly completed?: boolean | undefined;
}, false>;
query: State.SQLite.MaterializerContextQuery;
event: AnyDecoded;
}
ctx
.
query: <readonly string[]>(qb: QueryBuilder<readonly string[], any, any>) => readonly string[] (+1 overload)
query
(
const todos: State.SQLite.TableDef<State.SQLite.SqliteTableDefForInput<"todos", {
readonly id: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: true;
autoIncrement: false;
};
readonly text: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: false;
autoIncrement: false;
};
readonly completed: {
columnType: "integer";
schema: Schema.Schema<boolean, number, never>;
default: Some<false>;
nullable: false;
primaryKey: false;
autoIncrement: false;
};
readonly previousIds: {
...;
};
}>, State.SQLite.WithDefaults<...>, Schema.Schema<...>>
todos
.
select: <"id">(pluckColumn: "id") => QueryBuilder<readonly string[], State.SQLite.TableDefBase<State.SQLite.SqliteTableDefForInput<"todos", {
readonly id: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: true;
autoIncrement: false;
};
readonly text: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: false;
autoIncrement: false;
};
readonly completed: {
columnType: "integer";
schema: Schema.Schema<boolean, number, never>;
default: Some<false>;
nullable: false;
primaryKey: false;
autoIncrement: false;
};
readonly previousIds: {
...;
};
}>, State.SQLite.WithDefaults<...>>, "select" | ... 2 more ... | "row"> (+1 overload)

Selects and plucks a single column

select
('id'))
return
const todos: State.SQLite.TableDef<State.SQLite.SqliteTableDefForInput<"todos", {
readonly id: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: true;
autoIncrement: false;
};
readonly text: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: false;
autoIncrement: false;
};
readonly completed: {
columnType: "integer";
schema: Schema.Schema<boolean, number, never>;
default: Some<false>;
nullable: false;
primaryKey: false;
autoIncrement: false;
};
readonly previousIds: {
...;
};
}>, State.SQLite.WithDefaults<...>, Schema.Schema<...>>
todos
.
insert: (values: {
readonly text: string;
readonly id: string;
readonly completed?: boolean;
readonly previousIds?: readonly string[] | null;
}) => QueryBuilder<readonly {
readonly id: string;
readonly text: string;
readonly completed: boolean;
readonly previousIds: readonly string[] | null;
}[], State.SQLite.TableDefBase<State.SQLite.SqliteTableDefForInput<"todos", {
readonly id: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: true;
autoIncrement: false;
};
readonly text: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: false;
autoIncrement: false;
};
readonly completed: {
...;
};
readonly previousIds: {
...;
};
}>, State.SQLite.WithDefaults<...>>, "select" | ... 6 more ... | "row">

Insert a new row into the table

Example:

db.todos.insert({ id: '123', text: 'Buy milk', status: 'active' })

insert
({
id: string
id
,
text: string
text
,
completed?: boolean
completed
:
completed: boolean | undefined
completed
?? false,
previousIds?: readonly string[] | null
previousIds
})
}),
})

A materializer is always executed in a transaction. This transaction applies to:

  • All database write operations returned by the materializer.
  • Any ctx.query calls made within the materializer, ensuring a consistent view of the data.

Materializers can return:

  • A single database write operation.
  • An array of database write operations.
  • void (i.e., no return value) if no database modifications are needed.
  • An Effect that resolves to one of the above (e.g., Effect.succeed(writeOp) or Effect.void).

The context object passed to each materializer provides query for database reads, db for direct database access if needed, and event for the full event details.

If a materializer function throws an error, or if an Effect returned by a materializer fails, the entire transaction for that event will be rolled back. This means any database changes attempted by that materializer for the failing event will not be persisted. The error will be logged, and the system will typically halt or flag the event as problematic, depending on the specific LiveStore setup.

If the error happens on the client which tries to commit the event, the event will never be committed and pushed to the sync backend.

In the future there will be ways to configure the error-handling behaviour, e.g. to allow skipping an incoming event when a materializer fails in order to avoid the app getting stuck. However, skipping events might also lead to diverging state across clients and should be used with caution.

It’s strongly recommended to make sure your materializers are side-effect free and deterministic. This also implies passing in all necessary data via the event payload.

Example:

import {
function randomUUID(options?: RandomUUIDOptions): UUID

Generates a random RFC 4122 version 4 UUID. The UUID is generated using a cryptographic pseudorandom number generator.

@sincev15.6.0, v14.17.0

randomUUID
} from 'node:crypto'
import {
const defineMaterializer: <TEventDef extends State.SQLite.EventDef.AnyWithoutFn>(_eventDef: TEventDef, materializer: State.SQLite.Materializer<TEventDef>) => State.SQLite.Materializer<TEventDef>
defineMaterializer
,
import Events
Events
,
function nanoid<Type extends string>(size?: number): Type

Generate secure URL-friendly unique ID.

By default, the ID will have 21 symbols to have a collision probability similar to UUID v4.

import { nanoid } from 'nanoid'
model.id = nanoid() //=> "Uakgb_J5m9g-0JDMbcJqL"

@paramsize Size of the ID. The default size is 21.

@returnsA random string.

nanoid
,
import Schema
Schema
,
import State
State
, type
class Store<TSchema extends LiveStoreSchema = LiveStoreSchema.Any, TContext = {}>
Store
} from '@livestore/livestore'
import {
const todos: State.SQLite.TableDef<State.SQLite.SqliteTableDefForInput<"todos", {
readonly id: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: true;
autoIncrement: false;
};
readonly text: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: false;
autoIncrement: false;
};
readonly completed: {
columnType: "integer";
schema: Schema.Schema<boolean, number, never>;
default: Some<false>;
nullable: false;
primaryKey: false;
autoIncrement: false;
};
readonly previousIds: {
...;
};
}>, State.SQLite.WithDefaults<...>, Schema.Schema<...>>
todos
} from './example.ts'
declare const
const store: Store<LiveStoreSchema.Any, {}>
store
:
class Store<TSchema extends LiveStoreSchema = LiveStoreSchema.Any, TContext = {}>
Store
export const
const nondeterministicEvents: {
readonly todoCreated: State.SQLite.EventDef<"v1.TodoCreated", {
readonly text: string;
}, {
readonly text: string;
}, false>;
}
nondeterministicEvents
= {
todoCreated: State.SQLite.EventDef<"v1.TodoCreated", {
readonly text: string;
}, {
readonly text: string;
}, false>
todoCreated
:
import Events
Events
.
synced<"v1.TodoCreated", {
readonly text: string;
}, {
readonly text: string;
}>(args: {
name: "v1.TodoCreated";
schema: Schema.Schema<{
readonly text: string;
}, {
readonly text: string;
}, never>;
} & Omit<State.SQLite.DefineEventOptions<{
readonly text: string;
}, false>, "derived" | "clientOnly">): State.SQLite.EventDef<"v1.TodoCreated", {
readonly text: string;
}, {
readonly text: string;
}, false>
export synced
synced
({
name: "v1.TodoCreated"
name
: 'v1.TodoCreated',
schema: Schema.Schema<{
readonly text: string;
}, {
readonly text: string;
}, never>
schema
:
import Schema
Schema
.
function Struct<{
text: typeof Schema.String;
}>(fields: {
text: typeof Schema.String;
}): Schema.Struct<{
text: typeof Schema.String;
}> (+1 overload)

@since3.10.0

Struct
({
text: typeof Schema.String
text
:
import Schema
Schema
.
class String
export String

@since3.10.0

String
}),
}),
} as
type const = {
readonly todoCreated: State.SQLite.EventDef<"v1.TodoCreated", {
readonly text: string;
}, {
readonly text: string;
}, false>;
}
const
export const
const nondeterministicMaterializers: {
"v1.TodoCreated": State.SQLite.Materializer<State.SQLite.EventDef<"v1.TodoCreated", {
readonly text: string;
}, {
readonly text: string;
}, false>>;
}
nondeterministicMaterializers
=
import State
State
.
import SQLite
SQLite
.
const materializers: <{
readonly todoCreated: State.SQLite.EventDef<"v1.TodoCreated", {
readonly text: string;
}, {
readonly text: string;
}, false>;
}>(_eventDefRecord: {
readonly todoCreated: State.SQLite.EventDef<"v1.TodoCreated", {
readonly text: string;
}, {
readonly text: string;
}, false>;
}, handlers: {
"v1.TodoCreated": State.SQLite.Materializer<State.SQLite.EventDef<"v1.TodoCreated", {
readonly text: string;
}, {
readonly text: string;
}, false>>;
}) => {
"v1.TodoCreated": State.SQLite.Materializer<State.SQLite.EventDef<"v1.TodoCreated", {
readonly text: string;
}, {
readonly text: string;
}, false>>;
}
materializers
(
const nondeterministicEvents: {
readonly todoCreated: State.SQLite.EventDef<"v1.TodoCreated", {
readonly text: string;
}, {
readonly text: string;
}, false>;
}
nondeterministicEvents
, {
[
const nondeterministicEvents: {
readonly todoCreated: State.SQLite.EventDef<"v1.TodoCreated", {
readonly text: string;
}, {
readonly text: string;
}, false>;
}
nondeterministicEvents
.
todoCreated: State.SQLite.EventDef<"v1.TodoCreated", {
readonly text: string;
}, {
readonly text: string;
}, false>
todoCreated
.
name: "v1.TodoCreated"
name
]:
defineMaterializer<State.SQLite.EventDef<"v1.TodoCreated", {
readonly text: string;
}, {
readonly text: string;
}, false>>(_eventDef: State.SQLite.EventDef<"v1.TodoCreated", {
readonly text: string;
}, {
readonly text: string;
}, false>, materializer: State.SQLite.Materializer<State.SQLite.EventDef<"v1.TodoCreated", {
readonly text: string;
}, {
readonly text: string;
}, false>>): State.SQLite.Materializer<State.SQLite.EventDef<"v1.TodoCreated", {
readonly text: string;
}, {
readonly text: string;
}, false>>
defineMaterializer
(
const nondeterministicEvents: {
readonly todoCreated: State.SQLite.EventDef<"v1.TodoCreated", {
readonly text: string;
}, {
readonly text: string;
}, false>;
}
nondeterministicEvents
.
todoCreated: State.SQLite.EventDef<"v1.TodoCreated", {
readonly text: string;
}, {
readonly text: string;
}, false>
todoCreated
, ({
text: string
text
}) =>
const todos: State.SQLite.TableDef<State.SQLite.SqliteTableDefForInput<"todos", {
readonly id: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: true;
autoIncrement: false;
};
readonly text: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: false;
autoIncrement: false;
};
readonly completed: {
columnType: "integer";
schema: Schema.Schema<boolean, number, never>;
default: Some<false>;
nullable: false;
primaryKey: false;
autoIncrement: false;
};
readonly previousIds: {
...;
};
}>, State.SQLite.WithDefaults<...>, Schema.Schema<...>>
todos
.
insert: (values: {
readonly text: string;
readonly id: string;
readonly completed?: boolean;
readonly previousIds?: readonly string[] | null;
}) => QueryBuilder<readonly {
readonly id: string;
readonly text: string;
readonly completed: boolean;
readonly previousIds: readonly string[] | null;
}[], State.SQLite.TableDefBase<State.SQLite.SqliteTableDefForInput<"todos", {
readonly id: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: true;
autoIncrement: false;
};
readonly text: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: false;
autoIncrement: false;
};
readonly completed: {
...;
};
readonly previousIds: {
...;
};
}>, State.SQLite.WithDefaults<...>>, "select" | ... 6 more ... | "row">

Insert a new row into the table

Example:

db.todos.insert({ id: '123', text: 'Buy milk', status: 'active' })

insert
({
id: string
id
:
function randomUUID(options?: RandomUUIDOptions): UUID

Generates a random RFC 4122 version 4 UUID. The UUID is generated using a cryptographic pseudorandom number generator.

@sincev15.6.0, v14.17.0

randomUUID
(),
text: string
text
}),
),
})
const store: Store<LiveStoreSchema.Any, {}>
store
.
Store<LiveStoreSchema<TDbSchema extends DbSchema = DbSchema, TEventsDefRecord extends EventDefRecord = EventDefRecord>.Any, {}>.commit: <readonly [{
name: "v1.TodoCreated";
args: {
readonly text: string;
};
}]>(list_0: {
name: "v1.TodoCreated";
args: {
readonly text: string;
};
}) => void (+3 overloads)
commit
(
const nondeterministicEvents: {
readonly todoCreated: State.SQLite.EventDef<"v1.TodoCreated", {
readonly text: string;
}, {
readonly text: string;
}, false>;
}
nondeterministicEvents
.
todoCreated: (args: {
readonly text: string;
}) => {
name: "v1.TodoCreated";
args: {
readonly text: string;
};
}

Helper function to construct a partial event

todoCreated
({
text: string
text
: 'Buy groceries' }))
export const
const deterministicEvents: {
readonly todoCreated: State.SQLite.EventDef<"v1.TodoCreated", {
readonly text: string;
readonly id: string;
}, {
readonly text: string;
readonly id: string;
}, false>;
}
deterministicEvents
= {
todoCreated: State.SQLite.EventDef<"v1.TodoCreated", {
readonly text: string;
readonly id: string;
}, {
readonly text: string;
readonly id: string;
}, false>
todoCreated
:
import Events
Events
.
synced<"v1.TodoCreated", {
readonly text: string;
readonly id: string;
}, {
readonly text: string;
readonly id: string;
}>(args: {
name: "v1.TodoCreated";
schema: Schema.Schema<{
readonly text: string;
readonly id: string;
}, {
readonly text: string;
readonly id: string;
}, never>;
} & Omit<State.SQLite.DefineEventOptions<{
readonly text: string;
readonly id: string;
}, false>, "derived" | "clientOnly">): State.SQLite.EventDef<"v1.TodoCreated", {
readonly text: string;
readonly id: string;
}, {
readonly text: string;
readonly id: string;
}, false>
export synced
synced
({
name: "v1.TodoCreated"
name
: 'v1.TodoCreated',
schema: Schema.Schema<{
readonly text: string;
readonly id: string;
}, {
readonly text: string;
readonly id: string;
}, never>
schema
:
import Schema
Schema
.
function Struct<{
id: typeof Schema.String;
text: typeof Schema.String;
}>(fields: {
id: typeof Schema.String;
text: typeof Schema.String;
}): Schema.Struct<{
id: typeof Schema.String;
text: typeof Schema.String;
}> (+1 overload)

@since3.10.0

Struct
({
id: typeof Schema.String
id
:
import Schema
Schema
.
class String
export String

@since3.10.0

String
,
text: typeof Schema.String
text
:
import Schema
Schema
.
class String
export String

@since3.10.0

String
}),
}),
} as
type const = {
readonly todoCreated: State.SQLite.EventDef<"v1.TodoCreated", {
readonly text: string;
readonly id: string;
}, {
readonly text: string;
readonly id: string;
}, false>;
}
const
export const
const deterministicMaterializers: {
"v1.TodoCreated": State.SQLite.Materializer<State.SQLite.EventDef<"v1.TodoCreated", {
readonly text: string;
readonly id: string;
}, {
readonly text: string;
readonly id: string;
}, false>>;
}
deterministicMaterializers
=
import State
State
.
import SQLite
SQLite
.
const materializers: <{
readonly todoCreated: State.SQLite.EventDef<"v1.TodoCreated", {
readonly text: string;
readonly id: string;
}, {
readonly text: string;
readonly id: string;
}, false>;
}>(_eventDefRecord: {
readonly todoCreated: State.SQLite.EventDef<"v1.TodoCreated", {
readonly text: string;
readonly id: string;
}, {
readonly text: string;
readonly id: string;
}, false>;
}, handlers: {
"v1.TodoCreated": State.SQLite.Materializer<State.SQLite.EventDef<"v1.TodoCreated", {
readonly text: string;
readonly id: string;
}, {
readonly text: string;
readonly id: string;
}, false>>;
}) => {
"v1.TodoCreated": State.SQLite.Materializer<State.SQLite.EventDef<"v1.TodoCreated", {
readonly text: string;
readonly id: string;
}, {
readonly text: string;
readonly id: string;
}, false>>;
}
materializers
(
const deterministicEvents: {
readonly todoCreated: State.SQLite.EventDef<"v1.TodoCreated", {
readonly text: string;
readonly id: string;
}, {
readonly text: string;
readonly id: string;
}, false>;
}
deterministicEvents
, {
[
const deterministicEvents: {
readonly todoCreated: State.SQLite.EventDef<"v1.TodoCreated", {
readonly text: string;
readonly id: string;
}, {
readonly text: string;
readonly id: string;
}, false>;
}
deterministicEvents
.
todoCreated: State.SQLite.EventDef<"v1.TodoCreated", {
readonly text: string;
readonly id: string;
}, {
readonly text: string;
readonly id: string;
}, false>
todoCreated
.
name: "v1.TodoCreated"
name
]:
defineMaterializer<State.SQLite.EventDef<"v1.TodoCreated", {
readonly text: string;
readonly id: string;
}, {
readonly text: string;
readonly id: string;
}, false>>(_eventDef: State.SQLite.EventDef<"v1.TodoCreated", {
readonly text: string;
readonly id: string;
}, {
readonly text: string;
readonly id: string;
}, false>, materializer: State.SQLite.Materializer<State.SQLite.EventDef<"v1.TodoCreated", {
readonly text: string;
readonly id: string;
}, {
readonly text: string;
readonly id: string;
}, false>>): State.SQLite.Materializer<State.SQLite.EventDef<"v1.TodoCreated", {
readonly text: string;
readonly id: string;
}, {
readonly text: string;
readonly id: string;
}, false>>
defineMaterializer
(
const deterministicEvents: {
readonly todoCreated: State.SQLite.EventDef<"v1.TodoCreated", {
readonly text: string;
readonly id: string;
}, {
readonly text: string;
readonly id: string;
}, false>;
}
deterministicEvents
.
todoCreated: State.SQLite.EventDef<"v1.TodoCreated", {
readonly text: string;
readonly id: string;
}, {
readonly text: string;
readonly id: string;
}, false>
todoCreated
, ({
id: string
id
,
text: string
text
}) =>
const todos: State.SQLite.TableDef<State.SQLite.SqliteTableDefForInput<"todos", {
readonly id: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: true;
autoIncrement: false;
};
readonly text: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: false;
autoIncrement: false;
};
readonly completed: {
columnType: "integer";
schema: Schema.Schema<boolean, number, never>;
default: Some<false>;
nullable: false;
primaryKey: false;
autoIncrement: false;
};
readonly previousIds: {
...;
};
}>, State.SQLite.WithDefaults<...>, Schema.Schema<...>>
todos
.
insert: (values: {
readonly text: string;
readonly id: string;
readonly completed?: boolean;
readonly previousIds?: readonly string[] | null;
}) => QueryBuilder<readonly {
readonly id: string;
readonly text: string;
readonly completed: boolean;
readonly previousIds: readonly string[] | null;
}[], State.SQLite.TableDefBase<State.SQLite.SqliteTableDefForInput<"todos", {
readonly id: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: true;
autoIncrement: false;
};
readonly text: {
columnType: "text";
schema: Schema.Schema<string, string, never>;
default: None<never>;
nullable: false;
primaryKey: false;
autoIncrement: false;
};
readonly completed: {
...;
};
readonly previousIds: {
...;
};
}>, State.SQLite.WithDefaults<...>>, "select" | ... 6 more ... | "row">

Insert a new row into the table

Example:

db.todos.insert({ id: '123', text: 'Buy milk', status: 'active' })

insert
({
id: string
id
,
text: string
text
}),
),
})
const store: Store<LiveStoreSchema.Any, {}>
store
.
Store<LiveStoreSchema<TDbSchema extends DbSchema = DbSchema, TEventsDefRecord extends EventDefRecord = EventDefRecord>.Any, {}>.commit: <readonly [{
name: "v1.TodoCreated";
args: {
readonly text: string;
readonly id: string;
};
}]>(list_0: {
name: "v1.TodoCreated";
args: {
readonly text: string;
readonly id: string;
};
}) => void (+3 overloads)
commit
(
const deterministicEvents: {
readonly todoCreated: State.SQLite.EventDef<"v1.TodoCreated", {
readonly text: string;
readonly id: string;
}, {
readonly text: string;
readonly id: string;
}, false>;
}
deterministicEvents
.
todoCreated: (args: {
readonly text: string;
readonly id: string;
}) => {
name: "v1.TodoCreated";
args: {
readonly text: string;
readonly id: string;
};
}

Helper function to construct a partial event

todoCreated
({
id: string
id
:
nanoid<string>(size?: number): string

Generate secure URL-friendly unique ID.

By default, the ID will have 21 symbols to have a collision probability similar to UUID v4.

import { nanoid } from 'nanoid'
model.id = nanoid() //=> "Uakgb_J5m9g-0JDMbcJqL"

@paramsize Size of the ID. The default size is 21.

@returnsA random string.

nanoid
(),
text: string
text
: 'Buy groceries' }))