Skip to content

table

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:

  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 },
],
})

TName extends string

TColumns extends Columns | Any

TOptionsInput extends Partial<{ indexes: Index[]; }> = Partial<{ indexes: Index[]; }>

object & Partial<TOptionsInput>

TableDef<SqliteTableDefForInput<TName, TColumns>, WithDefaults<TColumns>>

  • Primary key columns are automatically non-nullable
  • Columns with State.SQLite.withUnique annotation automatically get unique indexes
  • The State.SQLite.withAutoIncrement annotation 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

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:

  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 },
],
})

TName extends string

TSchema extends AnyNoContext

TOptionsInput extends Partial<{ indexes: Index[]; }> = Partial<{ indexes: Index[]; }>

object & Partial<TOptionsInput>

TableDef<SqliteTableDefForSchemaInput<TName, Type<TSchema>, Encoded<TSchema>, TSchema>>

  • Primary key columns are automatically non-nullable
  • Columns with State.SQLite.withUnique annotation automatically get unique indexes
  • The State.SQLite.withAutoIncrement annotation 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

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:

  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 },
],
})

TSchema extends AnyNoContext

TOptionsInput extends Partial<{ indexes: Index[]; }> = Partial<{ indexes: Index[]; }>

object & Partial<TOptionsInput>

TableDef<SqliteTableDefForSchemaInput<string, Type<TSchema>, Encoded<TSchema>, TSchema>>

  • Primary key columns are automatically non-nullable
  • Columns with State.SQLite.withUnique annotation automatically get unique indexes
  • The State.SQLite.withAutoIncrement annotation 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