Getting Started
内容类型
用 TOML 定义数据表,自动获得完整的 CRUD API — 零代码、零 SQL。
内容类型让你用一个 TOML 文件定义数据模型。系统自动创建数据库表,生成完整的 REST API。零代码,零 SQL。
定义一个作品集
创建 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"]免费获得 API
启动服务 — 表已创建,你立刻拥有:
# 列表
curl http://localhost:9898/api/v1/cms/portfolios
# 详情
curl http://localhost:9898/api/v1/cms/portfolios/{id}
# 创建
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"]}'
# 更新
curl -X PUT http://localhost:9898/api/v1/cms/portfolios/{id} \
-H "Authorization: Bearer TOKEN" \
-H "Content-Type: application/json" \
-d '{"url":"https://mysaas.app"}'
# 删除
curl -X DELETE http://localhost:9898/api/v1/cms/portfolios/{id} \
-H "Authorization: Bearer TOKEN"添加关联
零中间表样板代码,直接关联内容类型:
# portfolio.toml — 作品集属于一个分类
[[fields]]
name = "category"
field_type = "relation"
relation = { relation_type = "many_to_one", target = "categories" }
# portfolio.toml — 作品集有多个标签
[[fields]]
name = "tags"
field_type = "relation"
relation = { relation_type = "many_to_many", target = "tags", through = "portfolios_tags" }查询时用 include 填充关联:
curl http://localhost:9898/api/v1/cms/portfolios?include=category,tags用协议添加行为
协议是可组合的行为 — 无需写代码,自动添加列和逻辑:
[content_type.implements]
protocols = ["timestampable", "ownable", "soft_deletable", "sortable"]| 协议 | 添加字段 | 作用 |
|---|---|---|
timestampable | created_at, updated_at | 自动管理时间戳 |
ownable | created_by, updated_by | 自动记录操作用户 |
soft_deletable | deleted_at | 逻辑删除,自动过滤 |
sortable | sort_key | 默认排序 |
statusable | status | 状态机 |
nestable | parent_id, depth | 父子树结构 |
metaable | __meta (JSON) | 任意元数据 |
访问控制
[content_type.api.list]
access = "public"
filter = 'status = "published"'
[content_type.api.create]
access = "admin"
[content_type.api.delete]
access = "admin"| 级别 | 含义 |
|---|---|
public | 无需认证 |
member | 需要登录 |
admin | 仅管理员 |
使用 CLI
raisfast ct new portfolio # 创建内容类型脚手架
raisfast ct check # 校验所有 TOML 文件
raisfast ct types portfolio -o types/portfolio.ts # 生成 TypeScript 类型