JavaScript quickstart

This guide creates an in-memory Lix repository, writes a file, reads its history, and undoes the latest change.

Install

npm install @lix-js/sdk

Write and update a file

import { openLix } from "@lix-js/sdk";

const lix = await openLix();

await lix.execute("INSERT INTO lix_file (path, content) VALUES ($1, $2)", [
  "/hello.txt",
  new TextEncoder().encode("Hello"),
]);

await lix.execute("UPDATE lix_file SET content = $1 WHERE path = $2", [
  new TextEncoder().encode("Hello from Lix"),
  "/hello.txt",
]);

Lix records both writes automatically. You do not need to create commits.

execute() runs one statement. To run several statements atomically, pass an array of statements to lix.executeBatch(). Do not concatenate SQL into one script string.

Read history

const history = await lix.execute(
  `SELECT path, content, lixcol_depth
     FROM lix_history('lix_file')
    WHERE path = $1
    ORDER BY lixcol_depth`,
  ["/hello.txt"],
);

for (const row of history.rows) {
  const bytes = row.content as Uint8Array;
  const text = bytes ? new TextDecoder().decode(bytes) : "<deleted>";
  console.log(row.lixcol_depth, text);
}

Depth 0 is the state at the head. Higher numbers walk back through history.

Undo the update

await lix.undo();
await lix.close();

The repository is in memory and disappears when the process ends. Continue with Persistence and Storage to save it locally or connect to a server.

Next