Skip to content

Commit 4be7a83

Browse files
committed
Docs: add a Getting Started section in README.md and a more detailed example in examples/simple-todo-db
1 parent 316d4e9 commit 4be7a83

File tree

2 files changed

+367
-21
lines changed

2 files changed

+367
-21
lines changed

README.md

Lines changed: 100 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ In simple terms, CRDTs make it possible for multiple users to **edit shared data
1919
- [What Can You Build with SQLite Sync?](#what-can-you-build-with-sqlite-sync)
2020
- [Documentation](#documentation)
2121
- [Installation](#installation)
22-
- [Usage](#usage)
22+
- [Getting Started](#getting-started)
2323
- [License](#license)
2424

2525
## Key Features
@@ -120,43 +120,122 @@ Download the appropriate pre-built binary for your platform from the official [R
120120
SELECT load_extension('./cloudsync');
121121
```
122122

123-
### Usage
123+
## Getting Started
124124

125-
Here's a quick example to get started with `sqlite-sync`:
125+
Here's a quick example to get started with SQLite Sync:
126+
127+
### Prerequisites
128+
129+
1. **SQLite Cloud Account**: Sign up at [SQLite Cloud](https://sqlitecloud.io/)
130+
2. **SQLite Sync Extension**: Download from [Releases](https://github.com/sqliteai/sqlite-sync/releases)
131+
132+
### SQLite Cloud Setup
133+
134+
1. Create a new project and database in your [SQLite Cloud Dashboard](https://dashboard.sqlitecloud.io/)
135+
2. Copy your connection string and API key from the dashboard
136+
3. Create tables with identical schema in both local and cloud databases
137+
4. Enable synchronization: click the **"OffSync"** button for your database and select each table you want to synchronize
138+
139+
### Local Database Setup
140+
141+
```bash
142+
# Start SQLite CLI
143+
sqlite3 myapp.db
144+
```
126145

127146
```sql
128147
-- Load the extension
129148
.load ./cloudsync
130149

131-
-- Create a table you want to synchronize
150+
-- Create a table (primary key MUST be TEXT for global uniqueness)
132151
CREATE TABLE IF NOT EXISTS my_data (
133-
id TEXT PRIMARY KEY,
134-
value TEXT
152+
id TEXT PRIMARY KEY NOT NULL,
153+
value TEXT NOT NULL DEFAULT '',
154+
created_at TEXT DEFAULT CURRENT_TIMESTAMP
135155
);
136156

137-
-- Initialize synchronization for the table
138-
-- This sets up internal metadata tables and triggers for CRDT functionality.
157+
-- Initialize table for synchronization
139158
SELECT cloudsync_init('my_data');
140159

141-
-- Insert some data. These changes will be tracked for synchronization.
142-
INSERT INTO my_data (id, value) VALUES (cloudsync_uuid(), 'Hello from device A!');
160+
-- Use your local database normally: read and write data using standard SQL queries
161+
-- The CRDT system automatically tracks all changes for synchronization
162+
163+
-- Example: Insert data (always use cloudsync_uuid() for globally unique IDs)
164+
INSERT INTO my_data (id, value) VALUES
165+
(cloudsync_uuid(), 'Hello from device A!'),
166+
(cloudsync_uuid(), 'Working offline is seamless!');
143167

144-
-- Configure network settings to connect to your SQLite Cloud database.
145-
-- Replace <your_connection_string> with your actual connection details.
146-
-- Example: 'sqlitecloud://<projectid>.sqlite.cloud/<db>.sqlite'
147-
SELECT cloudsync_network_init('<your_connection_string>');
168+
-- Example: Update and delete operations work normally
169+
UPDATE my_data SET value = 'Updated: Hello from device A!' WHERE value LIKE 'Hello from device A!';
148170

149-
-- Set your API key or authentication token.
150-
-- Use cloudsync_network_set_token() if you have an access token.
151-
SELECT cloudsync_network_set_apikey('YOUR_API_KEY');
171+
-- View your data
172+
SELECT * FROM my_data ORDER BY created_at;
152173

153-
-- Manually trigger a synchronization cycle.
154-
-- This sends local changes to the cloud and pulls down remote changes.
174+
-- Configure network connection before using the network sync functions
175+
SELECT cloudsync_network_init('sqlitecloud://your-project-id.sqlite.cloud/database.sqlite');
176+
SELECT cloudsync_network_set_apikey('your-api-key-here');
177+
178+
-- Sync with cloud: send local changes, then check the remote server for new changes
179+
-- and, if a package with changes is ready to be downloaded, applies them to the local database
180+
SELECT cloudsync_network_sync();
181+
-- Keep calling periodically. The function returns > 0 if data was received
182+
-- In production applications, you would typically call this periodically
183+
-- rather than manually (e.g., every few seconds)
155184
SELECT cloudsync_network_sync();
156185

157-
-- On another device (after loading extension and initializing the same table):
158-
-- Running SELECT cloudsync_network_sync(); would pull changes from device A.
186+
-- Before closing the database connection
187+
SELECT cloudsync_terminate();
188+
-- Close the database connection
189+
.quit
159190
```
191+
```sql
192+
-- On another device (or create another database for testing: sqlite3 myapp_2.db)
193+
-- Follow the same setup steps: load extension, create table, init sync, configure network
194+
195+
-- Load extension and create identical table structure
196+
.load ./cloudsync
197+
CREATE TABLE IF NOT EXISTS my_data (
198+
id TEXT PRIMARY KEY NOT NULL,
199+
value TEXT NOT NULL DEFAULT '',
200+
created_at TEXT DEFAULT CURRENT_TIMESTAMP
201+
);
202+
SELECT cloudsync_init('my_data');
203+
204+
-- Connect to the same cloud database
205+
SELECT cloudsync_network_init('sqlitecloud://your-project-id.sqlite.cloud/database.sqlite');
206+
SELECT cloudsync_network_set_apikey('your-api-key-here');
207+
208+
-- Sync to get data from the first device
209+
SELECT cloudsync_network_sync();
210+
-- repeat until data is received (returns > 0)
211+
SELECT cloudsync_network_sync();
212+
213+
-- View synchronized data
214+
SELECT * FROM my_data ORDER BY created_at;
215+
216+
-- Add data from this device to test bidirectional sync
217+
INSERT INTO my_data (id, value) VALUES
218+
(cloudsync_uuid(), 'Hello from device B!');
219+
220+
-- Sync again to send this device's changes
221+
SELECT cloudsync_network_sync();
222+
223+
-- The CRDT system ensures all devices eventually have the same data,
224+
-- with automatic conflict resolution and no data loss
225+
226+
-- Before closing the database connection
227+
SELECT cloudsync_terminate();
228+
-- Close the database connection
229+
.quit
230+
```
231+
232+
### For a Complete Example
233+
234+
See the [Simple Todo Database example](./examples/simple-todo-db/) for a comprehensive walkthrough including:
235+
- Multi-device collaboration
236+
- Offline scenarios
237+
- Row-level security setup
238+
- Conflict resolution demonstrations
160239

161240
## License
162241

0 commit comments

Comments
 (0)