Phần 3: Chọn storage đúng Part 3: Pick the right storage · Bài 2/3 Lesson 2/3

D1 cho dữ liệu có schema D1 for structured data

Workshop signup, quiz results, user preferences — bảng SQL nhỏ. Chạy migration với `wrangler d1 migrations`. Workshop signups, quiz results, user prefs — small SQL tables. Run migrations with `wrangler d1 migrations`.

D1 for structured data
Minh họa từ Cloudflare Reference Architecture (developers.cloudflare.com) Illustration from Cloudflare Reference Architecture (developers.cloudflare.com)

Các bước thực hiện Step-by-step

  1. `wrangler d1 create my-db` — lấy database_id. `wrangler d1 create my-db` — get database_id.
  2. Viết migration SQL trong `migrations/`. Write migration SQL in `migrations/`.
  3. `wrangler d1 migrations apply my-db --remote`. `wrangler d1 migrations apply my-db --remote`.
  4. Query từ Worker qua binding `env.DB.prepare()`. Query from Worker via binding `env.DB.prepare()`.

Giải thích chi tiết Detailed explanation

D1 = SQLite serverless — phù hợp bảng quan hệ nhỏ/vừa, signup form, analytics nhẹ. D1 is serverless SQLite — great for small/medium relational data, signups, light analytics.

Lưu ý (best practices) Note (best practices)

Tạo index cho cột thường dùng trong WHERE (email, user_id) hoặc ràng buộc UNIQUE. Chỉ index cột được query thường xuyên — index cũng cần ghi khi bảng thay đổi. Create indexes on columns used in WHERE clauses (email, user_id) or UNIQUE constraints. Index only frequently queried columns — indexes are updated on every table write.

Nguồn: Source: D1 — Use indexes D1 — Use indexes

Ví dụ triển khai (Cloudflare Resources) Deployment examples (Cloudflare Resources)

Tutorial, solution guide và reference từ developers.cloudflare.com/resources ↗ — gợi ý theo chủ đề bài học. Tutorials, solution guides, and reference docs from developers.cloudflare.com/resources ↗ — matched to this lesson topic.

Duyệt toàn bộ catalog → Browse full catalog →

Tài liệu Cloudflare Developers Cloudflare Developer docs

Cloudflare API (tham khảo) Cloudflare API (reference)

Dùng khi tự động hóa bước này qua script hoặc Terraform — cần API token phù hợp. Use when automating this step via script or Terraform — requires an appropriate API token.

developers.cloudflare.com/api ↗

Sơ đồ kiến trúc (Cloudflare Docs) Architecture diagrams (Cloudflare Docs)

Figure 1: Cloudflare Developer Platform

Fullstack applications Fullstack applications

Ví dụ fullstack trên Developer Platform — Workers/Pages, storage và AI services trong một kiến trúc thực tế. A practical example of how these services come together in a real fullstack application architecture.

Thuật ngữ: Concepts: Workers · Pages · D1 · R2 · KV · Durable Objects

Sơ đồ chính thức ↗ Official diagram ↗ · Serverless Serverless

Sản phẩm liên quan Related products