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:
Copy
// Pull changes from the remote databaseawait db.pull();// Push local changes to the remote databaseawait db.push();// Pull and push changes in one operationawait db.sync();
4
Working with Data
You can perform regular database operations alongside sync:
Copy
// Execute SQL statementsawait db.execute( "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)",);// Insert dataawait db.execute("INSERT INTO users (name) VALUES (?)", ["Alice"]);// Query dataconst result = await db.execute("SELECT * FROM users");console.log(result.rows);// Sync changes with remoteawait db.sync();