table
Call Signature
Section titled “Call Signature”table<
TName,TColumns,TOptionsInput>(args):TableDef<SqliteTableDefForInput<TName,TColumns>,WithDefaults<TColumns>>
Defined in: packages/@livestore/common/dist/schema/state/sqlite/table-def.d.ts:111
Creates a SQLite table definition from columns or an Effect Schema.
This function supports two main ways to define a table:
- Using explicit column definitions
- Using an Effect Schema (either the
nameproperty needs to be provided or the schema needs to have a title/identifier)
// Using explicit columnsconst 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 annotationsimport { 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 nameconst 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 indexesconst 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 }, ],})Type Parameters
Section titled “Type Parameters”TName extends string
TColumns
Section titled “TColumns”TColumns extends Columns | Any
TOptionsInput
Section titled “TOptionsInput”TOptionsInput extends Partial<{ indexes: Index[]; }> = Partial<{ indexes: Index[]; }>
Parameters
Section titled “Parameters”object & Partial<TOptionsInput>
Returns
Section titled “Returns”TableDef<SqliteTableDefForInput<TName, TColumns>, WithDefaults<TColumns>>
Remarks
Section titled “Remarks”- Primary key columns are automatically non-nullable
- Columns with
State.SQLite.withUniqueannotation automatically get unique indexes - The
State.SQLite.withAutoIncrementannotation only works with integer primary keys - Default values can be literal values or SQL expressions
- When using Effect Schema without explicit name, the schema must have a title or identifier annotation
Call Signature
Section titled “Call Signature”table<
TName,TSchema,TOptionsInput>(args):TableDef<SqliteTableDefForSchemaInput<TName,Type<TSchema>,Encoded<TSchema>,TSchema>>
Defined in: packages/@livestore/common/dist/schema/state/sqlite/table-def.d.ts:115
Creates a SQLite table definition from columns or an Effect Schema.
This function supports two main ways to define a table:
- Using explicit column definitions
- Using an Effect Schema (either the
nameproperty needs to be provided or the schema needs to have a title/identifier)
// Using explicit columnsconst 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 annotationsimport { 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 nameconst 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 indexesconst 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 }, ],})Type Parameters
Section titled “Type Parameters”TName extends string
TSchema
Section titled “TSchema”TSchema extends AnyNoContext
TOptionsInput
Section titled “TOptionsInput”TOptionsInput extends Partial<{ indexes: Index[]; }> = Partial<{ indexes: Index[]; }>
Parameters
Section titled “Parameters”object & Partial<TOptionsInput>
Returns
Section titled “Returns”TableDef<SqliteTableDefForSchemaInput<TName, Type<TSchema>, Encoded<TSchema>, TSchema>>
Remarks
Section titled “Remarks”- Primary key columns are automatically non-nullable
- Columns with
State.SQLite.withUniqueannotation automatically get unique indexes - The
State.SQLite.withAutoIncrementannotation only works with integer primary keys - Default values can be literal values or SQL expressions
- When using Effect Schema without explicit name, the schema must have a title or identifier annotation
Call Signature
Section titled “Call Signature”table<
TSchema,TOptionsInput>(args):TableDef<SqliteTableDefForSchemaInput<string,Type<TSchema>,Encoded<TSchema>,TSchema>>
Defined in: packages/@livestore/common/dist/schema/state/sqlite/table-def.d.ts:119
Creates a SQLite table definition from columns or an Effect Schema.
This function supports two main ways to define a table:
- Using explicit column definitions
- Using an Effect Schema (either the
nameproperty needs to be provided or the schema needs to have a title/identifier)
// Using explicit columnsconst 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 annotationsimport { 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 nameconst 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 indexesconst 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 }, ],})Type Parameters
Section titled “Type Parameters”TSchema
Section titled “TSchema”TSchema extends AnyNoContext
TOptionsInput
Section titled “TOptionsInput”TOptionsInput extends Partial<{ indexes: Index[]; }> = Partial<{ indexes: Index[]; }>
Parameters
Section titled “Parameters”object & Partial<TOptionsInput>
Returns
Section titled “Returns”TableDef<SqliteTableDefForSchemaInput<string, Type<TSchema>, Encoded<TSchema>, TSchema>>
Remarks
Section titled “Remarks”- Primary key columns are automatically non-nullable
- Columns with
State.SQLite.withUniqueannotation automatically get unique indexes - The
State.SQLite.withAutoIncrementannotation only works with integer primary keys - Default values can be literal values or SQL expressions
- When using Effect Schema without explicit name, the schema must have a title or identifier annotation