add sync node example

This commit is contained in:
Nikita Sivukhin
2025-10-03 14:18:52 +04:00
parent b67ef8a7eb
commit 73049a0205
6 changed files with 100 additions and 2 deletions

View File

@@ -199,3 +199,9 @@ Cargo.lock
npm
bundle
*.db
*.db-wal
*.db-changes
*.db-wal-revert
*.db-info

View File

@@ -0,0 +1,30 @@
# sync-node
This is a minimal example showing how to use [`@tursodatabase/sync`](https://www.npmjs.com/package/@tursodatabase/database) in the node.js
The `@tursodatabase/sync` package extends a regular database with **bidirectional synchronization** between a local file and a remote [Turso Cloud](https://turso.tech/) database.
It allows you to:
* **`pull()`**: fetch and apply remote changes into your local database.
This ensures your local state reflects the latest server-side updates.
* **`push()`**: upload local changes back to the remote database.
This is useful when youve made inserts/updates locally and want to replicate them to the cloud.
Together, these methods make it possible to work **offline-first** (mutating the local database) and later sync your changes to Turso Cloud, while also bringing in any updates from other clients.
---
## Usage
```bash
npm install
TURSO_AUTH_TOKEN=$(turso db tokens create <db-name>) # create auth token for the database in the Turso Cloud
TURSO_DATABASE_URL=$(turso db show <db-name> --url) # fetch URL for the database in the Turso Cloud
node index.mjs
```
Heres a version of your README with a short high-level explanation of what the **sync package** does, and what `pull`/`push` mean in practice:

View File

@@ -0,0 +1,40 @@
import { connect } from "@tursodatabase/sync";
const db = await connect({
path: "local.db", // local path to store database
authToken: process.env.TURSO_AUTH_TOKEN, // Turso Cloud auth token
url: process.env.TURSO_DATABASE_URL, // Turso Cloud database url
longPollTimeoutMs: 0, // optional long-polling interval for pull operation; useful in case when pull called in a loop
});
// execute multiple SQL statements with exec(...)
await db.exec(`
CREATE TABLE IF NOT EXISTS guestbook (comment TEXT, created_at DEFAULT (unixepoch()));
CREATE INDEX IF NOT EXISTS guestbook_idx ON guestbook (created_at);
`);
// use prepared statements and bind args to placeholders later
const select1 = db.prepare(`SELECT * FROM guestbook ORDER BY created_at DESC LIMIT ?`);
// use all(...) or get(...) methods to get all or one row from the query
console.info(await select1.all([5]))
try {
// pull new changes from the remote
await db.pull();
} catch (e) {
console.error('pull failed', e);
}
const insert = db.prepare(`INSERT INTO guestbook(comment) VALUES (?)`);
// use run(...) method if query only need to be executed till completion
await insert.run([`hello, turso at ${Math.floor(Date.now() / 1000)}`]);
const select2 = db.prepare(`SELECT * FROM guestbook ORDER BY created_at DESC LIMIT ?`);
console.info(await select2.all([5]))
try {
// push local changes to the remote
await db.push();
} catch (e) {
console.error('pull failed', e);
}

View File

@@ -0,0 +1,13 @@
{
"name": "sync-node",
"version": "1.0.0",
"main": "index.mjs",
"scripts": {
},
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"@tursodatabase/sync": "^0.2.0"
}
}

View File

@@ -14,7 +14,8 @@
"sync/packages/native",
"sync/packages/wasm",
"examples/database-node",
"examples/database-wasm-vite"
"examples/database-wasm-vite",
"examples/sync-node"
],
"version": "0.2.0"
}

View File

@@ -1648,7 +1648,7 @@ __metadata:
languageName: unknown
linkType: soft
"@tursodatabase/sync@workspace:sync/packages/native":
"@tursodatabase/sync@npm:^0.2.0, @tursodatabase/sync@workspace:sync/packages/native":
version: 0.0.0-use.local
resolution: "@tursodatabase/sync@workspace:sync/packages/native"
dependencies:
@@ -3672,6 +3672,14 @@ __metadata:
languageName: node
linkType: hard
"sync-node@workspace:examples/sync-node":
version: 0.0.0-use.local
resolution: "sync-node@workspace:examples/sync-node"
dependencies:
"@tursodatabase/sync": "npm:^0.2.0"
languageName: unknown
linkType: soft
"tar-fs@npm:^2.0.0":
version: 2.1.4
resolution: "tar-fs@npm:2.1.4"