FirestoreDataConverter interface

AppModelType प्रकार के उपयोगकर्ता ऑब्जेक्ट को DbModelType प्रकार के फायरस्टोर डेटा में बदलने के लिए withConverter() द्वारा उपयोग किया जाने वाला कनवर्टर .

कनवर्टर का उपयोग आपको फायरस्टोर से वस्तुओं को संग्रहीत और पुनर्प्राप्त करते समय सामान्य प्रकार के तर्क निर्दिष्ट करने की अनुमति देता है।

इस संदर्भ में, "ऐपमॉडल" एक वर्ग है जिसका उपयोग किसी एप्लिकेशन में संबंधित जानकारी और कार्यक्षमता को एक साथ पैकेज करने के लिए किया जाता है। उदाहरण के लिए, ऐसे वर्ग में जटिल, नेस्टेड डेटा प्रकारों वाले गुण, मेमोइज़ेशन के लिए उपयोग किए जाने वाले गुण, फायरस्टोर द्वारा समर्थित नहीं होने वाले प्रकारों के गुण (जैसे symbol और bigint हो सकते हैं ), और सहायक कार्य जो यौगिक संचालन करते हैं। ऐसी कक्षाएं फायरस्टोर डेटाबेस में संग्रहीत करने के लिए उपयुक्त और/या संभव नहीं हैं। इसके बजाय, ऐसे वर्गों के उदाहरणों को विशेष रूप से आदिम गुणों के साथ "सादे पुराने जावास्क्रिप्ट ऑब्जेक्ट्स" (पीओजेओ) में परिवर्तित करने की आवश्यकता है, जो संभावित रूप से अन्य पीओजेओ या पीओजेओ के सरणी के अंदर निहित हैं। इस संदर्भ में, इस प्रकार को "डीबीमॉडल" के रूप में संदर्भित किया जाता है और यह फायरस्टोर में बने रहने के लिए उपयुक्त वस्तु होगी। सुविधा के लिए, एप्लिकेशन FirestoreDataConverter कार्यान्वित कर सकते हैं और कनवर्टर को Firestore ऑब्जेक्ट, जैसे DocumentReference या Query के साथ पंजीकृत कर सकते हैं , फायरस्टोर में स्टोर करते समय AppModel स्वचालित रूप से DbModel में परिवर्तित करने के लिए, और Firestore से पुनर्प्राप्त करते समय DbModel को AppModel में परिवर्तित करने के लिए।

हस्ताक्षर:

export declare interface FirestoreDataConverter<AppModelType, DbModelType extends DocumentData = DocumentData> 

तरीकों

