इस क्विकस्टार्ट में, Cloud Firestore को सेट अप करने और डेटा जोड़ने का तरीका बताया गया है. इसके बाद, Firebase कंसोल में जोड़े गए डेटा को क्वेरी करने के लिए, कोर ऑपरेशन या पाइपलाइन ऑपरेशन का इस्तेमाल करने का तरीका बताया गया है.
Cloud Firestore डेटाबेस बनाना
- अगर आपने पहले से Firebase प्रोजेक्ट नहीं बनाया है, तो उसे बनाएं: Firebase कंसोल में, प्रोजेक्ट जोड़ें पर क्लिक करें. इसके बाद, स्क्रीन पर दिए गए निर्देशों का पालन करके, Firebase प्रोजेक्ट बनाएं या मौजूदा Google Cloud प्रोजेक्ट में Firebase सेवाएं जोड़ें.
Firebase कंसोल में अपना प्रोजेक्ट खोलें. बाएं पैनल में, बनाएं को बड़ा करें. इसके बाद, Firestore डेटाबेस चुनें.
डेटाबेस बनाएं पर क्लिक करें.
डेटाबेस मोड के लिए, Enterprise चुनें.
ऑपरेशन मोड के लिए, नेटिव मोड में Firestore चुनें. यह मोड, कोर और पाइपलाइन ऑपरेशन के साथ काम करता है.
अपने डेटाबेस के लिए कोई जगह चुनें.
अपने Cloud Firestore Security Rules के लिए, कोई शुरुआती मोड चुनें:
- टेस्ट मोड
मोबाइल और वेब क्लाइंट लाइब्रेरी का इस्तेमाल शुरू करने के लिए यह सही है. हालांकि, इससे कोई भी व्यक्ति आपके डेटा को पढ़ और बदल सकता है. जांच करने के बाद, अपने डेटा को सुरक्षित रखें सेक्शन को ज़रूर देखें.
वेब, Apple प्लैटफ़ॉर्म या Android SDK टूल का इस्तेमाल शुरू करने के लिए, टेस्ट मोड चुनें.
- प्रोडक्शन मोड
यह कुकी, मोबाइल और वेब क्लाइंट से सभी तरह के पढ़ने और लिखने के अनुरोधों को अस्वीकार करती है. पुष्टि किए गए आपके ऐप्लिकेशन सर्वर (Python) अब भी आपके डेटाबेस को ऐक्सेस कर सकते हैं.
Cloud Firestore Security Rules का आपका शुरुआती सेट, आपके डिफ़ॉल्ट Cloud Firestore डेटाबेस पर लागू होगा. अगर आपने अपने प्रोजेक्ट के लिए एक से ज़्यादा डेटाबेस बनाए हैं, तो हर डेटाबेस के लिए Cloud Firestore Security Rules को डिप्लॉय किया जा सकता है.
बनाएं पर क्लिक करें.
Cloud Firestore को चालू करने पर, Cloud API Manager में भी एपीआई चालू हो जाता है.
डेवलपमेंट एनवायरमेंट सेट अप करना
अपने ऐप्लिकेशन में ज़रूरी डिपेंडेंसी और क्लाइंट लाइब्रेरी जोड़ें.
Web
- अपने वेब ऐप्लिकेशन में Firebase जोड़ने के लिए, निर्देशों का पालन करें.
-
Private Preview के लिए Cloud Firestore एसडीके, npm पैकेज के तौर पर उपलब्ध है.
अपने npm प्रोजेक्ट में Firestore SDK टूल इंस्टॉल करने के लिए, यहां दिए गए निर्देश का इस्तेमाल करें.
npm install --save firebase@eap-firestore-pipelines
iOS+
- अपने iOS ऐप्लिकेशन में Firebase जोड़ने के लिए, निर्देशों का पालन करें.
- GitHub से Firebase SDK टूल का क्लोन बनाएं और पाइपलाइन ब्रांच देखें. उस जगह का नाम नोट करें जहां आपने इसे क्लोन किया है, क्योंकि आपको अगले चरण में इसकी ज़रूरत पड़ेगी:
git clone https://github.com/firebase/firebase-ios-sdk.git # or git clone git@github.com:firebase/firebase-ios-sdk.git cd firebase-ios-sdk # check out pipeline feature branch git fetch origin feat/pipeline/private-preview git checkout feat/pipeline/private-preview
- इसके बाद, डायरेक्ट्री (firebase-ios-sdk) को अपने Xcode प्रोजेक्ट में लोकल डिपेंडेंसी के तौर पर जोड़ें:
- फ़ाइल मेन्यू में जाकर, पैकेज डिपेंडेंसी जोड़ें चुनें.
- स्थानीय जोड़ें… बटन पर क्लिक करें. इसके बाद, firebase-ios-sdk डायरेक्ट्री ढूंढें. इसमें वह फ़ीचर ब्रांच होनी चाहिए जिसे आपने चेक आउट किया है.
Android
- अपने Android ऐप्लिकेशन में Firebase जोड़ने के लिए, निर्देशों का पालन करें.
- GitHub से Firebase SDK का क्लोन बनाएं, पाइपलाइन ब्रांच देखें, और
इसे लोकल मेवन पर पब्लिश करें:
# Prerequisites before you start: # Install Java 17 # Setup Android Development environments (having proper ANDROID_HOME, etc) git clone https://github.com/firebase/firebase-android-sdk.git # or git clone git@github.com:firebase/firebase-android-sdk.git cd firebase-android-sdk # check out pipeline feature branch git fetch origin feat/pipeline/private-preview git checkout feat/pipeline/private-preview # publish firebase SDK (without crashlytics) to maven local ./gradlew publishToMavenLocal -x :firebase-crashlytics:publishToMavenLocal -x :firebase-crashlytics-ndk:publishToMavenLocal # Optionally, if you want crashlytics built and published to mavel local # Make sure you have Android NDK 21 installed git submodule update --init --recursive ./gradlew publishToMavenLocal
- प्रोजेक्ट लेवल की settings.gradle.kts फ़ाइल में
mavenLocalजोड़ें:dependencyResolutionManagement { repositories { mavenLocal() // Add this line .. } }
- इसके बाद, एसडीके टूल का स्थानीय वर्शन जोड़ें:
... // Firestore 99.0.0-pipeline.preview.1 has pipelines implementation("com.google.firebase:firebase-firestore:99.0.0-pipeline.preview.1") // Firebase Authentication implementation("com.google.firebase:firebase-auth") ...
Python
- Firestore Python SDK को क्लोन करें और पाइपलाइन की झलक वाली ब्रांच देखें:
git clone https://github.com/googleapis/python-firestore.git # or git clone git@github.com:googleapis/python-firestore.git cd python-firestore # check out pipeline preview branch git fetch origin pipeline-preview git checkout pipeline-preview
- लोकल python-firestore सर्वर SDK टूल इंस्टॉल करें:
python -m pip install -e .
- Firebase Python Admin SDK को सामान्य तरीके से इंस्टॉल करें:
pip install --user firebase-admin
Cloud Firestore शुरू करें
Cloud Firestore का कोई इंस्टेंस शुरू करें:
Web
import { initializeApp } from "firebase/app"; import { getFirestore } from "firebase/firestore"; // TODO: Replace the following with your app's Firebase project configuration // See: https://support.google.com/firebase/answer/7015592 const firebaseConfig = { FIREBASE_CONFIGURATION }; // Initialize Firebase const app = initializeApp(firebaseConfig); // When initializing Firestore, remember to use the name of the database you created earlier: const db = initializeFirestore(app, {}, 'your-new-enterprise-database');
FIREBASE_CONFIGURATION को अपने वेब ऐप्लिकेशन के firebaseConfig से बदलें.
डिवाइस का कनेक्शन बंद होने पर भी डेटा को सेव रखने के लिए, ऑफ़लाइन डेटा चालू करें दस्तावेज़ देखें.
Swift
import FirebaseCore import FirebaseFirestore FirebaseApp.configure() // When initializing Firestore, remember to use the name of the database you created earlier: let db = Firestore.firestore(database: "your-new-enterprise-database")
Kotlin
// Access a Cloud Firestore instance from your Activity // When initializing Firestore, remember to use the name of the database you created earlier: val firestore = FirebaseFirestore.getInstance("your-new-enterprise-database")
Java
// Access a Cloud Firestore instance from your Activity // When initializing Firestore, remember to use the name of the database you created earlier: FirebaseFirestore firestore = FirebaseFirestore.getInstance("your-new-enterprise-database");
Python
Admin SDK का इस्तेमाल करके, अपने एंटरप्राइज़ डेटाबेस से पुष्टि करें:
import firebase_admin from firebase_admin import firestore def main(): default_app = firebase_admin.initialize_app() client = firestore.client(default_app, "your-new-enterprise-database") query = client.pipeline().database().limit(5) for result in query.execute(): print(result.data()) if __name__ == "__main__": main()
कोर ऑपरेशंस का इस्तेमाल करके डेटा जोड़ना
डेटा से क्वेरी करने के लिए, कोर ऑपरेशंस और पाइपलाइन ऑपरेशंस के बारे में जानने के लिए, कोर ऑपरेशंस का इस्तेमाल करके अपने डेटाबेस में डेटा जोड़ें.
Cloud Firestore Documents में डेटा सेव करता है. ये Documents, Collections में सेव किए जाते हैं. Cloud Firestore, दस्तावेज़ में पहली बार डेटा जोड़ने पर, कलेक्शन और दस्तावेज़ अपने-आप बना देता है. आपको कलेक्शन या दस्तावेज़ बनाने की ज़रूरत नहीं है.
यहां दिए गए उदाहरण कोड का इस्तेमाल करके, नया कलेक्शन और दस्तावेज़ बनाएं.
Web
import { collection, addDoc } from "firebase/firestore"; try { const docRef = await addDoc(collection(db, "users"), { first: "Ada", last: "Lovelace", born: 1815 }); console.log("Document written with ID: ", docRef.id); } catch (e) { console.error("Error adding document: ", e); }
Web
db.collection("users").add({ first: "Ada", last: "Lovelace", born: 1815 }) .then((docRef) => { console.log("Document written with ID: ", docRef.id); }) .catch((error) => { console.error("Error adding document: ", error); });
Swift
// Add a new document with a generated ID do { let ref = try await db.collection("users").addDocument(data: [ "first": "Ada", "last": "Lovelace", "born": 1815 ]) print("Document added with ID: \(ref.documentID)") } catch { print("Error adding document: \(error)") }
Kotlin
// Create a new user with a first and last name val user = hashMapOf( "first" to "Ada", "last" to "Lovelace", "born" to 1815, ) // Add a new document with a generated ID db.collection("users") .add(user) .addOnSuccessListener { documentReference -> Log.d(TAG, "DocumentSnapshot added with ID: ${documentReference.id}") } .addOnFailureListener { e -> Log.w(TAG, "Error adding document", e) }
Java
// Create a new user with a first and last name Map<String, Object> user = new HashMap<>(); user.put("first", "Ada"); user.put("last", "Lovelace"); user.put("born", 1815); // Add a new document with a generated ID db.collection("users") .add(user) .addOnSuccessListener(new OnSuccessListener<DocumentReference>() { @Override public void onSuccess(DocumentReference documentReference) { Log.d(TAG, "DocumentSnapshot added with ID: " + documentReference.getId()); } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { Log.w(TAG, "Error adding document", e); } });
Python
अब users कलेक्शन में कोई दूसरा दस्तावेज़ जोड़ें. ध्यान दें कि इस दस्तावेज़ में एक की-वैल्यू पेयर (बीच का नाम) शामिल है, जो पहले दस्तावेज़ में नहीं दिखता. किसी कलेक्शन में मौजूद दस्तावेज़ों में, अलग-अलग तरह की जानकारी हो सकती है.
Web
// Add a second document with a generated ID. import { addDoc, collection } from "firebase/firestore"; try { const docRef = await addDoc(collection(db, "users"), { first: "Alan", middle: "Mathison", last: "Turing", born: 1912 }); console.log("Document written with ID: ", docRef.id); } catch (e) { console.error("Error adding document: ", e); }
Web
// Add a second document with a generated ID. db.collection("users").add({ first: "Alan", middle: "Mathison", last: "Turing", born: 1912 }) .then((docRef) => { console.log("Document written with ID: ", docRef.id); }) .catch((error) => { console.error("Error adding document: ", error); });
Swift
// Add a second document with a generated ID. do { let ref = try await db.collection("users").addDocument(data: [ "first": "Alan", "middle": "Mathison", "last": "Turing", "born": 1912 ]) print("Document added with ID: \(ref.documentID)") } catch { print("Error adding document: \(error)") }
Kotlin
// Create a new user with a first, middle, and last name val user = hashMapOf( "first" to "Alan", "middle" to "Mathison", "last" to "Turing", "born" to 1912, ) // Add a new document with a generated ID db.collection("users") .add(user) .addOnSuccessListener { documentReference -> Log.d(TAG, "DocumentSnapshot added with ID: ${documentReference.id}") } .addOnFailureListener { e -> Log.w(TAG, "Error adding document", e) }
Java
// Create a new user with a first, middle, and last name Map<String, Object> user = new HashMap<>(); user.put("first", "Alan"); user.put("middle", "Mathison"); user.put("last", "Turing"); user.put("born", 1912); // Add a new document with a generated ID db.collection("users") .add(user) .addOnSuccessListener(new OnSuccessListener<DocumentReference>() { @Override public void onSuccess(DocumentReference documentReference) { Log.d(TAG, "DocumentSnapshot added with ID: " + documentReference.getId()); } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { Log.w(TAG, "Error adding document", e); } });
Python
कोर ऑपरेशंस का इस्तेमाल करके डेटा पढ़ना
Firebase कंसोल में डेटा व्यूअर का इस्तेमाल करके, यह तुरंत पुष्टि करें कि आपने Cloud Firestore में डेटा जोड़ा है.
पूरे कलेक्शन को वापस पाने के लिए, "get" तरीके का भी इस्तेमाल किया जा सकता है.
Web
import { collection, getDocs } from "firebase/firestore"; const querySnapshot = await getDocs(collection(db, "users")); querySnapshot.forEach((doc) => { console.log(`${doc.id} => ${doc.data()}`); });
Web
db.collection("users").get().then((querySnapshot) => { querySnapshot.forEach((doc) => { console.log(`${doc.id} => ${doc.data()}`); }); });
Swift
do { let snapshot = try await db.collection("users").getDocuments() for document in snapshot.documents { print("\(document.documentID) => \(document.data())") } } catch { print("Error getting documents: \(error)") }
Kotlin
db.collection("users") .get() .addOnSuccessListener { result -> for (document in result) { Log.d(TAG, "${document.id} => ${document.data}") } } .addOnFailureListener { exception -> Log.w(TAG, "Error getting documents.", exception) }
Java
db.collection("users") .get() .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() { @Override public void onComplete(@NonNull Task<QuerySnapshot> task) { if (task.isSuccessful()) { for (QueryDocumentSnapshot document : task.getResult()) { Log.d(TAG, document.getId() + " => " + document.getData()); } } else { Log.w(TAG, "Error getting documents.", task.getException()); } } });
Python
users_ref = db.collection("users") docs = users_ref.stream() for doc in docs: print(f"{doc.id} => {doc.to_dict()}")
पाइपलाइन ऑपरेशन का इस्तेमाल करके डेटा पढ़ना
अब पाइपलाइन क्वेरी के अनुभव की तुलना, कोर क्वेरी के अनुभव से की जा सकती है.
Web
const readDataPipeline = db.pipeline() .collection("users"); // Execute the pipeline and handle the result try { const querySnapshot = await execute(readDataPipeline); querySnapshot.results.forEach((result) => { console.log(`${result.id} => ${result.data()}`); }); } catch (error) { console.error("Error getting documents: ", error); }
Swift
do { // Initialize a Firestore Pipeline instance and specify the "users" collection as the // input stage. let snapshot = try await db.pipeline() .collection("users") .execute() // Execute the pipeline to retrieve documents. // Iterate through the documents in the pipeline results, similar to a regular query // snapshot. for result in snapshot.results { print("\(result.id ?? "no ID") => \(result.data)") } } catch { print("Error getting documents with pipeline: \(error)") }
Kotlin
val readDataPipeline = db.pipeline() .collection("users") // Execute the pipeline and handle the result readDataPipeline.execute() .addOnSuccessListener { result -> for (document in result) { println("${document.getId()} => ${document.getData()}") } } .addOnFailureListener { exception -> println("Error getting documents: $exception") }
Java
Pipeline readDataPipeline = db.pipeline() .collection("users"); readDataPipeline.execute() .addOnSuccessListener(new OnSuccessListener<Pipeline.Snapshot>() { @Override public void onSuccess(Pipeline.Snapshot snapshot) { for (PipelineResult result : snapshot.getResults()) { System.out.println(result.getId() + " => " + result.getData()); } } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { System.out.println("Error getting documents: " + e); } });
Python
pipeline = client.pipeline().collection("users") for result in pipeline.execute(): print(f"{result.id} => {result.data()}")
अपने डेटा को सुरक्षित रखना
अगर वेब, Android या Apple प्लैटफ़ॉर्म के SDK टूल का इस्तेमाल किया जा रहा है, तो Firebase Authentication और Cloud Firestore Security Rules का इस्तेमाल करके, Cloud Firestore में अपने डेटा को सुरक्षित रखें.
शुरू करने के लिए, यहां कुछ बुनियादी नियम सेट दिए गए हैं. कंसोल के नियम टैब में जाकर, सुरक्षा के नियमों में बदलाव किया जा सकता है.
पुष्टि करना ज़रूरी है
// Allow read/write access to a document keyed by the user's UID
service cloud.firestore {
match /databases/{database}/documents {
match /users/{uid} {
allow read, write: if request.auth != null && request.auth.uid == uid;
}
}
}
प्रोडक्शन मोड
// Deny read/write access to all users under any conditions
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if false;
}
}
}
अपने वेब, Android या iOS ऐप्लिकेशन को प्रोडक्शन में डिप्लॉय करने से पहले, यह पक्का करें कि सिर्फ़ आपके ऐप्लिकेशन क्लाइंट ही आपके Cloud Firestore डेटा को ऐक्सेस कर सकें. App Check से जुड़ा दस्तावेज़ देखें.
अगर सर्वर एसडीके टूल में से किसी एक का इस्तेमाल किया जा रहा है, तो Cloud Firestore में अपने डेटा को सुरक्षित रखने के लिए, पहचान और ऐक्सेस मैनेजमेंट (आईएएम) का इस्तेमाल करें.
अगले चरण
इन विषयों के बारे में ज़्यादा जानकारी पाकर, कोर और पाइपलाइन ऑपरेशंस के बारे में अपनी जानकारी बढ़ाएं:
- पक्का करें कि आपको कोर और पाइपलाइन ऑपरेशंस के बीच के अंतर के बारे में पता हो
- मुख्य कार्रवाइयों की मदद से क्वेरी करने के बारे में ज़्यादा जानें
- पाइपलाइन ऑपरेशंस की मदद से क्वेरी करने के बारे में ज़्यादा जानें.