UNPKG

veritatisdoloremque

Version:
63 lines (46 loc) 2.56 kB
<!-- Do not edit this file. It is automatically generated by API Documenter. --> [Home](./index.md) &gt; [@firebase/firestore](./firestore.md) &gt; [/](./firestore_.md) &gt; [FirestoreDataConverter](./firestore_.firestoredataconverter.md) ## FirestoreDataConverter interface Converter used by `withConverter()` to transform user objects of type `T` into Firestore data. Using the converter allows you to specify generic type arguments when storing and retrieving objects from Firestore. <b>Signature:</b> ```typescript export declare interface FirestoreDataConverter<T> ``` ## Example ```typescript class Post { constructor(readonly title: string, readonly author: string) {} toString(): string { return this.title + ', by ' + this.author; } } const postConverter = { toFirestore(post: Post): firebase.firestore.DocumentData { return {title: post.title, author: post.author}; }, fromFirestore( snapshot: firebase.firestore.QueryDocumentSnapshot, options: firebase.firestore.SnapshotOptions ): Post { const data = snapshot.data(options)!; return new Post(data.title, data.author); } }; const postSnap = await firebase.firestore() .collection('posts') .withConverter(postConverter) .doc().get(); const post = postSnap.data(); if (post !== undefined) { post.title; // string post.toString(); // Should be defined post.someNonExistentProperty; // TS error } ``` ## Methods | Method | Description | | --- | --- | | [fromFirestore(snapshot, options)](./firestore_.firestoredataconverter.fromfirestore.md) | Called by the Firestore SDK to convert Firestore data into an object of type T. You can access your data by calling: <code>snapshot.data(options)</code>. | | [toFirestore(modelObject)](./firestore_.firestoredataconverter.tofirestore.md) | Called by the Firestore SDK to convert a custom model object of type <code>T</code> into a plain JavaScript object (suitable for writing directly to the Firestore database). To use <code>set()</code> with <code>merge</code> and <code>mergeFields</code>, <code>toFirestore()</code> must be defined with <code>Partial&lt;T&gt;</code>. | | [toFirestore(modelObject, options)](./firestore_.firestoredataconverter.tofirestore_1.md) | Called by the Firestore SDK to convert a custom model object of type <code>T</code> into a plain JavaScript object (suitable for writing directly to the Firestore database). Used with , and with <code>merge:true</code> or <code>mergeFields</code>. |