Cloud Firestore দিয়ে শুরু করুন

এই কুইকস্টার্ট আপনাকে দেখায় কিভাবে Cloud Firestore সেট আপ করতে হয়, ডেটা যোগ করতে হয়, তারপর Firebase কনসোলে আপনার যোগ করা ডেটা অনুসন্ধান করতে কোর অপারেশন বা পাইপলাইন অপারেশন ব্যবহার করতে হয়।

একটি Cloud Firestore ডাটাবেস তৈরি করুন

  1. যদি আপনি ইতিমধ্যেই না করে থাকেন, তাহলে একটি Firebase প্রকল্প তৈরি করুন: Firebase কনসোলে , প্রকল্প যোগ করুন ক্লিক করুন, তারপর একটি Firebase প্রকল্প তৈরি করতে বা বিদ্যমান Google Cloud প্রকল্পে Firebase পরিষেবা যোগ করতে অন-স্ক্রীন নির্দেশাবলী অনুসরণ করুন।
  1. Firebase কনসোলে আপনার প্রজেক্টটি খুলুন। বাম প্যানেলে, Build প্রসারিত করুন এবং তারপর Firestore database নির্বাচন করুন।

  2. ডাটাবেস তৈরি করুন ক্লিক করুন।

  3. ডাটাবেস মোডের জন্য এন্টারপ্রাইজ নির্বাচন করুন।

  4. অপারেশন মোডের জন্য নেটিভ মোডে ফায়ারস্টোর নির্বাচন করুন, যা কোর এবং পাইপলাইন অপারেশন সমর্থন করে।

  5. আপনার ডাটাবেসের জন্য একটি অবস্থান নির্বাচন করুন।

  6. আপনার Cloud Firestore Security Rules জন্য একটি শুরুর মোড নির্বাচন করুন:

    পরীক্ষা মোড

    মোবাইল এবং ওয়েব ক্লায়েন্ট লাইব্রেরি দিয়ে শুরু করার জন্য ভালো, তবে যে কেউ আপনার ডেটা পড়তে এবং ওভাররাইট করতে পারে। পরীক্ষার পরে, আপনার ডেটা সুরক্ষিত করুন বিভাগটি পর্যালোচনা করতে ভুলবেন না।

    ওয়েব, অ্যাপল প্ল্যাটফর্ম, অথবা অ্যান্ড্রয়েড SDK ব্যবহার শুরু করতে, পরীক্ষা মোড নির্বাচন করুন।

    উৎপাদন মোড

    মোবাইল এবং ওয়েব ক্লায়েন্টদের থেকে সমস্ত পঠন এবং লেখার অনুমতি অস্বীকৃতি জানায়। আপনার প্রমাণীকৃত অ্যাপ্লিকেশন সার্ভার (পাইথন) এখনও আপনার ডাটাবেস অ্যাক্সেস করতে পারে।

    আপনার প্রাথমিক Cloud Firestore Security Rules আপনার ডিফল্ট Cloud Firestore ডাটাবেসে প্রযোজ্য হবে। আপনি যদি আপনার প্রকল্পের জন্য একাধিক ডাটাবেস তৈরি করেন, তাহলে আপনি প্রতিটি ডাটাবেসের জন্য Cloud Firestore Security Rules স্থাপন করতে পারেন।

  7. তৈরি করুন ক্লিক করুন।

যখন আপনি Cloud Firestore সক্ষম করেন, তখন এটি ক্লাউড এপিআই ম্যানেজারে এপিআইও সক্ষম করে।

আপনার উন্নয়ন পরিবেশ সেট আপ করুন

আপনার অ্যাপে প্রয়োজনীয় নির্ভরতা এবং ক্লায়েন্ট লাইব্রেরি যোগ করুন।

Web

  1. আপনার ওয়েব অ্যাপে Firebase যোগ করার জন্য নির্দেশাবলী অনুসরণ করুন।
  2. প্রাইভেট প্রিভিউয়ের জন্য Cloud Firestore SDK একটি npm প্যাকেজ হিসেবে উপলব্ধ।

    আপনার npm প্রজেক্টে Firestore SDK ইনস্টল করতে নিম্নলিখিত কমান্ডটি ব্যবহার করুন।

    npm install --save firebase@eap-firestore-pipelines
আইওএস+
  1. আপনার iOS অ্যাপে Firebase যোগ করার জন্য নির্দেশাবলী অনুসরণ করুন।
  2. 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
  3. তারপর আপনার Xcode প্রকল্পে স্থানীয় নির্ভরতা হিসেবে ডিরেক্টরি (firebase-ios-sdk) যোগ করুন:
    1. ফাইল মেনু থেকে, প্যাকেজ নির্ভরতা যোগ করুন নির্বাচন করুন।
    2. Add Local… বোতামে ক্লিক করুন, তারপর আপনি যে ফিচার ব্রাঞ্চটি চেক আউট করেছেন তার সাথে firebase-ios-sdk ডিরেক্টরিটি সনাক্ত করুন।
