Skip to content

Commit 5d6f16f

Browse files
committed
docs: update API.md and README.md to match the current implementation
1 parent 47bdf47 commit 5d6f16f

File tree

2 files changed

+68
-40
lines changed

2 files changed

+68
-40
lines changed

API.md

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ This document provides a reference for the SQLite functions provided by the `sql
2424
- [`cloudsync_network_cleanup()`](#cloudsync_network_cleanup)
2525
- [`cloudsync_network_set_token()`](#cloudsync_network_set_tokentoken)
2626
- [`cloudsync_network_set_apikey()`](#cloudsync_network_set_apikeyapikey)
27-
- [`cloudsync_network_has_unsent_changes()`](#cloudsync_network_has_unsent_changes)
2827
- [`cloudsync_network_send_changes()`](#cloudsync_network_send_changes)
2928
- [`cloudsync_network_check_changes()`](#cloudsync_network_check_changes)
3029
- [`cloudsync_network_sync()`](#cloudsync_network_syncwait_ms-max_retries)
3130
- [`cloudsync_network_reset_sync_version()`](#cloudsync_network_reset_sync_version)
31+
- [`cloudsync_network_has_unsent_changes()`](#cloudsync_network_has_unsent_changes)
3232
- [`cloudsync_network_logout()`](#cloudsync_network_logout)
3333

3434
---
@@ -357,34 +357,27 @@ SELECT cloudsync_network_set_apikey('your_api_key');
357357

358358
---
359359

360-
### `cloudsync_network_has_unsent_changes()`
360+
### `cloudsync_network_send_changes()`
361361

362-
**Description:** Checks if there are any local changes that have not yet been sent to the remote server.
362+
**Description:** Sends all unsent local changes to the remote server.
363363

364364
**Parameters:** None.
365365

366-
**Returns:** 1 if there are unsent changes, 0 otherwise.
367-
368-
**Example:**
366+
**Returns:** A JSON string with the sync status:
369367

370-
```sql
371-
SELECT cloudsync_network_has_unsent_changes();
368+
```json
369+
{"status":"synced|syncing|out-of-sync|error", "localVersion": N, "serverVersion": N}
372370
```
373371

374-
---
375-
376-
### `cloudsync_network_send_changes()`
377-
378-
**Description:** Sends all unsent local changes to the remote server.
379-
380-
**Parameters:** None.
381-
382-
**Returns:** None.
372+
- `status`: The current sync state — `"synced"` (all changes confirmed), `"syncing"` (changes sent but not yet confirmed), `"out-of-sync"` (local changes pending or gaps detected), or `"error"`.
373+
- `localVersion`: The latest local database version.
374+
- `serverVersion`: The latest version confirmed by the server.
383375

384376
**Example:**
385377

386378
```sql
387379
SELECT cloudsync_network_send_changes();
380+
-- '{"status":"synced","localVersion":5,"serverVersion":5}'
388381
```
389382

390383
---
@@ -399,16 +392,22 @@ This function is designed to be called periodically to keep the local database i
399392
To force an update and wait for changes (with a timeout), use [`cloudsync_network_sync(wait_ms, max_retries)`].
400393

401394
If the network is misconfigured or the remote server is unreachable, the function returns an error.
402-
On success, it returns `SQLITE_OK`, and the return value indicates how many changes were downloaded and applied.
403395

404396
**Parameters:** None.
405397

406-
**Returns:** The number of changes downloaded. Errors are reported via the SQLite return code.
398+
**Returns:** A JSON string with the number of changes applied:
399+
400+
```json
401+
{"rowsReceived": N}
402+
```
403+
404+
- `rowsReceived`: The number of rows downloaded and applied to the local database.
407405

408406
**Example:**
409407

410408
```sql
411409
SELECT cloudsync_network_check_changes();
410+
-- '{"rowsReceived":3}'
412411
```
413412

414413
---
@@ -425,13 +424,23 @@ SELECT cloudsync_network_check_changes();
425424
- `wait_ms` (INTEGER, optional): The time to wait in milliseconds between retries. Defaults to 100.
426425
- `max_retries` (INTEGER, optional): The maximum number of times to retry the synchronization. Defaults to 1.
427426

428-
**Returns:** The number of changes downloaded. Errors are reported via the SQLite return code.
427+
**Returns:** A JSON string with the full sync result:
428+
429+
```json
430+
{"status":"synced|syncing|out-of-sync|error", "localVersion": N, "serverVersion": N, "rowsReceived": N}
431+
```
432+
433+
- `status`: The current sync state — `"synced"`, `"syncing"`, `"out-of-sync"`, or `"error"`.
434+
- `localVersion`: The latest local database version.
435+
- `serverVersion`: The latest version confirmed by the server.
436+
- `rowsReceived`: The number of rows downloaded and applied during the check phase.
429437

430438
**Example:**
431439

