You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
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.
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)
0 commit comments