From 9ea40e92315895bc714e244849e8c6f22c30b526 Mon Sep 17 00:00:00 2001 From: Avinash Sajjanshetty Date: Thu, 18 Dec 2025 18:09:21 +0530 Subject: [PATCH 1/2] Add remote cloud encryption example --- examples/remote/cloud-encryption.js | 37 +++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 examples/remote/cloud-encryption.js diff --git a/examples/remote/cloud-encryption.js b/examples/remote/cloud-encryption.js new file mode 100644 index 0000000..1bfb251 --- /dev/null +++ b/examples/remote/cloud-encryption.js @@ -0,0 +1,37 @@ +// Example: Connecting to an encrypted Turso Cloud database +// +// This example shows how to connect to a Turso Cloud database with +// remote encryption using libsql-js. +// +// Documentation: https://docs.turso.tech/cloud/encryption +// +// Usage: +// +// export LIBSQL_URL="libsql://your-db.turso.io" +// export LIBSQL_AUTH_TOKEN="your-token" +// export LIBSQL_ENCRYPTION_KEY="encryption key in base64 format" +// node cloud-encryption +// +// The encryption key must be encoded in base64 format. + +import Database from "libsql"; + +const url = process.env.LIBSQL_URL; +const authToken = process.env.LIBSQL_AUTH_TOKEN; +const encryptionKey = process.env.LIBSQL_ENCRYPTION_KEY; + +const opts = { + authToken: authToken, + remoteEncryptionKey: encryptionKey, +}; + +const db = new Database(url, opts); + +db.exec("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT, email TEXT)"); +db.exec("INSERT OR REPLACE INTO users (id, name, email) VALUES (1, 'Alice', 'alice@example.org')"); +db.exec("INSERT OR REPLACE INTO users (id, name, email) VALUES (2, 'Bob', 'bob@example.com')"); + +const row = db.prepare("SELECT * FROM users WHERE id = ?").get(1); +console.log(`Name: ${row.name}, email: ${row.email}`); + +db.close(); \ No newline at end of file From c882aa0517b507c4f81e40699eba05dfba44dda5 Mon Sep 17 00:00:00 2001 From: Avinash Sajjanshetty Date: Thu, 18 Dec 2025 18:15:19 +0530 Subject: [PATCH 2/2] update sync example to pass remote encryption params --- examples/sync/example.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/examples/sync/example.js b/examples/sync/example.js index 81e7396..1def2f5 100644 --- a/examples/sync/example.js +++ b/examples/sync/example.js @@ -8,6 +8,22 @@ if (!url) { 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();