Skip to content

Commit 497c6d1

Browse files
SelaseKayMichaelVerdonmikehardy
authored
fix(firestore): type definitions (#8378)
--------- Co-authored-by: MichaelVerdon <[email protected]> Co-authored-by: Mike Hardy <[email protected]>
1 parent ed0f95a commit 497c6d1

File tree

8 files changed

+161
-91
lines changed

8 files changed

+161
-91
lines changed

packages/firestore/__tests__/firestore.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ import firestore, {
7777
deleteAllPersistentCacheIndexes,
7878
disablePersistentCacheIndexAutoCreation,
7979
enablePersistentCacheIndexAutoCreation,
80+
onSnapshotsInSync,
81+
documentId,
8082
} from '../lib';
8183

8284
const COLLECTION = 'firestore';
@@ -736,6 +738,14 @@ describe('Firestore', function () {
736738
it('`sum` is properly exposed to end user', function () {
737739
expect(sum).toBeDefined();
738740
});
741+
742+
it('`onSnapshotsInSync` is properly exposed to end user', function () {
743+
expect(onSnapshotsInSync).toBeDefined();
744+
});
745+
746+
it('`documentId` is properly exposed to end user', function () {
747+
expect(documentId).toBeDefined();
748+
});
739749
});
740750

741751
describe('test `console.warn` is called for RNFB v8 API & not called for v9 API', function () {

packages/firestore/lib/modular/FieldPath.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,10 @@ export declare class FieldPath {
1111

1212
isEqual(other: FieldPath): boolean;
1313
}
14+
15+
/**
16+
* Returns a special sentinel FieldPath to refer to the ID of a document
17+
* It can be used in queries to sort or filter by the document ID
18+
*/
19+
20+
export declare function documentId(): FieldPath;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
import FirestoreFieldPath from '../FirestoreFieldPath';
22

33
export const FieldPath = FirestoreFieldPath;
4+
5+
export function documentId() {
6+
return FieldPath.documentId();
7+
}

packages/firestore/lib/modular/index.d.ts

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,11 @@ export function doc(
185185
* a document.
186186
* @returns The `DocumentReference` instance.
187187
*/
188-
export function doc<T>(
189-
reference: CollectionReference<T>,
188+
export declare function doc<AppModelType, DbModelType extends DocumentData>(
189+
reference: CollectionReference<AppModelType, DbModelType>,
190190
path?: string,
191191
...pathSegments: string[]
192-
): DocumentReference<T>;
192+
): DocumentReference<AppModelType, DbModelType>;
193193

194194
/**
195195
* Gets a `DocumentReference` instance that refers to a document within
@@ -203,11 +203,11 @@ export function doc<T>(
203203
* a document.
204204
* @returns The `DocumentReference` instance.
205205
*/
206-
export function doc(
207-
reference: DocumentReference<unknown>,
206+
export declare function doc<AppModelType, DbModelType extends DocumentData>(
207+
reference: DocumentReference<AppModelType, DbModelType>,
208208
path: string,
209209
...pathSegments: string[]
210-
): DocumentReference<DocumentData>;
210+
): DocumentReference<DocumentData, DocumentData>;
211211

212212
export function doc<T>(
213213
parent: Firestore | CollectionReference<T> | DocumentReference<unknown>,
@@ -231,7 +231,7 @@ export function collection(
231231
firestore: Firestore,
232232
path: string,
233233
...pathSegments: string[]
234-
): CollectionReference<DocumentData>;
234+
): CollectionReference<DocumentData, DocumentData>;
235235

236236
/**
237237
* Gets a `CollectionReference` instance that refers to a subcollection of
@@ -245,11 +245,29 @@ export function collection(
245245
* to a collection.
246246
* @returns The `CollectionReference` instance.
247247
*/
248-
export function collection(
249-
reference: CollectionReference<unknown>,
248+
export declare function collection<AppModelType, DbModelType extends DocumentData>(
249+
reference: CollectionReference<AppModelType, DbModelType>,
250250
path: string,
251251
...pathSegments: string[]
252-
): CollectionReference<DocumentData>;
252+
): CollectionReference<DocumentData, DocumentData>;
253+
254+
/**
255+
* Gets a `CollectionReference` instance that refers to a subcollection of
256+
* `reference` at the specified relative path.
257+
*
258+
* @param reference - A reference to a document.
259+
* @param path - A slash-separated path to a collection.
260+
* @param pathSegments - Additional path segments to apply relative to the first
261+
* argument.
262+
* @throws If the final path has an even number of segments and does not point
263+
* to a collection.
264+
* @returns The `CollectionReference` instance.
265+
*/
266+
export declare function collection<AppModelType, DbModelType extends DocumentData>(
267+
reference: DocumentReference<AppModelType, DbModelType>,
268+
path: string,
269+
...pathSegments: string[]
270+
): CollectionReference<DocumentData, DocumentData>;
253271

254272
/**
255273
* Gets a `CollectionReference` instance that refers to a subcollection of
@@ -302,7 +320,10 @@ export declare function refEqual<AppModelType, DbModelType extends DocumentData>
302320
* will be included. Cannot contain a slash.
303321
* @returns The created `Query`.
304322
*/
305-
export function collectionGroup(firestore: Firestore, collectionId: string): Query<DocumentData>;
323+
export function collectionGroup(
324+
firestore: Firestore,
325+
collectionId: string,
326+
): Query<DocumentData, DocumentData>;
306327

307328
/**
308329
* Writes to the document referred to by this `DocumentReference`. If the
@@ -383,10 +404,10 @@ export function updateDoc(
383404
* newly created document after it has been written to the backend (Note that it
384405
* won't resolve while you're offline).
385406
*/
386-
export function addDoc<T>(
387-
reference: CollectionReference<T>,
388-
data: WithFieldValue<T>,
389-
): Promise<DocumentReference<T>>;
407+
export declare function addDoc<AppModelType, DbModelType extends DocumentData>(
408+
reference: CollectionReference<AppModelType, DbModelType>,
409+
data: WithFieldValue<AppModelType>,
410+
): Promise<DocumentReference<AppModelType, DbModelType>>;
390411

391412
/**
392413
* Re-enables use of the network for this {@link Firestore} instance after a prior
@@ -701,7 +722,7 @@ export function loadBundle(
701722
* @param name - The name of the query.
702723
* @returns A named Query.
703724
*/
704-
export function namedQuery(firestore: Firestore, name: string): Query<DocumentData>;
725+
export function namedQuery(firestore: Firestore, name: string): Promise<Query | null>;
705726

706727
/**
707728
* Creates a write batch, used for performing multiple writes as a single

packages/firestore/lib/modular/query.d.ts

Lines changed: 37 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,11 @@ export type QueryNonFilterConstraint =
100100
* @throws if any of the provided query constraints cannot be combined with the
101101
* existing or new constraints.
102102
*/
103-
export function query<T>(
104-
query: Query<T>,
103+
export declare function query<AppModelType, DbModelType extends DocumentData>(
104+
query: Query<AppModelType, DbModelType>,
105105
compositeFilter: QueryCompositeFilterConstraint,
106106
...queryConstraints: QueryNonFilterConstraint[]
107-
): Query<T>;
107+
): Query<AppModelType, DbModelType>;
108108

109109
/**
110110
* Creates a new immutable instance of {@link Query} that is extended to also
@@ -116,7 +116,10 @@ export function query<T>(
116116
* @throws if any of the provided query constraints cannot be combined with the
117117
* existing or new constraints.
118118
*/
119-
export function query<T>(query: Query<T>, ...queryConstraints: IQueryConstraint[]): Query<T>;
119+
export declare function query<AppModelType, DbModelType extends DocumentData>(
120+
query: Query<AppModelType, DbModelType>,
121+
...queryConstraints: QueryConstraint[]
122+
): Query<AppModelType, DbModelType>;
120123

121124
export function query<T>(
122125
query: Query<T>,
@@ -173,7 +176,7 @@ export type OrderByDirection = 'desc' | 'asc';
173176
*/
174177
export function orderBy(
175178
fieldPath: string | FieldPath,
176-
directionStr: OrderByDirection = 'asc',
179+
directionStr?: OrderByDirection = 'asc',
177180
): QueryOrderByConstraint;
178181

179182
/**
@@ -285,7 +288,9 @@ export declare function getDocFromServer<T>(
285288
*
286289
* @returns A `Promise` that will be resolved with the results of the query.
287290
*/
288-
export function getDocs<T>(query: Query<T>): Promise<QuerySnapshot<T>>;
291+
export declare function getDocs<AppModelType, DbModelType extends DocumentData>(
292+
query: Query<AppModelType, DbModelType>,
293+
): Promise<QuerySnapshot<AppModelType, DbModelType>>;
289294

290295
/**
291296
* Executes the query and returns the results as a `QuerySnapshot` from cache.
@@ -294,15 +299,19 @@ export function getDocs<T>(query: Query<T>): Promise<QuerySnapshot<T>>;
294299
*
295300
* @returns A `Promise` that will be resolved with the results of the query.
296301
*/
297-
export function getDocsFromCache<T>(query: Query<T>): Promise<QuerySnapshot<T>>;
302+
export declare function getDocsFromCache<AppModelType, DbModelType extends DocumentData>(
303+
query: Query<AppModelType, DbModelType>,
304+
): Promise<QuerySnapshot<AppModelType, DbModelType>>;
298305

299306
/**
300307
* Executes the query and returns the results as a `QuerySnapshot` from the
301308
* server. Returns an error if the network is not available.
302309
*
303310
* @returns A `Promise` that will be resolved with the results of the query.
304311
*/
305-
export function getDocsFromServer<T>(query: Query<T>): Promise<QuerySnapshot<T>>;
312+
export declare function getDocsFromServer<AppModelType, DbModelType extends DocumentData>(
313+
query: Query<AppModelType, DbModelType>,
314+
): Promise<QuerySnapshot<AppModelType, DbModelType>>;
306315

307316
/**
308317
* Deletes the document referred to by the specified `DocumentReference`.
@@ -311,53 +320,34 @@ export function getDocsFromServer<T>(query: Query<T>): Promise<QuerySnapshot<T>>
311320
* @returns A Promise resolved once the document has been successfully
312321
* deleted from the backend (note that it won't resolve while you're offline).
313322
*/
314-
export function deleteDoc(reference: DocumentReference<unknown>): Promise<void>;
323+
export declare function deleteDoc<AppModelType, DbModelType extends DocumentData>(
324+
reference: DocumentReference<AppModelType, DbModelType>,
325+
): Promise<void>;
315326

316327
/**
317-
* Creates a `QueryConstraint` with the specified ending point.
318-
*
319-
* Using `startAt()`, `startAfter()`, `endBefore()`, `endAt()` and `equalTo()`
320-
* allows you to choose arbitrary starting and ending points for your queries.
321-
*
322-
* The ending point is inclusive, so children with exactly the specified value
323-
* will be included in the query. The optional key argument can be used to
324-
* further limit the range of the query. If it is specified, then children that
325-
* have exactly the specified value must also have a key name less than or equal
326-
* to the specified key.
327-
*
328-
* You can read more about `endAt()` in
329-
* {@link https://firebase.google.com/docs/database/web/lists-of-data#filtering_data | Filtering data}.
328+
* Creates a QueryEndAtConstraint that modifies the result set to end at the provided fields relative to the order of the query.
329+
* The order of the field values must match the order of the order by clauses of the query.
330330
*
331-
* @param value - The value to end at. The argument type depends on which
332-
* `orderBy*()` function was used in this query. Specify a value that matches
333-
* the `orderBy*()` type. When used in combination with `orderByKey()`, the
334-
* value must be a string.
335-
* @param key - The child key to end at, among the children with the previously
336-
* specified priority. This argument is only allowed if ordering by child,
337-
* value, or priority.
331+
* @param fieldValues
338332
*/
339-
export function endAt(value: number | string | boolean | null, key?: string): QueryConstraint;
333+
export declare function endAt(...fieldValues: unknown[]): QueryEndAtConstraint;
340334

341335
/**
342-
* Creates a `QueryConstraint` with the specified ending point (exclusive).
343-
*
344-
* Using `startAt()`, `startAfter()`, `endBefore()`, `endAt()` and `equalTo()`
345-
* allows you to choose arbitrary starting and ending points for your queries.
346-
*
347-
* The ending point is exclusive. If only a value is provided, children
348-
* with a value less than the specified value will be included in the query.
349-
* If a key is specified, then children must have a value less than or equal
350-
* to the specified value and a key name less than the specified key.
336+
* reates a QueryEndAtConstraint that modifies the result set to end at the provided document (inclusive).
337+
* The end position is relative to the order of the query. The document must contain all of the fields provided in the orderBy of the query.
338+
* @param snapshot
339+
*/
340+
export function endAt<AppModelType, DbModelType extends DocumentData>(
341+
snapshot: DocumentSnapshot<AppModelType, DbModelType>,
342+
): QueryEndAtConstraint;
343+
344+
/**
345+
* Creates a QueryEndAtConstraint that modifies the result set to end before the provided fields relative to the order of the query.
346+
* The order of the field values must match the order of the order by clauses of the query.
351347
*
352-
* @param value - The value to end before. The argument type depends on which
353-
* `orderBy*()` function was used in this query. Specify a value that matches
354-
* the `orderBy*()` type. When used in combination with `orderByKey()`, the
355-
* value must be a string.
356-
* @param key - The child key to end before, among the children with the
357-
* previously specified priority. This argument is only allowed if ordering by
358-
* child, value, or priority.
348+
* @param fieldValues
359349
*/
360-
export function endBefore(value: number | string | boolean | null, key?: string): QueryConstraint;
350+
export declare function endBefore(...fieldValues: unknown[]): QueryEndAtConstraint;
361351

362352
/**
363353
* Creates a new `QueryConstraint` that is limited to return only the last

packages/firestore/lib/modular/query.js

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -130,27 +130,25 @@ export function startAfter(...docOrFields) {
130130
}
131131

132132
/**
133-
* @param {number | string | boolean | null} value
134-
* @param {string?} key
135-
* @returns {QueryConstraint}
133+
* Creates a QueryEndAtConstraint that modifies the result set to end at the provided fields relative to the order of the query.
134+
* The order of the field values must match the order of the order by clauses of the query.
135+
*
136+
* @param {*} ...args Can be either a DocumentSnapshot or an array of field values.
136137
*/
137-
export function endAt(value, key) {
138-
if (!key) {
139-
return new QueryConstraint('endAt', value);
140-
}
141-
return new QueryConstraint('endAt', value, key);
138+
139+
export function endAt(...args) {
140+
return new QueryConstraint('endAt', ...args);
142141
}
143142

144143
/**
145-
* @param {number | string | boolean | null} value
146-
* @param {string?} key
147-
* @returns {QueryConstraint}
144+
* Creates a QueryEndAtConstraint that modifies the result set to end before the provided fields relative to the order of the query.
145+
* The order of the field values must match the order of the order by clauses of the query.
146+
*
147+
* @param {*} fieldValues
148148
*/
149-
export function endBefore(value, key) {
150-
if (!key) {
151-
return new QueryConstraint('endBefore', value);
152-
}
153-
return new QueryConstraint('endBefore', value, key);
149+
150+
export function endBefore(...fieldValues) {
151+
return new QueryConstraint('endBefore', ...fieldValues);
154152
}
155153

156154
/**

0 commit comments

Comments
 (0)