तरीका विवरण
फ़ायरस्टोर से (स्नैपशॉट, विकल्प) फायरस्टोर डेटा को AppModelType प्रकार के ऑब्जेक्ट में परिवर्तित करने के लिए फायरस्टोर SDK द्वारा कॉल किया गया। आप कॉल करके अपने डेटा तक पहुंच सकते हैं: snapshot.data(options) आम तौर पर, snapshot.data() से लौटाए गए डेटा को DbModelType पर डाला जा सकता है; हालाँकि, इसकी गारंटी नहीं है क्योंकि फायरस्टोर डेटाबेस पर स्कीमा लागू नहीं करता है। उदाहरण के लिए, एप्लिकेशन के पिछले संस्करण से लिखता है या किसी अन्य क्लाइंट से लिखता है जिसने टाइप कनवर्टर का उपयोग नहीं किया है, उसमें विभिन्न गुणों और/या संपत्ति प्रकारों के साथ डेटा लिखा जा सकता है। कार्यान्वयन को यह चुनने की आवश्यकता होगी कि क्या गैर-अनुरूप डेटा से शालीनतापूर्वक पुनर्प्राप्त किया जाए या कोई त्रुटि उत्पन्न की जाए। इस विधि को ओवरराइड करने के लिए, देखें।
टूफ़ायरस्टोर(मॉडलऑब्जेक्ट) फायरस्टोर एसडीके द्वारा AppModelType प्रकार के एक कस्टम मॉडल ऑब्जेक्ट को DbModelType प्रकार के एक सादे जावास्क्रिप्ट ऑब्जेक्ट (फायरस्टोर डेटाबेस पर सीधे लिखने के लिए उपयुक्त) में परिवर्तित करने के लिए कॉल किया गया। merge और mergeFields के साथ set() उपयोग करने के लिए, toFirestore() PartialWithFieldValue<AppModelType> के साथ परिभाषित किया जाना चाहिए। WithFieldValue<T> प्रकार T का विस्तार करता है ताकि फ़ील्डवैल्यू जैसे deleteField() को संपत्ति मान के रूप में उपयोग करने की अनुमति मिल सके।
टूफ़ायरस्टोर(मॉडलऑब्जेक्ट, विकल्प) फायरस्टोर एसडीके द्वारा AppModelType प्रकार के एक कस्टम मॉडल ऑब्जेक्ट को DbModelType प्रकार के एक सादे जावास्क्रिप्ट ऑब्जेक्ट (फायरस्टोर डेटाबेस पर सीधे लिखने के लिए उपयुक्त) में परिवर्तित करने के लिए कॉल किया गया। setDoc() के साथ प्रयोग किया जाता है , और merge:true या mergeFields PartialWithFieldValue<T> प्रकार , ArrayUnion() जैसे फ़ील्डवैल्यू को संपत्ति मान के रूप में उपयोग करने की अनुमति देने के लिए Partial<T> का विस्तार करता है। यह नेस्टेड फ़ील्ड्स को छोड़ने की अनुमति देकर नेस्टेड Partial भी समर्थन करता है।

FirestoreDataConverter.fromFirestore()

फायरस्टोर डेटा को AppModelType प्रकार के ऑब्जेक्ट में परिवर्तित करने के लिए फायरस्टोर SDK द्वारा कॉल किया गया . आप कॉल करके अपना डेटा एक्सेस कर सकते हैं: snapshot.data(options) .

आम तौर पर, snapshot.data() से लौटाए गए डेटा को DbModelType पर डाला जा सकता है ; हालाँकि, इसकी गारंटी नहीं है क्योंकि फायरस्टोर डेटाबेस पर स्कीमा लागू नहीं करता है। उदाहरण के लिए, एप्लिकेशन के पिछले संस्करण से लिखता है या किसी अन्य क्लाइंट से लिखता है जिसने टाइप कनवर्टर का उपयोग नहीं किया है, उसमें विभिन्न गुणों और/या संपत्ति प्रकारों के साथ डेटा लिखा जा सकता है। कार्यान्वयन को यह चुनने की आवश्यकता होगी कि क्या गैर-अनुरूप डेटा से शालीनतापूर्वक पुनर्प्राप्त किया जाए या कोई त्रुटि उत्पन्न की जाए।

इस विधि को ओवरराइड करने के लिए, देखें।

हस्ताक्षर:

fromFirestore(snapshot: QueryDocumentSnapshot<DocumentData, DocumentData>, options?: SnapshotOptions): AppModelType;

पैरामीटर

पैरामीटर प्रकार विवरण
स्नैपशॉट क्वेरी दस्तावेज़ स्नैपशॉट < दस्तावेज़डेटा , दस्तावेज़ डेटा > एक QueryDocumentSnapshot जिसमें आपका डेटा और मेटाडेटा शामिल है।
विकल्प स्नैपशॉट विकल्प प्रारंभिक कॉल से data() तक SnapshotOptions

रिटर्न:

ऐपमॉडल प्रकार

FirestoreDataConverter.toFirestore()

AppModelType प्रकार के एक कस्टम मॉडल ऑब्जेक्ट को DbModelType प्रकार के सादे जावास्क्रिप्ट ऑब्जेक्ट (सीधे फायरस्टोर डेटाबेस में लिखने के लिए उपयुक्त) में परिवर्तित करने के लिए फायरस्टोर SDK द्वारा कॉल किया गया . merge और mergeFields के साथ set() का उपयोग करना , toFirestore() PartialWithFieldValue<AppModelType> के साथ परिभाषित किया जाना चाहिए .

