字段类型参考

本页内容

EmDash 支持 16 种字段类型来定义内容模式。每种类型都映射到 SQLite 列类型,并提供相应的管理界面。

概述

下表列出了每种字段类型及其 SQLite 列:

类型SQLite 列描述
stringTEXT短文本输入
textTEXT多行文本
urlTEXTURL 值
numberREAL十进制数
integerINTEGER整数
booleanINTEGER真/假
datetimeTEXT日期和时间
selectTEXT从选项中单选
multiSelectJSON多选
portableTextJSON富文本内容
imageTEXT图像引用
fileTEXT文件引用
referenceTEXT对另一个条目的引用
jsonJSON任意 JSON 数据
slugTEXTURL 安全标识符
repeaterJSON重复字段组

文本类型

string

短的单行文本。用于标题、名称和短值。

{
  slug: "title",
  label: "Title",
  type: "string",
  required: true,
  validation: {
    minLength: 1,
    maxLength: 200,
  },
}

验证选项:

  • minLength — 最小字符数
  • maxLength — 最大字符数
  • pattern — 要匹配的正则表达式模式

小部件选项:

  • 无特定选项

text

多行纯文本。用于描述、摘要和较长的纯文本。

{
  slug: "excerpt",
  label: "Excerpt",
  type: "text",
  options: {
    rows: 3,
  },
}

验证选项:

  • minLength — 最小字符数
  • maxLength — 最大字符数

小部件选项:

  • rows — 文本区域的行数(默认:3)

slug

URL 安全标识符。从另一个字段自动生成或手动输入。

{
  slug: "slug",
  label: "URL Slug",
  type: "slug",
  required: true,
  unique: true,
}

Slug 会自动清理:小写,空格替换为连字符,特殊字符移除。

数字类型

number

十进制数。用于价格、评分和测量值。

{
  slug: "price",
  label: "Price",
  type: "number",
  required: true,
  validation: {
    min: 0,
    max: 999999.99,
  },
}

验证选项:

  • min — 最小值
  • max — 最大值

存储为 SQLite REAL(64 位浮点数)。

integer

整数。用于数量、计数和订单值。

{
  slug: "quantity",
  label: "Quantity",
  type: "integer",
  defaultValue: 1,
  validation: {
    min: 0,
    max: 1000,
  },
}

验证选项:

  • min — 最小值
  • max — 最大值

存储为 SQLite INTEGER。

boolean

真或假。用于开关和标志。

{
  slug: "featured",
  label: "Featured",
  type: "boolean",
  defaultValue: false,
}

存储为 SQLite INTEGER(0 或 1)。

日期和时间

datetime

日期和时间值。以 ISO 8601 格式存储。

{
  slug: "publishedAt",
  label: "Published At",
  type: "datetime",
}

验证选项:

  • min — 最小日期(ISO 字符串)
  • max — 最大日期(ISO 字符串)

存储格式: 2025-01-24T12:00:00.000Z

选择类型

select

从预定义选项中单选。

{
  slug: "status",
  label: "Status",
  type: "select",
  required: true,
  defaultValue: "draft",
  validation: {
    options: ["draft", "published", "archived"],
  },
}

验证选项:

  • options — 允许值的数组(必需)

存储为包含所选值的 TEXT。

multiSelect

从预定义选项中多选。

{
  slug: "tags",
  label: "Tags",
  type: "multiSelect",
  validation: {
    options: ["news", "tutorial", "review", "opinion"],
  },
}

验证选项:

  • options — 允许值的数组(必需)

存储为 JSON 数组:["news", "tutorial"]

富文本内容

portableText

使用 Portable Text 格式的富文本内容。支持标题、列表、链接、图像和自定义块。

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

值存储为 Portable Text 块的 JSON 数组,例如:

[
	{
		"_type": "block",
		"style": "normal",
		"children": [{ "_type": "span", "text": "Hello world" }]
	}
]

插件可以向编辑器添加自定义块类型(嵌入、小部件等)。这些会出现在斜杠命令菜单中,并在网站上自动呈现。参见 Portable Text 渲染组件

媒体类型

image

对已上传图像的引用。包括尺寸和替代文本等元数据。

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

小部件选项:

  • showPreview — 在管理界面中显示图像预览(默认:true)

值存储为包含媒体引用及其元数据的对象:

{
	"id": "01HXK5MZSN...",
	"url": "https://cdn.example.com/image.jpg",
	"alt": "Description",
	"width": 1920,
	"height": 1080
}

file

对已上传文件(如文档或 PDF)的引用。

{
  slug: "document",
  label: "Document",
  type: "file",
}

值存储为包含文件引用及其元数据的对象:

{
	"id": "01HXK5MZSN...",
	"url": "https://cdn.example.com/doc.pdf",
	"filename": "report.pdf",
	"mimeType": "application/pdf",
	"size": 102400
}

关系类型

reference

对另一个内容条目的引用。

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

小部件选项:

  • collection — 目标集合 slug(必需)
  • allowMultiple — 允许多个引用(默认:false)

单个引用存储为目标条目 ID:

"01HXK5MZSN..."

多个引用存储为条目 ID 数组:

["01HXK5MZSN...", "01HXK6NATS..."]

灵活类型

json

任意 JSON 数据。用于复杂的嵌套结构、第三方集成或没有固定架构的数据。

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

原样存储在 SQLite JSON 列中。

字段属性

所有字段都支持这些通用属性:

属性类型描述
slugstring唯一标识符(必需)
labelstring显示名称(必需)
typeFieldType字段类型(必需)
requiredboolean需要值(默认:false)
uniqueboolean强制唯一性(默认:false)
defaultValueunknown新条目的默认值
validationobject特定类型的验证规则
widgetstring自定义小部件覆盖
optionsobject小部件配置
sortOrdernumber管理界面中的显示顺序

保留字段 slug

这些 slug 是保留的,不能使用:

  • id
  • slug
  • status
  • author_id
  • primary_byline_id
  • created_at
  • updated_at
  • published_at
  • scheduled_at
  • deleted_at
  • version
  • live_revision_id
  • draft_revision_id
  • terms
  • bylines
  • byline

TypeScript 类型

导入字段类型定义以供编程使用:

import type { FieldType, Field, CreateFieldInput } from "emdash";

const fieldTypes: FieldType[] = [
	"string",
	"text",
	"url",
	"number",
	"integer",
	"boolean",
	"datetime",
	"select",
	"multiSelect",
	"portableText",
	"image",
	"file",
	"reference",
	"json",
	"slug",
	"repeater",
];