Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: use QueryDocumentSnapshot in FirestoreDataConverter #965

Merged
merged 4 commits into from
Apr 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions dev/src/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {FieldPath, validateFieldPath} from './path';
import {DocumentReference} from './reference';
import {Serializer} from './serializer';
import {Timestamp} from './timestamp';
import {ApiMapValue, DocumentData, UpdateMap} from './types';
import {ApiMapValue, defaultConverter, DocumentData, UpdateMap} from './types';
import {isEmpty, isObject, isPlainObject} from './util';

import api = google.firestore.v1;
Expand Down Expand Up @@ -381,11 +381,29 @@ export class DocumentSnapshot<T = DocumentData> {
return undefined;
}

const obj: DocumentData = {};
for (const prop of Object.keys(fields)) {
obj[prop] = this._serializer.decodeValue(fields[prop]);
// We only want to use the converter and create a new QueryDocumentSnapshot
// if a converter has been provided.
if (this.ref._converter !== defaultConverter) {
const untypedReference = new DocumentReference(
this.ref.firestore,
this.ref._path
);
return this.ref._converter.fromFirestore(
new QueryDocumentSnapshot<DocumentData>(
untypedReference,
this._fieldsProto!,
this.readTime,
this.createTime!,
this.updateTime!
)
);
} else {
const obj: DocumentData = {};
for (const prop of Object.keys(fields)) {
obj[prop] = this._serializer.decodeValue(fields[prop]);
}
return obj as T;
}
return this.ref._converter.fromFirestore(obj);
}

/**
Expand Down
9 changes: 6 additions & 3 deletions dev/src/reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -558,8 +558,9 @@ export class DocumentReference<T = DocumentData> implements Serializable {
* return {title: post.title, author: post.author};
* },
* fromFirestore(
* data: FirebaseFirestore.DocumentData
* snapshot: FirebaseFirestore.QueryDocumentSnapshot
* ): Post {
* const data = snapshot.data();
* return new Post(data.title, data.author);
* }
* };
Expand Down Expand Up @@ -2167,8 +2168,9 @@ export class Query<T = DocumentData> {
* return {title: post.title, author: post.author};
* },
* fromFirestore(
* data: FirebaseFirestore.DocumentData
* snapshot: FirebaseFirestore.QueryDocumentSnapshot
* ): Post {
* const data = snapshot.data();
* return new Post(data.title, data.author);
* }
* };
Expand Down Expand Up @@ -2440,8 +2442,9 @@ export class CollectionReference<T = DocumentData> extends Query<T> {
* return {title: post.title, author: post.author};
* },
* fromFirestore(
* data: FirebaseFirestore.DocumentData
* snapshot: FirebaseFirestore.QueryDocumentSnapshot
* ): Post {
* const data = snapshot.data();
* return new Post(data.title, data.author);
* }
* };
Expand Down
15 changes: 6 additions & 9 deletions dev/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {FieldPath} from './path';
import {Timestamp} from './timestamp';

import api = google.firestore.v1;
import {QueryDocumentSnapshot} from './document';

/**
* A map in the format of the Proto API
Expand Down Expand Up @@ -113,7 +114,7 @@ export type RBTree = any;
* return {title: post.title, author: post.author};
* },
* fromFirestore(
* data: FirebaseFirestore.DocumentData
* snapshot: FirebaseFirestore.QueryDocumentSnapshot
* ): Post {
* return new Post(data.title, data.author);
* }
Expand Down Expand Up @@ -142,23 +143,19 @@ export interface FirestoreDataConverter<T> {
* Called by the Firestore SDK to convert Firestore data into an object of
* type T.
*/
fromFirestore(data: DocumentData): T;
fromFirestore(snapshot: QueryDocumentSnapshot): T;
}

/**
* A default converter to use when none is provided.
* @private
*/
export const defaultConverter: FirestoreDataConverter<DocumentData> = {
toFirestore(
modelObject: FirebaseFirestore.DocumentData
): FirebaseFirestore.DocumentData {
toFirestore(modelObject: DocumentData): DocumentData {
return modelObject;
},
fromFirestore(
data: FirebaseFirestore.DocumentData
): FirebaseFirestore.DocumentData {
return data;
fromFirestore(snapshot: QueryDocumentSnapshot): DocumentData {
return snapshot.data()!;
},
};

Expand Down
4 changes: 2 additions & 2 deletions dev/test/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ xdescribe('firestore.d.ts', () => {
toFirestore(modelObject: DocumentData): DocumentData {
return modelObject;
},
fromFirestore(data: DocumentData): DocumentData {
return data;
fromFirestore(snapshot: QueryDocumentSnapshot): DocumentData {
return snapshot.data();
},
};

Expand Down
9 changes: 7 additions & 2 deletions dev/test/util/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ import {Duplex} from 'stream';
import * as through2 from 'through2';

import * as proto from '../../protos/firestore_v1_proto_api';
import {Firestore} from '../../src';
import {
Firestore,
FirestoreDataConverter,
QueryDocumentSnapshot,
} from '../../src';
import {ClientPool} from '../../src/pool';
import {DocumentData, GapicClient} from '../../src/types';

Expand Down Expand Up @@ -348,7 +352,8 @@ export const postConverter = {
toFirestore(post: Post): DocumentData {
return {title: post.title, author: post.author};
},
fromFirestore(data: DocumentData): Post {
fromFirestore(snapshot: QueryDocumentSnapshot): Post {
const data = snapshot.data();
return new Post(data.title, data.author);
},
};
5 changes: 3 additions & 2 deletions types/firestore.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ declare namespace FirebaseFirestore {
* return {title: post.title, author: post.author};
* },
* fromFirestore(
* data: FirebaseFirestore.DocumentData
* snapshot: FirebaseFirestore.QueryDocumentSnapshot
* ): Post {
* const data = snapshot.data();
* return new Post(data.title, data.author);
* }
* };
Expand Down Expand Up @@ -92,7 +93,7 @@ declare namespace FirebaseFirestore {
* Called by the Firestore SDK to convert Firestore data into an object of
* type T.
*/
fromFirestore(data: DocumentData): T;
fromFirestore(snapshot: QueryDocumentSnapshot): T;
}

/**
Expand Down