1

Installation

Install the Turso Sync package:
npm install @tursodatabase/sync
2

Getting Started

To sync a database hosted at Turso Cloud:
import { connect } from "@tursodatabase/sync";

const db = await connect({
  path: "local.db", // path used as a prefix for local files created by sync-engine
  url: "https://<db>.turso.io", // URL of the remote database: turso db show <db>
  authToken: "...", // auth token issued from the Turso Cloud: turso db tokens create <db>
  clientName: "turso-sync-example", // arbitrary client name
});
3

Sync Operations

The db object has the same functions as the Database class from @tursodatabase/database package but adds additional methods for sync:
// Pull changes from the remote database
await db.pull();

// Push local changes to the remote database
await db.push();

// Pull and push changes in one operation
await db.sync();
4

Working with Data

You can perform regular database operations alongside sync:
// Execute SQL statements
await db.execute(
  "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)",
);

// Insert data
await db.execute("INSERT INTO users (name) VALUES (?)", ["Alice"]);

// Query data
const result = await db.execute("SELECT * FROM users");
console.log(result.rows);

// Sync changes with remote
await db.sync();