Skip to content

Add support for Typescript Custom Mapping #2240

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

Merged
merged 37 commits into from
Dec 10, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
eff1d9b
working with generics fully implemented, no tests
Oct 4, 2019
f46af07
[AUTOMATED]: Prettier Code Styling
Oct 4, 2019
fb566cf
round 1 schmidt comments
Oct 4, 2019
c5d8a5f
[AUTOMATED]: Prettier Code Styling
Oct 4, 2019
aed17d1
resolve gil comments, no parent behavior
Oct 7, 2019
ea737f9
updated tests and fixed converterFromArgs
Oct 9, 2019
d6c6355
Merge branch 'master' into bc/generics
Oct 22, 2019
e2b67b3
add more default generic for compiling
Oct 23, 2019
7c150db
[AUTOMATED]: Prettier Code Styling
Oct 23, 2019
3f9bc7c
working with test + don't always pass converter
Nov 6, 2019
6fc432f
Merge branch 'master' into bc/generics
Nov 7, 2019
3096f4b
updated to withConverter, needs test cleanup
Nov 12, 2019
42297c6
[AUTOMATED]: Prettier Code Styling
Nov 12, 2019
338c876
change documentSnapshot methods to <unknown>
Nov 12, 2019
7c3c509
Merge branch 'master' into bc/generics
Nov 14, 2019
d02d5b4
remove unneeded code, added tests
Nov 18, 2019
90138b1
[AUTOMATED]: Prettier Code Styling
Nov 18, 2019
f571f6f
add comments to withConverter
Nov 19, 2019
e351bc8
Merge branch 'master' into bc/generics
Nov 20, 2019
12b602b
[AUTOMATED]: Prettier Code Styling
Nov 20, 2019
2dd8945
resolved comments, need to update jsdoc
Nov 26, 2019
95340bd
Merge branch 'master' into bc/generics
Nov 26, 2019
ba1d7ff
Merge branch 'bc/generics' of github.com:firebase/firebase-js-sdk int…
Nov 26, 2019
a03e009
add comment
Nov 26, 2019
877b3d0
[AUTOMATED]: Prettier Code Styling
Nov 26, 2019
84ebd0b
Merge branch 'master' into bc/generics
Dec 2, 2019
fef36eb
resolve comments from michael pt. 2
Dec 3, 2019
3abad69
Merge branch 'master' into bc/generics
Dec 3, 2019
2ecf3f8
add comment for toString() comparison
Dec 3, 2019
f79041c
remove defaultDocumentdataConverter, add readonly access, update comm…
Dec 5, 2019
6034dae
[AUTOMATED]: Prettier Code Styling
Dec 5, 2019
3c3dbca
add periods to param comments in index.d.ts
Dec 5, 2019
0fce460
remove toString() check in isEqual()
Dec 5, 2019
ab90ea7
change <unknown> to any, resolve sebastian's comments
Dec 9, 2019
b652bf9
[AUTOMATED]: Prettier Code Styling
Dec 9, 2019
a0b0975
add changelog
Dec 9, 2019
2627c4d
change database.ts back to unknown, update changelog
Dec 9, 2019
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
100 changes: 85 additions & 15 deletions packages/firebase/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7087,11 +7087,62 @@ declare namespace firebase.firestore {
*/
export function setLogLevel(logLevel: LogLevel): void;

/**
* Converter used by `withConverter()` to transform user objects into
* Firestore data.
*
* Using the converter allows you to specify generic type arguments when
* storing and retrieving objects from Firestore.
*
* @example
* ```typescript
* class Post {
* constructor(readonly title: string, readonly author: string) {}
* byline(): string {
* return this.title + ', by ' + this.author;
* }
* }
*
* const PostConverter = {
* toFirestore(post: Post): DocumentData {
* return {title: post.title, author: post.author};
* },
* fromFirestore(snapshot: DocumentSnapshot, options: 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.byline(); // Should be defined
* post.someNonExistentProperty; // TS error
* }
* ```
*/
export interface FirestoreDataConverter<T> {
// Converts a model object of type T into plain Firestore DocumentData.
/**
* Called by the Firestore SDK to convert a custom model object of type T
* into a plain Javacsript object (suitable for writing directly to the
* Firestore database).
*/
toFirestore(modelObject: T): DocumentData;
// Converts a Firestore DocumentSnapshot into a model object of type T.
fromFirestore(snapshot: DocumentSnapshot, options: SnapshotOptions): T;

/**
* Called by the Firestore SDK to convert Firestore data into an object of
* type T. You can access your data by calling:
*
* snapshot.data(options)
*
* @param snapshot A DocumentSnapshot containing your data and metadata.
* @param options The SnapshotOptions from the intial call to data().
*/
fromFirestore(snapshot: QueryDocumentSnapshot, options: SnapshotOptions): T;
}

/**
Expand Down Expand Up @@ -7140,7 +7191,7 @@ declare namespace firebase.firestore {
* @param collectionPath A slash-separated path to a collection.
* @return The `CollectionReference` instance.
*/
collection(collectionPath: string): CollectionReference;
collection(collectionPath: string): CollectionReference<DocumentData>;

/**
* Gets a `DocumentReference` instance that refers to the document at the
Expand All @@ -7149,7 +7200,7 @@ declare namespace firebase.firestore {
* @param documentPath A slash-separated path to a document.
* @return The `DocumentReference` instance.
*/
doc(documentPath: string): DocumentReference;
doc(documentPath: string): DocumentReference<DocumentData>;

/**
* Creates and returns a new Query that includes all documents in the
Expand All @@ -7161,7 +7212,7 @@ declare namespace firebase.firestore {
* will be included. Cannot contain a slash.
* @return The created Query.
*/
collectionGroup(collectionId: string): Query;
collectionGroup(collectionId: string): Query<DocumentData>;

/**
* Executes the given `updateFunction` and then attempts to commit the changes
Expand Down Expand Up @@ -7925,7 +7976,11 @@ declare namespace firebase.firestore {
): () => void;

/**
* Converts a DocumentReference into a DocumentReference<U>.
* Applies a custom data converter to this DocumentReference, allowing you
* to use your own custom model objects with Firestore. When you call
* set(), get(), etc. on the returned DocumentReference instance, it will
* use the provided converter to convert data to/from type U (your custom
* model object type).
*
* @param converter Converts objects to and from Firestore
* @return A DocumentReference<U> that uses the provided converter
Expand Down Expand Up @@ -8303,7 +8358,7 @@ declare namespace firebase.firestore {
* the snapshot listener.
*/
onSnapshot(observer: {
next?: (snapshot: QuerySnapshot) => void;
next?: (snapshot: QuerySnapshot<T>) => void;
error?: (error: Error) => void;
complete?: () => void;
}): () => void;
Expand All @@ -8324,7 +8379,7 @@ declare namespace firebase.firestore {
onSnapshot(
options: SnapshotListenOptions,
observer: {
next?: (snapshot: QuerySnapshot) => void;
next?: (snapshot: QuerySnapshot<T>) => void;
error?: (error: Error) => void;
complete?: () => void;
}
Expand All @@ -8346,7 +8401,7 @@ declare namespace firebase.firestore {
* the snapshot listener.
*/
onSnapshot(
onNext: (snapshot: QuerySnapshot) => void,
onNext: (snapshot: QuerySnapshot<T>) => void,
onError?: (error: Error) => void,
onCompletion?: () => void
): () => void;
Expand All @@ -8369,10 +8424,21 @@ declare namespace firebase.firestore {
*/
onSnapshot(
options: SnapshotListenOptions,
onNext: (snapshot: QuerySnapshot) => void,
onNext: (snapshot: QuerySnapshot<T>) => void,
onError?: (error: Error) => void,
onCompletion?: () => void
): () => void;

/**
* Applies a custom data converter to this Query, allowing you to use your
* own custom model objects with Firestore. When you call data() on the
* underlying QueryDocumentSnapshot, it will use the provided converter to
* convert data to/from type U (your custom model object type).
*
* @param converter Converts objects to and from Firestore
* @return A Query<U> that uses the provided converter
*/
withConverter<U>(converter: FirestoreDataConverter<U>): Query<U>;
}

/**
Expand Down Expand Up @@ -8433,7 +8499,7 @@ declare namespace firebase.firestore {
* @param other The `QuerySnapshot` to compare against.
* @return true if this `QuerySnapshot` is equal to the provided one.
*/
isEqual(other: QuerySnapshot): boolean;
isEqual(other: QuerySnapshot<T>): boolean;
}

/**
Expand All @@ -8450,7 +8516,7 @@ declare namespace firebase.firestore {
readonly type: DocumentChangeType;

/** The document affected by this change. */
readonly doc: QueryDocumentSnapshot;
readonly doc: QueryDocumentSnapshot<T>;

/**
* The index of the changed document in the result set immediately prior to
Expand Down Expand Up @@ -8509,7 +8575,7 @@ declare namespace firebase.firestore {
* @return A Promise resolved with a `DocumentReference` pointing to the
* newly created document after it has been written to the backend.
*/
add(data: DocumentData): Promise<DocumentReference<T>>;
add(data: T): Promise<DocumentReference<T>>;

/**
* Returns true if this `CollectionReference` is equal to the provided one.
Expand All @@ -8520,7 +8586,11 @@ declare namespace firebase.firestore {
isEqual(other: CollectionReference<T>): boolean;

/**
* Converts a CollectionReference into a CollectionReference<U>.
* Applies a custom data converter to this CollectionReference, allowing you
* to use your own custom model objects with Firestore. When you call
* set(), get(), etc. on the underlying DocumentReference instance, it will
* use the provided converter to convert data to/from type U (your custom
* model object type).
*
* @param converter Converts objects to and from Firestore
* @return A CollectionReference<U> that uses the provided converter
Expand Down
12 changes: 2 additions & 10 deletions packages/firestore-types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@ export type LogLevel = 'debug' | 'error' | 'silent';
export function setLogLevel(logLevel: LogLevel): void;

export interface FirestoreDataConverter<T> {
// Converts a model object of type T into plain Firestore DocumentData.
toFirestore(modelObject: T): DocumentData;
// Converts a Firestore DocumentSnapshot into a model object of type T.
fromFirestore(snapshot: DocumentSnapshot, options: SnapshotOptions): T;

fromFirestore(snapshot: QueryDocumentSnapshot, options: SnapshotOptions): T;
}

export class FirebaseFirestore {
Expand Down Expand Up @@ -247,7 +246,6 @@ export interface SnapshotOptions {
readonly serverTimestamps?: 'estimate' | 'previous' | 'none';
}

/** Metadata about a snapshot, describing the state of the snapshot. */
export interface SnapshotMetadata {
readonly hasPendingWrites: boolean;
readonly fromCache: boolean;
Expand Down Expand Up @@ -351,12 +349,6 @@ export class Query<T = DocumentData> {
onCompletion?: () => void
): () => void;

/**
* Converts a Query into a Query<U>.
*
* @param converter Converts objects to and from Firestore
* @return A Query<U> that uses the provided converter
*/
withConverter<U>(converter: FirestoreDataConverter<U>): Query<U>;
}

Expand Down
Loading