Skip to content

Commit

Permalink
Improve sync example
Browse files Browse the repository at this point in the history
  • Loading branch information
penberg committed Sep 12, 2023
1 parent 546894d commit 705cf15
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
23 changes: 13 additions & 10 deletions examples/sync/example.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Database from "libsql";
import reader from "readline-sync";

const url = process.env.LIBSQL_URL;
if (!url) {
Expand All @@ -11,20 +12,22 @@ const db = new Database("hello.db", options);

db.sync();

var rows = undefined;
db.exec("CREATE TABLE IF NOT EXISTS guest_book_entries (comment TEXT)");

console.log("After sync:");
db.sync();

rows = db.prepare("SELECT * FROM users").all();
for (const row of rows) {
console.log(row);
}
const comment = reader.question("Enter your comment: ");

db.exec("INSERT INTO users VALUES (4, 'Pekka Enberg')");
console.log(comment);

console.log("After write:");
db.exec("INSERT INTO guest_book_entries (comment) VALUES ('" + comment + "')");
//const stmt = db.prepare("INSERT INTO guest_book_entries (comment) VALUES (?)");
//stmt.run(comment);

db.sync();

rows = db.prepare("SELECT * FROM users").all();
console.log("Guest book entries:");
const rows = db.prepare("SELECT * FROM guest_book_entries").all();
for (const row of rows) {
console.log(row);
console.log(" - " + row.comment);
}
3 changes: 2 additions & 1 deletion examples/sync/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"libsql": "^0.1.0"
"libsql": "^0.1.0",
"readline-sync": "^1.4.10"
}
}

0 comments on commit 705cf15

Please sign in to comment.