import {
createStorePromise,
Events,
makeSchema,
nanoid,
queryDb,
Schema,
signal,
State,
storeOptions,
} from '@livestore/livestore'
// 1. Define events
const events = {
todoCreated: Events.synced({
name: 'v1.TodoCreated',
schema: Schema.Struct({ id: Schema.String, text: Schema.String }),
}),
todoCompleted: Events.synced({
name: 'v1.TodoCompleted',
schema: Schema.Struct({ id: Schema.String }),
}),
}
// 2. Define state tables
const tables = {
todos: State.SQLite.table({
name: 'todos',
columns: {
id: State.SQLite.text({ primaryKey: true }),
text: State.SQLite.text({ default: '' }),
completed: State.SQLite.boolean({ default: false }),
},
}),
}
// 3. Compose the schema
const schema = makeSchema({
events,
state: State.SQLite.makeState({ tables }),
})
// 4. Create a store
const store = await createStorePromise({ schema, adapter, storeId: 'todos' })
// 5. Query and commit
const todos = store.query(queryDb(tables.todos.query))
store.commit(events.todoCreated({ id: nanoid(), text: 'Buy groceries' }))