Skip to content

Commit cca4735

Browse files
posvadconeybe
andauthored
types(firestore): make withConverter retro compatible (#7441)
* types(firestore): make `withConverter` retro compatible * style: format * changeset added * reference.ts: add default DocumentData parameter type for NewDbModelType to the other withConverter() functions * add tests * database.test.ts: move an inline comment to be above the appropriate line * yarn docgen devsite --------- Co-authored-by: Denver Coneybeare <[email protected]>
1 parent c95e0aa commit cca4735

File tree

11 files changed

+103
-18
lines changed

11 files changed

+103
-18
lines changed

.changeset/flat-cups-agree.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@firebase/firestore': minor
3+
'firebase': minor
4+
---
5+
6+
Added a default template type parameter to withConverter() functions to improve backwards compatibility with the v9 SDK

common/api-review/firestore-lite.api.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export class CollectionReference<AppModelType = DocumentData, DbModelType extend
8686
get parent(): DocumentReference<DocumentData, DocumentData> | null;
8787
get path(): string;
8888
readonly type = "collection";
89-
withConverter<NewAppModelType, NewDbModelType extends DocumentData>(converter: FirestoreDataConverter<NewAppModelType, NewDbModelType>): CollectionReference<NewAppModelType, NewDbModelType>;
89+
withConverter<NewAppModelType, NewDbModelType extends DocumentData = DocumentData>(converter: FirestoreDataConverter<NewAppModelType, NewDbModelType>): CollectionReference<NewAppModelType, NewDbModelType>;
9090
withConverter(converter: null): CollectionReference<DocumentData, DocumentData>;
9191
}
9292

@@ -126,7 +126,7 @@ export class DocumentReference<AppModelType = DocumentData, DbModelType extends
126126
get parent(): CollectionReference<AppModelType, DbModelType>;
127127
get path(): string;
128128
readonly type = "document";
129-
withConverter<NewAppModelType, NewDbModelType extends DocumentData>(converter: FirestoreDataConverter<NewAppModelType, NewDbModelType>): DocumentReference<NewAppModelType, NewDbModelType>;
129+
withConverter<NewAppModelType, NewDbModelType extends DocumentData = DocumentData>(converter: FirestoreDataConverter<NewAppModelType, NewDbModelType>): DocumentReference<NewAppModelType, NewDbModelType>;
130130
withConverter(converter: null): DocumentReference<DocumentData, DocumentData>;
131131
}
132132

@@ -270,7 +270,7 @@ export class Query<AppModelType = DocumentData, DbModelType extends DocumentData
270270
readonly firestore: Firestore;
271271
readonly type: 'query' | 'collection';
272272
withConverter(converter: null): Query<DocumentData, DocumentData>;
273-
withConverter<NewAppModelType, NewDbModelType extends DocumentData>(converter: FirestoreDataConverter<NewAppModelType, NewDbModelType>): Query<NewAppModelType, NewDbModelType>;
273+
withConverter<NewAppModelType, NewDbModelType extends DocumentData = DocumentData>(converter: FirestoreDataConverter<NewAppModelType, NewDbModelType>): Query<NewAppModelType, NewDbModelType>;
274274
}
275275

276276
// @public

common/api-review/firestore.api.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export class CollectionReference<AppModelType = DocumentData, DbModelType extend
9292
get parent(): DocumentReference<DocumentData, DocumentData> | null;
9393
get path(): string;
9494
readonly type = "collection";
95-
withConverter<NewAppModelType, NewDbModelType extends DocumentData>(converter: FirestoreDataConverter<NewAppModelType, NewDbModelType>): CollectionReference<NewAppModelType, NewDbModelType>;
95+
withConverter<NewAppModelType, NewDbModelType extends DocumentData = DocumentData>(converter: FirestoreDataConverter<NewAppModelType, NewDbModelType>): CollectionReference<NewAppModelType, NewDbModelType>;
9696
withConverter(converter: null): CollectionReference<DocumentData, DocumentData>;
9797
}
9898

