-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #148 from tursodatabase/offline-writes
Offline writes
- Loading branch information
Showing
10 changed files
with
796 additions
and
65 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import Database from "libsql"; | ||
import reader from "readline-sync"; | ||
|
||
const dbPath = process.env.LIBSQL_DB_PATH; | ||
if (!dbPath) { | ||
throw new Error("Environment variable LIBSQL_DB_PATH is not set."); | ||
} | ||
const syncUrl = process.env.LIBSQL_SYNC_URL; | ||
if (!syncUrl) { | ||
throw new Error("Environment variable LIBSQL_SYNC_URL is not set."); | ||
} | ||
const authToken = process.env.LIBSQL_AUTH_TOKEN; | ||
|
||
const options = { syncUrl: syncUrl, authToken: authToken, offline: true }; | ||
const db = new Database(dbPath, options); | ||
|
||
db.exec("CREATE TABLE IF NOT EXISTS guest_book_entries (text TEXT)"); | ||
|
||
const comment = reader.question("Enter your comment: "); | ||
|
||
console.log(comment); | ||
|
||
const stmt = db.prepare("INSERT INTO guest_book_entries (text) 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.text); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"name": "libsql-example", | ||
"version": "1.0.0", | ||
"description": "", | ||
"type": "module", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"libsql": "../..", | ||
"readline-sync": "^1.4.10" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.