sqlite-schema-design
Skill này làm gì
Hướng dẫn này bao quát cách thiết kế bảng cho SQLite Sync, hệ thống giữ các cơ sở dữ liệu SQLite đồng nhất trên nhiều thiết bị. Dành cho lập trình viên xây dựng ứng dụng đồng bộ cần quy tắc đồng bộ, không phải lời khuyên cơ sở dữ liệu chung.
Khi nào nên dùng
- ✓Khi bạn thêm một bảng mới cần đồng bộ giữa các thiết bị.
- ✓Khi bạn thay đổi một cột trong bảng đã được đồng bộ.
- ✓Khi bạn lập kế hoạch di chuyển cơ sở dữ liệu phải hoạt động với SQLite Sync.
Liên kết skill
https://github.com/fastrepl/anarlog/blob/main/.agents/skills/sqlite-schema-design/SKILL.mdSao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.
Cách dùng
Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.
Tin nhắn có sẵn để dán
Dành cho Claude Code, Codex hoặc Cursor: chạy lệnh trong thư mục dự án của bạn và skill sẽ được cài đặt.
Tải tệp SKILL.md về và thả vào thư mục skill của agent.
Ví dụ cách nhờ AI
“Thiết kế một bảng ghi chú đồng bộ giữa các thiết bị của người dùng bằng SQLite Sync.”
5 skill mới mỗi tuần. Đọc chỉ 1 phút.
Cách hoạt động
Skill trước tiên kiểm tra xem một bảng có nên đồng bộ không, rồi áp dụng các quy tắc đồng bộ. Các quy tắc gồm khóa chính dạng văn bản ổn định tạo bằng hàm trợ giúp UUID của sync, không dùng id số tự tăng, và thay đổi schema qua các bước alter của sync. Cơ sở dữ liệu cục bộ và cloud phải giữ cùng một schema.
Nội dung kỹ thuật (SKILL.md gốc)
Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.
Goal
Design tables that behave correctly under SQLite Sync's CRDT replication model.
This skill is specifically for CloudSync-backed schemas:
- tables are initialized through
cloudsync_init(...) - sync is enabled with
cloudsync_enable(...) - identifiers should be generated with
cloudsync_uuid() - schema changes must go through
cloudsync_begin_alter(...)andcloudsync_commit_alter(...) - local and cloud databases must keep the same schema
Do not treat this as ordinary SQLite schema design. SQLite Sync imposes extra rules around keys, defaults, foreign keys, and schema evolution.
Workflow
1. Decide Whether The Table Is Synced
Before proposing DDL, classify the table:
- synced application data: must satisfy SQLite Sync constraints
- local-only cache or ephemeral state: should usually stay out of CloudSync
Only apply this skill to synced tables or to tables that may become synced soon.
2. Require A Stable, Globally Unique Primary Key
For synced tables:
- always declare an explicit primary key
- prefer
TEXT PRIMARY KEY NOT NULL - generate ids with
cloudsync_uuid() - do not use auto-incrementing integer ids
SQLite Sync docs explicitly recommend UUIDv7-style globally unique ids for CRDT workloads. Integer autoincrement ids are a bad fit because multiple devices can create rows independently.
CREATE TABLE document (
id TEXT PRIMARY KEY NOT NULL DEFAULT (cloudsync_uuid()),
workspace_id TEXT NOT NULL,
title TEXT NOT NULL DEFAULT '',
body TEXT NOT NULL DEFAULT '',
archived INTEGER NOT NULL DEFAULT 0,
created_at INTEGER NOT NULL DEFAULT (unixepoch()),
updated_at INTEGER NOT NULL DEFAULT (unixepoch())
) STRICT;
If the environment cannot use a function call in DEFAULT, generate the id in application code, but still use cloudsync_uuid() as the canonical id strategy.
3. Make Inserts Merge-Safe With Real Defaults
SQLite Sync best practices call out a non-obvious CRDT constraint: merges can happen column-by-column, so missing values are much more dangerous than in a single-node SQLite app.
For synced tables:
- every non-primary-key
NOT NULLcolumn should have a meaningfulDEFAULT - avoid required columns that only application code knows how to populate
- prefer simple scalar defaults over nullable columns when the field is logically always present
Good:
title TEXT NOT NULL DEFAULT ''
archived INTEGER NOT NULL DEFAULT 0
sort_order INTEGER NOT NULL DEFAULT 0
Bad:
title TEXT NOT NULL
archived INTEGER NOT NULL
without defaults on a synced table.
4. Keep The Local And Cloud Schemas Identical
The getting-started docs require the local synced database and the SQLite Cloud database to share the same schema.
When designing or reviewing a schema:
- treat local and remote DDL as one contract
- do not introduce "client-only" columns on synced tables
- do not rely on drift being harmless
- ensure migrations are applied consistently before sync resumes
If a field is only needed locally, it likely belongs in a separate non-synced table.
5. Be Conservative With Foreign Keys
SQLite Sync best practices explicitly warn that foreign keys can interact poorly with CRDT replication.
Use foreign keys on synced tables only when the integrity guarantee is worth the operational cost.
If you keep them:
- make child foreign key columns nullable when the relationship is optional
- if a foreign key column has a
DEFAULT, that default must beNULLor reference an actually valid parent row - avoid fake sentinel ids such as
'root'unless that parent row is guaranteed to exist everywhere - index the child foreign key columns
Prefer ownership patterns that tolerate out-of-order arrival between related rows.
6. Avoid Triggers And Implicit Write Logic On Synced Tables
SQLite Sync best practices advise minimizing triggers because they make replicated writes harder to reason about.
For synced tables:
- avoid triggers that mutate synced columns
- avoid hidden side effects on insert or update
- prefer explicit application writes
- keep derived or bookkeeping writes in non-synced tables if possible
If a trigger is unavoidable, review it as part of the replication design, not as a local SQLite convenience.
7. Scope Uniqueness For RLS And Multi-Tenant Sync
The introduction docs emphasize row-level security and multi-tenant access patterns.
That changes uniqueness design:
- if the real rule is "unique per workspace/user/team", enc
Câu hỏi thường gặp
Vì sao không dùng id tự tăng?
Nhiều thiết bị có thể tạo dòng cùng lúc, nên bộ đếm số nguyên sẽ bị trùng. Hướng dẫn yêu cầu id duy nhất toàn cục.
Quy tắc này áp dụng cho mọi bảng không?
Không. Chỉ bảng cần đồng bộ mới cần các quy tắc này, còn bảng cache chỉ cục bộ thường nằm ngoài đồng bộ.
Làm sao thay đổi một bảng đã đồng bộ?
Thay đổi đi qua các bước alter begin và commit, nên schema giữ nguyên ở mọi nơi.
Chi tiết
Mô tả gốc của tác giả
Design or review schemas for `crates/cloudsync` using SQLite Sync constraints, not generic SQLite advice. Use when adding synced tables, changing synced columns, or planning CloudSync-safe migrations.
repo: fastrepl/anarlog
Độ phổ biến
- Sao GitHub
- 9.7k
- Vị trí xếp hạng
- #12214
- Giấy phép
- MIT
- Cập nhật
- 1/11/2026
Bình luận
- …
Thêm skill từ kho mã này
no-use-effect
9.7kDạy agent của bạn tránh useEffect trong code React và dùng các mẫu đơn giản hơn, như giá trị dẫn xuất, hook truy vấn và trình xử lý sự kiện.
anarlog
9.7kGiúp agent của bạn tìm trong ghi chú cuộc họp Anarlog, bản tóm tắt, bản chép lời, người tham gia và việc cần làm, hoặc nhập một bản ghi âm cục bộ.
Skill tương tự
discrawl
394kCho phép agent của bạn tìm kiếm và tóm tắt kho lưu trữ tin nhắn Discord trên máy, kiểm tra độ mới của dữ liệu và đọc tin nhắn trực tiếp.
clickhouse-io
280kGiúp agent của bạn tối ưu truy vấn và schema trong ClickHouse theo thực hành tốt nhất cho phân tích dữ liệu hiệu năng cao khi truy vấn chậm.
scientific-db-pubmed-database
280kGiúp agent của bạn tìm bài báo khoa học trong lĩnh vực y sinh. Hữu ích cho nghiên cứu cần thông tin y khoa cụ thể.