FirestoreDataConverter interface

withConverter() দ্বারা ব্যবহৃত কনভার্টার AppModelType টাইপের ব্যবহারকারীর অবজেক্টকে DbModelType টাইপের Firestore ডেটাতে রূপান্তরিত করতে .

কনভার্টার ব্যবহার করে আপনি Firestore থেকে বস্তু সংরক্ষণ এবং পুনরুদ্ধার করার সময় জেনেরিক ধরনের আর্গুমেন্ট নির্দিষ্ট করতে পারবেন।

এই প্রসঙ্গে, একটি "AppModel" একটি ক্লাস যা একটি অ্যাপ্লিকেশনে সম্পর্কিত তথ্য এবং কার্যকারিতা একসাথে প্যাকেজ করতে ব্যবহৃত হয়। এই ধরনের ক্লাসে, উদাহরণস্বরূপ, জটিল, নেস্টেড ডেটা টাইপের বৈশিষ্ট্য, মেমোাইজেশনের জন্য ব্যবহৃত বৈশিষ্ট্য, ফায়ারস্টোর দ্বারা সমর্থিত নয় এমন বৈশিষ্ট্যগুলির বৈশিষ্ট্য থাকতে পারে (যেমন symbol এবং bigint ), এবং সহায়ক ফাংশন যা যৌগিক ক্রিয়াকলাপ সম্পাদন করে। এই ধরনের ক্লাস উপযুক্ত এবং/অথবা Firestore ডাটাবেসে সংরক্ষণ করা সম্ভব নয়। পরিবর্তে, এই ধরনের ক্লাসের উদাহরণগুলিকে "সাধারণ পুরানো জাভাস্ক্রিপ্ট অবজেক্ট" (POJOs) তে রূপান্তর করতে হবে যা একচেটিয়াভাবে আদিম বৈশিষ্ট্য সহ, সম্ভাব্যভাবে অন্যান্য POJO বা POJO-এর অ্যারেগুলির মধ্যে নেস্ট করা হয়। এই প্রসঙ্গে, এই প্রকারটিকে "DbModel" হিসাবে উল্লেখ করা হয় এবং এটি Firestore-এ টিকে থাকার জন্য উপযুক্ত একটি বস্তু হবে৷ সুবিধার জন্য, অ্যাপ্লিকেশনগুলি FirestoreDataConverter প্রয়োগ করতে পারে এবং Firestore বস্তুর সাথে কনভার্টার নিবন্ধন করতে পারে, যেমন DocumentReference বা Query , Firestore-এ সঞ্চয় করার সময় AppModel স্বয়ংক্রিয়ভাবে DbModel এ রূপান্তর করতে এবং Firestore থেকে পুনরুদ্ধার করার সময় DbModel কে AppModel এ রূপান্তর করতে।

স্বাক্ষর:

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

পদ্ধতি

পদ্ধতি বর্ণনা
ফায়ারস্টোর থেকে (স্ন্যাপশট, বিকল্প) Firestore SDK দ্বারা Firestore ডেটাকে AppModelType টাইপের অবজেক্টে রূপান্তর করার জন্য ডাকা হয়। আপনি কল করে আপনার ডেটা অ্যাক্সেস করতে পারেন: snapshot.data(options) সাধারণত, snapshot.data() থেকে ফিরে আসা ডেটা DbModelType এ কাস্ট করা যেতে পারে; যাইহোক, এটি নিশ্চিত নয় কারণ ফায়ারস্টোর ডাটাবেসে একটি স্কিমা প্রয়োগ করে না। উদাহরণস্বরূপ, অ্যাপ্লিকেশনটির পূর্ববর্তী সংস্করণ থেকে লেখা বা অন্য ক্লায়েন্ট থেকে লেখা যা টাইপ কনভার্টার ব্যবহার করেনি সেগুলি বিভিন্ন বৈশিষ্ট্য এবং/অথবা সম্পত্তির ধরন সহ লিখিত ডেটা থাকতে পারে। বাস্তবায়নের জন্য অ-অনুসঙ্গিক ডেটা থেকে সুন্দরভাবে পুনরুদ্ধার করা বা একটি ত্রুটি নিক্ষেপ করা চয়ন করতে হবে। এই পদ্ধতি ওভাররাইড করতে, দেখুন.
toFirestore(মডেল অবজেক্ট) Firestore SDK দ্বারা অ্যাপমডেল টাইপ টাইপের একটি কাস্টম মডেল অবজেক্টকে AppModelType টাইপের একটি প্লেইন জাভাস্ক্রিপ্ট অবজেক্টে DbModelType সরাসরি Firestore ডাটাবেসে লেখার জন্য উপযুক্ত) রূপান্তর করতে বলা হয়েছে। set() merge এবং mergeFields সাথে ব্যবহার করতে, toFirestore() PartialWithFieldValue<AppModelType> দিয়ে সংজ্ঞায়িত করতে হবে। WithFieldValue<T> টাইপ টি T করে যাতে ফিল্ড ভ্যালু যেমন deleteField() প্রপার্টি মান হিসেবে ব্যবহার করা যায়।
toFirestore(মডেল অবজেক্ট, বিকল্প) Firestore SDK দ্বারা অ্যাপমডেল টাইপ টাইপের একটি কাস্টম মডেল অবজেক্টকে AppModelType টাইপের একটি প্লেইন জাভাস্ক্রিপ্ট অবজেক্টে DbModelType সরাসরি Firestore ডাটাবেসে লেখার জন্য উপযুক্ত) রূপান্তর করতে বলা হয়েছে। setDoc() এর সাথে ব্যবহৃত , এবং merge:true বা mergeFields . PartialWithFieldValue<T> প্রকার Partial<T> প্রসারিত করে যাতে ফিল্ড ভ্যালু যেমন arrayUnion() প্রপার্টি মান হিসাবে ব্যবহার করা যায়। এটি নেস্টেড ক্ষেত্রগুলিকে বাদ দেওয়ার অনুমতি দিয়ে নেস্টেড Partial সমর্থন করে।

