יצירת מסד נתונים ב-Firestore באמצעות ספריית לקוח של שרת
במדריך למתחילים הזה נסביר איך להגדיר את Firestore, להוסיף נתונים ולקרוא נתונים באמצעות ספריית הלקוח של השרת ב-C#, Go, Java, Node.js, PHP, Python או Ruby.
לפני שמתחילים
- נכנסים לחשבון Google Cloud . אנחנו ממליצים למשתמשים חדשים ב- Google Cloudליצור חשבון כדי שיוכלו להעריך את הביצועים של המוצרים שלנו בתרחישים מהעולם האמיתי. לקוחות חדשים מקבלים בחינם גם קרדיט בשווי 300$ להרצה, לבדיקה ולפריסה של עומסי העבודה.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
יצירת מסד נתונים של Firestore במצב Native
אם זה פרויקט חדש, צריך ליצור מכונה של מסד נתונים ב-Firestore.
במסך Select a database service, בוחרים באפשרות Firestore in Native mode.
בוחרים מיקום ל-Firestore.
אם אתם לא יכולים לבחור מיקום, סימן שכבר הגדרתם את המיקום של משאבי ברירת המחדל Google Cloud בפרויקט. חלק מהמשאבים בפרויקט (כמו המכונה שמוגדרת כברירת מחדל ב-Firestore) חולקים תלות משותפת במיקום, ואפשר להגדיר את המיקום שלהם במהלך יצירת הפרויקט או במהלך הגדרת שירות אחר שחולק את התלות הזו במיקום.
לוחצים על Create Database.
כשיוצרים פרויקט ב-Firestore, ממשק ה-API מופעל גם במנהל ה-API של Cloud.
מגדירים אימות
כדי להריץ את ספריית הלקוח, קודם צריך להגדיר אימות על ידי יצירת חשבון שירות והגדרה של משתנה סביבה.
הוספת ספריית הלקוח של השרת לאפליקציה
מוסיפים לאפליקציה את יחסי התלות וספריות הלקוח הנדרשות.
Java
מוסיפים את ספריית Firestore Java לאפליקציה:
-
שימוש ב-Maven:
<dependencyManagement> <dependencies> <dependency> <groupId>com.google.cloud</groupId> <artifactId>libraries-bom</artifactId> <version>26.65.0</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>com.google.cloud</groupId> <artifactId>google-cloud-firestore</artifactId> </dependency>
- אם אתם משתמשים ב-Gradle או מגדירים בלי BOM, קראו את הקובץ README של לקוח Firestore ל-Java.
-
שימוש בסביבת פיתוח משולבת (IDE):
אם משתמשים ב-VS Code, ב-IntelliJ או ב-Eclipse, אפשר להוסיף את ספריות הלקוח לפרויקט באמצעות יישומי הפלאגין הבאים של IDE:
באמצעות יישומי הפלאגין תוכלו להשתמש בפונקציות נוספות, כמו ניהול מפתחות לחשבונות שירות. לפרטים נוספים, קראו את מאמרי העזרה של כל אחד מיישומי הפלאגין.
Python
מוסיפים את ספריית Firestore Python לאפליקציה:
pip install --upgrade google-cloud-firestore
Node.js
מוסיפים לאפליקציה את ספריית Firestore Node.js:
npm install --save @google-cloud/firestore
המשך
מתקינים את ספריית Firestore Go:
go get cloud.google.com/go/firestore
מוסיפים את ספריית Firestore Go לאפליקציה:
import "cloud.google.com/go/firestore"
PHP
- מתקינים ומפעילים את תוסף gRPC ל-PHP, שנדרש כדי להשתמש בספריית הלקוח.
-
מוסיפים את ספריית Firestore PHP לאפליקציה:
composer require google/cloud-firestore
C#
-
מוסיפים את ספריית Firestore C# לאפליקציה בקובץ
.csproj:<ItemGroup> <PackageReference Include="Google.Cloud.Firestore" Version="1.1.0-beta01" /> </ItemGroup>
-
מוסיפים לקובץ
Program.csאת הנתונים הבאים:using Google.Cloud.Firestore;
Ruby
-
מוסיפים את ספריית Firestore Ruby לאפליקציה ב-
Gemfile:gem "google-cloud-firestore"
-
מתקינים את יחסי התלות מהקובץ
Gemfileבאמצעות:bundle install
הפעלה של Firestore במצב Native
מאתחלים מכונה של Firestore:
Java
import com.google.cloud.firestore.Firestore; import com.google.cloud.firestore.FirestoreOptions;
FirestoreOptions firestoreOptions = FirestoreOptions.getDefaultInstance().toBuilder() .setProjectId(projectId) .setCredentials(GoogleCredentials.getApplicationDefault()) .build(); Firestore db = firestoreOptions.getService();
Python
from google.cloud import firestore # The `project` parameter is optional and represents which project the client # will act on behalf of. If not supplied, the client falls back to the default # project inferred from the environment. db = firestore.Client(project="my-project-id")
Python
(Async)
from google.cloud import firestore # The `project` parameter is optional and represents which project the client # will act on behalf of. If not supplied, the client falls back to the default # project inferred from the environment. db = firestore.AsyncClient(project="my-project-id")
Node.js
const Firestore = require('@google-cloud/firestore'); const db = new Firestore({ projectId: 'YOUR_PROJECT_ID', keyFilename: '/path/to/keyfile.json', });
המשך
import ( "context" "flag" "fmt" "log" "google.golang.org/api/iterator" "cloud.google.com/go/firestore" ) func createClient(ctx context.Context) *firestore.Client { // Sets your Google Cloud Platform project ID. projectID := "YOUR_PROJECT_ID" client, err := firestore.NewClient(ctx, projectID) if err != nil { log.Fatalf("Failed to create client: %v", err) } // Close client when done with // defer client.Close() return client }
PHP
use Google\Cloud\Firestore\FirestoreClient; /** * Initialize Cloud Firestore with default project ID. */ function setup_client_create(string $projectId = null) { // Create the Cloud Firestore client if (empty($projectId)) { // The `projectId` parameter is optional and represents which project the // client will act on behalf of. If not supplied, the client falls back to // the default project inferred from the environment. $db = new FirestoreClient(); printf('Created Cloud Firestore client with default project ID.' . PHP_EOL); } else { $db = new FirestoreClient([ 'projectId' => $projectId, ]); printf('Created Cloud Firestore client with project ID: %s' . PHP_EOL, $projectId); } }
C#
Ruby
require "google/cloud/firestore" # The `project_id` parameter is optional and represents which project the # client will act on behalf of. If not supplied, the client falls back to the # default project inferred from the environment. firestore = Google::Cloud::Firestore.new project_id: project_id puts "Created Cloud Firestore client with given project ID."
הוספת נתונים
ב-Firestore, הנתונים נשמרים במסמכים, שמאוחסנים באוספים. בפעם הראשונה שמוסיפים נתונים למסמך, Firestore יוצר אוספים ומסמכים באופן משתמע. אין צורך ליצור אוספים או מסמכים באופן מפורש.
יוצרים אוסף חדש ומסמך באמצעות הקוד לדוגמה הבא.
Java
כדי לבצע אימות ב-Firestore, אתם צריכים להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.
Python
כדי לבצע אימות ב-Firestore, אתם צריכים להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.
Python
(Async)
Node.js
כדי לבצע אימות ב-Firestore, אתם צריכים להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.
Go
כדי לבצע אימות ב-Firestore, אתם צריכים להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.
PHP
כדי לבצע אימות ב-Firestore, אתם צריכים להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.
C#
כדי לבצע אימות ב-Firestore, אתם צריכים להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.
Ruby
כדי לבצע אימות ב-Firestore, אתם צריכים להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.
עכשיו צריך להוסיף עוד מסמך לאוסף users. שימו לב שהמסמך הזה כולל צמד מפתח/ערך (שם אמצעי) שלא מופיע במסמך הראשון. מסמכים באוסף יכולים להכיל קבוצות שונות של מידע.
Java
כדי לבצע אימות ב-Firestore, אתם צריכים להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.
Python
כדי לבצע אימות ב-Firestore, אתם צריכים להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.
Python
(Async)
Node.js
כדי לבצע אימות ב-Firestore, אתם צריכים להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.
Go
כדי לבצע אימות ב-Firestore, אתם צריכים להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.
PHP
כדי לבצע אימות ב-Firestore, אתם צריכים להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.
C#
כדי לבצע אימות ב-Firestore, אתם צריכים להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.
Ruby
כדי לבצע אימות ב-Firestore, אתם צריכים להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.
קריאת נתונים
כדי לוודא במהירות שהוספתם נתונים ל-Firestore, השתמשו בכלי להצגת נתונים במסוף Firebase.
אפשר גם להשתמש ב-method get כדי לאחזר את כל האוסף.
Java
כדי לבצע אימות ב-Firestore, אתם צריכים להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.
Python
כדי לבצע אימות ב-Firestore, אתם צריכים להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.
Python
(Async)
Node.js
כדי לבצע אימות ב-Firestore, אתם צריכים להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.
Go
כדי לבצע אימות ב-Firestore, אתם צריכים להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.
PHP
כדי לבצע אימות ב-Firestore, אתם צריכים להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.
C#
כדי לבצע אימות ב-Firestore, אתם צריכים להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.
Ruby
כדי לבצע אימות ב-Firestore, אתם צריכים להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.
השלבים הבאים
כדי להעמיק את הידע שלכם, אתם יכולים לקרוא על הנושאים הבאים:
- מודל נתונים – מידע נוסף על מבנה הנתונים ב-Firestore, כולל נתונים היררכיים ואוספי משנה.
- הוספת נתונים – מידע נוסף על יצירה ועדכון של נתונים ב-Firestore.
- קבלת נתונים – מידע נוסף על אחזור נתונים.
- ביצוע שאילתות פשוטות ומורכבות – איך מריצים שאילתות פשוטות ומורכבות.
- סידור והגבלת שאילתות – איך לסדר ולהגביל את הנתונים שמוחזרים על ידי השאילתות.