Skip to content

Commit

Permalink
feat!: Fix the UpdateData incorrect parameter type issue (#1887)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
3 people committed Sep 27, 2023
1 parent 2e98caa commit 0afadef
Show file tree
Hide file tree
Showing 19 changed files with 1,166 additions and 571 deletions.
2 changes: 1 addition & 1 deletion dev/conformance/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ const convertInput = {
}
return args;
},
snapshot: (snapshot: ConformanceProto) => {
snapshot: (snapshot: ConformanceProto): QuerySnapshot => {
const docs: QueryDocumentSnapshot[] = [];
const changes: DocumentChange[] = [];
const readTime = Timestamp.fromProto(snapshot.readTime);
Expand Down
41 changes: 20 additions & 21 deletions dev/src/bulk-writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
MAX_RETRY_ATTEMPTS,
} from './backoff';
import {RateLimiter} from './rate-limiter';
import {DocumentReference} from './reference';
import {Timestamp} from './timestamp';
import {
Deferred,
Expand Down Expand Up @@ -337,7 +336,7 @@ export class BulkWriterError extends Error {
readonly message: string,

/** The document reference the operation was performed on. */
readonly documentRef: firestore.DocumentReference<unknown>,
readonly documentRef: firestore.DocumentReference<any, any>,

Check warning on line 339 in dev/src/bulk-writer.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Check warning on line 339 in dev/src/bulk-writer.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

/** The type of operation performed. */
readonly operationType: 'create' | 'set' | 'update' | 'delete',
Expand Down Expand Up @@ -576,9 +575,9 @@ export class BulkWriter {
* });
* ```
*/
create<T>(
documentRef: firestore.DocumentReference<T>,
data: firestore.WithFieldValue<T>
create<AppModelType, DbModelType extends firestore.DocumentData>(
documentRef: firestore.DocumentReference<AppModelType, DbModelType>,
data: firestore.WithFieldValue<AppModelType>
): Promise<WriteResult> {
this._verifyNotClosed();
return this._enqueue(documentRef, 'create', bulkCommitBatch =>
Expand Down Expand Up @@ -616,8 +615,8 @@ export class BulkWriter {
* });
* ```
*/
delete<T>(
documentRef: firestore.DocumentReference<T>,
delete<AppModelType, DbModelType extends firestore.DocumentData>(
documentRef: firestore.DocumentReference<AppModelType, DbModelType>,
precondition?: firestore.Precondition
): Promise<WriteResult> {
this._verifyNotClosed();
Expand All @@ -626,14 +625,14 @@ export class BulkWriter {
);
}

set<T>(
documentRef: firestore.DocumentReference<T>,
data: Partial<T>,
set<AppModelType, DbModelType extends firestore.DocumentData>(
documentRef: firestore.DocumentReference<AppModelType, DbModelType>,
data: Partial<AppModelType>,
options: firestore.SetOptions
): Promise<WriteResult>;
set<T>(
documentRef: firestore.DocumentReference<T>,
data: T
set<AppModelType, DbModelType extends firestore.DocumentData>(
documentRef: firestore.DocumentReference<AppModelType, DbModelType>,
data: AppModelType
): Promise<WriteResult>;
/**
* Write to the document referred to by the provided
Expand Down Expand Up @@ -675,9 +674,9 @@ export class BulkWriter {
* });
* ```
*/
set<T>(
documentRef: firestore.DocumentReference<T>,
data: firestore.PartialWithFieldValue<T>,
set<AppModelType, DbModelType extends firestore.DocumentData>(
documentRef: firestore.DocumentReference<AppModelType, DbModelType>,
data: firestore.PartialWithFieldValue<AppModelType>,
options?: firestore.SetOptions
): Promise<WriteResult> {
this._verifyNotClosed();
Expand All @@ -687,7 +686,7 @@ export class BulkWriter {
} else {
return bulkCommitBatch.set(
documentRef,
data as firestore.WithFieldValue<T>
data as firestore.WithFieldValue<AppModelType>
);
}
});
Expand Down Expand Up @@ -737,9 +736,9 @@ export class BulkWriter {
* });
* ```
*/
update<T>(
documentRef: firestore.DocumentReference<T>,
dataOrField: firestore.UpdateData<T> | string | FieldPath,
update<AppModelType, DbModelType extends firestore.DocumentData>(
documentRef: firestore.DocumentReference<AppModelType, DbModelType>,
dataOrField: firestore.UpdateData<DbModelType> | string | FieldPath,
...preconditionOrValues: Array<
{lastUpdateTime?: Timestamp} | unknown | string | FieldPath
>
Expand Down Expand Up @@ -783,7 +782,7 @@ export class BulkWriter {
*/
onWriteResult(
successCallback: (
documentRef: firestore.DocumentReference<unknown>,
documentRef: firestore.DocumentReference<any, any>,

Check warning on line 785 in dev/src/bulk-writer.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Check warning on line 785 in dev/src/bulk-writer.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
result: WriteResult
) => void
): void {
Expand Down
45 changes: 30 additions & 15 deletions dev/src/collection-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,20 @@ import {compareArrays} from './order';
*
* @class CollectionGroup
*/
export class CollectionGroup<T = firestore.DocumentData>
extends Query<T>
implements firestore.CollectionGroup<T>
export class CollectionGroup<
AppModelType = firestore.DocumentData,
DbModelType extends firestore.DocumentData = firestore.DocumentData
>
extends Query<AppModelType, DbModelType>
implements firestore.CollectionGroup<AppModelType, DbModelType>
{
/** @private */
constructor(
firestore: Firestore,
collectionId: string,
converter: firestore.FirestoreDataConverter<T> | undefined
converter:
| firestore.FirestoreDataConverter<AppModelType, DbModelType>
| undefined
) {
super(
firestore,
Expand Down Expand Up @@ -74,7 +79,7 @@ export class CollectionGroup<T = firestore.DocumentData>
*/
async *getPartitions(
desiredPartitionCount: number
): AsyncIterable<QueryPartition<T>> {
): AsyncIterable<QueryPartition<AppModelType, DbModelType>> {
validateInteger('desiredPartitionCount', desiredPartitionCount, {
minValue: 1,
});
Expand Down Expand Up @@ -141,7 +146,8 @@ export class CollectionGroup<T = firestore.DocumentData>
* Applies a custom data converter to this `CollectionGroup`, allowing you
* to use your own custom model objects with Firestore. When you call get()
* on the returned `CollectionGroup`, the provided converter will convert
* between Firestore data and your custom type U.
* between Firestore data of type `NewDbModelType` and your custom type
* `NewAppModelType`.
*
* Using the converter allows you to specify generic type arguments when
* storing and retrieving objects from Firestore.
Expand Down Expand Up @@ -185,17 +191,26 @@ export class CollectionGroup<T = firestore.DocumentData>
* ```
* @param {FirestoreDataConverter | null} converter Converts objects to and
* from Firestore. Passing in `null` removes the current converter.
* @return {CollectionGroup} A `CollectionGroup<U>` that uses the provided
* @return {CollectionGroup} A `CollectionGroup` that uses the provided
* converter.
*/
withConverter(converter: null): CollectionGroup<firestore.DocumentData>;
withConverter<U>(
converter: firestore.FirestoreDataConverter<U>
): CollectionGroup<U>;
withConverter<U>(
converter: firestore.FirestoreDataConverter<U> | null
): CollectionGroup<U> {
return new CollectionGroup<U>(
withConverter(converter: null): CollectionGroup;
withConverter<
NewAppModelType,
NewDbModelType extends firestore.DocumentData = firestore.DocumentData
>(
converter: firestore.FirestoreDataConverter<NewAppModelType, NewDbModelType>
): CollectionGroup<NewAppModelType, NewDbModelType>;
withConverter<
NewAppModelType,
NewDbModelType extends firestore.DocumentData = firestore.DocumentData
>(
converter: firestore.FirestoreDataConverter<
NewAppModelType,
NewDbModelType
> | null
): CollectionGroup<NewAppModelType, NewDbModelType> {
return new CollectionGroup<NewAppModelType, NewDbModelType>(
this.firestore,
this._queryOptions.collectionId,
converter ?? defaultConverter()
Expand Down
14 changes: 8 additions & 6 deletions dev/src/document-change.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ export type DocumentChangeType = 'added' | 'removed' | 'modified';
*
* @class DocumentChange
*/
export class DocumentChange<T = firestore.DocumentData>
implements firestore.DocumentChange<T>
export class DocumentChange<
AppModelType = firestore.DocumentData,
DbModelType extends firestore.DocumentData = firestore.DocumentData
> implements firestore.DocumentChange<AppModelType, DbModelType>
{
private readonly _type: DocumentChangeType;
private readonly _document: QueryDocumentSnapshot<T>;
private readonly _document: QueryDocumentSnapshot<AppModelType, DbModelType>;
private readonly _oldIndex: number;
private readonly _newIndex: number;

Expand All @@ -46,7 +48,7 @@ export class DocumentChange<T = firestore.DocumentData>
*/
constructor(
type: DocumentChangeType,
document: QueryDocumentSnapshot<T>,
document: QueryDocumentSnapshot<AppModelType, DbModelType>,
oldIndex: number,
newIndex: number
) {
Expand Down Expand Up @@ -103,7 +105,7 @@ export class DocumentChange<T = firestore.DocumentData>
* unsubscribe();
* ```
*/
get doc(): QueryDocumentSnapshot<T> {
get doc(): QueryDocumentSnapshot<AppModelType, DbModelType> {
return this._document;
}

Expand Down Expand Up @@ -181,7 +183,7 @@ export class DocumentChange<T = firestore.DocumentData>
* @param {*} other The value to compare against.
* @return true if this `DocumentChange` is equal to the provided value.
*/
isEqual(other: firestore.DocumentChange<T>): boolean {
isEqual(other: firestore.DocumentChange<AppModelType, DbModelType>): boolean {
if (this === other) {
return true;
}
Expand Down
13 changes: 8 additions & 5 deletions dev/src/document-reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import api = google.firestore.v1;
* @private
* @internal
*/
export class DocumentReader<T> {
export class DocumentReader<AppModelType, DbModelType extends DocumentData> {
/** An optional field mask to apply to this read. */
fieldMask?: FieldPath[];
/** An optional transaction ID to use for this read. */
Expand All @@ -49,7 +49,7 @@ export class DocumentReader<T> {
*/
constructor(
private firestore: Firestore,
private allDocuments: Array<DocumentReference<T>>
private allDocuments: Array<DocumentReference<AppModelType, DbModelType>>
) {
for (const docRef of this.allDocuments) {
this.outstandingDocuments.add(docRef.formattedName);
Expand All @@ -61,20 +61,23 @@ export class DocumentReader<T> {
*
* @param requestTag A unique client-assigned identifier for this request.
*/
async get(requestTag: string): Promise<Array<DocumentSnapshot<T>>> {
async get(
requestTag: string
): Promise<Array<DocumentSnapshot<AppModelType, DbModelType>>> {
await this.fetchDocuments(requestTag);

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

for (const docRef of this.allDocuments) {
const document = this.retrievedDocuments.get(docRef.formattedName);
if (document !== undefined) {
// Recreate the DocumentSnapshot with the DocumentReference
// containing the original converter.
const finalDoc = new DocumentSnapshotBuilder(
docRef as DocumentReference<T>
docRef as DocumentReference<AppModelType, DbModelType>
);
finalDoc.fieldsProto = document._fieldsProto;
finalDoc.readTime = document.readTime;
Expand Down
Loading

0 comments on commit 0afadef

Please sign in to comment.