באמצעות ה-CLI של Firebase אתם יכולים לנהל את הפרויקטים ב-Firebase בספריית פרויקטים מקומית עם בקרת גרסאות. ההרשאות האלה כוללות שירותי SQL Connect בפרויקטים, מחברים לשירותים האלה ומשאבים כמו סכימה, מקורות של שאילתות ומוטציות לכל מחבר. באמצעות ה-CLI אפשר גם להתקין ולהפעיל את האמולטור של Firebase SQL Connect. ה-CLI הוא חלופה יעילה לעבודה במסוף Firebase.
הוראות להתקנת הניסוי של ה-CLI של Firebase לתוכנית Private Preview, ופקודות CLI שקשורות ל-SQL Connect, מופיעות בחומר העזר של ה-CLI של Firebase.
במדריך הזה מפורטים:
- רשומות ספציפיות ל-SQL Connect בקובץ התצורה של פרויקט
firebase.json. - SQL Connect הגדרות ב-
dataconnect.yamlוב-connector.yaml. - משתני סביבה של אמולטור SQL Connect.
קובצי תצורה של פרויקט Firebase
חומר עזר בנושא קובץ התצורה firebase.json
משתמשים במפתחות dataconnect ו-emulators.dataconnect בקובץ firebase.json כדי להגדיר שירות אחד או יותר של SQL Connect ואת האמולטור המקומי בפרויקט:
{
"dataconnect": {
"source": "dataconnect"
},
"emulators": {
"dataconnect": {
"host": "localhost",
"port": 9399,
"postgresPort": 5432,
"dataDir": "dataconnect/.dataconnect/pgliteData"
}
}
}
הקובץ firebase.json תומך במפתחות SQL Connect הבאים:
-
dataconnect: נדרש לפרויקטים של SQL Connect. מקבל אובייקט אחד או מערך של אובייקטים כשיש בפרויקט כמה שירותים.-
source: שדה חובה. הנתיב לספרייה שמכילה את קובץ השירות שלdataconnect.yaml. -
predeployו-postdeploy: אופציונלי. מחרוזות פקודות או מערכים של פקודות להרצה לפני או אחרי פריסת משאבי SQL Connect.
-
-
emulators.dataconnect: הגדרה אופציונלית לאמולטור של SQL Connect.-
host: אופציונלי. שם המארח של האמולטור. ברירת המחדל היא"localhost". -
port: אופציונלי. יציאה לאמולטור של SQL Connect. ערך ברירת המחדל הוא9399. -
postgresPort: אופציונלי. היציאה של שרת PGlite PostgreSQL המובנה. ברירת המחדל היא5432. המערכת מתעלמת מהמדיניות הזו כשמשתנה הסביבהFIREBASE_DATACONNECT_POSTGRESQL_STRINGמוגדר. -
dataDir: אופציונלי. הספרייה שבה האמולטור שומר באופן אוטומטי את נתוני מסד הנתונים המקומי של PGlite בין הפעלות. המערכת מתעלמת מהמדיניות הזו כשמשתנה הסביבהFIREBASE_DATACONNECT_POSTGRESQL_STRINGמוגדר.
-
הסבר על ההגדרות של dataconnect.yaml
הקובץ dataconnect.yaml מאחסן מידע על הגדרות המיקום של מקורות הסכימה של האפליקציה, מקורות המחברים ופרטי החיבור של מקור הנתונים. הקובץ משמש גם כמסמן של ספריית הפרויקט ל-CLI של Firebase.
המפתח schemaValidation קובע את רמת האימות של הסכימה שמתבצעת כשסכימות מועברות במהלך הפריסה. אם לא מגדירים ערך, במצב אינטראקטיבי הפקודה firebase dataconnect:sql:migrate תבקש אישור לפני החלת השינויים התואמים הנדרשים, ואז תבקש אישור לפני הפעלת השינויים המחמירים האופציונליים. למידע על ההתנהגות של --force ו---non-interactive, קראו את המאמר dataconnect:sql:migrate.
כשההגדרה מופעלת, ההתנהגות היא כזו:
- מצב
STRICT. סכימת מסד הנתונים חייבת להתאים בדיוק לסכימת האפליקציה כדי שאפשר יהיה לפרוס את סכימת האפליקציה. כל הטבלאות או העמודות שלא נמצאות בשימוש בסכימה של SQL Connect יימחקו ממסד הנתונים. - מצב
COMPATIBLE. סכימת מסד הנתונים חייבת להיות תואמת לסכימת האפליקציה לפני שאפשר לפרוס את סכימת האפליקציה. כל שינוי נוסף נחשב אופציונלי. "תואם" פירושו שהעברות הסכימה מבוססות על סכימת האפליקציה שאתם כותבים. רכיבים במסד הנתונים שלא נמצאים בשימוש בסכימה של האפליקציה לא משתנים. לכן, אחרי הפריסה, יכול להיות שה-backend יכיל סכימות, טבלאות ועמודות שלא בשימוש.
הערכים של מפתחות אחרים בקובץ הזה מוסברים בהערות שבהמשך.
# The top-level Firebase SQL Connect YAML file.
# The Firebase SQL Connect API version to target.
# Optional. Defaults to the latest version.
specVersion: string
# The ID of the Firebase SQL Connect service resource.
# Required.
serviceId: string
# The location of the Firebase SQL Connect service.
# Required.
location: string
# Required.
schema:
# Relative path to the directory for schema definitions.
# Recursively loads all .gql files in this directory.
# Required. Typically "./schema" (set by firebase init).
source: string
# Datasource connection information.
# Required.
datasource:
# Required.
postgresql:
# The name of the PostgreSQL database.
# Required.
database: string
cloudSql:
# The ID of the Cloud SQL instance resource.
# Required. For supported PostgreSQL versions, see:
# https://firebase.google.com/docs/sql-connect/manage-services-and-databases#supported-postgres-versions
instanceId: string
# Schema validation mode for schema migrations.
# Optional. If unspecified, migrations prompt for required compatible
# changes and then optional strict changes.
# Set to "STRICT" or "COMPATIBLE".
schemaValidation: string
# Required.
# Relative paths to directories for connector definitions.
# Recursively loads all .gql files in the listed directories.
# All directories specified MUST contain a connector.yaml file.
connectorDirs: [string]
קובץ ה-YAML מניח מבנה ספרייה שמוגדר כברירת מחדל (אבל ניתן להגדרה):
./(project root)
/dataconnect
dataconnect.yaml
/schema
*.gql
/example
connector.yaml
*.gql
הפניה להגדרות של connector.yaml
משתמשים בקובץ connector.yaml כדי להגדיר את מזהה המחבר ואת האפשרויות ליצירת SDK.
# The connector-level YAML file.
# Required. The connector name of the Firebase SQL Connect connector resource.
connectorId: string
# Optional. If not specified, no generated libraries (i.e. type-safe SDKs) will
# be generated.
generate:
# Optional.
javascriptSdk:
# Path to the directory that will be updated with the latest generated
# web TypeScript SDK.
# Required.
outputDir: string
# Name of the Javascript package to be created.
# Required. Example: @dataconnect/generated
package: string
# Path to your package.json directory. If specified, the new generated
# SDK will be installed in this path.
# Optional. If not provided, the package will not be auto-installed for
# you.
packageJsonDir: string
# Enable React framework bindings.
# Optional. Default to false.
react: Boolean
# Enable Angular framework bindings.
# Optional. Default to false.
angular: Boolean
# Enable and configure client-side caching for the generated SDK.
# Optional. If the `clientCache` key is not present, caching is disabled.
clientCache:
# The maximum age allowed for cached results before fetching fresh
# values. A value of "0" means that responses are cached, but the
# client SDK will always fetch fresh values; the cached values are
# only used when CACHE_ONLY is specified to executeQuery() and as the
# initial result returned from subscribe().
# Examples: "0", "30s", "1h30m"
# Optional. Default: "0"
maxAge: string
# Configures how results are to be cached.
# Optional. Only "memory" is allowed for JavaScript SDKs.
storage: string
# Optional.
dartSdk:
# Path to the directory that will be updated with the latest generated
# Flutter Dart SDK.
# Required.
outputDir: string
# Name of the Dart package to be created.
# Required. Example: "dataconnect_generated"
package: string
# Enable and configure client-side caching for the generated SDK.
# Optional. If the `clientCache` key is not present, caching is disabled.
clientCache:
# The maximum age allowed for cached results before fetching fresh
# values. A value of "0" means that responses are cached, but the
# client SDK will always fetch fresh values; the cached values are
# only used when CACHE_ONLY is specified to execute() and as the
# initial result returned from subscribe().
# Examples: "0", "30s", "1h30m"
# Optional. Default: "0"
maxAge: string
# Configures whether results are to be cached in persistent storage
# or in memory ("persistent" or "memory").
# Optional.
# Default: "persistent" for Android and iOS targets, "memory" for web.
storage: string
# Optional.
kotlinSdk:
# Path to the directory that will be updated with the latest generated
# Android SDK.
# Required.
outputDir: string
# Name of the package to be created.
# Required. Example: com.google.firebase.dataconnect.generated
package: string
# Enable and configure client-side caching for the generated SDK.
# Optional. If the `clientCache` key is not present, caching is disabled.
clientCache:
# The maximum age allowed for cached results before fetching fresh
# values. A value of "0" means that responses are cached, but the
# client SDK will always fetch fresh values; the cached values are
# only used when CACHE_ONLY is specified to execute().
# Examples: "0", "30s", "1h30m"
# Optional. Default: "0"
maxAge: string
# Configures whether results are to be cached in persistent storage
# or in memory ("persistent" or "memory").
# Optional. Default: persistent
storage: string
# Optional.
adminNodeSdk:
# Path to the directory that will be updated with the latest generated
# Node Admin SDK.
# Required.
outputDir: string
# Path to your package.json directory. If specified, the new generated
# SDK will be installed in this path.
# Optional. If not provided, the package will not be auto-installed for
# you.
packageJsonDir: string
# Name of the package to be created (for example: @dataconnect/admin-generated).
# Required.
package: string
# Optional.
swiftSdk:
# Path to the directory that will be updated with the latest generated
# iOS Swift SDK.
# Required.
outputDir: string
# Name of the Swift package to be created.
# Required. Example: "FirebaseDataConnectGenerated"
package: string
# Enable and configure client-side caching for the generated SDK.
# Optional. If the `clientCache` key is not present, caching is disabled.
clientCache:
# The maximum age allowed for cached results before fetching fresh
# values. A value of "0" means that responses are cached, but the
# client SDK will always fetch fresh values; the cached values are
# only used when CACHE_ONLY is specified to execute().
# Examples: "0", "30s", "1h30m"
# Optional. Default: "0"
maxAge: string
# Configures whether results are to be cached in persistent storage
# or in memory ("persistent" or "memory").
# Optional. Default: persistent
storage: string
משתני סביבה של אמולטור
ה-CLI של Firebase תומך במשתני סביבה שמחליפים את ההגדרות המקומיות של האמולטור של SQL Connect, שנקבעו בקובץ firebase.json.
-
FIREBASE_DATACONNECT_POSTGRESQL_STRING: אופציונלי. URI של חיבור ל-PostgreSQL באמצעות הסכימהpostgres://אוpostgresql://, למשלpostgres://USERNAME:PASSWORD@HOST:PORT?sslmode=disable.- עדיפות: כשמגדירים ערך לא ריק, האמולטור מתחבר לשרת PostgreSQL שצוין במקום להפעיל את המכונה המובנית של PGlite. המערכת מתעלמת מהמפתחות
emulators.dataconnect.postgresPortו-emulators.dataconnect.dataDirבקובץfirebase.json. - בחירת מסד נתונים: האמולטור משתמש בשדה
databaseבקטעschema.datasource.postgresqlבקובץdataconnect.yaml, ומחליף את כל שמות מסדי הנתונים ב-URI של החיבור. אם משמיטים את השדהdatabase, האמולטור משתמש במסד הנתוניםpostgres. - תמונות מצב והתנהגות במקרה של כשל: ב-SQL Connect, לדגל
--import, לדגל--export-on-exitולפקודהfirebase emulators:exportאין השפעה. אמולטורים אחרים עדיין מייבאים ומייצאים כרגיל. אם ה-URI לא תקין או שאי אפשר לגשת למסד הנתונים, הפעלת האמולטור נכשלת במקום לחזור ל-PGlite.
- עדיפות: כשמגדירים ערך לא ריק, האמולטור מתחבר לשרת PostgreSQL שצוין במקום להפעיל את המכונה המובנית של PGlite. המערכת מתעלמת מהמפתחות
במאמר התחברות למכונה מותאמת אישית של PostgreSQL מוסבר איך להגדיר את המכונה, מהן הדרישות המוקדמות לתוספים ומהם היתרונות והחסרונות של PGlite.