Database Documentation

Comprehensive guide to CatCMS database architecture, schema, migrations, and operations using Cloudflare D1 and Drizzle ORM. [object Object]

Overview

CatCMS uses Cloudflare D1, a serverless SQLite database that runs at the edge, providing:

  • Global Distribution - Database replicated across Cloudflare’s network
  • Edge Compute - Low-latency queries from any location
  • SQLite Compatible - Standard SQL syntax with SQLite extensions
  • Zero Configuration - No connection pools or server management
  • ACID Compliance - Full transactional support
  • Cost Effective - Pay-per-request pricing with generous free tier

Technology Stack

πŸ—„οΈ

Cloudflare D1

Serverless SQLite at the edge

πŸ”§

Drizzle ORM

Type-safe database operations

πŸ“

SQL Migrations

Version-controlled schema changes

⚑

Drizzle Kit

SQL generation and introspection

βœ…

Zod + Drizzle Zod

Runtime type validation

πŸ”„

Collection Sync

Config-managed collections


D1 Database Setup

Create D1 Database

Creating D1 Databases

Configure wrangler.jsonc

Wrangler Configuration

Environment Access

Accessing Bindings in Worker


Complete Database Schema

Core Tables

The database includes comprehensive tables for content management, user authentication, media storage, plugins, workflows, and system logging.

users

User authentication and profiles with role-based access control.

Users Table Schema

collections

Content collection definitions with JSON schemas.

Collections Table Schema

content

Actual content items with versioning and workflow support.

Content Table Schema


Drizzle ORM Integration

Schema Definition

Drizzle Schema

Database Connection

Database Connection


Migration System

Migration Service

The migration system handles database schema changes in a versioned, controlled manner.

Migration Service

Running Migrations

Migration Commands


Collection Sync System

Collections can be managed through configuration files, allowing version-controlled schema definitions.

Sync Service

Collection Sync

Running Collection Sync

Sync Command


Query Patterns and Examples

Basic CRUD Operations

Create Operations

Read Operations

Advanced Queries

Pagination


Prepared Statements

D1 supports prepared statements for better performance and SQL injection prevention.

Prepared Statements

Batch Operations

Batch Insert


Database Performance

Indexing Strategy

Database Indexes

Caching Strategies

Three-Tier Caching


Backup and Restore

Export Database

Database Export

Import Database

Database Import


Wrangler D1 Commands

Database Management

Wrangler Commands

Execute SQL

SQL Execution

Database Best Practices

  • Always use transactions for multi-step operations - Validate input with Zod schemas before inserting - Use prepared statements for repeated queries - Implement soft deletes for recovery capability - Log important operations for audit trails

Next Steps

Was this page helpful?