অ্যান্ড্রয়েড
  1. আপনার অ্যান্ড্রয়েড অ্যাপে Firebase যোগ করার জন্য নির্দেশাবলী অনুসরণ করুন।
  2. 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
  3. প্রকল্প স্তরে mavenLocal যোগ করুন settings.gradle.kts :
    dependencyResolutionManagement {
      repositories {
        mavenLocal() // Add this line
        ..
      }
    }
  4. তারপর, SDK এর স্থানীয় সংস্করণ যোগ করুন:
    ...
    // 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")
    ...
পাইথন
  1. ফায়ারস্টোর পাইথন 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
  2. স্থানীয় পাইথন-ফায়ারস্টোর সার্ভার SDK ইনস্টল করুন:
    python -m pip install -e .
  3. যথারীতি 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');

আপনার ওয়েব অ্যাপের firebaseConfig দিয়ে FIREBASE_CONFIGURATION প্রতিস্থাপন করুন।

ডিভাইসটি সংযোগ বিচ্ছিন্ন হয়ে গেলেও ডেটা ধরে রাখতে, অফলাইন ডেটা সক্ষম করুন ডকুমেন্টেশনটি দেখুন।

সুইফট
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");
পাইথন

অ্যাডমিন 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 ডকুমেন্টসে ডেটা সঞ্চয় করে, যা কালেকশনে সংরক্ষিত থাকে। 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);
});
সুইফট
দ্রষ্টব্য: এই পণ্যটি watchOS এবং অ্যাপ ক্লিপ টার্গেটগুলিতে উপলব্ধ নয়।
// 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);
            }
        });
পাইথন
doc_ref = db.collection("users").document("alovelace")
doc_ref.set({"first": "Ada", "last": "Lovelace", "born": 1815})

এখন 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);
});
সুইফট
দ্রষ্টব্য: এই পণ্যটি watchOS এবং অ্যাপ ক্লিপ টার্গেটগুলিতে উপলব্ধ নয়।
// 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);
            }
        });
পাইথন
doc_ref = db.collection("users").document("aturing")
doc_ref.set({"first": "Alan", "middle": "Mathison", "last": "Turing", "born": 1912})

কোর অপারেশন ব্যবহার করে ডেটা পড়ুন

আপনি 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()}`);
    });
});
সুইফট
দ্রষ্টব্য: এই পণ্যটি watchOS এবং অ্যাপ ক্লিপ টার্গেটগুলিতে উপলব্ধ নয়।
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());
                }
            }
        });
পাইথন
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);
}
সুইফট
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);
    }
});
পাইথন
pipeline = client.pipeline().collection("users")
for result in pipeline.execute():
    print(f"{result.id} => {result.data()}")

আপনার ডেটা সুরক্ষিত করুন

আপনি যদি ওয়েব, অ্যান্ড্রয়েড, অথবা অ্যাপল প্ল্যাটফর্ম SDK ব্যবহার করেন, তাহলে Cloud Firestore আপনার ডেটা সুরক্ষিত করতে ফায়ারবেস প্রমাণীকরণ এবং Cloud Firestore Security Rules ব্যবহার করুন।

শুরু করার জন্য এখানে কিছু মৌলিক নিয়ম সেট দেওয়া হল যা আপনি ব্যবহার করতে পারেন। আপনি কনসোলের নিয়ম ট্যাবে আপনার নিরাপত্তা নিয়ম পরিবর্তন করতে পারেন।

প্রমাণীকরণ প্রয়োজন

// 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;
    }
  }
}

আপনার ওয়েব, অ্যান্ড্রয়েড, অথবা iOS অ্যাপটি প্রোডাকশনে স্থাপন করার আগে, শুধুমাত্র আপনার অ্যাপ ক্লায়েন্টরা যাতে আপনার Cloud Firestore ডেটা অ্যাক্সেস করতে পারে তা নিশ্চিত করার জন্য পদক্ষেপ নিন। অ্যাপ চেক ডকুমেন্টেশন দেখুন।

আপনি যদি সার্ভার SDK ব্যবহার করেন, তাহলে Cloud Firestore আপনার ডেটা সুরক্ষিত করতে আইডেন্টিটি অ্যান্ড অ্যাক্সেস ম্যানেজমেন্ট (IAM) ব্যবহার করুন।

পরবর্তী পদক্ষেপ

নিম্নলিখিত বিষয়গুলি ব্যবহার করে কোর এবং পাইপলাইন অপারেশন সম্পর্কে আপনার জ্ঞান আরও গভীর করুন: