集合與欄位

本頁內容

集合(collection)是一種內容類型(文章、頁面、產品)。它的欄位定義設置每個條目的資料結構。

建立集合

透過管理面板的內容類型建立集合。每個集合具有以下屬性:

EmDash內容類型展示頁面、文章和自訂集合及其功能
屬性描述
slugURL安全識別碼(例如 postsproducts)
label顯示名稱(例如「部落格文章」)
labelSingular單數形式(例如「文章」)
description編輯者的可選描述
icon管理側邊欄的Lucide圖示名稱
supports草稿、修訂、預覽、排程、搜尋、SEO等功能

集合功能

建立集合時,啟用您需要的功能:

功能描述
drafts啟用草稿/已發布工作流程
revisions透過版本快照追蹤內容歷史
preview為草稿內容產生簽署預覽URL
scheduling排程在未來日期發布內容

以下集合啟用了所有四項功能:

{
  slug: "posts",
  label: "Blog Posts",
  labelSingular: "Post",
  supports: ["drafts", "revisions", "preview", "scheduling"]
}

欄位類型

EmDash支援16種欄位類型,這些類型對應到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管理介面中的顯示標籤
typeFieldType16種欄位類型之一
requiredboolean欄位是否必須有值
uniqueboolean值是否必須在條目之間唯一
defaultValueunknown新條目的預設值
validationobject類型特定的驗證規則
widgetstring自訂小工具識別碼
optionsobject小工具特定組態
sortOrdernumber編輯器中的顯示順序

驗證規則

validation物件因欄位類型而異。其完整形式為:

interface FieldValidation {
	required?: boolean; // All types
	min?: number; // number, integer
	max?: number; // number, integer
	minLength?: number; // string, text
	maxLength?: number; // string, text
	pattern?: string; // string (regex)
	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 rows)
	showPreview?: boolean; // image, file
	collection?: string; // reference (target collection)
	allowMultiple?: boolean; // reference (multiple refs)
	[key: string]: unknown; // Custom widget options
}

以下參照欄位連結多個產品:

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

查詢集合

使用提供的查詢函式來取得內容。這些遵循Astro的即時集合模式,回傳結構化結果。以下範例顯示了常見的查詢選項:

import { getEmDashCollection, getEmDashEntry } from "emdash";

// Get all entries - returns { entries, error }
const { entries: posts } = await getEmDashCollection("posts");

// Filter by status
const { entries: drafts } = await getEmDashCollection("posts", {
	status: "draft",
});

// Limit results
const { entries: recent } = await getEmDashCollection("posts", {
	limit: 5,
});

// Filter by taxonomy
const { entries: newsPosts } = await getEmDashCollection("posts", {
	where: { category: "news" },
});

// Get a single entry by slug - returns { entry, error, isPreview }
const { entry: post } = await getEmDashEntry("posts", "my-post-slug");

// Handle errors
const { entries, error } = await getEmDashCollection("posts");
if (error) {
	console.error("Failed to load posts:", error);
}

類型產生

執行npx emdash types從您的架構產生TypeScript類型。產生的檔案包含每個集合的一個介面:

export interface Post {
	title: string;
	content: PortableTextBlock[];
	excerpt?: string;
	featuredImage?: string;
	author: string; // reference ID
}

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

資料庫對應

欄位類型對應到SQLite欄位類型如下:

欄位類型SQLite類型註解
stringTEXT
textTEXT
slugTEXT
urlTEXT
numberREAL64位元浮點數
integerINTEGER64位元有符號整數
booleanINTEGER0或1
datetimeTEXTISO 8601格式
selectTEXT
multiSelectJSON字串陣列
portableTextJSON區塊陣列
imageTEXT媒體ID
fileTEXT媒體ID
referenceTEXT條目ID
jsonJSON任意JSON
repeaterJSON子欄位陣列

下一步