Headless CMS Sanity 数据建模——定义文档内容的结构
定義內容的結構。
使用代碼定義內容模型是設計使然。它使版本控制變得更容易,并使開發(fā)人員能夠控制數(shù)據(jù)結構的布局方式。我們讓在界面中添加、更改和刪除字段變得毫不費力。
觀看Schema 工作原理的視頻,或滾動閱讀它的工作原理。
你的第一個 Schema
當 Sanity 啟動時,默認在項目 schemas 文件夾中查找 schema.js。讓我們來構建一個簡單的 Schema 開始:
// 首先,必須引入 schema-creator import createSchema from "part:@sanity/base/schema-creator";// 然后引入插件可能暴露的 Schema 類型expose them import schemaTypes from "all:part:@sanity/base/schema-type";// 接著創(chuàng)建 Schema export default createSchema({// 名稱name: "mySchema",// 連接文檔類型,目前只需要一個types: schemaTypes.concat([{// 類型顯示名稱title: "Person",//API 標識符name: "person",// 文檔類型為 `document `type: "document",// 字段聲明fields: [// 該文檔類型僅有一個字段{// 顯示名稱title: "Name",// API 標識符name: "name",// 字段類型type: "string",},],},]), });這個 Schema 配置創(chuàng)建了一個 person 的文檔類型,它包含一個 name 字段。
稍后通過 API 獲取這樣的文檔時,我們會得到這樣的文檔:
{"_id": "45681087-46e7-42e7-80a4-65b776e19f91","_type": "person","name": "Ken Kesey" }一個文檔引用另一個文檔
現(xiàn)在來設計一個描述一本書的簡單文檔。類型數(shù)組中,我們添加這樣一個對象:
{title: 'Book',name: 'book',type: 'document',fields: [{title: 'Title',name: 'title',type: 'string'},{title: 'Cover',name: 'cover',type: 'image'},{title: 'Author',name: 'author',// `reference` 類型來指向另一個文檔type: 'reference',// 這個引用只允許指向 `person` 類型的文檔// 雖然可以列舉更多類型,但盡量保持精簡:to: [{type: 'person'}]}] }這個 Schema 創(chuàng)建了一個叫做 book 的文檔類型,它包含三個字段:
- 標題
- 封面圖片
- 作者(該字段引用了另一個文檔,在 to 字段里描述引用的文檔類型。列出了一條規(guī)則為,類型可以是 person)
表單看起來是這個樣子:
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-p8lP5bGO-1659519232132)(https://cdn.sanity.io/images/3do82whm/next/5ec1ee1a6caae30d9a6b83c8212cdffec3c3f4a4-691x764.png?w=691&h=764&fit=clip&auto=format)]
當通過 API 獲取這個文檔時,我們會得到:
{_id: "d1760c53-428c-4324-9297-ac8313276c45",_type: "book",title: "One Flew Over the Cuckoos Nest",cover: {_type: "image",asset: {_ref: "image-Su3NWQ712Yg0ACas3JN9VpcS-322x450-jpg",_type: "reference"}},author: {_ref: "45681087-46e7-42e7-80a4-65b776e19f91",_type: "reference"} }如您所見,作者字段沒有提及 Ken Kesey,但字段 _ref 中包含了 Ken Kesey 文檔的 id。當獲取引用的文檔時,可以輕松的通過指示 API 去體會為目標文檔的實際內容。您可以在 Query 教程 中相關信息。
數(shù)組的一種使用
讓我們談談數(shù)組:有些書有不止一位作者。我們應該通過將作者字段設置為引用數(shù)組來改進 Book 文檔類型:
{title: 'Authors',name: 'authors',type: 'array',of: [{type: 'reference',to: [{type: 'person'}]}] }從 API 返回的文檔將如下所示:
{_id: "drafts.e7f370d0-f86f-4a09-96ea-12f1d9b236c4",_type: "book",title: "The Illuminatus! Trilogy",cover: {_type: "image",asset: {_ref: "image-Ov3HwbkOYkNrM2yabmBr2M8T-318x473-jpg",_type: "reference"}},authors: [{_ref: "9a8eb52c-bf37-4d6e-9321-8c4674673198",_type: "reference"},{_ref: "ee58f2ff-33ed-4273-8031-b74b5664ff5e",_type: "reference"}] }組織架構
最后的注意點是組織你的文件(們)。在這個示例中,我們將兩種類型文檔的定義都堆積到了同一個 JS 文件中。不推薦這么做;這會很快讓代碼失控。推薦的做法是在每個單獨的文件中描述每種文檔類型:
// 文件: schemas/schema.js import createSchema from 'part:@sanity/base/schema-creator' import schemaTypes from 'all:part:@sanity/base/schema-type' import person from './person'export default createSchema({name: 'mySchema',types: schemaTypes.concat([person]) })// 文件: schemas/person.js export default {title: "Person",name: "person",type: "document",fields: [{title: "Name",name: "name",type: "string",}] }這涵蓋了非常基礎的內容,但還有更多內容!現(xiàn)在,讓我們深入探討使用 Sanity 對內容進行建模時的最佳實踐。
總結
以上是生活随笔為你收集整理的Headless CMS Sanity 数据建模——定义文档内容的结构的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 手机搜狐 html5,手机搜狐网计划改版
- 下一篇: C++版和MATLAB版调用摄像头显示画