add basic examples for database-wasm package

This commit is contained in:
Nikita Sivukhin
2025-10-03 13:20:13 +04:00
parent 7905841990
commit b67ef8a7eb
13 changed files with 337 additions and 11 deletions

View File

@@ -0,0 +1,12 @@
# database-node
This is a minimal example showing how to use [`@tursodatabase/database`](https://www.npmjs.com/package/@tursodatabase/database) in the node.js
---
## Usage
```bash
npm install
node index.mjs
```

View File

@@ -0,0 +1,21 @@
import { connect } from "@tursodatabase/database";
const db = await connect("local.db", {
timeout: 1000, // busy timeout for handling high-concurrency write cases
});
// 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 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 select = 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 select.all([5]))

View File

@@ -0,0 +1,14 @@
{
"name": "database-node",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"@tursodatabase/database": "^0.2.0"
}
}