Cloud Functions की मदद से, कोड को डिप्लॉय किया जा सकता है. इससे, MongoDB के साथ काम करने वाले डेटाबेस में हुए बदलावों से ट्रिगर होने वाले इवेंट को मैनेज किया जा सकता है. इससे आपको अपने सर्वर चलाए बिना, अपने ऐप्लिकेशन में सर्वर-साइड फ़ंक्शनैलिटी जोड़ने की सुविधा मिलती है.
Cloud Functions (2nd gen)
Cloud Functions for Firebase (दूसरी जनरेशन) को Cloud Run और Eventarc की मदद से बनाया गया है. यह आपको ज़्यादा बेहतर इन्फ़्रास्ट्रक्चर, परफ़ॉर्मेंस और स्केलेबिलिटी पर बेहतर कंट्रोल, और फ़ंक्शन के रनटाइम पर ज़्यादा कंट्रोल देता है. दूसरी जनरेशन के बारे में ज़्यादा जानने के लिए, Cloud Functions for Firebase (दूसरी जनरेशन) लेख पढ़ें.
MongoDB के साथ काम करने वाले फ़ंक्शन ट्रिगर के साथ Cloud Firestore
Cloud Functions for Firebase SDK, MongoDB के साथ काम करने वाले Cloud Firestore के लिए, इवेंट ट्रिगर को एक्सपोर्ट करता है. इससे आपको MongoDB के साथ काम करने वाले Cloud Firestore के खास इवेंट से जुड़े हैंडलर बनाने में मदद मिलती है:
Node.js
| इवेंट टाइप | ट्रिगर |
|---|---|
onDocumentCreated |
यह इवेंट तब ट्रिगर होता है, जब किसी दस्तावेज़ में पहली बार कुछ लिखा जाता है. |
onDocumentUpdated |
यह तब ट्रिगर होता है, जब कोई दस्तावेज़ पहले से मौजूद हो और उसकी कोई वैल्यू बदल गई हो. |
onDocumentDeleted |
यह सूचना तब ट्रिगर होती है, जब कोई दस्तावेज़ मिटाया जाता है. |
onDocumentWritten |
यह तब ट्रिगर होता है, जब onDocumentCreated, onDocumentUpdated या onDocumentDeleted ट्रिगर होता है. |
onDocumentCreatedWithAuthContext |
onDocumentCreated में पुष्टि करने के लिए अतिरिक्त जानकारी शामिल है |
onDocumentWrittenWithAuthContext |
onDocumentWritten में पुष्टि करने के लिए अतिरिक्त जानकारी शामिल है |
onDocumentDeletedWithAuthContext |
onDocumentDeleted में पुष्टि करने के लिए अतिरिक्त जानकारी शामिल है |
onDocumentUpdatedWithAuthContext |
onDocumentUpdated में पुष्टि करने के लिए अतिरिक्त जानकारी शामिल है |
Python
| इवेंट टाइप | ट्रिगर |
|---|---|
on_document_created |
यह इवेंट तब ट्रिगर होता है, जब किसी दस्तावेज़ में पहली बार कुछ लिखा जाता है. |
on_document_updated |
यह तब ट्रिगर होता है, जब कोई दस्तावेज़ पहले से मौजूद हो और उसकी कोई वैल्यू बदल गई हो. |
on_document_deleted |
यह सूचना तब ट्रिगर होती है, जब कोई दस्तावेज़ मिटाया जाता है. |
on_document_written |
यह तब ट्रिगर होता है, जब on_document_created, on_document_updated या on_document_deleted ट्रिगर होता है. |
on_document_created_with_auth_context |
on_document_created में पुष्टि करने के लिए अतिरिक्त जानकारी शामिल है |
on_document_updated_with_auth_context |
on_document_updated में पुष्टि करने के लिए अतिरिक्त जानकारी शामिल है |
on_document_deleted_with_auth_context |
on_document_deleted में पुष्टि करने के लिए अतिरिक्त जानकारी शामिल है |
on_document_written_with_auth_context |
on_document_written में पुष्टि करने के लिए अतिरिक्त जानकारी शामिल है |
MongoDB के साथ काम करने वाले Cloud Firestore के इवेंट, सिर्फ़ दस्तावेज़ों में हुए बदलावों पर ट्रिगर होते हैं. अगर MongoDB के साथ काम करने वाले Cloud Firestore के किसी दस्तावेज़ में कोई बदलाव नहीं किया जाता है, तो अपडेट या राइट इवेंट जनरेट नहीं होता है. किसी खास फ़ील्ड में इवेंट नहीं जोड़े जा सकते.
अगर आपने अब तक Cloud Functions for Firebase के लिए कोई प्रोजेक्ट चालू नहीं किया है, तो Cloud Functions for Firebase प्रोजेक्ट को कॉन्फ़िगर और सेट अप करने के लिए, Cloud Functions for Firebase (दूसरी जनरेशन) का इस्तेमाल शुरू करना लेख पढ़ें.
MongoDB के साथ काम करने वाले ट्रिगर किए गए फ़ंक्शन की मदद से Cloud Firestore में डेटा लिखना
फ़ंक्शन ट्रिगर तय करना
MongoDB के साथ काम करने वाले Cloud Firestore ट्रिगर को तय करने के लिए, दस्तावेज़ का पाथ और इवेंट का टाइप तय करें:
Node.js
const {
onDocumentWritten,
onDocumentCreated,
onDocumentUpdated,
onDocumentDeleted,
Change,
FirestoreEvent
} = require('firebase-functions/v2/firestore');
exports.myfunction = onDocumentWritten("my-collection/{docId}", (event) => {
/* ... */
});
Python
from firebase_functions.firestore_fn import (
on_document_created,
on_document_deleted,
on_document_updated,
on_document_written,
Event,
Change,
DocumentSnapshot,
)
@on_document_created(document="users/{userId}")
def myfunction(event: Event[DocumentSnapshot]) -> None:
दस्तावेज़ के पाथ, किसी खास दस्तावेज़ या वाइल्डकार्ड पैटर्न को रेफ़रंस कर सकते हैं.
कोई एक दस्तावेज़ तय करना
अगर आपको किसी दस्तावेज़ में किसी भी बदलाव के लिए इवेंट ट्रिगर करना है, तो यहां दिए गए फ़ंक्शन का इस्तेमाल करें.
Node.js
const {
onDocumentWritten,
Change,
FirestoreEvent
} = require('firebase-functions/v2/firestore');
exports.myfunction = onDocumentWritten("users/marie", (event) => {
// Your code here
});
Python
from firebase_functions.firestore_fn import (
on_document_written,
Event,
Change,
DocumentSnapshot,
)
@on_document_written(document="users/marie")
def myfunction(event: Event[Change[DocumentSnapshot]]) -> None:
वाइल्डकार्ड का इस्तेमाल करके, दस्तावेज़ों का ग्रुप तय करना
अगर आपको दस्तावेज़ों के किसी ग्रुप, जैसे कि किसी कलेक्शन में मौजूद किसी भी दस्तावेज़ से ट्रिगर अटैच करना है, तो दस्तावेज़ के आईडी की जगह {wildcard} का इस्तेमाल करें:
Node.js
const {
onDocumentWritten,
Change,
FirestoreEvent
} = require('firebase-functions/v2/firestore');
exports.myfunction = onDocumentWritten("users/{userId}", (event) => {
// If we set `/users/marie` to {name: "Marie"} then
// event.params.userId == "marie"
// ... and ...
// event.data.after.data() == {name: "Marie"}
});
Python
from firebase_functions.firestore_fn import (
on_document_written,
Event,
Change,
DocumentSnapshot,
)
@on_document_written(document="users/{userId}")
def myfunction(event: Event[Change[DocumentSnapshot]]) -> None:
# If we set `/users/marie` to {name: "Marie"} then
event.params["userId"] == "marie" # True
# ... and ...
event.data.after.to_dict() == {"name": "Marie"} # True
इस उदाहरण में, जब users में मौजूद किसी दस्तावेज़ के किसी फ़ील्ड में बदलाव किया जाता है, तो यह userId नाम के वाइल्डकार्ड से मेल खाता है.
वाइल्डकार्ड से मैच होने वाली वैल्यू, दस्तावेज़ के पाथ से एक्सट्रैक्ट की जाती हैं और event.params में सेव की जाती हैं.
आपका ट्रिगर, किसी दस्तावेज़ की ओर हमेशा पॉइंट करना चाहिए. भले ही, आपने वाइल्डकार्ड का इस्तेमाल किया हो.
इवेंट ट्रिगर
नया दस्तावेज़ बनाए जाने पर किसी फ़ंक्शन को ट्रिगर करना
किसी फ़ंक्शन को तब ट्रिगर किया जा सकता है, जब किसी कलेक्शन में कोई नया दस्तावेज़ बनाया जाता है. यह उदाहरण फ़ंक्शन, हर बार नई उपयोगकर्ता प्रोफ़ाइल जोड़े जाने पर ट्रिगर होता है:
Node.js
const {
onDocumentCreated,
Change,
FirestoreEvent
} = require('firebase-functions/v2/firestore');
exports.createuser = onDocumentCreated("users/{userId}", (event) => {
// Get an object representing the document
// e.g. {'name': 'Marie', 'age': 66}
const snapshot = event.data;
if (!snapshot) {
console.log("No data associated with the event");
return;
}
const data = snapshot.data();
// access a particular field as you would any JS property
const name = data.name;
// perform more operations ...
});
पुष्टि करने के लिए ज़्यादा जानकारी पाने के लिए, onDocumentCreatedWithAuthContext का इस्तेमाल करें.
Python
from firebase_functions.firestore_fn import (
on_document_created,
Event,
DocumentSnapshot,
)
@on_document_created(document="users/{userId}")
def myfunction(event: Event[DocumentSnapshot]) -> None:
# Get a dictionary representing the document
# e.g. {'name': 'Marie', 'age': 66}
new_value = event.data.to_dict()
# Access a particular field as you would any dictionary
name = new_value["name"]
# Perform more operations ...
दस्तावेज़ अपडेट होने पर किसी फ़ंक्शन को ट्रिगर करना
दस्तावेज़ अपडेट होने पर, किसी फ़ंक्शन को ट्रिगर किया जा सकता है. अगर कोई उपयोगकर्ता अपनी प्रोफ़ाइल बदलता है, तो यह उदाहरण फ़ंक्शन ट्रिगर होता है:
Node.js
const {
onDocumentUpdated,
Change,
FirestoreEvent
} = require('firebase-functions/v2/firestore');
exports.updateuser = onDocumentUpdated("users/{userId}", (event) => {
// Get an object representing the document
// e.g. {'name': 'Marie', 'age': 66}
const newValue = event.data.after.data();
// access a particular field as you would any JS property
const name = newValue.name;
// perform more operations ...
});
पुष्टि करने के लिए ज़्यादा जानकारी पाने के लिए, onDocumentUpdatedWithAuthContext का इस्तेमाल करें.
Python
from firebase_functions.firestore_fn import (
on_document_updated,
Event,
Change,
DocumentSnapshot,
)
@on_document_updated(document="users/{userId}")
def myfunction(event: Event[Change[DocumentSnapshot]]) -> None:
# Get a dictionary representing the document
# e.g. {'name': 'Marie', 'age': 66}
new_value = event.data.after.to_dict()
# Access a particular field as you would any dictionary
name = new_value["name"]
# Perform more operations ...
दस्तावेज़ मिटाने पर किसी फ़ंक्शन को ट्रिगर करना
किसी दस्तावेज़ को मिटाने पर भी फ़ंक्शन को ट्रिगर किया जा सकता है. यह उदाहरण फ़ंक्शन तब ट्रिगर होता है, जब कोई उपयोगकर्ता अपनी प्रोफ़ाइल मिटाता है:
Node.js
const {
onDocumentDeleted,
Change,
FirestoreEvent
} = require('firebase-functions/v2/firestore');
exports.deleteuser = onDocumentDeleted("users/{userId}", (event) => {
// Get an object representing the document
// e.g. {'name': 'Marie', 'age': 66}
const snap = event.data;
const data = snap.data();
// perform more operations ...
});
पुष्टि करने के लिए ज़्यादा जानकारी पाने के लिए, onDocumentDeletedWithAuthContext का इस्तेमाल करें.
Python
from firebase_functions.firestore_fn import (
on_document_deleted,
Event,
DocumentSnapshot,
)
@on_document_deleted(document="users/{userId}")
def myfunction(event: Event[DocumentSnapshot|None]) -> None:
# Perform more operations ...
दस्तावेज़ में हुए सभी बदलावों के लिए किसी फ़ंक्शन को ट्रिगर करना
अगर आपको इस बात से कोई फ़र्क़ नहीं पड़ता कि किस तरह का इवेंट ट्रिगर हो रहा है, तो "दस्तावेज़ लिखा गया" इवेंट ट्रिगर का इस्तेमाल करके, MongoDB के साथ काम करने वाले Cloud Firestore के दस्तावेज़ में हुए सभी बदलावों को सुना जा सकता है. अगर किसी उपयोगकर्ता को बनाया, अपडेट किया या मिटाया जाता है, तो यह उदाहरण फ़ंक्शन ट्रिगर होता है:
Node.js
const {
onDocumentWritten,
Change,
FirestoreEvent
} = require('firebase-functions/v2/firestore');
exports.modifyuser = onDocumentWritten("users/{userId}", (event) => {
// Get an object with the current document values.
// If the document does not exist, it was deleted
const document = event.data.after.data();
// Get an object with the previous document values
const previousValues = event.data.before.data();
// perform more operations ...
});
पुष्टि करने के लिए ज़्यादा जानकारी पाने के लिए, onDocumentWrittenWithAuthContext का इस्तेमाल करें.
Python
from firebase_functions.firestore_fn import (
on_document_written,
Event,
Change,
DocumentSnapshot,
)
@on_document_written(document="users/{userId}")
def myfunction(event: Event[Change[DocumentSnapshot | None]]) -> None:
# Get an object with the current document values.
# If the document does not exist, it was deleted.
document = (event.data.after.to_dict()
if event.data.after is not None else None)
# Get an object with the previous document values.
# If the document does not exist, it was newly created.
previous_values = (event.data.before.to_dict()
if event.data.before is not None else None)
# Perform more operations ...
डेटा पढ़ना और लिखना
जब कोई फ़ंक्शन ट्रिगर होता है, तो वह इवेंट से जुड़े डेटा का स्नैपशॉट उपलब्ध कराता है. इस स्नैपशॉट का इस्तेमाल, उस दस्तावेज़ को पढ़ने या उसमें लिखने के लिए किया जा सकता है जिसने इवेंट को ट्रिगर किया है. इसके अलावा, Firebase Admin SDK का इस्तेमाल करके, अपने डेटाबेस के अन्य हिस्सों को ऐक्सेस किया जा सकता है.
इवेंट का डेटा
पढ़ने से जुड़ा डेटा
किसी फ़ंक्शन के ट्रिगर होने पर, आपको अपडेट किए गए दस्तावेज़ से डेटा मिल सकता है. इसके अलावा, अपडेट से पहले का डेटा भी मिल सकता है. अपडेट से पहले का डेटा पाने के लिए, event.data.before का इस्तेमाल किया जा सकता है. इसमें अपडेट से पहले के दस्तावेज़ का स्नैपशॉट होता है.
इसी तरह, event.data.after में अपडेट के बाद दस्तावेज़ के स्नैपशॉट की स्थिति शामिल होती है.
Node.js
exports.updateuser2 = onDocumentUpdated("users/{userId}", (event) => {
// Get an object with the current document values.
// If the document does not exist, it was deleted
const newValues = event.data.after.data();
// Get an object with the previous document values
const previousValues = event.data.before.data();
});
Python
@on_document_updated(document="users/{userId}")
def myfunction(event: Event[Change[DocumentSnapshot]]) -> None:
# Get an object with the current document values.
new_value = event.data.after.to_dict()
# Get an object with the previous document values.
prev_value = event.data.before.to_dict()
प्रॉपर्टी को उसी तरह ऐक्सेस किया जा सकता है जिस तरह किसी अन्य ऑब्जेक्ट को ऐक्सेस किया जाता है. इसके अलावा, कुछ फ़ील्ड ऐक्सेस करने के लिए, get फ़ंक्शन का इस्तेमाल किया जा सकता है:
Node.js
// Fetch data using standard accessors
const age = event.data.after.data().age;
const name = event.data.after.data()['name'];
// Fetch data using built in accessor
const experience = event.data.after.data.get('experience');
Python
# Get the value of a single document field.
age = event.data.after.get("age")
# Convert the document to a dictionary.
age = event.data.after.to_dict()["age"]
डेटा सेव करने की अनुमति दें
हर फ़ंक्शन इनवॉकेशन, Cloud Firestore with MongoDB compatibility डेटाबेस में मौजूद किसी खास दस्तावेज़ से जुड़ा होता है. आपके पास उस दस्तावेज़ को ऐक्सेस करने का विकल्प होता है. यह दस्तावेज़, आपके फ़ंक्शन को लौटाए गए स्नैपशॉट में मौजूद होता है.
दस्तावेज़ के रेफ़रंस में update(), set(), और remove() जैसे तरीके शामिल होते हैं. इसलिए, फ़ंक्शन को ट्रिगर करने वाले दस्तावेज़ में बदलाव किया जा सकता है.
Node.js
const {onDocumentUpdated} = require('firebase-functions/v2/firestore');
exports.countnamechanges = onDocumentUpdated('users/{userId}', (event) => {
// Retrieve the current and previous value
const data = event.data.after.data();
const previousData = event.data.before.data();
// We'll only update if the name has changed.
// This is crucial to prevent infinite loops.
if (data.name == previousData.name) {
return null;
}
// Retrieve the current count of name changes
let count = data.name_change_count;
if (!count) {
count = 0;
}
// Then return a promise of a set operation to update the count
return event.data.after.ref.set({
name_change_count: count + 1
}, {merge: true});
});
Python
@on_document_updated(document="users/{userId}")
def myfunction(event: Event[Change[DocumentSnapshot]]) -> None:
# Get the current and previous document values.
new_value = event.data.after
prev_value = event.data.before
# We'll only update if the name has changed.
# This is crucial to prevent infinite loops.
if new_value.get("name") == prev_value.get("name"):
return
# Retrieve the current count of name changes
count = new_value.to_dict().get("name_change_count", 0)
# Update the count
new_value.reference.update({"name_change_count": count + 1})
उपयोगकर्ता की पुष्टि करने से जुड़ी जानकारी ऐक्सेस करना
इनमें से किसी इवेंट टाइप का इस्तेमाल करने पर, आपको उस मुख्य व्यक्ति के बारे में उपयोगकर्ता की पुष्टि करने वाली जानकारी मिल सकती है जिसने इवेंट को ट्रिगर किया है. यह जानकारी, बेस इवेंट में मिली जानकारी के अलावा होती है.
Node.js
onDocumentCreatedWithAuthContextonDocumentWrittenWithAuthContextonDocumentDeletedWithAuthContextonDocumentUpdatedWithAuthContext
Python
on_document_created_with_auth_contexton_document_updated_with_auth_contexton_document_deleted_with_auth_contexton_document_written_with_auth_context
पुष्टि करने के कॉन्टेक्स्ट में उपलब्ध डेटा के बारे में जानकारी पाने के लिए, पुष्टि करने का कॉन्टेक्स्ट देखें. यहां दिए गए उदाहरण में, पुष्टि करने से जुड़ी जानकारी पाने का तरीका बताया गया है:
Node.js
const {onDocumentWrittenWithAuthContext} = require('firebase-functions/v2/firestore');
exports.syncUser = onDocumentWrittenWithAuthContext("users/{userId}", (event) => {
const snapshot = event.data.after;
if (!snapshot) {
console.log("No data associated with the event");
return;
}
const data = snapshot.data();
// retrieve auth context from event
const { authType, authId } = event;
let verified = false;
if (authType === "system") {
// system-generated users are automatically verified
verified = true;
} else if (authType === "unknown" || authType === "unauthenticated") {
// admin users from a specific domain are verified
if (authId.endsWith("@example.com")) {
verified = true;
}
}
return data.after.ref.set({
created_by: authId,
verified,
}, {merge: true});
});
Python
@on_document_updated_with_auth_context(document="users/{userId}")
def myfunction(event: Event[Change[DocumentSnapshot]]) -> None:
# Get the current and previous document values.
new_value = event.data.after
prev_value = event.data.before
# Get the auth context from the event
user_auth_type = event.auth_type
user_auth_id = event.auth_id
ट्रिगर इवेंट के बाहर का डेटा
Cloud Functions को भरोसेमंद एनवायरमेंट में एक्ज़ीक्यूट किया जाता है. उन्हें आपके प्रोजेक्ट पर सेवा खाते के तौर पर अनुमति दी गई है. साथ ही, Firebase Admin SDK का इस्तेमाल करके, डेटा को पढ़ा और लिखा जा सकता है:
Node.js
const { initializeApp } = require('firebase-admin/app');
const { getFirestore, Timestamp, FieldValue } = require('firebase-admin/firestore');
initializeApp();
const db = getFirestore();
exports.writetofirestore = onDocumentWritten("some/doc", (event) => {
db.doc('some/otherdoc').set({ ... });
});
exports.writetofirestore = onDocumentWritten('users/{userId}', (event) => {
db.doc('some/otherdoc').set({
// Update otherdoc
});
});
Python
from firebase_admin import firestore, initialize_app
import google.cloud.firestore
initialize_app()
@on_document_written(document="some/doc")
def myfunction(event: Event[Change[DocumentSnapshot | None]]) -> None:
firestore_client: google.cloud.firestore.Client = firestore.client()
firestore_client.document("another/doc").set({
# ...
})
सीमाएं
- ऑर्डर करने की कोई गारंटी नहीं है. तेज़ी से बदलाव होने पर, फ़ंक्शन को अनचाहे क्रम में शुरू किया जा सकता है.
- इवेंट कम से कम एक बार डिलीवर किए जाते हैं. हालांकि, एक इवेंट से फ़ंक्शन को कई बार कॉल किया जा सकता है. 'सिर्फ़ एक बार' सिद्धांत पर निर्भर रहने से बचें और आईडम्पोटेंट फ़ंक्शन लिखें.
- ट्रिगर, किसी एक डेटाबेस से जुड़ा होता है. एक से ज़्यादा डेटाबेस से मैच करने वाला ट्रिगर नहीं बनाया जा सकता.
- किसी डेटाबेस को मिटाने से, उस डेटाबेस के लिए बनाए गए ट्रिगर अपने-आप नहीं मिटते. ट्रिगर इवेंट डिलीवर करना बंद कर देता है. हालांकि, यह तब तक मौजूद रहता है, जब तक ट्रिगर को मिटाया नहीं जाता.