Skip to content
Contents

Embedded

Use Rindle as an embedded database

Embedded databases apply the same saved migration steps as rindle migrate apply. Author steps with @rindle/migrate, compile SQL with rindle migrate compile or compileMigration, or load the JSON step files your application ships.

Each step runs in one transaction. Replaying the same id and changes skips it; reusing an id with different changes fails. If a later step fails, earlier steps stay committed. Read appliedMigrations() to see what landed. Column backfills and index builds run with writes blocked; migrations currently use the blocking route.

compileMigration(id, priorSchema, sql) compiles against the schema the migration was authored for. Save its returned step and replay that step on startup. Recompiling an old script against the schema it already changed can fail, for example because its table already exists.

@rindle/replica stores pages in a file when you supply path. Apply migrations before the typed schema registers its tables or any views are materialized:

import {
  compileMigration, createReplicaStore, createSchema, number, string, table,
} from "@rindle/replica";

// Author once, then save this step with the application, or load its JSON file.
const init = compileMigration("0001_init", [], `
  CREATE TABLE issue (id REAL PRIMARY KEY, title TEXT NOT NULL);
`);

const issue = table("issue")
  .columns({ id: number(), title: string() })
  .primaryKey("id");
const schema = createSchema({ tables: [issue] });
const store = createReplicaStore(schema, {
  path: "app.paged",
  migrations: [init],
});

const issues = store.query.issue.orderBy("id", "asc").materialize();
issues.subscribe(() => console.table(issues.data));
await store.write((tx) => tx.add("issue", { id: 1, title: "first" }));
issues.destroy();

Omitting path gives a temporary file that is removed when the handle is finalized.

The low-level NativeReplicaDb and NativeClusterDb also expose migrate(steps), schema() and appliedMigrations(). migrate returns { applied, ended }. On the single-thread handle, ended contains caller query ids whose tables changed; register those queries again against the new schema. The cluster delivers schema faults through onEvent; its returned ended contains engine ids for diagnostics. A transaction must be closed before migrating.

The standalone paged browser home is an opt-in entry point, @rindle/wasm/embedded. Its data lives in wasm memory for the lifetime of the handle; it does not persist across a reload. Initialize its separate artifact before compiling or applying steps:

import {
  initEmbeddedWasm, compileMigration, EmbeddedBackend,
  Store, createSchema, number, string, table,
} from "@rindle/wasm/embedded";

await initEmbeddedWasm();
const init = compileMigration("0001_init", [], `
  CREATE TABLE issue (id REAL PRIMARY KEY, title TEXT NOT NULL);
`);
const backend = new EmbeddedBackend([init]);

const issue = table("issue")
  .columns({ id: number(), title: string() })
  .primaryKey("id");
const store = new Store(createSchema({ tables: [issue] }), backend);
const issues = store.query.issue.orderBy("id", "asc").materialize();
await store.write((tx) => tx.add("issue", { id: 1, title: "first" }));
console.table(issues.data);
issues.destroy();

console.table(backend.appliedMigrations());
backend.free();

createEmbeddedStore(schema, steps) initializes the artifact and constructs the store for you. To change an existing backend, destroy its materialized views, call backend.migrate(steps), then construct a store with the updated typed schema. Migration while views remain open is refused. backend.schema() returns its current schema image.

The main @rindle/wasm entry point remains the optimistic browser cache. Its registered sources are separate from this standalone database.

The embedded Rust migration surface is rindle_paged::PagedDb:

use rindle_paged::PagedDb;

let (db, _) = PagedDb::open("app.paged", &Default::default())?;
// Load the reviewed JSON step your application ships.
let init = serde_json::from_str(include_str!("0001_init.json"))?;
let result = db.migrate(&[init])?;
println!("applied: {:?}, ended queries: {:?}", result.applied, result.ended);
let journal = db.applied_migrations()?;

db.schema() returns the current image. rindle_sql_ddl::compile_step(id, prior, sql) is the same compiler the CLI and JavaScript bindings use. Close open write transactions before migrating, and re-register queries named by result.ended after a schema change.

Adding a Server

The same migration step files apply to a hosted database through rindle migrate apply --cloud. See SQL migrations and Get Started for the managed setup.