-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathexample.js
More file actions
51 lines (37 loc) · 1.34 KB
/
example.js
File metadata and controls
51 lines (37 loc) · 1.34 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
import Database from "libsql";
import reader from "readline-sync";
const url = process.env.LIBSQL_URL;
if (!url) {
throw new Error("Environment variable LIBSQL_URL is not set.");
}
const authToken = process.env.LIBSQL_AUTH_TOKEN;
const options = { syncUrl: url, authToken: authToken };
// Sync also supports Turso Cloud encryption.
//
// Documentation: https://docs.turso.tech/cloud/encryption
//
//
// export LIBSQL_ENCRYPTION_KEY="encryption key in base64 format"
//
// The encryption key must be encoded in base64 format.
const encryptionKey = process.env.LIBSQL_ENCRYPTION_KEY;
if (encryptionKey) {
options.remoteEncryptionKey = encryptionKey;
}
const db = new Database("hello.db", options);
db.sync();
db.exec("CREATE TABLE IF NOT EXISTS guest_book_entries (comment TEXT)");
db.sync();
const comment = reader.question("Enter your comment: ");
console.log(comment);
const stmt = db.prepare("INSERT INTO guest_book_entries (comment) VALUES (?)");
stmt.run(comment);
console.log("max write replication index: " + db.maxWriteReplicationIndex());
const replicated = db.sync();
console.log("frames synced: " + replicated.frames_synced);
console.log("frame no: " + replicated.frame_no);
console.log("Guest book entries:");
const rows = db.prepare("SELECT * FROM guest_book_entries").all();
for (const row of rows) {
console.log(" - " + row.comment);
}