-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathhello.mjs
More file actions
55 lines (47 loc) · 2.03 KB
/
hello.mjs
File metadata and controls
55 lines (47 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Minimal walkthrough of the SQLRite Node.js bindings.
//
// Run after:
//
// cd sdk/nodejs
// npm install
// npm run build
//
// node examples/nodejs/hello.mjs
//
// Shape mirrors `better-sqlite3` so JavaScript devs who've used
// that library can pick this up without reading the docs. Rows
// come back as plain objects keyed by column name.
import { Database } from '../../sdk/nodejs/index.js';
// Pass `:memory:` for a transient in-memory DB (matching better-
// sqlite3's convention); pass a file path like 'foo.sqlrite' for a
// file-backed DB that auto-saves on every write.
const db = new Database(':memory:');
db.exec('CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)');
db.exec("INSERT INTO users (name, age) VALUES ('alice', 30)");
db.exec("INSERT INTO users (name, age) VALUES ('bob', 25)");
db.exec("INSERT INTO users (name, age) VALUES ('carol', 40)");
// .columns() reports projection-order names so you know the shape
// without running the query yet.
const select = db.prepare('SELECT id, name, age FROM users');
console.log('Columns:', select.columns());
// .all() gives you an array of row objects — one object per row.
console.log('\nAll users:');
for (const row of select.all()) {
console.log(` ${row.id}: ${row.name} (${row.age})`);
}
// .get() returns the first row (or null if the query is empty) —
// handy for lookups by primary key.
const first = select.get();
console.log('\nFirst user:', first);
// Transactions: BEGIN / INSERT / ROLLBACK leaves the table
// unchanged. The `inTransaction` getter is live throughout.
db.exec('BEGIN');
console.log('\ninTransaction:', db.inTransaction);
db.exec("INSERT INTO users (name, age) VALUES ('phantom', 99)");
const midCount = db.prepare('SELECT id FROM users').all().length;
console.log(`Mid-transaction row count: ${midCount}`);
db.exec('ROLLBACK');
console.log('inTransaction after rollback:', db.inTransaction);
const finalCount = db.prepare('SELECT id FROM users').all().length;
console.log(`Post-rollback row count: ${finalCount}`);
db.close();