在 Firebase Data Connect 中,您可以根據工作流程和環境,透過不同方式執行大量資料載入和更新作業:
在本機原型設計中,當您嘗試替代結構定義時,可以使用 VS Code 擴充功能、Data Connect 模擬器和本機資料庫執行個體,在本機開發環境中建立及呼叫資料播種突變。
在正式版開發中,如果結構定義穩定,且您要執行較大的 CI/CD 流程並管理正式版資料,可以選擇下列兩種做法:
建議您使用 Firebase Admin SDK,這是一組在具備特殊權限的環境中執行的程式庫。
您也可以搭配 Cloud SQL 執行個體使用 SQL 工具,執行大量載入和更新作業,但前提是您修改的是資料,而非資料庫結構定義。直接使用 SQL 工具修改資料庫結構定義,可能會導致 Data Connect 結構定義和連接器中斷。
本機原型設計:在本機執行個體中提供種子資料
在入門指南中,您已設定應用程式,使用臨時插入突變將單一記錄新增至單一表格。
為了能使用,電影評論應用程式需要電影、評論和使用者資料,才能製作原型查詢和變動,並在多個資料表上使用聯結和其他作業,以及真實資料。您可以擴充結構定義,並為資料庫植入資料。
原型設計環境需要程式碼才能執行資料播種作業。本指南提供一些範例,說明:
- 在個別資料表中使用
_insertMany
和_upsertMany
- 在相關資料表中使用
_insertMany
更新電影評論應用程式結構定義
您可以使用 _insertMany
和 _upsertMany
突變,一次更新一個資料庫資料表,或更新透過聯結關係相關的多個資料表。下圖顯示擴充的電影評論應用程式結構定義,有助於說明這些用途和範例。這會將 schema.gql
擴展到起始 Movie
類型以外,納入 Actor
和 MovieActor
類型,因此我們可以製作更複雜的查詢原型。
# Actors
# Suppose an actor can participate in multiple movies and movies can have multiple actors
# Movie - Actors (or vice versa) is a many to many relationship
type Actor @table {
id: UUID!
imageUrl: String!
name: String! @col(name: "name", dataType: "varchar(30)")
}
# Join table for many-to-many relationship for movies and actors
# The 'key' param signifies the primary key(s) of this table
# In this case, the keys are [movieId, actorId], the generated fields of the reference types [movie, actor]
type MovieActor @table(key: ["movie", "actor"]) {
# @ref creates a field in the current table (MovieActor) that holds the primary key of the referenced type
# In this case, @ref(fields: "movieId", references: "id") is implied
movie: Movie!
# movieId: UUID! <- this is created by the implied @ref
actor: Actor!
# actorId: UUID! <- this is created by the implied @ref
role: String! # "main" or "supporting"
}
寫入變異,以植入零狀態資料
在原型設計期間,如果需要針對一系列離散值測試查詢和變動,可以填入多筆記錄的資料。舉例來說,您可能會想新增多筆電影記錄,並使用不同類型的類型和分級,以便測試比較和篩選功能。
將資料傳播至 Movie
和 Actor
資料表
視原型設計階段而定,您可以採用「開始使用」指南中介紹的相同技巧,插入一或兩筆記錄,也就是在 VS Code 擴充功能中使用 CodeLens 建立 _insert
突變、硬式編碼資料,以及在 VS Code 中執行這些突變。
最終,使用 _insertMany
運算將多筆記錄新增至資料表會更有意義。在電影評論應用程式範例中,這會在 Movie
和 Actor
中插入初始資料集。
如要執行下列突變,請使用 VS Code Firebase 擴充功能,在適當的檔案編輯器檢視畫面中,按一下「Run (Production)」(執行 (正式環境))或「Run (Local)」(執行 (本機)) CodeLens 按鈕,視您是使用正式環境服務還是本機資料庫製作原型而定。
# insertMany for Movie
# 2 records shown
mutation {
movie_insertMany(data: [
{
id: "550e8400-e29b-41d4-a716-446655440000",
title: "Inception",
imageUrl: "https://firebasestorage.googleapis.com/v0/b/fdc-quickstart-web.appspot.com/o/movies%2Finception.jpg?alt=media&token=07b09781-b302-4623-a5c3-1956d0143168",
genre: "sci-fi",
},
{
id: "550e8400-e29b-41d4-a716-446655440001",
title: "The Matrix",
imageUrl: "https://firebasestorage.googleapis.com/v0/b/fdc-quickstart-web.appspot.com/o/movies%2Fthe_matrix.jpg?alt=media&token=4975645d-fef8-409e-84a5-bcc1046e2059",
genre: "action",
}
])
}
# insertMany for Actor
# 2 records shown
mutation {
actor_insertMany(data: [
{
id: "123e4567-e89b-12d3-a456-426614174000",
imageUrl: "https://firebasestorage.googleapis.com/v0/b/fdc-quickstart-web.appspot.com/o/actors%2Fdicaprio.jpeg?alt=media&token=452e030a-efa5-4ef4-bb81-502b23241316",
name: "Leonardo DiCaprio"
},
{
id: "123e4567-e89b-12d3-a456-426614174001",
imageUrl: "https://firebasestorage.googleapis.com/v0/b/fdc-quickstart-web.appspot.com/o/actors%2Fkeanu.jpg?alt=media&token=6056520c-ef3e-4823-aad0-108aab163115",
name: "Keanu Reeves"
}
])
}
將資料傳播至 MovieActor
聯結資料表
如要使用聯結和其他複雜作業測試查詢和突變,可以將多筆記錄新增至 MovieActor
資料表。
在這種關係中更新多個表格時,您可以新增 @transaction
指令,確保更新作業順利完成。
mutation @transaction {
movie_insertMany(data: [
{
id: "550e8400-e29b-41d4-a716-446655440000",
title: "Inception",
imageUrl: "https://firebasestorage.googleapis.com/v0/b/fdc-quickstart-web.appspot.com/o/movies%2Finception.jpg?alt=media&token=07b09781-b302-4623-a5c3-1956d0143168",
genre: "sci-fi",
},
{
id: "550e8400-e29b-41d4-a716-446655440001",
title: "The Matrix",
imageUrl: "https://firebasestorage.googleapis.com/v0/b/fdc-quickstart-web.appspot.com/o/movies%2Fthe_matrix.jpg?alt=media&token=4975645d-fef8-409e-84a5-bcc1046e2059",
genre: "action",
}
])
actor_insertMany(data: [
{
id: "123e4567-e89b-12d3-a456-426614174000",
imageUrl: "https://firebasestorage.googleapis.com/v0/b/fdc-quickstart-web.appspot.com/o/actors%2Fdicaprio.jpeg?alt=media&token=452e030a-efa5-4ef4-bb81-502b23241316",
name: "Leonardo DiCaprio"
},
{
id: "123e4567-e89b-12d3-a456-426614174001",
imageUrl: "https://firebasestorage.googleapis.com/v0/b/fdc-quickstart-web.appspot.com/o/actors%2Fkeanu.jpg?alt=media&token=6056520c-ef3e-4823-aad0-108aab163115",
name: "Keanu Reeves"
}
])
}
撰寫變異來重設種子資料
在原型設計和執行 CI/CD 時,將資料重設為零狀態,以便對新資料集執行一系列新測試,這會很有幫助。
如要這麼做,如果原型程式碼未將記錄新增至表格,請使用 Data Connect 提供的 _upsertMany
突變。
在下列範例中,系統會使用初始值呼叫 movie_upsertMany
,將電影記錄更新為原始狀態。
mutation {
# Execute an upsertMany operation to update the Movie table
movie_upsertMany(data: [
{
id: "550e8400-e29b-41d4-a716-446655440000",
title: "Inception",
imageUrl: "https://firebasestorage.googleapis.com/v0/b/fdc-quickstart-web.appspot.com/o/movies%2Finception.jpg?alt=media&token=07b09781-b302-4623-a5c3-1956d0143168",
genre: "sci-fi",
},
{
id: "550e8400-e29b-41d4-a716-446655440001",
title: "The Matrix",
imageUrl: "https://firebasestorage.googleapis.com/v0/b/fdc-quickstart-web.appspot.com/o/movies%2Fthe_matrix.jpg?alt=media&token=4975645d-fef8-409e-84a5-bcc1046e2059",
genre: "action",
}
…
}
開發正式版:使用 Admin SDK 填入及更新
如果您想在享有權限的環境中工作,可以使用 Firebase Admin SDK。如果您想載入數千筆記錄,這就是重要的用途,因為對實際工作環境資料進行大量資料作業至關重要。
安裝 Firebase Admin SDK
即使您主要在本機工作,Firebase 仍建議設定 Admin SDK,以便從具備權限的環境 (包括本機環境) 使用 Firebase Data Connect。您需要為 Node.js 設定 Admin SDK。
進一步瞭解如何在其他用途中使用 Admin SDK。Data Connect
大量載入及更新生產資料
大量資料管理 API 會代表您建構 GraphQL 突變,而不是要求您使用先前所述的 executeGraphQL
API 建構 mutation {...}
字串,以便在本機新增幾列資料。
管理 API 的主要優點是能夠分別管理及重複使用 CI/CD 流程的資料陣列,或是為生產資料設定大型大量資料檔案。
下列程式碼片段示範如何設定大量資料指令碼。
import { initializeApp } from 'firebase-admin/app';
import { getDataConnect } from 'firebase-admin/data-connect';
const app = initializeApp();
const dc = getDataConnect({ location: "us-west2", serviceId: "my-service" });
const data = [
{
id: "550e8400-e29b-41d4-a716-446655440000",
title: "Inception",
imageUrl: "https://firebasestorage.googleapis.com/v0/b/fdc-quickstart-web.appspot.com/o/movies%2Finception.jpg?alt=media&token=07b09781-b302-4623-a5c3-1956d0143168",
genre: "sci-fi",
},
{
id: "550e8400-e29b-41d4-a716-446655440001",
title: "The Matrix",
imageUrl: "https://firebasestorage.googleapis.com/v0/b/fdc-quickstart-web.appspot.com/o/movies%2Fthe_matrix.jpg?alt=media&token=4975645d-fef8-409e-84a5-bcc1046e2059",
genre: "action",
}
];
// Methods of the bulk operations API
const resp = await dc.insert("movie" /*table name*/, data[0]);
// Or
const resp = await dc.insertMany("movie" /*table name*/, data);
// Or
const resp = await dc.upsert("movie" /*table name*/, data[0]);
// Or
const resp = await dc.upsertMany("movie" /*table name*/, data);
正式版開發:使用 SQL 大量更新資料
在正式環境中使用穩定結構定義,且未修改結構定義時,您可以在 Cloud SQL 執行個體中管理資料載入和更新。
請參閱 PostgreSQL 適用的 Cloud SQL 資料匯入指南。