@@ -152,7 +152,7 @@ export class DocumentReference<AppModelType = DocumentData, DbModelType extends
152152
get parent(): CollectionReference<AppModelType, DbModelType>;
153153
get path(): string;
154154
readonly type = "document";
155-
withConverter<NewAppModelType, NewDbModelType extends DocumentData>(converter: FirestoreDataConverter<NewAppModelType, NewDbModelType>): DocumentReference<NewAppModelType, NewDbModelType>;
155+
withConverter<NewAppModelType, NewDbModelType extends DocumentData = DocumentData>(converter: FirestoreDataConverter<NewAppModelType, NewDbModelType>): DocumentReference<NewAppModelType, NewDbModelType>;
156156
withConverter(converter: null): DocumentReference<DocumentData, DocumentData>;
157157
}
158158

@@ -525,7 +525,7 @@ export class Query<AppModelType = DocumentData, DbModelType extends DocumentData
525525
readonly firestore: Firestore;
526526
readonly type: 'query' | 'collection';
527527
withConverter(converter: null): Query<DocumentData, DocumentData>;
528-
withConverter<NewAppModelType, NewDbModelType extends DocumentData>(converter: FirestoreDataConverter<NewAppModelType, NewDbModelType>): Query<NewAppModelType, NewDbModelType>;
528+
withConverter<NewAppModelType, NewDbModelType extends DocumentData = DocumentData>(converter: FirestoreDataConverter<NewAppModelType, NewDbModelType>): Query<NewAppModelType, NewDbModelType>;
529529
}
530530

531531
// @public

docs-devsite/firestore_.collectionreference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ Applies a custom data converter to this `CollectionReference`<!-- -->, allowing
8282
<b>Signature:</b>
8383
8484
```typescript
85-
withConverter<NewAppModelType, NewDbModelType extends DocumentData>(converter: FirestoreDataConverter<NewAppModelType, NewDbModelType>): CollectionReference<NewAppModelType, NewDbModelType>;
85+
withConverter<NewAppModelType, NewDbModelType extends DocumentData = DocumentData>(converter: FirestoreDataConverter<NewAppModelType, NewDbModelType>): CollectionReference<NewAppModelType, NewDbModelType>;
8686
```
8787
8888
### Parameters

docs-devsite/firestore_.documentreference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ Applies a custom data converter to this `DocumentReference`<!-- -->, allowing yo
103103
<b>Signature:</b>
104104

105105
```typescript
106-
withConverter<NewAppModelType, NewDbModelType extends DocumentData>(converter: FirestoreDataConverter<NewAppModelType, NewDbModelType>): DocumentReference<NewAppModelType, NewDbModelType>;
106+
withConverter<NewAppModelType, NewDbModelType extends DocumentData = DocumentData>(converter: FirestoreDataConverter<NewAppModelType, NewDbModelType>): DocumentReference<NewAppModelType, NewDbModelType>;
107107
```
108108

109109
### Parameters

docs-devsite/firestore_.query.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ Applies a custom data converter to this query, allowing you to use your own cust
108108
<b>Signature:</b>
109109

110110
```typescript
111-
withConverter<NewAppModelType, NewDbModelType extends DocumentData>(converter: FirestoreDataConverter<NewAppModelType, NewDbModelType>): Query<NewAppModelType, NewDbModelType>;
111+
withConverter<NewAppModelType, NewDbModelType extends DocumentData = DocumentData>(converter: FirestoreDataConverter<NewAppModelType, NewDbModelType>): Query<NewAppModelType, NewDbModelType>;
112112
```
113113

114114
### Parameters

docs-devsite/firestore_lite.collectionreference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ Applies a custom data converter to this `CollectionReference`<!-- -->, allowing
8282
<b>Signature:</b>
8383
8484
```typescript
85-
withConverter<NewAppModelType, NewDbModelType extends DocumentData>(converter: FirestoreDataConverter<NewAppModelType, NewDbModelType>): CollectionReference<NewAppModelType, NewDbModelType>;
85+
withConverter<NewAppModelType, NewDbModelType extends DocumentData = DocumentData>(converter: FirestoreDataConverter<NewAppModelType, NewDbModelType>): CollectionReference<NewAppModelType, NewDbModelType>;
8686
```
8787
8888
### Parameters

