This example demonstrates how to set up a client-side SQLite database using OPFS (Origin Private File System) with Effect's @effect/sql-sqlite-wasm package.
Setup
The implementation consists of 3 main files:
Worker
worker.ts creates a Web Worker that runs the SQLite database with OPFS storage.
It uses OpfsWorker.run from @effect/sql-sqlite-wasm to initialize and manage the worker lifecycle.
This is where you specify the SQLite database name (
sync.sqlitein the example).
SQL Client Layer
sql.ts sets up the SQLite client and database migrations.
The SqliteClient comes from @effect/sql-sqlite-wasm and it's created from the worker.
Adding
?workerallows to import the worker as a module invite.Add also the following
vite.config.ts:import { defineConfig } from "vite"; export default defineConfig({ optimizeDeps: { exclude: ["@effect/wa-sqlite"], }, worker: { format: "es", }, });
The SqlClient and SqliteClient layers are created from a Migrator. In the example the initial migration creates the settings table and inserts an example row.
SqlClientandSqliteClientare merged in a singleSqlLivelayer.
Service
The Settings service is an example of how to use SqlClient to query the database.
This is not specific to SQLite, it uses the standard
@effect/sqlAPI.
You can then provide SqlLive to access the SQLite database instance.
When you run a query, the layer would spawn the worker, initialize the database (migrations), and execute the query on the SQLite database 🪄