Content Types
概述
用 TOML 定义数据表,自动获得完整的 CRUD API — 无需写代码,无需写 SQL。
内容类型是 RaisFast 的 Schema 驱动数据建模系统。你只需编写 TOML 定义,系统自动完成一切:
- 自动创建 数据库表(含所有列和索引)
- 自动生成 完整的 REST API(列表 / 详情 / 创建 / 更新 / 删除)
- 自动接入 验证、访问控制、缓存和关联解析
工作流程
TOML 定义 → 自动迁移 → 自动 API → 直接使用
↓ ↓ ↓
Schema 文件 CREATE TABLE / GET/POST/
extensions/ ALTER TABLE PUT/DELETE最小示例
创建 extensions/content_types/portfolio.toml:
[content_type]
name = "Portfolio"
singular = "portfolio"
plural = "portfolios"
table = "portfolios"
[[fields]]
name = "title"
field_type = "text"
required = true
[[fields]]
name = "url"
field_type = "text"
[[fields]]
name = "cover"
field_type = "media"
media_config = { accept = ["image/*"], max_count = 1 }
[[fields]]
name = "description"
field_type = "text"
[[fields]]
name = "tech_stack"
field_type = "json"
[content_type.implements]
protocols = ["timestampable", "sortable"]启动服务器后立刻获得:
# 列表
curl http://localhost:9898/api/v1/cms/portfolios
# 创建
curl -X POST http://localhost:9898/api/v1/cms/portfolios \
-H "Authorization: Bearer TOKEN" \
-H "Content-Type: application/json" \
-d '{"title":"My SaaS App","url":"https://mysaas.com","tech_stack":["Rust","React"]}'Schema 头部
[content_type] 部分定义内容类型的身份:
| 字段 | 必填 | 默认值 | 说明 |
|---|---|---|---|
name | 是 | — | 显示名称 |
singular | 是 | — | 标识符,用作注册表键和 API 路径 |
plural | 是 | — | 复数形式,用于 API 路径 |
table | 是 | — | 数据库表名 |
description | 否 | "" | 描述 |
slug_field | 否 | — | 自动生成 slug 的源字段名 |
kind | 否 | "collection" | "collection"(多条记录)或 "single"(单条记录) |
builtin | 否 | false | 设为 true 则不注入默认字段 |
两种类型
Collection(集合)
默认类型。多条记录,完整 CRUD API:
[content_type]
kind = "collection"生成 5 个端点:GET(列表)、GET /{id}、POST、PUT /{id}、DELETE /{id}。
Single(单例)
适用于单条记录的模型(站点设置、首页配置):
[content_type]
kind = "single"仅生成 2 个端点:GET(首次请求自动创建)和 PUT(更新唯一记录)。
TOML 文件位置
内容类型定义从以下位置加载:
extensions/content_types/*.toml内置模板(博客、电商)也会在首次运行时自动加载。
