Skip to content

Commit bf62340

Browse files
authored
Merge pull request #132 from sqlitecloud/docs/sqlite-sync
docs: update sqlite sync docs
2 parents 3da4833 + 71e73fa commit bf62340

35 files changed

+1940
-17
lines changed

.github/workflows/aisearch.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616

1717
steps:
1818
- uses: actions/checkout@v4
19-
19+
2020
- uses: sqliteai/sqlite-aisearch-action@v1
2121
with:
2222
connection_string: ${{ secrets.PROJECT_STRING }}

sqlite-cloud/_nav.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,41 @@ const sidebarNav: SidebarNavStruct = [
77
{ title: "Overview", filePath: "ai-overview", type: "inner", level: 0 },
88
{ title: "SQLite-AI", filePath: "sqlite-ai", type: "inner", level: 0 },
99
{ title: "SQLite-JS", filePath: "sqlite-js", type: "inner", level: 0 },
10-
{ title: "SQLite-Sync", filePath: "sqlite-sync", type: "inner", level: 0 },
10+
{ title: "SQLite-Sync", type: "inner", level: 0 },
11+
{ title: "Introduction", filePath: "sqlite-sync-introduction", type: "inner", level: 1 },
12+
{ title: "Getting Started", filePath: "sqlite-sync-getting-started", type: "inner", level: 1 },
13+
{ title: "Best Practices", filePath: "sqlite-sync-best-practices", type: "inner", level: 1 },
14+
{ title: "Quick Starts", type: "inner", level: 1 },
15+
{ title: "Android", filePath: "sqlite-sync-quick-start-android", type: "inner", level: 2 },
16+
{ title: "iOS", filePath: "sqlite-sync-quick-start-ios", type: "inner", level: 2 },
17+
{ title: "Linux", filePath: "sqlite-sync-quick-start-linux", type: "inner", level: 2 },
18+
{ title: "macOS", filePath: "sqlite-sync-quick-start-macos", type: "inner", level: 2 },
19+
{ title: "React Native Expo", filePath: "sqlite-sync-quick-start-expo", type: "inner", level: 2 },
20+
{ title: "WASM", filePath: "sqlite-sync-quick-start-wasm", type: "inner", level: 2 },
21+
{ title: "Windows", filePath: "sqlite-sync-quick-start-windows", type: "inner", level: 2 },
22+
{ title: "API Reference", type: "inner", level: 1 },
23+
{ title: "cloudsync_init", filePath: "sqlite-sync-api-cloudsync-init", type: "inner", level: 2 },
24+
{ title: "cloudsync_enable", filePath: "sqlite-sync-api-cloudsync-enable", type: "inner", level: 2 },
25+
{ title: "cloudsync_disable", filePath: "sqlite-sync-api-cloudsync-disable", type: "inner", level: 2 },
26+
{ title: "cloudsync_is_enabled", filePath: "sqlite-sync-api-cloudsync-is-enabled", type: "inner", level: 2 },
27+
{ title: "cloudsync_cleanup", filePath: "sqlite-sync-api-cloudsync-cleanup", type: "inner", level: 2 },
28+
{ title: "cloudsync_terminate", filePath: "sqlite-sync-api-cloudsync-terminate", type: "inner", level: 2 },
29+
{ title: "cloudsync_version", filePath: "sqlite-sync-api-cloudsync-version", type: "inner", level: 2 },
30+
{ title: "cloudsync_siteid", filePath: "sqlite-sync-api-cloudsync-siteid", type: "inner", level: 2 },
31+
{ title: "cloudsync_db_version", filePath: "sqlite-sync-api-cloudsync-db-version", type: "inner", level: 2 },
32+
{ title: "cloudsync_uuid", filePath: "sqlite-sync-api-cloudsync-uuid", type: "inner", level: 2 },
33+
{ title: "cloudsync_begin_alter", filePath: "sqlite-sync-api-cloudsync-begin-alter", type: "inner", level: 2 },
34+
{ title: "cloudsync_commit_alter", filePath: "sqlite-sync-api-cloudsync-commit-alter", type: "inner", level: 2 },
35+
{ title: "cloudsync_network_init", filePath: "sqlite-sync-api-cloudsync-network-init", type: "inner", level: 2 },
36+
{ title: "cloudsync_network_cleanup", filePath: "sqlite-sync-api-cloudsync-network-cleanup", type: "inner", level: 2 },
37+
{ title: "cloudsync_network_set_token", filePath: "sqlite-sync-api-cloudsync-network-set-token", type: "inner", level: 2 },
38+
{ title: "cloudsync_network_set_apikey", filePath: "sqlite-sync-api-cloudsync-network-set-apikey", type: "inner", level: 2 },
39+
{ title: "cloudsync_network_has_unsent_changes", filePath: "sqlite-sync-api-cloudsync-network-has-unsent-changes", type: "inner", level: 2 },
40+
{ title: "cloudsync_network_send_changes", filePath: "sqlite-sync-api-cloudsync-network-send-changes", type: "inner", level: 2 },
41+
{ title: "cloudsync_network_check_changes", filePath: "sqlite-sync-api-cloudsync-network-check-changes", type: "inner", level: 2 },
42+
{ title: "cloudsync_network_sync", filePath: "sqlite-sync-api-cloudsync-network-sync", type: "inner", level: 2 },
43+
{ title: "cloudsync_network_reset_sync_version", filePath: "sqlite-sync-api-cloudsync-network-reset-sync-version", type: "inner", level: 2 },
44+
{ title: "cloudsync_network_logout", filePath: "sqlite-sync-api-cloudsync-network-logout", type: "inner", level: 2 },
1145
{ title: "SQLite-Vector", filePath: "sqlite-vector", type: "inner", level: 0 },
1246
{ title: "MCP (Model Context Protocol)", filePath: "mcp-server", type: "inner", level: 0 },
1347

sqlite-cloud/sqlite-ai/sqlite-sync.mdx

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
title: "cloudsync_begin_alter(table_name)"
3+
description: SQLite Sync is a multi-platform extension that brings a true local-first experience to your applications with minimal effort.
4+
category: platform
5+
status: publish
6+
slug: sqlite-sync-api-cloudsync-begin-alter
7+
---
8+
9+
**Description:** Prepares a synchronized table for schema changes. This function must be called before altering the table. Failure to use `cloudsync_begin_alter` and `cloudsync_commit_alter` can lead to synchronization errors and data divergence.
10+
11+
**Parameters:**
12+
13+
- `table_name` (TEXT): The name of the table that will be altered.
14+
15+
**Returns:** None.
16+
17+
**Example:**
18+
19+
```sql
20+
SELECT cloudsync_init('my_table');
21+
-- ... later
22+
SELECT cloudsync_begin_alter('my_table');
23+
ALTER TABLE my_table ADD COLUMN new_column TEXT;
24+
SELECT cloudsync_commit_alter('my_table');
25+
```
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
title: "cloudsync_cleanup(table_name)"
3+
description: SQLite Sync is a multi-platform extension that brings a true local-first experience to your applications with minimal effort.
4+
category: platform
5+
status: publish
6+
slug: sqlite-sync-api-cloudsync-cleanup
7+
---
8+
9+
**Description:** Removes the `sqlite-sync` synchronization mechanism from a specified table or all tables. This operation drops the associated `_cloudsync` metadata table and removes triggers from the target table(s). Use this function when synchronization is no longer desired for a table.
10+
11+
**Parameters:**
12+
13+
- `table_name` (TEXT): The name of the table to clean up.
14+
15+
**Returns:** None.
16+
17+
**Example:**
18+
19+
```sql
20+
-- Clean up a single table
21+
SELECT cloudsync_cleanup('my_table');
22+
23+
```
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
title: "cloudsync_commit_alter(table_name)"
3+
description: SQLite Sync is a multi-platform extension that brings a true local-first experience to your applications with minimal effort.
4+
category: platform
5+
status: publish
6+
slug: sqlite-sync-api-cloudsync-commit-alter
7+
---
8+
9+
**Description:** Finalizes schema changes for a synchronized table. This function must be called after altering the table's schema, completing the process initiated by `cloudsync_begin_alter` and ensuring CRDT data consistency.
10+
11+
**Parameters:**
12+
13+
- `table_name` (TEXT): The name of the table that was altered.
14+
15+
**Returns:** None.
16+
17+
**Example:**
18+
19+
```sql
20+
SELECT cloudsync_init('my_table');
21+
-- ... later
22+
SELECT cloudsync_begin_alter('my_type');
23+
ALTER TABLE my_table ADD COLUMN new_column TEXT;
24+
SELECT cloudsync_commit_alter('my_table');
25+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
title: "cloudsync_db_version()"
3+
description: SQLite Sync is a multi-platform extension that brings a true local-first experience to your applications with minimal effort.
4+
category: platform
5+
status: publish
6+
slug: sqlite-sync-api-cloudsync-db-version
7+
---
8+
9+
**Description:** Returns the current database version.
10+
11+
**Parameters:** None.
12+
13+
**Returns:** The database version as an INTEGER.
14+
15+
**Example:**
16+
17+
```sql
18+
SELECT cloudsync_db_version();
19+
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
title: "cloudsync_disable(table_name)"
3+
description: SQLite Sync is a multi-platform extension that brings a true local-first experience to your applications with minimal effort.
4+
category: platform
5+
status: publish
6+
slug: sqlite-sync-api-cloudsync-disable
7+
---
8+
9+
**Description:** Disables synchronization for the specified table.
10+
11+
**Parameters:**
12+
13+
- `table_name` (TEXT): The name of the table to disable.
14+
15+
**Returns:** None.
16+
17+
**Example:**
18+
19+
```sql
20+
SELECT cloudsync_disable('my_table');
21+
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
title: "cloudsync_enable(table_name)"
3+
description: SQLite Sync is a multi-platform extension that brings a true local-first experience to your applications with minimal effort.
4+
category: platform
5+
status: publish
6+
slug: sqlite-sync-api-cloudsync-enable
7+
---
8+
9+
**Description:** Enables synchronization for the specified table.
10+
11+
**Parameters:**
12+
13+
- `table_name` (TEXT): The name of the table to enable.
14+
15+
**Returns:** None.
16+
17+
**Example:**
18+
19+
```sql
20+
SELECT cloudsync_enable('my_table');
21+
```
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
title: "cloudsync_init(table_name, [crdt_algo], [force])"
3+
description: SQLite Sync is a multi-platform extension that brings a true local-first experience to your applications with minimal effort.
4+
category: platform
5+
status: publish
6+
slug: sqlite-sync-api-cloudsync-init
7+
---
8+
9+
**Description:** Initializes a table for `sqlite-sync` synchronization. This function is idempotent and needs to be called only once per table on each site; configurations are stored in the database and automatically loaded with the extension.
10+
11+
Before initialization, `cloudsync_init` performs schema sanity checks to ensure compatibility with CRDT requirements and best practices. These checks include:
12+
13+
- Primary keys should not be auto-incrementing integers; GUIDs (UUIDs, ULIDs) are highly recommended to prevent multi-node collisions.
14+
- All primary key columns must be `NOT NULL`.
15+
- All non-primary key `NOT NULL` columns must have a `DEFAULT` value.
16+
17+
**Schema Design Considerations:**
18+
19+
When designing your database schema for SQLite Sync, follow these essential requirements:
20+
21+
- **Primary Keys**: Use TEXT primary keys with `cloudsync_uuid()` for globally unique identifiers. Avoid auto-incrementing integers.
22+
- **Column Constraints**: All NOT NULL columns (except primary keys) must have DEFAULT values to prevent synchronization errors.
23+
- **UNIQUE Constraints**: In multi-tenant scenarios, use composite UNIQUE constraints (e.g., `UNIQUE(tenant_id, email)`) instead of global uniqueness.
24+
- **Foreign Key Compatibility**: Be aware of potential conflicts during CRDT merge operations and RLS policy interactions.
25+
- **Trigger Compatibility**: Triggers may cause duplicate operations or be called multiple times due to column-by-column processing.
26+
27+
For comprehensive guidelines, see the [Database Schema Recommendations](/docs/sqlite-sync-best-practices) section.
28+
29+
The function supports three overloads:
30+
31+
- `cloudsync_init(table_name)`: Uses the default 'cls' CRDT algorithm.
32+
- `cloudsync_init(table_name, crdt_algo)`: Specifies a CRDT algorithm ('cls', 'dws', 'aws', 'gos').
33+
- `cloudsync_init(table_name, crdt_algo, force)`: Specifies an algorithm and, if `force` is `true` (or `1`), skips the integer primary key check (use with caution, GUIDs are strongly recommended).
34+
35+
**Parameters:**
36+
37+
- `table_name` (TEXT): The name of the table to initialize.
38+
- `crdt_algo` (TEXT, optional): The CRDT algorithm to use. Can be "cls", "dws", "aws", "gos". Defaults to "cls".
39+
- `force` (BOOLEAN, optional): If `true` (or `1`), it skips the check that prevents the use of a single-column INTEGER primary key. Defaults to `false`. It is strongly recommended to use globally unique primary keys instead of integers.
40+
41+
**Returns:** None.
42+
43+
**Example:**
44+
45+
```sql
46+
-- Initialize a single table for synchronization with the Causal-Length Set (CLS) Algorithm (default)
47+
SELECT cloudsync_init('my_table');
48+
49+
-- Initialize a single table for synchronization with a different algorithm Delete-Wins Set (DWS)
50+
SELECT cloudsync_init('my_table', 'dws');
51+
52+
```

0 commit comments

Comments
 (0)