Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/sync/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
local.db*
.env
__pycache__/
30 changes: 26 additions & 4 deletions examples/sync/README.md
Original file line number Diff line number Diff line change
@@ -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 <your_database_name>
```
2. Get your database URL:
```bash
turso db show <your_database_name>
```
3. Create an auth token:
```bash
turso db tokens create <your_database_name>
```
4. Add the URL and token to a `.env` file in your project root:
```dotenv
TURSO_DATABASE_URL=<your_database_url>
TURSO_AUTH_TOKEN=<your_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.
29 changes: 29 additions & 0 deletions examples/sync/main.py
Original file line number Diff line number Diff line change
@@ -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 <your_database_name>

2. Get your database URL:
turso db show <your_database_name>

3. Create an auth token:
turso db tokens create <your_database_name>

4. Add the URL and token to a `.env` file in your project root:
TURSO_DATABASE_URL=<your_database_url>
TURSO_AUTH_TOKEN=<your_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")
Expand Down