SQLiteStateStore
Automatic Engine Detection
Section titled “Automatic Engine Detection”SQLiteStateStore automatically detects available SQLite engines in this order:
- Bun SQLite (
bun:sqlite
) - Built into Bun runtime - libSQL (
@libsql/client
) - Turso-compatible SQLite
Explicit Engine Selection
Section titled “Explicit Engine Selection”Force a specific SQLite engine:
const app = await alchemy("my-app", { stateStore: (scope) => new SQLiteStateStore(scope, { engine: "bun", filename: ".alchemy/state.sqlite" })});
const app = await alchemy("my-app", { stateStore: (scope) => new SQLiteStateStore(scope, { engine: "libsql", url: "file:.alchemy/state.sqlite" })});
Custom Database Location
Section titled “Custom Database Location”Change the database file location:
const app = await alchemy("my-app", { stateStore: (scope) => new SQLiteStateStore(scope, { filename: "./data/my-app-state.db" })});
Environment Variable Configuration
Section titled “Environment Variable Configuration”Use environment variables for flexible configuration:
ALCHEMY_STATE_FILE=./data/production-state.sqlite
// alchemy.run.ts - automatically uses ALCHEMY_STATE_FILEconst app = await alchemy("my-app", { stateStore: (scope) => new SQLiteStateStore(scope)});
Engine-Specific Options
Section titled “Engine-Specific Options”Configure engine-specific options:
const app = await alchemy("my-app", { stateStore: (scope) => new SQLiteStateStore(scope, { engine: "bun", readonly: false, create: true, safeIntegers: true, strict: true })});
const app = await alchemy("my-app", { stateStore: (scope) => new SQLiteStateStore(scope, { engine: "libsql", url: "libsql://my-db.turso.io", authToken: process.env.TURSO_AUTH_TOKEN, syncUrl: "file:./local-replica.db" })});
Development vs Production
Section titled “Development vs Production”Use different configurations for different environments:
const isDev = process.env.NODE_ENV === "development";
const app = await alchemy("my-app", { stateStore: (scope) => new SQLiteStateStore(scope, { filename: isDev ? ".alchemy/dev-state.sqlite" : "/var/lib/myapp/prod-state.sqlite", })});
Per-Stage Databases
Section titled “Per-Stage Databases”Use separate databases for different deployment stages:
const stage = process.env.STAGE ?? "dev";
const app = await alchemy("my-app", { stage, stateStore: (scope) => new SQLiteStateStore(scope, { filename: `.alchemy/state-${stage}.sqlite`, })});
Turso Database
Section titled “Turso Database”Connect to a remote Turso database:
const app = await alchemy("my-app", { stateStore: (scope) => new SQLiteStateStore(scope, { engine: "libsql", url: process.env.TURSO_DATABASE_URL, authToken: process.env.TURSO_AUTH_TOKEN })});
Local Replica with Sync
Section titled “Local Replica with Sync”Use a local replica that syncs with remote:
const app = await alchemy("my-app", { stateStore: (scope) => new SQLiteStateStore(scope, { engine: "libsql", url: process.env.TURSO_DATABASE_URL, authToken: process.env.TURSO_AUTH_TOKEN, syncUrl: "file:./local-replica.db", syncInterval: 60 // Sync every 60 seconds })});
Engine Performance
Section titled “Engine Performance”- Bun SQLite: Fastest for Bun runtime, zero additional dependencies
- libSQL: Best for remote/distributed scenarios, async API
Peer Dependencies
Section titled “Peer Dependencies”The SQLiteStateStore requires drizzle-orm
as a peer dependency. Additionally, if you are using the libsql
engine (default for Node.js), you will need to install @libsql/client
as a peer dependency.
If you see dependency errors:
[SQLiteStateStore] Missing `@libsql/client` peer dependency. Please `npm install @libsql/client`.
Install the required packages:
npm install drizzle-orm @libsql/client
Permission Errors
Section titled “Permission Errors”Ensure the directory is writable:
mkdir -p .alchemychmod 755 .alchemy