-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathindex.mjs
48 lines (38 loc) · 1.16 KB
/
index.mjs
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
import { createClient } from "@libsql/client";
const client = createClient({
url: "file:local.db",
});
await client.batch(
[
"DROP TABLE IF EXISTS users",
"CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)",
"INSERT INTO users (name) VALUES ('Iku Turso')",
],
"write",
);
const names = ["John Doe", "Mary Smith", "Alice Jones", "Mark Taylor"];
let transaction, secondTransaction;
try {
transaction = await client.transaction("write");
for (const name of names) {
await transaction.execute({
sql: "INSERT INTO users (name) VALUES (?)",
args: [name],
});
}
await transaction.rollback();
secondTransaction = await client.transaction("write");
for (const name of names) {
await secondTransaction.execute({
sql: "INSERT INTO users (name) VALUES (?)",
args: [name],
});
}
await secondTransaction.commit();
} catch (e) {
console.error(e);
await transaction?.rollback();
await secondTransaction?.rollback();
}
const result = await client.execute("SELECT * FROM users");
console.log("Users:", result.rows);