Skip to content

Commit 0afadef

Browse files
tom-andersengcf-owl-bot[bot]sofisl
authored
feat!: Fix the UpdateData incorrect parameter type issue (#1887)
Fix the UpdateData incorrect parameter type issue (#1887) --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: sofisl <[email protected]>
1 parent 2e98caa commit 0afadef

19 files changed

+1166
-571
lines changed

dev/conformance/runner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ const convertInput = {
193193
}
194194
return args;
195195
},
196-
snapshot: (snapshot: ConformanceProto) => {
196+
snapshot: (snapshot: ConformanceProto): QuerySnapshot => {
197197
const docs: QueryDocumentSnapshot[] = [];
198198
const changes: DocumentChange[] = [];
199199
const readTime = Timestamp.fromProto(snapshot.readTime);

dev/src/bulk-writer.ts

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import {
2828
MAX_RETRY_ATTEMPTS,
2929
} from './backoff';
3030
import {RateLimiter} from './rate-limiter';
31-
import {DocumentReference} from './reference';
3231
import {Timestamp} from './timestamp';
3332
import {
3433
Deferred,
@@ -337,7 +336,7 @@ export class BulkWriterError extends Error {
337336
readonly message: string,
338337

339338
/** The document reference the operation was performed on. */
340-
readonly documentRef: firestore.DocumentReference<unknown>,
339+
readonly documentRef: firestore.DocumentReference<any, any>,
341340

342341
/** The type of operation performed. */
343342
readonly operationType: 'create' | 'set' | 'update' | 'delete',
@@ -576,9 +575,9 @@ export class BulkWriter {
576575
* });
577576
* ```
578577
*/
579-
create<T>(
580-
documentRef: firestore.DocumentReference<T>,
581-
data: firestore.WithFieldValue<T>
578+
create<AppModelType, DbModelType extends firestore.DocumentData>(
579+
documentRef: firestore.DocumentReference<AppModelType, DbModelType>,
580+
data: firestore.WithFieldValue<AppModelType>
582581
): Promise<WriteResult> {
583582
this._verifyNotClosed();
584583
return this._enqueue(documentRef, 'create', bulkCommitBatch =>
@@ -616,8 +615,8 @@ export class BulkWriter {
616615
* });
617616
* ```
618617
*/
619-
delete<T>(
620-
documentRef: firestore.DocumentReference<T>,
618+
delete<AppModelType, DbModelType extends firestore.DocumentData>(
619+
documentRef: firestore.DocumentReference<AppModelType, DbModelType>,
621620
precondition?: firestore.Precondition
622621
): Promise<WriteResult> {
623622
this._verifyNotClosed();
@@ -626,14 +625,14 @@ export class BulkWriter {
626625
);
627626
}
628627

629-
set<T>(
630-
documentRef: firestore.DocumentReference<T>,
631-
data: Partial<T>,
628+
set<AppModelType, DbModelType extends firestore.DocumentData>(
629+
documentRef: firestore.DocumentReference<AppModelType, DbModelType>,
630+
data: Partial<AppModelType>,
632631
options: firestore.SetOptions
633632
): Promise<WriteResult>;
634-
set<T>(
635-
documentRef: firestore.DocumentReference<T>,
636-
data: T
633+
set<AppModelType, DbModelType extends firestore.DocumentData>(
634+
documentRef: firestore.DocumentReference<AppModelType, DbModelType>,
635+
data: AppModelType
637636
): Promise<WriteResult>;
638637
/**
639638
* Write to the document referred to by the provided
@@ -675,9 +674,9 @@ export class BulkWriter {
675674
* });
676675
* ```
677676
*/
678-
set<T>(
679-
documentRef: firestore.DocumentReference<T>,
680-
data: firestore.PartialWithFieldValue<T>,
677+
set<AppModelType, DbModelType extends firestore.DocumentData>(
678+
documentRef: firestore.DocumentReference<AppModelType, DbModelType>,
679+
data: firestore.PartialWithFieldValue<AppModelType>,
681680
options?: firestore.SetOptions
682681
): Promise<WriteResult> {
683682
this._verifyNotClosed();
@@ -687,7 +686,7 @@ export class BulkWriter {
687686
} else {
688687
return bulkCommitBatch.set(
689688
documentRef,
690-
data as firestore.WithFieldValue<T>
689+
data as firestore.WithFieldValue<AppModelType>
691690
);
692691
}
693692
});
@@ -737,9 +736,9 @@ export class BulkWriter {
737736
* });
738737
* ```
739738
*/
740-
update<T>(
741-
documentRef: firestore.DocumentReference<T>,
742-
dataOrField: firestore.UpdateData<T> | string | FieldPath,
739+
update<AppModelType, DbModelType extends firestore.DocumentData>(
740+
documentRef: firestore.DocumentReference<AppModelType, DbModelType>,
741+
dataOrField: firestore.UpdateData<DbModelType> | string | FieldPath,
743742
...preconditionOrValues: Array<
744743
{lastUpdateTime?: Timestamp} | unknown | string | FieldPath
745744
>
@@ -783,7 +782,7 @@ export class BulkWriter {
783782
*/
784783
onWriteResult(
785784
successCallback: (
786-
documentRef: firestore.DocumentReference<unknown>,
785+
documentRef: firestore.DocumentReference<any, any>,
787786
result: WriteResult
788787
) => void
789788
): void {

dev/src/collection-group.ts

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,20 @@ import {compareArrays} from './order';
3535
*
3636
* @class CollectionGroup
3737
*/
38-
export class CollectionGroup<T = firestore.DocumentData>
39-
extends Query<T>
40-
implements firestore.CollectionGroup<T>
38+
export class CollectionGroup<
39+
AppModelType = firestore.DocumentData,
40+
DbModelType extends firestore.DocumentData = firestore.DocumentData
41+
>
42+
extends Query<AppModelType, DbModelType>
43+
implements firestore.CollectionGroup<AppModelType, DbModelType>
4144
{
4245
/** @private */
4346
constructor(
4447
firestore: Firestore,
4548
collectionId: string,
46-
converter: firestore.FirestoreDataConverter<T> | undefined
49+
converter:
50+
| firestore.FirestoreDataConverter<AppModelType, DbModelType>
51+
| undefined
4752
) {
4853
super(
4954
firestore,
@@ -74,7 +79,7 @@ export class CollectionGroup<T = firestore.DocumentData>
7479
*/
7580
async *getPartitions(
7681
desiredPartitionCount: number
77-
): AsyncIterable<QueryPartition<T>> {
82+
): AsyncIterable<QueryPartition<AppModelType, DbModelType>> {
7883
validateInteger('desiredPartitionCount', desiredPartitionCount, {
7984
minValue: 1,
8085
});
@@ -141,7 +146,8 @@ export class CollectionGroup<T = firestore.DocumentData>
141146
* Applies a custom data converter to this `CollectionGroup`, allowing you
142147
* to use your own custom model objects with Firestore. When you call get()
143148
* on the returned `CollectionGroup`, the provided converter will convert
144-
* between Firestore data and your custom type U.
149+
* between Firestore data of type `NewDbModelType` and your custom type
150+
* `NewAppModelType`.
145151
*
146152
* Using the converter allows you to specify generic type arguments when
147153
* storing and retrieving objects from Firestore.
@@ -185,17 +191,26 @@ export class CollectionGroup<T = firestore.DocumentData>
185191
* ```
186192
* @param {FirestoreDataConverter | null} converter Converts objects to and
187193
* from Firestore. Passing in `null` removes the current converter.
188-
* @return {CollectionGroup} A `CollectionGroup<U>` that uses the provided
194+
* @return {CollectionGroup} A `CollectionGroup` that uses the provided
189195
* converter.
190196
*/
191-
withConverter(converter: null): CollectionGroup<firestore.DocumentData>;
192-
withConverter<U>(
193-
converter: firestore.FirestoreDataConverter<U>
194-
): CollectionGroup<U>;
195-
withConverter<U>(
196-
converter: firestore.FirestoreDataConverter<U> | null
197-
): CollectionGroup<U> {
198-
return new CollectionGroup<U>(
197+
withConverter(converter: null): CollectionGroup;
198+
withConverter<
199+
NewAppModelType,
200+
NewDbModelType extends firestore.DocumentData = firestore.DocumentData
201+
>(
202+
converter: firestore.FirestoreDataConverter<NewAppModelType, NewDbModelType>
203+
): CollectionGroup<NewAppModelType, NewDbModelType>;
204+
withConverter<
205+
NewAppModelType,
206+
NewDbModelType extends firestore.DocumentData = firestore.DocumentData
207+
>(
208+
converter: firestore.FirestoreDataConverter<
209+
NewAppModelType,
210+
NewDbModelType
211+
> | null
212+
): CollectionGroup<NewAppModelType, NewDbModelType> {
213+
return new CollectionGroup<NewAppModelType, NewDbModelType>(
199214
this.firestore,
200215
this._queryOptions.collectionId,
201216
converter ?? defaultConverter()

dev/src/document-change.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ export type DocumentChangeType = 'added' | 'removed' | 'modified';
2626
*
2727
* @class DocumentChange
2828
*/
29-
export class DocumentChange<T = firestore.DocumentData>
30-
implements firestore.DocumentChange<T>
29+
export class DocumentChange<
30+
AppModelType = firestore.DocumentData,
31+
DbModelType extends firestore.DocumentData = firestore.DocumentData
32+
> implements firestore.DocumentChange<AppModelType, DbModelType>
3133
{
3234
private readonly _type: DocumentChangeType;
33-
private readonly _document: QueryDocumentSnapshot<T>;
35+
private readonly _document: QueryDocumentSnapshot<AppModelType, DbModelType>;
3436
private readonly _oldIndex: number;
3537
private readonly _newIndex: number;
3638

@@ -46,7 +48,7 @@ export class DocumentChange<T = firestore.DocumentData>
4648
*/
4749
constructor(
4850
type: DocumentChangeType,
49-
document: QueryDocumentSnapshot<T>,
51+
document: QueryDocumentSnapshot<AppModelType, DbModelType>,
5052
oldIndex: number,
5153
newIndex: number
5254
) {
@@ -103,7 +105,7 @@ export class DocumentChange<T = firestore.DocumentData>
103105
* unsubscribe();
104106
* ```
105107
*/
106-
get doc(): QueryDocumentSnapshot<T> {
108+
get doc(): QueryDocumentSnapshot<AppModelType, DbModelType> {
107109
return this._document;
108110
}
109111

@@ -181,7 +183,7 @@ export class DocumentChange<T = firestore.DocumentData>
181183
* @param {*} other The value to compare against.
182184
* @return true if this `DocumentChange` is equal to the provided value.
183185
*/
184-
isEqual(other: firestore.DocumentChange<T>): boolean {
186+
isEqual(other: firestore.DocumentChange<AppModelType, DbModelType>): boolean {
185187
if (this === other) {
186188
return true;
187189
}

dev/src/document-reader.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import api = google.firestore.v1;
3131
* @private
3232
* @internal
3333
*/
34-
export class DocumentReader<T> {
34+
export class DocumentReader<AppModelType, DbModelType extends DocumentData> {
3535
/** An optional field mask to apply to this read. */
3636
fieldMask?: FieldPath[];
3737
/** An optional transaction ID to use for this read. */
@@ -49,7 +49,7 @@ export class DocumentReader<T> {
4949
*/
5050
constructor(
5151
private firestore: Firestore,
52-
private allDocuments: Array<DocumentReference<T>>
52+
private allDocuments: Array<DocumentReference<AppModelType, DbModelType>>
5353
) {
5454
for (const docRef of this.allDocuments) {
5555
this.outstandingDocuments.add(docRef.formattedName);
@@ -61,20 +61,23 @@ export class DocumentReader<T> {
6161
*
6262
* @param requestTag A unique client-assigned identifier for this request.
6363
*/
64-
async get(requestTag: string): Promise<Array<DocumentSnapshot<T>>> {
64+
async get(
65+
requestTag: string
66+
): Promise<Array<DocumentSnapshot<AppModelType, DbModelType>>> {
6567
await this.fetchDocuments(requestTag);
6668

6769
// BatchGetDocuments doesn't preserve document order. We use the request
6870
// order to sort the resulting documents.
69-
const orderedDocuments: Array<DocumentSnapshot<T>> = [];
71+
const orderedDocuments: Array<DocumentSnapshot<AppModelType, DbModelType>> =
72+
[];
7073

7174
for (const docRef of this.allDocuments) {
7275
const document = this.retrievedDocuments.get(docRef.formattedName);
7376
if (document !== undefined) {
7477
// Recreate the DocumentSnapshot with the DocumentReference
7578
// containing the original converter.
7679
const finalDoc = new DocumentSnapshotBuilder(
77-
docRef as DocumentReference<T>
80+
docRef as DocumentReference<AppModelType, DbModelType>
7881
);
7982
finalDoc.fieldsProto = document._fieldsProto;
8083
finalDoc.readTime = document.readTime;

0 commit comments

Comments
 (0)