WithFieldValue<T> प्रकार T का विस्तार करता है ताकि फ़ील्डवैल्यू जैसे deleteField() को संपत्ति मान के रूप में उपयोग करने की अनुमति मिल सके।

हस्ताक्षर:

toFirestore(modelObject: WithFieldValue<AppModelType>): WithFieldValue<DbModelType>;

पैरामीटर

पैरामीटर प्रकार विवरण
मॉडलऑब्जेक्ट फ़ील्डवैल्यू के साथ <ऐपमॉडलटाइप>

रिटर्न:

फ़ील्डवैल्यू के साथ <DbModelType>

FirestoreDataConverter.toFirestore()

AppModelType प्रकार के एक कस्टम मॉडल ऑब्जेक्ट को DbModelType प्रकार के सादे जावास्क्रिप्ट ऑब्जेक्ट (सीधे फायरस्टोर डेटाबेस में लिखने के लिए उपयुक्त) में परिवर्तित करने के लिए फायरस्टोर SDK द्वारा कॉल किया गया . setDoc() के साथ प्रयोग किया जाता है , और merge:true या mergeFields .

PartialWithFieldValue<T> प्रकार , ArrayUnion() जैसे फ़ील्डवैल्यू को संपत्ति मान के रूप में उपयोग करने की अनुमति देने के लिए Partial<T> का विस्तार करता है। यह नेस्टेड फ़ील्ड्स को छोड़ने की अनुमति देकर नेस्टेड Partial भी समर्थन करता है।

हस्ताक्षर:

toFirestore(modelObject: PartialWithFieldValue<AppModelType>, options: SetOptions): PartialWithFieldValue<DbModelType>;

पैरामीटर

पैरामीटर प्रकार विवरण
मॉडलऑब्जेक्ट आंशिकविथफ़ील्डवैल्यू <ऐपमॉडलटाइप>
विकल्प विकल्प सेट करें

रिटर्न:

आंशिकविथफ़ील्डवैल्यू <DbModelType>

उदाहरण

सरल उदाहरण

const numberConverter = {
    toFirestore(value: WithFieldValue<number>) {
        return { value };
    },
    fromFirestore(snapshot: QueryDocumentSnapshot, options: SnapshotOptions) {
        return snapshot.data(options).value as number;
    }
};

async function simpleDemo(db: Firestore): Promise<void> {
    const documentRef = doc(db, 'values/value123').withConverter(numberConverter);

    // converters are used with `setDoc`, `addDoc`, and `getDoc`
    await setDoc(documentRef, 42);
    const snapshot1 = await getDoc(documentRef);
    assertEqual(snapshot1.data(), 42);

    // converters are not used when writing data with `updateDoc`
    await updateDoc(documentRef, { value: 999 });
    const snapshot2 = await getDoc(documentRef);
    assertEqual(snapshot2.data(), 999);
}

उन्नत उदाहरण

// The Post class is a model that is used by our application.
// This class may have properties and methods that are specific
// to our application execution, which do not need to be persisted
// to Firestore.
class Post {
    constructor(
        readonly title: string,
        readonly author: string,
        readonly lastUpdatedMillis: number
    ) {}
    toString(): string {
        return `${this.title} by ${this.author}`;
    }
}

// The PostDbModel represents how we want our posts to be stored
// in Firestore. This DbModel has different properties (`ttl`,
// `aut`, and `lut`) from the Post class we use in our application.
interface PostDbModel {
    ttl: string;
    aut: { firstName: string; lastName: string };
    lut: Timestamp;
}

// The `PostConverter` implements `FirestoreDataConverter` and specifies
// how the Firestore SDK can convert `Post` objects to `PostDbModel`
// objects and vice versa.
class PostConverter implements FirestoreDataConverter<Post, PostDbModel> {
    toFirestore(post: WithFieldValue<Post>): WithFieldValue<PostDbModel> {
        return {
            ttl: post.title,
            aut: this._autFromAuthor(post.author),
            lut: this._lutFromLastUpdatedMillis(post.lastUpdatedMillis)
        };
    }