docs-devsite/firestore_lite.documentreference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ Applies a custom data converter to this `DocumentReference`<!-- -->, allowing yo
103103
<b>Signature:</b>
104104

105105
```typescript
106-
withConverter<NewAppModelType, NewDbModelType extends DocumentData>(converter: FirestoreDataConverter<NewAppModelType, NewDbModelType>): DocumentReference<NewAppModelType, NewDbModelType>;
106+
withConverter<NewAppModelType, NewDbModelType extends DocumentData = DocumentData>(converter: FirestoreDataConverter<NewAppModelType, NewDbModelType>): DocumentReference<NewAppModelType, NewDbModelType>;
107107
```
108108

109109
### Parameters

docs-devsite/firestore_lite.query.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ Applies a custom data converter to this query, allowing you to use your own cust
108108
<b>Signature:</b>
109109

110110
```typescript
111-
withConverter<NewAppModelType, NewDbModelType extends DocumentData>(converter: FirestoreDataConverter<NewAppModelType, NewDbModelType>): Query<NewAppModelType, NewDbModelType>;
111+
withConverter<NewAppModelType, NewDbModelType extends DocumentData = DocumentData>(converter: FirestoreDataConverter<NewAppModelType, NewDbModelType>): Query<NewAppModelType, NewDbModelType>;
112112
```
113113

114114
### Parameters

