ב-Firebase SQL Connect, אפשר לבצע טעינה ועדכון של נתונים בכמות גדולה בדרכים שונות, בהתאם לתהליכי העבודה ולסביבות שלכם:
באב טיפוס מקומי, כשמנסים סכימות חלופיות, אפשר ליצור מוטציות של נתוני seeding ולקרוא להן בסביבת פיתוח מקומית באמצעות התוסף SQL Connect VS Code, האמולטור SQL Connect ומופע מקומי של מסד נתונים.
בפיתוח של מוצר, עם סכימה יציבה, כשמבצעים תהליכי CI/CD גדולים יותר ומנהלים נתוני ייצור, יש שתי אפשרויות:
הגישה המועדפת היא להשתמש ב-Firebase Admin SDK, קבוצה של ספריות שפועלות בסביבות עם הרשאות.
אתם יכולים גם להשתמש בכלי SQL עם מופע Cloud SQL כדי לבצע טעינות ועדכונים בכמות גדולה, כל עוד אתם משנים נתונים ולא את סכימת מסד הנתונים. שינוי סכימת מסד הנתונים ישירות באמצעות כלי SQL עלול לגרום לשיבוש בסכימה ובמחברים של SQL 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
בהתאם לשלב של יצירת אב טיפוס, אפשר להשתמש באותה טכניקה שמוסברת במדריך לתחילת העבודה כדי להוסיף רשומה אחת או שתיים: כלומר, אפשר להשתמש ב-CodeLenses בתוסף SQL Connect ל-VS Code כדי ליצור _insert מוטציות, להקשיח נתונים ולהריץ את המוטציות האלה ב-VS Code.
בסופו של דבר, יהיה הגיוני יותר להוסיף הרבה רשומות לטבלה באמצעות פעולת _insertMany. בדוגמה של אפליקציית ביקורת הסרטים, הפעולה הזו מוסיפה קבוצה ראשונית של נתונים ב-Movie וב-Actor.
כדי להריץ את השינויים הבאים באמצעות התוסף SQL Connect VS Code, בתצוגת עורך הקבצים המתאימה, לוחצים על הלחצנים 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"
}
])
}
הזנת נתוני Seed לטבלת ה-Join MovieActor
כדי לבדוק שאילתות ומוטציות באמצעות שאילתות איחוד (join) ופעולות מורכבות אחרות, אפשר להוסיף כמה רשומות לטבלה 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"
}
])
}
הוספת נתוני התחלה לטבלאות קשורות באמצעות פעולות מקוננות (קשרים של אחד לרבים ושל אחד לאחד)
כדי לאכלס טבלאות קשורות באופן אטומי, אפשר לבצע הוספה יחסית מקוננת באמצעות מטענים מילוליים. כך נוצר רשומה ראשית ורשומות משניות משויכות בפעולה אחת, בלי שנדרשת התאמה ידנית של מפתח זר.
במקרים של קשרים מסוג אחד לרבים, צריך לספק מערך של רשומות צאצא מוטמעות. במקרים של קשרים אחד על אחד, צריך לספק אובייקט צאצא מוטמע יחיד.
# Nested insert for Movie and Review (one-to-many)
# 1 movie and 2 reviews shown
mutation {
movie_insert(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",
reviews_on_movie: [
{
id: "123e4567-e89b-12d3-a456-426614174002",
rating: 5,
reviewText: "Amazing concept!",
user: { id: "user-uuid-123" }
},
{
id: "123e4567-e89b-12d3-a456-426614174003",
rating: 4,
reviewText: "A bit confusing, but great.",
user: { id: "user-uuid-456" }
}
]
})
}
# Nested insert for User and Profile (one-to-one)
mutation {
user_insert(data: {
id: "user-uuid-123",
name: "Alice",
profile_on_user: {
bio: "Avid moviegoer and critic."
}
})
}
כתיבת מוטציה לאיפוס נתוני ה-seed
במהלך יצירת אב טיפוס וביצוע CI/CD, יכול להיות שימושי לאפס את הנתונים למצב אפס כדי להריץ סדרה חדשה של בדיקות על קבוצת נתונים חדשה.
כדי לעשות זאת, אם קוד האב-טיפוס לא מוסיף רשומות לטבלאות, צריך להשתמש במוטציה _upsertMany שסופקה על ידי SQL Connect.
בדוגמה הבאה, הפונקציה 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
גם אם אתם עובדים בעיקר באופן מקומי, מומלץ להגדיר את Admin SDK כדי שתוכלו להשתמש ב-Firebase SQL Connect מסביבה עם הרשאות, כולל הסביבה המקומית שלכם. צריך להגדיר את Admin SDK ל-Node.js.
מידע נוסף על שימוש ב-SDK לאדמינים בSQL Connectתרחישי שימוש אחרים
ביצוע טעינות ועדכונים בכמות גדולה של נתוני ייצור
ה-API לניהול נתונים בכמות גדולה יוצר מוטציות של GraphQL בשמכם, במקום לבקש מכם ליצור מחרוזות mutation {...} באמצעות ה-API executeGraphQL שתיארנו קודם כדי להוסיף כמה שורות פה ושם באופן מקומי.
יתרון משמעותי של ה-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",
// Nested reviews can be inserted atomically along with the movie
reviews_on_movie: [
{
rating: 5,
reviewText: "Amazing concept!",
user: { id: "user-123" } // Link to existing user
}
]
},
{
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",
reviews_on_movie: [
{
rating: 5,
reviewText: "A masterpiece of sci-fi action.",
user: { id: "user-456" }
}
]
}
];
// 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 כדי לנהל את טעינות הנתונים והעדכונים.
מידע נוסף על ייבוא נתונים זמין במדריך Cloud SQL ל-PostgreSQL.