    fromFirestore(snapshot: QueryDocumentSnapshot, options: SnapshotOptions): Post {
        const data = snapshot.data(options) as PostDbModel;
        const author = `${data.aut.firstName} ${data.aut.lastName}`;
        return new Post(data.ttl, author, data.lut.toMillis());
    }

    _autFromAuthor(
        author: string | FieldValue
    ): { firstName: string; lastName: string } | FieldValue {
        if (typeof author !== 'string') {
            // `author` is a FieldValue, so just return it.
            return author;
        }
        const [firstName, lastName] = author.split(' ');
        return {firstName, lastName};
    }

    _lutFromLastUpdatedMillis(
        lastUpdatedMillis: number | FieldValue
    ): Timestamp | FieldValue {
        if (typeof lastUpdatedMillis !== 'number') {
            // `lastUpdatedMillis` must be a FieldValue, so just return it.
            return lastUpdatedMillis;
        }
        return Timestamp.fromMillis(lastUpdatedMillis);
    }
}

async function advancedDemo(db: Firestore): Promise<void> {
    // Create a `DocumentReference` with a `FirestoreDataConverter`.
    const documentRef = doc(db, 'posts/post123').withConverter(new PostConverter());

    // The `data` argument specified to `setDoc()` is type checked by the
    // TypeScript compiler to be compatible with `Post`. Since the `data`
    // argument is typed as `WithFieldValue<Post>` rather than just `Post`,
    // this allows properties of the `data` argument to also be special
    // Firestore values that perform server-side mutations, such as
    // `arrayRemove()`, `deleteField()`, and `serverTimestamp()`.
    await setDoc(documentRef, {
        title: 'My Life',
        author: 'Foo Bar',
        lastUpdatedMillis: serverTimestamp()
    });

    // The TypeScript compiler will fail to compile if the `data` argument to
    // `setDoc()` is _not_ compatible with `WithFieldValue<Post>`. This
    // type checking prevents the caller from specifying objects with incorrect
    // properties or property values.
    // @ts-expect-error "Argument of type { ttl: string; } is not assignable
    // to parameter of type WithFieldValue<Post>"
    await setDoc(documentRef, { ttl: 'The Title' });

    // When retrieving a document with `getDoc()` the `DocumentSnapshot`
    // object's `data()` method returns a `Post`, rather than a generic object,
    // which would have been returned if the `DocumentReference` did _not_ have a
    // `FirestoreDataConverter` attached to it.
    const snapshot1: DocumentSnapshot<Post> = await getDoc(documentRef);
    const post1: Post = snapshot1.data()!;
    if (post1) {
        assertEqual(post1.title, 'My Life');
        assertEqual(post1.author, 'Foo Bar');
    }

    // The `data` argument specified to `updateDoc()` is type checked by the
    // TypeScript compiler to be compatible with `PostDbModel`. Note that
    // unlike `setDoc()`, whose `data` argument must be compatible with `Post`,
    // the `data` argument to `updateDoc()` must be compatible with
    // `PostDbModel`. Similar to `setDoc()`, since the `data` argument is typed
    // as `WithFieldValue<PostDbModel>` rather than just `PostDbModel`, this
    // allows properties of the `data` argument to also be those special
    // Firestore values, like `arrayRemove()`, `deleteField()`, and
    // `serverTimestamp()`.
    await updateDoc(documentRef, {
        'aut.firstName': 'NewFirstName',
        lut: serverTimestamp()
    });

    // The TypeScript compiler will fail to compile if the `data` argument to
    // `updateDoc()` is _not_ compatible with `WithFieldValue<PostDbModel>`.
    // This type checking prevents the caller from specifying objects with
    // incorrect properties or property values.
    // @ts-expect-error "Argument of type { title: string; } is not assignable
    // to parameter of type WithFieldValue<PostDbModel>"
    await updateDoc(documentRef, { title: 'New Title' });
    const snapshot2: DocumentSnapshot<Post> = await getDoc(documentRef);
    const post2: Post = snapshot2.data()!;
    if (post2) {
        assertEqual(post2.title, 'My Life');
        assertEqual(post2.author, 'NewFirstName Bar');
    }
}