From 619414d0119e6a2ccf2c3b1e8efe45845f2b3d8e Mon Sep 17 00:00:00 2001 From: Denver Coneybeare Date: Thu, 6 Jul 2023 15:41:41 -0400 Subject: [PATCH] firestore-types: fix typings to be consistent with the breaking changes for UpdateData from https://github.com/firebase/firebase-js-sdk/pull/7310 --- packages/firestore-types/index.d.ts | 232 ++++++++++++++++++---------- 1 file changed, 150 insertions(+), 82 deletions(-) diff --git a/packages/firestore-types/index.d.ts b/packages/firestore-types/index.d.ts index d198fd78866..9657432b238 100644 --- a/packages/firestore-types/index.d.ts +++ b/packages/firestore-types/index.d.ts @@ -49,11 +49,20 @@ export type LogLevel = export function setLogLevel(logLevel: LogLevel): void; -export interface FirestoreDataConverter { - toFirestore(modelObject: T): DocumentData; - toFirestore(modelObject: Partial, options: SetOptions): DocumentData; +export interface FirestoreDataConverter< + AppModelType, + DbModelType extends DocumentData = DocumentData +> { + toFirestore(modelObject: AppModelType): DbModelType; + toFirestore( + modelObject: Partial, + options: SetOptions + ): Partial; - fromFirestore(snapshot: QueryDocumentSnapshot, options: SnapshotOptions): T; + fromFirestore( + snapshot: QueryDocumentSnapshot, + options: SnapshotOptions + ): AppModelType; } export class FirebaseFirestore { @@ -71,11 +80,13 @@ export class FirebaseFirestore { enablePersistence(settings?: PersistenceSettings): Promise; - collection(collectionPath: string): CollectionReference; + collection( + collectionPath: string + ): CollectionReference; - doc(documentPath: string): DocumentReference; + doc(documentPath: string): DocumentReference; - collectionGroup(collectionId: string): Query; + collectionGroup(collectionId: string): Query; runTransaction( updateFunction: (transaction: Transaction) => Promise @@ -106,7 +117,7 @@ export class FirebaseFirestore { bundleData: ArrayBuffer | ReadableStream | string ): LoadBundleTask; - namedQuery(name: string): Promise | null>; + namedQuery(name: string): Promise | null>; INTERNAL: { delete: () => Promise }; } @@ -185,45 +196,63 @@ export class Blob { export class Transaction { private constructor(); - get(documentRef: DocumentReference): Promise>; + get( + documentRef: DocumentReference + ): Promise>; - set( - documentRef: DocumentReference, - data: Partial, + set( + documentRef: DocumentReference, + data: Partial, options: SetOptions ): Transaction; - set(documentRef: DocumentReference, data: T): Transaction; + set( + documentRef: DocumentReference, + data: AppModelType + ): Transaction; - update(documentRef: DocumentReference, data: UpdateData): Transaction; - update( - documentRef: DocumentReference, + update( + documentRef: DocumentReference, + data: DbModelType + ): Transaction; + update( + documentRef: DocumentReference, field: string | FieldPath, value: any, ...moreFieldsAndValues: any[] ): Transaction; - delete(documentRef: DocumentReference): Transaction; + delete( + documentRef: DocumentReference + ): Transaction; } export class WriteBatch { private constructor(); - set( - documentRef: DocumentReference, - data: Partial, + set( + documentRef: DocumentReference, + data: Partial, options: SetOptions ): WriteBatch; - set(documentRef: DocumentReference, data: T): WriteBatch; + set( + documentRef: DocumentReference, + data: AppModelType + ): WriteBatch; - update(documentRef: DocumentReference, data: UpdateData): WriteBatch; - update( - documentRef: DocumentReference, + update( + documentRef: DocumentReference, + data: DbModelType + ): WriteBatch; + update( + documentRef: DocumentReference, field: string | FieldPath, value: any, ...moreFieldsAndValues: any[] ): WriteBatch; - delete(documentRef: DocumentReference): WriteBatch; + delete( + documentRef: DocumentReference + ): WriteBatch; commit(): Promise; } @@ -241,22 +270,27 @@ export interface GetOptions { readonly source?: 'default' | 'server' | 'cache'; } -export class DocumentReference { +export class DocumentReference< + AppModelType = DocumentData, + DbModelType extends DocumentData = DocumentData +> { private constructor(); readonly id: string; readonly firestore: FirebaseFirestore; - readonly parent: CollectionReference; + readonly parent: CollectionReference; readonly path: string; - collection(collectionPath: string): CollectionReference; + collection( + collectionPath: string + ): CollectionReference; - isEqual(other: DocumentReference): boolean; + isEqual(other: DocumentReference): boolean; - set(data: Partial, options: SetOptions): Promise; - set(data: T): Promise; + set(data: Partial, options: SetOptions): Promise; + set(data: AppModelType): Promise; - update(data: UpdateData): Promise; + update(data: DbModelType): Promise; update( field: string | FieldPath, value: any, @@ -265,35 +299,39 @@ export class DocumentReference { delete(): Promise; - get(options?: GetOptions): Promise>; + get( + options?: GetOptions + ): Promise>; onSnapshot(observer: { - next?: (snapshot: DocumentSnapshot) => void; + next?: (snapshot: DocumentSnapshot) => void; error?: (error: FirestoreError) => void; complete?: () => void; }): () => void; onSnapshot( options: SnapshotListenOptions, observer: { - next?: (snapshot: DocumentSnapshot) => void; + next?: (snapshot: DocumentSnapshot) => void; error?: (error: FirestoreError) => void; complete?: () => void; } ): () => void; onSnapshot( - onNext: (snapshot: DocumentSnapshot) => void, + onNext: (snapshot: DocumentSnapshot) => void, onError?: (error: FirestoreError) => void, onCompletion?: () => void ): () => void; onSnapshot( options: SnapshotListenOptions, - onNext: (snapshot: DocumentSnapshot) => void, + onNext: (snapshot: DocumentSnapshot) => void, onError?: (error: FirestoreError) => void, onCompletion?: () => void ): () => void; - withConverter(converter: null): DocumentReference; - withConverter(converter: FirestoreDataConverter): DocumentReference; + withConverter(converter: null): DocumentReference; + withConverter( + converter: FirestoreDataConverter + ): DocumentReference; } export interface SnapshotOptions { @@ -307,27 +345,31 @@ export interface SnapshotMetadata { isEqual(other: SnapshotMetadata): boolean; } -export class DocumentSnapshot { +export class DocumentSnapshot< + AppModelType = DocumentData, + DbModelType extends DocumentData = DocumentData +> { protected constructor(); readonly exists: boolean; - readonly ref: DocumentReference; + readonly ref: DocumentReference; readonly id: string; readonly metadata: SnapshotMetadata; - data(options?: SnapshotOptions): T | undefined; + data(options?: SnapshotOptions): AppModelType | undefined; get(fieldPath: string | FieldPath, options?: SnapshotOptions): any; - isEqual(other: DocumentSnapshot): boolean; + isEqual(other: DocumentSnapshot): boolean; } export class QueryDocumentSnapshot< - T = DocumentData -> extends DocumentSnapshot { + AppModelType = DocumentData, + DbModelType extends DocumentData = DocumentData +> extends DocumentSnapshot { private constructor(); - data(options?: SnapshotOptions): T; + data(options?: SnapshotOptions): AppModelType; } export type OrderByDirection = 'desc' | 'asc'; @@ -344,7 +386,10 @@ export type WhereFilterOp = | 'array-contains-any' | 'not-in'; -export class Query { +export class Query< + AppModelType = DocumentData, + DbModelType extends DocumentData = DocumentData +> { protected constructor(); readonly firestore: FirebaseFirestore; @@ -353,54 +398,60 @@ export class Query { fieldPath: string | FieldPath, opStr: WhereFilterOp, value: any - ): Query; + ): Query; orderBy( fieldPath: string | FieldPath, directionStr?: OrderByDirection - ): Query; + ): Query; - limit(limit: number): Query; + limit(limit: number): Query; - limitToLast(limit: number): Query; + limitToLast(limit: number): Query; - startAt(snapshot: DocumentSnapshot): Query; - startAt(...fieldValues: any[]): Query; + startAt( + snapshot: DocumentSnapshot + ): Query; + startAt(...fieldValues: any[]): Query; - startAfter(snapshot: DocumentSnapshot): Query; - startAfter(...fieldValues: any[]): Query; + startAfter( + snapshot: DocumentSnapshot + ): Query; + startAfter(...fieldValues: any[]): Query; - endBefore(snapshot: DocumentSnapshot): Query; - endBefore(...fieldValues: any[]): Query; + endBefore( + snapshot: DocumentSnapshot + ): Query; + endBefore(...fieldValues: any[]): Query; - endAt(snapshot: DocumentSnapshot): Query; - endAt(...fieldValues: any[]): Query; + endAt(snapshot: DocumentSnapshot): Query; + endAt(...fieldValues: any[]): Query; - isEqual(other: Query): boolean; + isEqual(other: Query): boolean; - get(options?: GetOptions): Promise>; + get(options?: GetOptions): Promise>; onSnapshot(observer: { - next?: (snapshot: QuerySnapshot) => void; + next?: (snapshot: QuerySnapshot) => void; error?: (error: FirestoreError) => void; complete?: () => void; }): () => void; onSnapshot( options: SnapshotListenOptions, observer: { - next?: (snapshot: QuerySnapshot) => void; + next?: (snapshot: QuerySnapshot) => void; error?: (error: FirestoreError) => void; complete?: () => void; } ): () => void; onSnapshot( - onNext: (snapshot: QuerySnapshot) => void, + onNext: (snapshot: QuerySnapshot) => void, onError?: (error: FirestoreError) => void, onCompletion?: () => void ): () => void; onSnapshot( options: SnapshotListenOptions, - onNext: (snapshot: QuerySnapshot) => void, + onNext: (snapshot: QuerySnapshot) => void, onError?: (error: FirestoreError) => void, onCompletion?: () => void ): () => void; @@ -409,51 +460,68 @@ export class Query { withConverter(converter: FirestoreDataConverter): Query; } -export class QuerySnapshot { +export class QuerySnapshot< + AppModelType = DocumentData, + DbModelType extends DocumentData = DocumentData +> { private constructor(); - readonly query: Query; + readonly query: Query; readonly metadata: SnapshotMetadata; - readonly docs: Array>; + readonly docs: Array>; readonly size: number; readonly empty: boolean; - docChanges(options?: SnapshotListenOptions): Array>; + docChanges( + options?: SnapshotListenOptions + ): Array>; forEach( - callback: (result: QueryDocumentSnapshot) => void, + callback: ( + result: QueryDocumentSnapshot + ) => void, thisArg?: any ): void; - isEqual(other: QuerySnapshot): boolean; + isEqual(other: QuerySnapshot): boolean; } export type DocumentChangeType = 'added' | 'removed' | 'modified'; -export interface DocumentChange { +export interface DocumentChange< + AppModelType = DocumentData, + DbModelType extends DocumentData = DocumentData +> { readonly type: DocumentChangeType; - readonly doc: QueryDocumentSnapshot; + readonly doc: QueryDocumentSnapshot; readonly oldIndex: number; readonly newIndex: number; } -export class CollectionReference extends Query { +export class CollectionReference< + AppModelType = DocumentData, + DbModelType extends DocumentData = DocumentData +> extends Query { private constructor(); readonly id: string; - readonly parent: DocumentReference | null; + readonly parent: DocumentReference | null; readonly path: string; - doc(documentPath?: string): DocumentReference; + doc(documentPath?: string): DocumentReference; - add(data: T): Promise>; + add( + data: AppModelType + ): Promise>; - isEqual(other: CollectionReference): boolean; + isEqual(other: CollectionReference): boolean; - withConverter(converter: null): CollectionReference; - withConverter( - converter: FirestoreDataConverter - ): CollectionReference; + withConverter( + converter: null + ): CollectionReference; + withConverter( + converter: FirestoreDataConverter + ): CollectionReference; } export class FieldValue {