回到 Canner

Canner CMS 整合 Firebase 資料結構

Array 處理

Firebase 的資料都是以 JSON 格式儲存,但是在 Firebase 中沒有 array ,所以從 Canner 上傳的資料如果是 array 的話我們會轉換至 object 的格式讓資料在 Firebase 上是合法的。

array 在 Firebase Realtime Database 中是不合法的,但是在 canner.data.js 可以使用 array。在做資料上傳時我們會處理成 Firebase 能支持的資料格式。

舉例來說,你在 canner.data.js 上可以用 array 的格式處理 posts

1
2
3
4
5
6
7
8
9
10
11
12
{
  posts: [
    {
      title: "title 1",
      content: "content 1"
    },
    {
      title: "title 2",
      content: "content 2"
    }
  ]
}

當你使用 canner data:import 時我們會把 array 轉乘 object 並讓 Firebase 的 SDK 產生 unique key。

1
2
3
4
5
6
7
8
9
10
11
12
{
  posts: {
    UNIQUE_ID1: {
      title: "title 1",
      content: "content 1"
    },
    UNIQUE_ID2: {
      title: "title 2",
      content: "content 2"
    }
  }
}

當開發者選用 array type 的 CMS UI plugins 去呈現這筆資料,Canner CMS 從 Firebase 取得資料後,就會自動將 key 值作為每筆 post_id 以保證資料格式的正確性。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
  posts: [
    {
      _id: UNIQUE_ID1,
      title: "title 1",
      content: "content 1"
    },
    {
      _id: UNIQUE_ID2,
      title: "title 2",
      content: "content 2"
    }
  ]
}

而在更新資料方面,也會用 Firebase 提供的 API 取得正確的 key,保證在 Firebase 上的資料變更也是符合 Firebase 的資料規範。