集合与字段

本页内容

集合是 EmDash 内容模型的基础。每个集合表示一种内容类型(文章、页面、产品等),并包含决定数据形状的字段定义。

创建集合

在后台 内容类型 中创建集合。每个集合包含:

EmDash 内容类型:页面、文章、自定义集合及其功能
属性说明
slugURL 安全标识(如 posts, products
label显示名称(如「Blog Posts」)
labelSingular单数形式(如「Post」)
description给编辑的可选说明
icon后台侧栏用的 Lucide 图标名
supports草稿、修订、预览、定时发布、搜索、SEO 等能力

集合能力

创建集合时可启用所需能力。

能力说明
drafts草稿/发布工作流
revisions用版本快照记录内容历史
preview为草稿生成带签名的预览 URL
scheduling预约未来时间发布
// 启用全部能力的集合示例
{
  slug: "posts",
  label: "Blog Posts",
  labelSingular: "Post",
  supports: ["drafts", "revisions", "preview", "scheduling"]
}

字段类型

EmDash 支持 15 种字段类型,并映射到 SQLite 列类型。

文本

string

短文本输入。映射到 TEXT 列。

{ slug: "title", type: "string", label: "Title" }

text

多行文本区域。映射到 TEXT 列。

{ slug: "excerpt", type: "text", label: "Excerpt" }

slug

URL 安全的 slug 字段。映射到 TEXT 列。

{ slug: "handle", type: "slug", label: "URL Handle" }

富文本

portableText

富文本编辑器(TipTap/ProseMirror)。以 JSON 存储。

{ slug: "content", type: "portableText", label: "Content" }

Portable Text 为块级格式,在不嵌入 HTML 的情况下保留结构。

json

任意 JSON 数据。以 JSON 存储。

{ slug: "metadata", type: "json", label: "Custom Metadata" }

数值

number

小数。映射到 REAL 列。

{ slug: "price", type: "number", label: "Price" }

integer

整数。映射到 INTEGER 列。

{ slug: "quantity", type: "integer", label: "Stock Quantity" }

布尔与日期时间

boolean

布尔开关。映射到 INTEGER(0/1)。

{ slug: "featured", type: "boolean", label: "Featured Post" }

datetime

日期时间选择器。以 ISO 8601 字符串存储。

{ slug: "eventDate", type: "datetime", label: "Event Date" }

选择

select

从列表中选一项。映射到 TEXT 列。

{
  slug: "status",
  type: "select",
  label: "Product Status",
  validation: {
    options: ["active", "discontinued", "coming_soon"]
  }
}

multiSelect

从列表中选多项。以 JSON 数组存储。

{
  slug: "features",
  type: "multiSelect",
  label: "Product Features",
  validation: {
    options: ["wireless", "waterproof", "eco-friendly"]
  }
}

媒体与引用

image

从媒体库选图。媒体 ID 存为 TEXT

{ slug: "featuredImage", type: "image", label: "Featured Image" }

file

从媒体库选文件。媒体 ID 存为 TEXT

{ slug: "attachment", type: "file", label: "PDF Attachment" }

reference

引用另一集合中的条目。条目 ID 存为 TEXT

{
  slug: "author",
  type: "reference",
  label: "Author",
  options: {
    collection: "authors"
  }
}

字段属性

所有字段均可使用下列属性。

属性类型说明
slugstring数据库列名
labelstring后台 UI 显示标签
typeFieldType15 种字段类型之一
requiredboolean是否必填
uniqueboolean是否要求在条目间唯一
defaultValueunknown新条目的默认值
validationobject按类型的校验规则
widgetstring自定义小组件标识
optionsobject小组件专属配置
sortOrdernumber在编辑器中的显示顺序

校验规则

validation 对象因字段类型而异。

interface FieldValidation {
	required?: boolean; // 所有类型
	min?: number; // number, integer
	max?: number; // number, integer
	minLength?: number; // string, text
	maxLength?: number; // string, text
	pattern?: string; // string(正则)
	options?: string[]; // select, multiSelect
}

带校验的示例:

{
  slug: "email",
  type: "string",
  label: "Email Address",
  required: true,
  unique: true,
  validation: {
    pattern: "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"
  }
}

小组件选项

options 配置字段专属 UI 行为。

interface FieldWidgetOptions {
	rows?: number; // text(textarea 行数)
	showPreview?: boolean; // image, file
	collection?: string; // reference(目标集合)
	allowMultiple?: boolean; // reference(多选引用)
	[key: string]: unknown; // 自定义小组件选项
}

引用字段示例:

{
  slug: "relatedProducts",
  type: "reference",
  label: "Related Products",
  options: {
    collection: "products",
    allowMultiple: true
  }
}

查询集合

使用提供的查询函数获取内容。遵循 Astro Live Collections 模式,返回结构化结果。

import { getEmDashCollection, getEmDashEntry } from "emdash";

// 所有条目 — 返回 { entries, error }
const { entries: posts } = await getEmDashCollection("posts");

// 按状态筛选
const { entries: drafts } = await getEmDashCollection("posts", {
	status: "draft",
});

// 限制条数
const { entries: recent } = await getEmDashCollection("posts", {
	limit: 5,
});

// 按分类法筛选
const { entries: newsPosts } = await getEmDashCollection("posts", {
	where: { category: "news" },
});

// 按 slug 取一条 — 返回 { entry, error, isPreview }
const { entry: post } = await getEmDashEntry("posts", "my-post-slug");

// 错误处理
const { entries, error } = await getEmDashCollection("posts");
if (error) {
	console.error("Failed to load posts:", error);
}

生成类型

npx emdash types 根据 schema 生成 TypeScript 类型。

// .emdash/types.ts(生成)
export interface Post {
	title: string;
	content: PortableTextBlock[];
	excerpt?: string;
	featuredImage?: string;
	author: string; // 引用 ID
}

export interface Product {
	title: string;
	price: number;
	description: PortableTextBlock[];
}

数据库映射

字段类型映射到 SQLite 列类型。

字段类型SQLite 类型备注
stringTEXT
textTEXT
slugTEXT
numberREAL64 位浮点
integerINTEGER64 位有符号整数
booleanINTEGER0 或 1
datetimeTEXTISO 8601 格式
selectTEXT
multiSelectJSON字符串数组
portableTextJSON块数组
imageTEXT媒体 ID
fileTEXT媒体 ID
referenceTEXT条目 ID
jsonJSON任意 JSON

下一步