432440
```sql
433441
-- Perform a single synchronization cycle
434442
SELECT cloudsync_network_sync();
443+
-- '{"status":"synced","localVersion":5,"serverVersion":5,"rowsReceived":3}'
435444

436445
-- Perform a synchronization cycle with custom retry settings
437446
SELECT cloudsync_network_sync(500, 3);
@@ -455,9 +464,25 @@ SELECT cloudsync_network_reset_sync_version();
455464

456465
---
457466

467+
### `cloudsync_network_has_unsent_changes()`
468+
469+
**Description:** Checks if there are any local changes that have not yet been sent to the remote server.
470+
471+
**Parameters:** None.
472+
473+
**Returns:** 1 if there are unsent changes, 0 otherwise.
474+
475+
**Example:**
476+
477+
```sql
478+
SELECT cloudsync_network_has_unsent_changes();
479+
```
480+
481+
---
482+
458483
### `cloudsync_network_logout()`
459484

460-
**Description:** Logs out the current user and cleans up all local data from synchronized tables. This function deletes and then re-initializes synchronized tables, useful for switching users or resetting the local database. **Warning:** This function deletes all data from synchronized tables. Use with caution.
485+
**Description:** Logs out the current user and cleans up all local data from synchronized tables. This function deletes and then re-initializes synchronized tables, useful for switching users or resetting the local database. **Warning:** This function deletes all data from synchronized tables. Use with caution. Consider calling [`cloudsync_network_has_unsent_changes()`](#cloudsync_network_has_unsent_changes) before logout to check for unsent local changes and warn the user before data that has not been fully synchronized to the remote server is deleted.
461486

462487
**Parameters:** None.
463488

README.md

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,24 @@ The sync layer is tightly integrated with [**SQLite Cloud**](https://sqlitecloud
5050

5151
## Row-Level Security
5252

53-
Thanks to the underlying SQLite Cloud infrastructure, **SQLite Sync supports Row-Level Security (RLS)**—allowing you to define **precise access control at the row level**:
53+
Thanks to the underlying SQLite Cloud infrastructure, **SQLite Sync supports Row-Level Security (RLS)**—allowing you to use a **single shared cloud database** while each client only sees and modifies its own data. RLS policies are enforced on the server, so the security boundary is at the database level, not in application code.
5454

5555
- Control not just who can read or write a table, but **which specific rows** they can access.
56-
- Enforce security policies on the server—no need for client-side filtering.
56+
- Each device syncs only the rows it is authorized to see—no full dataset download, no client-side filtering.
5757

5858
For example:
5959

6060
- User A can only see and edit their own data.
6161
- User B can access a different set of rows—even within the same shared table.
6262

63-
**Benefits of RLS**:
63+
**Benefits**:
6464

65-
- **Data isolation**: Ensure users only access what they’re authorized to see.
66-
- **Built-in privacy**: Security policies are enforced at the database level.
67-
- **Simplified development**: Reduce or eliminate complex permission logic in your application code.
65+
- **Single database, multiple tenants**: One cloud database serves all users. RLS policies partition data per user or role, eliminating the need to provision separate databases.
66+
- **Efficient sync**: Each client downloads only its authorized rows, reducing bandwidth and local storage.
67+
- **Server-enforced security**: Policies are evaluated on the server during sync. A compromised or modified client cannot bypass access controls.
68+
- **Simplified development**: No need to implement permission logic in your application—define policies once in the database and they apply everywhere.
69+
70+
For more information, see the [SQLite Cloud RLS documentation](https://docs.sqlitecloud.io/docs/rls).
6871

6972
### What Can You Build with SQLite Sync?
7073

@@ -102,7 +105,12 @@ SQLite Sync is ideal for building collaborative and distributed apps across web,
102105

103106
## Documentation
104107

105-
For detailed information on all available functions, their parameters, and examples, refer to the [comprehensive API Reference](./API.md).
108+
For detailed information on all available functions, their parameters, and examples, refer to the [comprehensive API Reference](./API.md). The API includes:
109+
110+
- **Configuration Functions** — initialize, enable, and disable sync on tables
111+
- **Helper Functions** — version info, site IDs, UUID generation
112+
- **Schema Alteration Functions** — safely alter synced tables
113+
- **Network Functions** — connect, authenticate, send/receive changes, and monitor sync status
106114

107115
## Installation
108116

@@ -284,12 +292,13 @@ SELECT cloudsync_network_set_apikey('your-api-key-here');
284292
-- Or use token authentication (required for Row-Level Security)
285293
-- SELECT cloudsync_network_set_token('your_auth_token');
286294

287-
-- Sync with cloud: send local changes, then check the remote server for new changes
295+
-- Sync with cloud: send local changes, then check the remote server for new changes
288296
-- and, if a package with changes is ready to be downloaded, applies them to the local database
289297
SELECT cloudsync_network_sync();
290-
-- Keep calling periodically. The function returns > 0 if data was received
291-
-- In production applications, you would typically call this periodically
292-
-- rather than manually (e.g., every few seconds)
298+
-- Returns a JSON string with sync status, e.g.:
299+
-- '{"status":"synced","localVersion":5,"serverVersion":5,"rowsReceived":3}'
300+
-- Keep calling periodically. In production applications, you would typically
301+
-- call this periodically rather than manually (e.g., every few seconds)
293302
SELECT cloudsync_network_sync();
294303

295304
-- Before closing the database connection
@@ -314,9 +323,9 @@ SELECT cloudsync_init('my_data');
314323
SELECT cloudsync_network_init('sqlitecloud://your-project-id.sqlite.cloud/database.sqlite');
315324
SELECT cloudsync_network_set_apikey('your-api-key-here');
316325

317-
-- Sync to get data from the first device
326+
-- Sync to get data from the first device
318327
SELECT cloudsync_network_sync();
319-
-- repeat until data is received (returns > 0)
328+
-- Repeat — check rowsReceived in the JSON result to see if data was received
320329
SELECT cloudsync_network_sync();
321330

322331
-- View synchronized data
@@ -454,12 +463,6 @@ Be aware that certain types of triggers can cause errors during synchronization
454463
- If a trigger modifies a table that is also synchronized with SQLite Sync, changes performed by the trigger may be applied twice during the merge operation
455464
- This can lead to constraint violations or unexpected data states depending on the table's constraints
456465

457-
**Column-by-Column Processing**
458-
- SQLite Sync applies changes column-by-column during synchronization
459-
- UPDATE triggers may be called multiple times for a single row as each column is processed
460-
- This can result in unexpected trigger behavior
461-
462-
463466

464467
## License
465468

0 commit comments

Comments
 (0)