The Environment API lets you use Lix across different platforms: browser tabs, Node.js servers, or test suites.
Environments define where the Lix engine executes and how data is stored. You can run Lix in-memory for testing, use OPFS for browser persistence, or build custom integrations for your own backend.
When building applications with Lix, you need different storage and runtime strategies depending on where your code runs:
Environments solve this by providing a consistent interface while handling platform-specific details under the hood.
The Lix SDK provides two built-in environments that cover most use cases:
The simplest environment that runs everything in memory on the current thread.
When to use:
Characteristics:
A production-ready browser environment using Web Workers and OPFS SAH (Origin Private File System with Synchronous Access Handles). SAH provides the fastest SQLite performance in browsers by allowing synchronous file operations in a worker.
When to use:
Characteristics:
Each environment is a runtime container that hosts the Lix Engine and manages two key responsibilities:
Environments decide where and how your SQLite database lives: in memory, in browser storage, or on disk. They handle creating, loading, and persisting your data.
Environments determine where the Lix engine executes:
The engine always runs inside the environment, never separately.
Most applications should use openLix() which automatically selects the right environment:
import { openLix } from "@lix/sdk";
// Automatically uses OpfsSahEnvironment in browsers,
// InMemoryEnvironment in Node.js
const lix = await openLix();When you need specific control:
import { openLix, OpfsSahEnvironment, InMemoryEnvironment } from "@lix/sdk";
// For production browser apps
const lix = await openLix({
environment: new OpfsSahEnvironment({ key: "my-app" }),
});
// For tests
const lix = await openLix({
environment: new InMemoryEnvironment(),
});If you need to run Lix somewhere else (another storage backend, a specialized worker, a native shell), you can implement your own environment by satisfying the LixEnvironment interface.
At minimum you must provide persistence primitives (create, exists, exec, export, close) and implement open() so the SDK can boot the engine next to your database. The call() method must forward engine RPCs across your boundary (e.g. to a worker thread or other process).
Environments that support isolated execution can additionally expose spawnActor(opts):
const actor = await environment.spawnActor?.({
entryModule: new URL("./diff-worker.js", import.meta.url).href,
name: "diff-render",
});
actor?.subscribe((message) => console.log("actor message", message));
actor?.post({ type: "render", payload: diffs });The SDK uses this capability to offload work (for example, diff rendering) when available, but will fall back to running tasks inline when spawnActor is absent. When authoring a custom environment you only need to implement this hook if you can actually spawn workers in your target runtime; otherwise, you can omit it.