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 36 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
232 changes: 166 additions & 66 deletions packages/firebase/index.d.ts

Large diffs are not rendered by default.

150 changes: 85 additions & 65 deletions packages/firestore-types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,24 @@ export type LogLevel = 'debug' | 'error' | 'silent';

export function setLogLevel(logLevel: LogLevel): void;

export interface FirestoreDataConverter<T> {
toFirestore(modelObject: T): DocumentData;

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

export class FirebaseFirestore {
private constructor();

settings(settings: Settings): void;

enablePersistence(settings?: PersistenceSettings): Promise<void>;

collection(collectionPath: string): CollectionReference;
collection(collectionPath: string): CollectionReference<DocumentData>;

doc(documentPath: string): DocumentReference;
doc(documentPath: string): DocumentReference<DocumentData>;

collectionGroup(collectionId: string): Query;
collectionGroup(collectionId: string): Query<DocumentData>;

runTransaction<T>(
updateFunction: (transaction: Transaction) => Promise<T>
Expand Down Expand Up @@ -126,43 +132,43 @@ export class Blob {
export class Transaction {
private constructor();

get(documentRef: DocumentReference): Promise<DocumentSnapshot>;
get<T>(documentRef: DocumentReference<T>): Promise<DocumentSnapshot<T>>;

set(
documentRef: DocumentReference,
data: DocumentData,
set<T>(
documentRef: DocumentReference<T>,
data: T,
options?: SetOptions
): Transaction;

update(documentRef: DocumentReference, data: UpdateData): Transaction;
update(documentRef: DocumentReference<any>, data: UpdateData): Transaction;
update(
documentRef: DocumentReference,
documentRef: DocumentReference<any>,
field: string | FieldPath,
value: any,
...moreFieldsAndValues: any[]
): Transaction;

delete(documentRef: DocumentReference): Transaction;
delete(documentRef: DocumentReference<any>): Transaction;
}

export class WriteBatch {
private constructor();

set(
documentRef: DocumentReference,
data: DocumentData,
set<T>(
documentRef: DocumentReference<T>,
data: T,
options?: SetOptions
): WriteBatch;

update(documentRef: DocumentReference, data: UpdateData): WriteBatch;
update(documentRef: DocumentReference<any>, data: UpdateData): WriteBatch;
update(
documentRef: DocumentReference,
documentRef: DocumentReference<any>,
field: string | FieldPath,
value: any,
...moreFieldsAndValues: any[]
): WriteBatch;

delete(documentRef: DocumentReference): WriteBatch;
delete(documentRef: DocumentReference<any>): WriteBatch;

commit(): Promise<void>;
}
Expand All @@ -180,19 +186,19 @@ export interface GetOptions {
readonly source?: 'default' | 'server' | 'cache';
}

export class DocumentReference {
export class DocumentReference<T = DocumentData> {
private constructor();

readonly id: string;
readonly firestore: FirebaseFirestore;
readonly parent: CollectionReference;
readonly parent: CollectionReference<T>;
readonly path: string;

collection(collectionPath: string): CollectionReference;
collection(collectionPath: string): CollectionReference<DocumentData>;

isEqual(other: DocumentReference): boolean;
isEqual(other: DocumentReference<T>): boolean;

set(data: DocumentData, options?: SetOptions): Promise<void>;
set(data: T, options?: SetOptions): Promise<void>;

update(data: UpdateData): Promise<void>;
update(
Expand All @@ -203,64 +209,68 @@ export class DocumentReference {

delete(): Promise<void>;

get(options?: GetOptions): Promise<DocumentSnapshot>;
get(options?: GetOptions): Promise<DocumentSnapshot<T>>;

onSnapshot(observer: {
next?: (snapshot: DocumentSnapshot) => void;
next?: (snapshot: DocumentSnapshot<T>) => void;
error?: (error: FirestoreError) => void;
complete?: () => void;
}): () => void;
onSnapshot(
options: SnapshotListenOptions,
observer: {
next?: (snapshot: DocumentSnapshot) => void;
next?: (snapshot: DocumentSnapshot<T>) => void;
error?: (error: Error) => void;
complete?: () => void;
}
): () => void;
onSnapshot(
onNext: (snapshot: DocumentSnapshot) => void,
onNext: (snapshot: DocumentSnapshot<T>) => void,
onError?: (error: Error) => void,
onCompletion?: () => void
): () => void;
onSnapshot(
options: SnapshotListenOptions,
onNext: (snapshot: DocumentSnapshot) => void,
onNext: (snapshot: DocumentSnapshot<T>) => void,
onError?: (error: Error) => void,
onCompletion?: () => void
): () => void;

withConverter<U>(converter: FirestoreDataConverter<U>): DocumentReference<U>;
}

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;

isEqual(other: SnapshotMetadata): boolean;
}

export class DocumentSnapshot {
export class DocumentSnapshot<T = DocumentData> {
protected constructor();

readonly exists: boolean;
readonly ref: DocumentReference;
readonly ref: DocumentReference<T>;
readonly id: string;
readonly metadata: SnapshotMetadata;

data(options?: SnapshotOptions): DocumentData | undefined;
data(options?: SnapshotOptions): T | undefined;

get(fieldPath: string | FieldPath, options?: SnapshotOptions): any;

isEqual(other: DocumentSnapshot): boolean;
isEqual(other: DocumentSnapshot<T>): boolean;
}

export class QueryDocumentSnapshot extends DocumentSnapshot {
export class QueryDocumentSnapshot<T = DocumentData> extends DocumentSnapshot<
T
> {
private constructor();
data(options?: SnapshotOptions): DocumentData;

data(options?: SnapshotOptions): T;
}

export type OrderByDirection = 'desc' | 'asc';
Expand All @@ -275,104 +285,114 @@ export type WhereFilterOp =
| 'in'
| 'array-contains-any';

export class Query {
export class Query<T = DocumentData> {
protected constructor();

readonly firestore: FirebaseFirestore;

where(fieldPath: string | FieldPath, opStr: WhereFilterOp, value: any): Query;
where(
fieldPath: string | FieldPath,
opStr: WhereFilterOp,
value: any
): Query<T>;

orderBy(
fieldPath: string | FieldPath,
directionStr?: OrderByDirection
): Query;
): Query<T>;

limit(limit: number): Query;
limit(limit: number): Query<T>;

limitToLast(limit: number): Query;
limitToLast(limit: number): Query<T>;

startAt(snapshot: DocumentSnapshot): Query;
startAt(...fieldValues: any[]): Query;
startAt(snapshot: DocumentSnapshot<any>): Query<T>;
startAt(...fieldValues: any[]): Query<T>;

startAfter(snapshot: DocumentSnapshot): Query;
startAfter(...fieldValues: any[]): Query;
startAfter(snapshot: DocumentSnapshot<any>): Query<T>;
startAfter(...fieldValues: any[]): Query<T>;

endBefore(snapshot: DocumentSnapshot): Query;
endBefore(...fieldValues: any[]): Query;
endBefore(snapshot: DocumentSnapshot<any>): Query<T>;
endBefore(...fieldValues: any[]): Query<T>;

endAt(snapshot: DocumentSnapshot): Query;
endAt(...fieldValues: any[]): Query;
endAt(snapshot: DocumentSnapshot<any>): Query<T>;
endAt(...fieldValues: any[]): Query<T>;

isEqual(other: Query): boolean;
isEqual(other: Query<T>): boolean;

get(options?: GetOptions): Promise<QuerySnapshot>;
get(options?: GetOptions): Promise<QuerySnapshot<T>>;

onSnapshot(observer: {
next?: (snapshot: QuerySnapshot) => void;
next?: (snapshot: QuerySnapshot<T>) => void;
error?: (error: Error) => void;
complete?: () => void;
}): () => void;
onSnapshot(
options: SnapshotListenOptions,
observer: {
next?: (snapshot: QuerySnapshot) => void;
next?: (snapshot: QuerySnapshot<T>) => void;
error?: (error: Error) => void;
complete?: () => void;
}
): () => void;
onSnapshot(
onNext: (snapshot: QuerySnapshot) => void,
onNext: (snapshot: QuerySnapshot<T>) => void,
onError?: (error: Error) => void,
onCompletion?: () => void
): () => void;
onSnapshot(
options: SnapshotListenOptions,
onNext: (snapshot: QuerySnapshot) => void,
onNext: (snapshot: QuerySnapshot<T>) => void,
onError?: (error: Error) => void,
onCompletion?: () => void
): () => void;

withConverter<U>(converter: FirestoreDataConverter<U>): Query<U>;
}

export class QuerySnapshot {
export class QuerySnapshot<T = DocumentData> {
private constructor();

readonly query: Query;
readonly query: Query<T>;
readonly metadata: SnapshotMetadata;
readonly docs: QueryDocumentSnapshot[];
readonly docs: Array<QueryDocumentSnapshot<T>>;
readonly size: number;
readonly empty: boolean;

docChanges(options?: SnapshotListenOptions): DocumentChange[];
docChanges(options?: SnapshotListenOptions): Array<DocumentChange<T>>;

forEach(
callback: (result: QueryDocumentSnapshot) => void,
callback: (result: QueryDocumentSnapshot<T>) => void,
thisArg?: any
): void;

isEqual(other: QuerySnapshot): boolean;
isEqual(other: QuerySnapshot<T>): boolean;
}

export type DocumentChangeType = 'added' | 'removed' | 'modified';

export interface DocumentChange {
export interface DocumentChange<T = DocumentData> {
readonly type: DocumentChangeType;
readonly doc: QueryDocumentSnapshot;
readonly doc: QueryDocumentSnapshot<T>;
readonly oldIndex: number;
readonly newIndex: number;
}

export class CollectionReference extends Query {
export class CollectionReference<T = DocumentData> extends Query<T> {
private constructor();

readonly id: string;
readonly parent: DocumentReference | null;
readonly parent: DocumentReference<DocumentData> | null;
readonly path: string;

doc(documentPath?: string): DocumentReference;
doc(documentPath?: string): DocumentReference<T>;

add(data: T): Promise<DocumentReference<T>>;

add(data: DocumentData): Promise<DocumentReference>;
isEqual(other: CollectionReference<T>): boolean;

isEqual(other: CollectionReference): boolean;
withConverter<U>(
converter: FirestoreDataConverter<U>
): CollectionReference<U>;
}

export class FieldValue {
Expand Down
7 changes: 6 additions & 1 deletion packages/firestore/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# Unreleased (1.8.0)
# Unreleased
- [feature] Added support for strongly typed collections, documents, and
queries. You can now use `withConverter()` to supply a custom data
converter that will convert between Firestore data and your custom type.

# 1.8.0
- [changed] Improved the performance of repeatedly executed queries when
persistence is enabled. Recently executed queries should see dramatic
improvements. This benefit is reduced if changes accumulate while the query
Expand Down
Loading