move examples to the top-level directory

This commit is contained in:
Nikita Sivukhin
2025-10-06 18:01:54 +04:00
parent 0b9a5f331c
commit 926fd25cdd
28 changed files with 1186 additions and 739 deletions

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,35 @@
{
"name": "sync-node",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "sync-node",
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"@tursodatabase/sync": "../../../bindings/javascript/sync/packages/native"
}
},
"../../../bindings/javascript/sync/packages/native": {
"name": "@tursodatabase/sync",
"version": "0.2.0",
"license": "MIT",
"dependencies": {
"@tursodatabase/database-common": "^0.2.0",
"@tursodatabase/sync-common": "^0.2.0"
},
"devDependencies": {
"@napi-rs/cli": "^3.1.5",
"@types/node": "^24.3.1",
"typescript": "^5.9.2",
"vitest": "^3.2.4"
}
},
"node_modules/@tursodatabase/sync": {
"resolved": "../../../bindings/javascript/sync/packages/native",
"link": true
}
}
}

View File

@@ -0,0 +1,16 @@
{
"name": "sync-node",
"version": "1.0.0",
"main": "index.mjs",
"author": "",
"license": "ISC",
"description": "",
"scripts": {
"tsc-build": "echo 'no tsc-build'",
"build": "echo 'no build'",
"test": "echo 'no tests'"
},
"dependencies": {
"@tursodatabase/sync": "../../../bindings/javascript/sync/packages/native"
}
}