FirestoreDataConverter.fromFirestore()

Firestore SDK দ্বারা Firestore ডেটাকে AppModelType টাইপের একটি বস্তুতে রূপান্তর করার জন্য ডাকা হয় . আপনি কল করে আপনার ডেটা অ্যাক্সেস করতে পারেন: snapshot.data(options) .

সাধারণত, snapshot.data() থেকে ফিরে আসা ডেটা DbModelType এ কাস্ট করা যেতে পারে ; যাইহোক, এটি নিশ্চিত নয় কারণ ফায়ারস্টোর ডাটাবেসে একটি স্কিমা প্রয়োগ করে না। উদাহরণস্বরূপ, অ্যাপ্লিকেশনটির পূর্ববর্তী সংস্করণ থেকে লেখা বা অন্য ক্লায়েন্ট থেকে লেখা যা টাইপ কনভার্টার ব্যবহার করেনি সেগুলি বিভিন্ন বৈশিষ্ট্য এবং/অথবা সম্পত্তির ধরন সহ লিখিত ডেটা থাকতে পারে। বাস্তবায়নের জন্য অ-অনুসঙ্গিক ডেটা থেকে সুন্দরভাবে পুনরুদ্ধার করা বা একটি ত্রুটি নিক্ষেপ করা চয়ন করতে হবে।

এই পদ্ধতি ওভাররাইড করতে, দেখুন.

স্বাক্ষর:

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

পরামিতি

প্যারামিটার টাইপ বর্ণনা
স্ন্যাপশট QueryDocumentSnapshot < ডকুমেন্ট ডেটা , ডকুমেন্ট ডেটা > আপনার ডেটা এবং মেটাডেটা ধারণকারী একটি QueryDocumentSnapshot .
বিকল্প স্ন্যাপশট বিকল্প প্রাথমিক কল থেকে data() SnapshotOptions

রিটার্ন:

অ্যাপমডেল টাইপ

FirestoreDataConverter.toFirestore()

Firestore SDK দ্বারা অ্যাপমডেল টাইপ টাইপের একটি কাস্টম মডেল অবজেক্টকে AppModelType টাইপের প্লেইন জাভাস্ক্রিপ্ট অবজেক্টে ( DbModelType Firestore ডাটাবেসে লেখার জন্য উপযুক্ত) রূপান্তর করতে বলা হয়েছে . merge এবং mergeFields সাথে set() ব্যবহার করতে , toFirestore() PartialWithFieldValue<AppModelType> দিয়ে সংজ্ঞায়িত করতে হবে .

WithFieldValue<T> টাইপ টি T করে যাতে ফিল্ড ভ্যালু যেমন deleteField() প্রপার্টি মান হিসেবে ব্যবহার করা যায়।

স্বাক্ষর:

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

পরামিতি

প্যারামিটার টাইপ বর্ণনা
মডেল অবজেক্ট ফিল্ড ভ্যালু সহ <AppModelType>

রিটার্ন:

ফিল্ড ভ্যালু সহ <DbModelType>

FirestoreDataConverter.toFirestore()

Firestore SDK দ্বারা অ্যাপমডেল টাইপ টাইপের একটি কাস্টম মডেল অবজেক্টকে AppModelType টাইপের প্লেইন জাভাস্ক্রিপ্ট অবজেক্টে ( DbModelType Firestore ডাটাবেসে লেখার জন্য উপযুক্ত) রূপান্তর করতে বলা হয়েছে . setDoc() এর সাথে ব্যবহৃত , এবং merge:true বা mergeFields .

PartialWithFieldValue<T> প্রকার Partial<T> প্রসারিত করে যাতে ফিল্ড ভ্যালু যেমন arrayUnion() প্রপার্টি মান হিসাবে ব্যবহার করা যায়। এটি নেস্টেড ক্ষেত্রগুলিকে বাদ দেওয়ার অনুমতি দিয়ে নেস্টেড Partial সমর্থন করে।

স্বাক্ষর:

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

পরামিতি

প্যারামিটার টাইপ বর্ণনা
মডেল অবজেক্ট PartialWithFieldValue <AppModelType>
বিকল্প সেট অপশন

রিটার্ন:

PartialWithFieldValue <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');
    }
}