diff --git a/examples/sync/.gitignore b/examples/sync/.gitignore index cc2c2f7..6c45fc6 100644 --- a/examples/sync/.gitignore +++ b/examples/sync/.gitignore @@ -1 +1,3 @@ local.db* +.env +__pycache__/ diff --git a/examples/sync/README.md b/examples/sync/README.md index dcd5a7c..47216c6 100644 --- a/examples/sync/README.md +++ b/examples/sync/README.md @@ -1,19 +1,41 @@ -# Local +# Sync Embedded Replica -This example demonstrates how to use libSQL with a synced database (local file synced with a remote database). +This example demonstrates how to use libSQL with a synced database (local file synced with a remote Turso database). ## Install Dependencies ```bash -pip install libsql +pip install python-dotenv ``` +**Requirements:** +- An existing database on Turso cloud. +- A `.env` file with your Turso DB credentials. + +Setup: +1. Create a new database with: +```bash +turso db create +``` +2. Get your database URL: +```bash +turso db show +``` +3. Create an auth token: +```bash +turso db tokens create +``` +4. Add the URL and token to a `.env` file in your project root: +```dotenv +TURSO_DATABASE_URL= +TURSO_AUTH_TOKEN= +``` ## Running Execute the example: ```bash -TURSO_DATABASE_URL="..." TURSO_AUTH_TOKEN="..." python3 main.py +python3 main.py ``` This will create a local database file that syncs with a remote database, insert some data, and query it. diff --git a/examples/sync/main.py b/examples/sync/main.py index b937016..74554e3 100644 --- a/examples/sync/main.py +++ b/examples/sync/main.py @@ -1,5 +1,34 @@ +""" +Local Example: Using libSQL with a synced Turso database + +This script demonstrates how to use libSQL with a local database file +that syncs with a remote Turso database. + +Requirements: +- An existing database on Turso cloud. +- A `.env` file with your Turso database credentials. + +Setup: +1. Create a new database with: + turso db create + +2. Get your database URL: + turso db show + +3. Create an auth token: + turso db tokens create + +4. Add the URL and token to a `.env` file in your project root: + TURSO_DATABASE_URL= + TURSO_AUTH_TOKEN= + +""" + import libsql import os +from dotenv import load_dotenv + +load_dotenv() url = os.getenv("TURSO_DATABASE_URL") auth_token = os.getenv("TURSO_AUTH_TOKEN")