字段类型参考

本页内容

EmDash 支持 14 种字段类型用于定义内容模式。每种类型映射到 SQLite 列类型,并提供相应的管理后台 UI。

Overview

TypeSQLite ColumnDescription
stringTEXT短文本单行输入
textTEXT多行文本
numberREAL小数
integerINTEGER整数
booleanINTEGER真 / 假
datetimeTEXT日期与时间
selectTEXT从选项中单选
multiSelectJSON多选
portableTextJSON富文本内容
imageTEXT图片引用
fileTEXT文件引用
referenceTEXT指向另一条目的引用
jsonJSON任意 JSON 数据
slugTEXTURL 安全标识符

Text Types

string

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

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

Validation options:

  • minLength — 最少字符数
  • maxLength — 最多字符数
  • pattern — 需匹配的正则模式

Widget options:

  • 无特定项

text

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

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

Validation options:

  • minLength — 最少字符数
  • maxLength — 最多字符数

Widget options:

  • rows — textarea 行数(默认:3)

slug

URL 安全标识符。可从其他字段自动生成或手动输入。

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

slug 会自动规范化:小写、空格变连字符、移除特殊字符。

Number Types

number

小数。用于价格、评分和度量。

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

Validation options:

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

以 SQLite REAL(64 位浮点)存储。

integer

整数。用于数量、计数和排序值。

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

Validation options:

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

以 SQLite INTEGER 存储。

boolean

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

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

以 SQLite INTEGER(0 或 1)存储。

Date and Time

datetime

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

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

Validation options:

  • min — 最早日期(ISO 字符串)
  • max — 最晚日期(ISO 字符串)

Storage format: 2025-01-24T12:00:00.000Z

Selection Types

select

从预定义选项中单选。

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

Validation options:

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

以包含所选值的 TEXT 存储。

multiSelect

从预定义选项中多选。

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

Validation options:

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

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

Rich Content

portableText

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

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

以 Portable Text 块的 JSON 数组存储:

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

插件可向编辑器添加自定义块类型(嵌入、小组件等)。它们出现在斜杠命令菜单中,并在站点上自动渲染。参见 Creating Plugins — Portable Text Block Types.

Media Types

image

指向已上传图片的引用。包含尺寸、替代文本等元数据。

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

Widget options:

  • showPreview — 在后台显示图片预览(默认:true)

Stored value:

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

file

指向已上传文件(文档、PDF 等)的引用。

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

Stored value:

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

Relational Types

reference

指向另一条内容条目的引用。

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

Widget options:

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

Single reference stored value:

"01HXK5MZSN..."

Multiple references stored value:

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

Flexible Types

json

任意 JSON 数据。用于嵌套结构、第三方集成或无固定模式的数据。

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

原样存储在 SQLite JSON 列中。

Field Properties

所有字段都支持以下通用属性:

PropertyTypeDescription
slugstring唯一标识符(必填)
labelstring显示名称(必填)
typeFieldType字段类型(必填)
requiredboolean要求有值(默认:false)
uniqueboolean强制唯一(默认:false)
defaultValueunknown新条目的默认值
validationobject按类型的校验规则
widgetstring自定义小组件覆盖
optionsobject小组件配置
sortOrdernumber在后台中的显示顺序

Reserved Field Slugs

以下 slug 已保留,不能使用:

  • id
  • slug
  • status
  • author_id
  • created_at
  • updated_at
  • published_at
  • deleted_at
  • version

TypeScript Types

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

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

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