packages/firestore/src/lite-api/reference.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,16 @@ export class Query<
159159
* @param converter - Converts objects to and from Firestore.
160160
* @returns A `Query` that uses the provided converter.
161161
*/
162-
withConverter<NewAppModelType, NewDbModelType extends DocumentData>(
162+
withConverter<
163+
NewAppModelType,
164+
NewDbModelType extends DocumentData = DocumentData
165+
>(
163166
converter: FirestoreDataConverter<NewAppModelType, NewDbModelType>
164167
): Query<NewAppModelType, NewDbModelType>;
165-
withConverter<NewAppModelType, NewDbModelType extends DocumentData>(
168+
withConverter<
169+
NewAppModelType,
170+
NewDbModelType extends DocumentData = DocumentData
171+
>(
166172
converter: FirestoreDataConverter<NewAppModelType, NewDbModelType> | null
167173
): Query<NewAppModelType, NewDbModelType> {
168174
return new Query<NewAppModelType, NewDbModelType>(
@@ -246,7 +252,10 @@ export class DocumentReference<
246252
* @param converter - Converts objects to and from Firestore.
247253
* @returns A `DocumentReference` that uses the provided converter.
248254
*/
249-
withConverter<NewAppModelType, NewDbModelType extends DocumentData>(
255+
withConverter<
256+
NewAppModelType,
257+
NewDbModelType extends DocumentData = DocumentData
258+
>(
250259
converter: FirestoreDataConverter<NewAppModelType, NewDbModelType>
251260
): DocumentReference<NewAppModelType, NewDbModelType>;
252261
/**
@@ -257,7 +266,10 @@ export class DocumentReference<
257266
* use a converter.
258267
*/
259268
withConverter(converter: null): DocumentReference<DocumentData, DocumentData>;
260-
withConverter<NewAppModelType, NewDbModelType extends DocumentData>(
269+
withConverter<
270+
NewAppModelType,
271+
NewDbModelType extends DocumentData = DocumentData
272+
>(
261273
converter: FirestoreDataConverter<NewAppModelType, NewDbModelType> | null
262274
): DocumentReference<NewAppModelType, NewDbModelType> {
263275
return new DocumentReference<NewAppModelType, NewDbModelType>(
@@ -328,7 +340,10 @@ export class CollectionReference<
328340
* @param converter - Converts objects to and from Firestore.
329341
* @returns A `CollectionReference` that uses the provided converter.
330342
*/
331-
withConverter<NewAppModelType, NewDbModelType extends DocumentData>(
343+
withConverter<
344+
NewAppModelType,
345+
NewDbModelType extends DocumentData = DocumentData
346+
>(
332347
converter: FirestoreDataConverter<NewAppModelType, NewDbModelType>
333348
): CollectionReference<NewAppModelType, NewDbModelType>;
334349
/**
@@ -341,7 +356,10 @@ export class CollectionReference<
341356
withConverter(
342357
converter: null
343358
): CollectionReference<DocumentData, DocumentData>;
344-
withConverter<NewAppModelType, NewDbModelType extends DocumentData>(
359+
withConverter<
360+
NewAppModelType,
361+
NewDbModelType extends DocumentData = DocumentData
362+
>(
345363
converter: FirestoreDataConverter<NewAppModelType, NewDbModelType> | null
346364
): CollectionReference<NewAppModelType, NewDbModelType> {
347365
return new CollectionReference<NewAppModelType, NewDbModelType>(

packages/firestore/test/integration/api/database.test.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1813,6 +1813,67 @@ apiDescribe('Database', persistence => {
18131813
expect(refEqual(untypedDocRef, ref)).to.be.true;
18141814
});
18151815
});
1816+
1817+
it('DocumentReference.withConverter() default DbModelType', () => {
1818+
const converter = {
1819+
toFirestore: (value: number) => {
1820+
return { value };
1821+
},
1822+
fromFirestore: (snapshot: QueryDocumentSnapshot) => {
1823+
return snapshot.data()['value'] as number;
1824+
}
1825+
};
1826+
return withTestDoc(persistence, async docRef => {
1827+
// The line below should compile since the DbModelType type parameter of
1828+
// DocumentReference.withConverter() has a default value.
1829+
const typedDocRef = docRef.withConverter<number>(converter);
1830+
await setDoc(typedDocRef, 42);
1831+
const snapshot = await getDoc(typedDocRef);
1832+
expect(snapshot.data()).to.equal(42);
1833+
});
1834+
});
1835+
1836+
it('CollectionReference.withConverter() default DbModelType', () => {
1837+
const converter = {
1838+
toFirestore: (value: number) => {
1839+
return { value };
1840+
},
1841+
fromFirestore: (snapshot: QueryDocumentSnapshot) => {
1842+
return snapshot.data()['value'] as number;
1843+
}
1844+
};
1845+
const testDocs = { doc1: { value: 42 } };
1846+
return withTestCollection(persistence, testDocs, async collectionRef => {
1847+
// The line below should compile since the DbModelType type parameter of
1848+
// CollectionReference.withConverter() has a default value.
1849+
const typedCollectionRef =
1850+
collectionRef.withConverter<number>(converter);
1851+
const snapshot = await getDocs(typedCollectionRef);
1852+
expect(snapshot.size).to.equal(1);
1853+
expect(snapshot.docs[0].data()).to.equal(42);
1854+
});
1855+
});
1856+
1857+
it('Query.withConverter() default DbModelType', () => {
1858+
const converter = {
1859+
toFirestore: (value: number) => {
1860+
return { value };
1861+
},
1862+
fromFirestore: (snapshot: QueryDocumentSnapshot) => {
1863+
return snapshot.data()['value'] as number;
1864+
}
1865+
};
1866+
const testDocs = { doc1: { value: 42 } };
1867+
return withTestCollection(persistence, testDocs, async collectionRef => {
1868+
const query_ = query(collectionRef, where('value', '==', 42));
1869+
// The line below should compile since the DbModelType type parameter of
1870+
// Query.withConverter() has a default value.
1871+
const typedQuery = query_.withConverter<number>(converter);
1872+
const snapshot = await getDocs(typedQuery);
1873+
expect(snapshot.size).to.equal(1);
1874+
expect(snapshot.docs[0].data()).to.equal(42);
1875+
});
1876+
});
18161877
});
18171878

18181879
// TODO(b/196858864): This test regularly times out on CI.

0 commit comments

Comments
 (0)