Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit b50fc5b

Browse files
Merge pull request #1476 from milansar/master
Firestore - Paginate data with query cursors #1469
2 parents d7184eb + 3cdbc3a commit b50fc5b

File tree

3 files changed

+87
-32
lines changed

3 files changed

+87
-32
lines changed

src/firebase.android.ts

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2294,6 +2294,7 @@ firebase.firestore._getDocumentReference = (docRef?: JDocumentReference): firest
22942294
id: docRef.getId(),
22952295
parent: firebase.firestore._getCollectionReference(docRef.getParent()),
22962296
path: docRef.getPath(),
2297+
firestore: firebase.firestore,
22972298
collection: cp => firebase.firestore.collection(`${collectionPath}/${docRef.getId()}/${cp}`),
22982299
set: (data: any, options?: firestore.SetOptions) => firebase.firestore.set(collectionPath, docRef.getId(), data, options),
22992300
get: (options?: firestore.GetOptions) => firebase.firestore.getDocument(collectionPath, docRef.getId(), options),
@@ -2324,17 +2325,18 @@ firebase.firestore._getCollectionReference = (colRef?: JCollectionReference): fi
23242325
return {
23252326
id: colRef.getId(),
23262327
parent: firebase.firestore._getDocumentReference(colRef.getParent()),
2328+
firestore: firebase.firestore,
23272329
doc: (documentPath?: string) => firebase.firestore.doc(collectionPath, documentPath),
23282330
add: document => firebase.firestore.add(collectionPath, document),
23292331
get: (options?: firestore.GetOptions) => firebase.firestore.get(collectionPath, options),
23302332
where: (fieldPath: string, opStr: firestore.WhereFilterOp, value: any) => firebase.firestore.where(collectionPath, fieldPath, opStr, value),
23312333
orderBy: (fieldPath: string, directionStr: firestore.OrderByDirection): firestore.Query => firebase.firestore.orderBy(collectionPath, fieldPath, directionStr, colRef),
23322334
limit: (limit: number): firestore.Query => firebase.firestore.limit(collectionPath, limit, colRef),
23332335
onSnapshot: (optionsOrCallback: firestore.SnapshotListenOptions | ((snapshot: QuerySnapshot) => void), callbackOrOnError?: (snapshotOrError: QuerySnapshot | Error) => void, onError?: (error: Error) => void) => firebase.firestore.onCollectionSnapshot(colRef, optionsOrCallback, callbackOrOnError, onError),
2334-
startAfter: (snapshot: DocumentSnapshot): firestore.Query => firebase.firestore.startAfter(collectionPath, snapshot, colRef),
2335-
startAt: (snapshot: DocumentSnapshot): firestore.Query => firebase.firestore.startAt(collectionPath, snapshot, colRef),
2336-
endAt: (snapshot: DocumentSnapshot): firestore.Query => firebase.firestore.endAt(collectionPath, snapshot, colRef),
2337-
endBefore: (snapshot: DocumentSnapshot): firestore.Query => firebase.firestore.endBefore(collectionPath, snapshot, colRef),
2336+
startAfter: (snapshotOrFieldValue: DocumentSnapshot | any, ...fieldValues: any[]): firestore.Query => firebase.firestore.startAfter(collectionPath, snapshotOrFieldValue,fieldValues, colRef),
2337+
startAt: (snapshotOrFieldValue: DocumentSnapshot | any, ...fieldValues: any[]): firestore.Query => firebase.firestore.startAt(collectionPath, snapshotOrFieldValue,fieldValues, colRef),
2338+
endAt: (snapshotOrFieldValue: DocumentSnapshot | any, ...fieldValues: any[]): firestore.Query => firebase.firestore.endAt(collectionPath, snapshotOrFieldValue,fieldValues, colRef),
2339+
endBefore: (snapshotOrFieldValue: DocumentSnapshot | any, ...fieldValues: any[]): firestore.Query => firebase.firestore.endBefore(collectionPath, snapshotOrFieldValue,fieldValues, colRef)
23382340
};
23392341
};
23402342

@@ -2579,10 +2581,11 @@ firebase.firestore._getQuery = (collectionPath: string, query: com.google.fireba
25792581
orderBy: (fp: string, directionStr: firestore.OrderByDirection): firestore.Query => firebase.firestore.orderBy(collectionPath, fp, directionStr, query),
25802582
limit: (limit: number): firestore.Query => firebase.firestore.limit(collectionPath, limit, query),
25812583
onSnapshot: (optionsOrCallback: firestore.SnapshotListenOptions | ((snapshot: QuerySnapshot) => void), callbackOrOnError?: (snapshotOrError: QuerySnapshot | Error) => void, onError?: (error: Error) => void) => firebase.firestore.onCollectionSnapshot(query, optionsOrCallback, callbackOrOnError, onError),
2582-
startAfter: (snapshot: DocumentSnapshot) => firebase.firestore.startAfter(collectionPath, snapshot, query),
2583-
startAt: (snapshot: DocumentSnapshot) => firebase.firestore.startAt(collectionPath, snapshot, query),
2584-
endAt: (snapshot: DocumentSnapshot) => firebase.firestore.endAt(collectionPath, snapshot, query),
2585-
endBefore: (snapshot: DocumentSnapshot) => firebase.firestore.endBefore(collectionPath, snapshot, query),
2584+
startAfter: (snapshotOrFieldValue: DocumentSnapshot | any, ...fieldValues: any[]): firestore.Query => firebase.firestore.startAfter(collectionPath, snapshotOrFieldValue,fieldValues,query),
2585+
startAt: (snapshotOrFieldValue: DocumentSnapshot | any, ...fieldValues: any[]): firestore.Query => firebase.firestore.startAt(collectionPath, snapshotOrFieldValue,fieldValues,query),
2586+
endAt: (snapshotOrFieldValue: DocumentSnapshot | any, ...fieldValues: any[]): firestore.Query => firebase.firestore.endAt(collectionPath, snapshotOrFieldValue,fieldValues,query),
2587+
endBefore: (snapshotOrFieldValue: DocumentSnapshot | any, ...fieldValues: any[]): firestore.Query => firebase.firestore.endBefore(collectionPath, snapshotOrFieldValue,fieldValues,query),
2588+
firestore: firebase.firestore
25862589
};
25872590
};
25882591

@@ -2631,22 +2634,43 @@ firebase.firestore.limit = (collectionPath: string, limit: number, query: com.go
26312634
return firebase.firestore._getQuery(collectionPath, query);
26322635
};
26332636

2634-
firebase.firestore.startAfter = (collectionPath: string, snapshot: DocumentSnapshot, query: com.google.firebase.firestore.Query): firestore.Query => {
2635-
return firebase.firestore._getQuery(collectionPath, query.startAfter(snapshot.android));
2637+
firebase.firestore.startAfter = (collectionPath: string, snapshotOrFieldValue: DocumentSnapshot | any, fieldValues: any[], query: com.google.firebase.firestore.Query): firestore.Query => {
2638+
return firebase.firestore._getQuery(collectionPath, query.startAfter(firebase.firestore._getSnapshotOrFieldValues(snapshotOrFieldValue,fieldValues)));
26362639
};
26372640

2638-
firebase.firestore.startAt = (collectionPath: string, snapshot: DocumentSnapshot, query: com.google.firebase.firestore.Query): firestore.Query => {
2639-
return firebase.firestore._getQuery(collectionPath, query.startAt(snapshot.android));
2641+
firebase.firestore.startAt = (collectionPath: string, snapshotOrFieldValue: DocumentSnapshot | any, fieldValues: any[], query: com.google.firebase.firestore.Query): firestore.Query => {
2642+
return firebase.firestore._getQuery(collectionPath, query.startAt(firebase.firestore._getSnapshotOrFieldValues(snapshotOrFieldValue,fieldValues)));
26402643
};
26412644

2642-
firebase.firestore.endAt = (collectionPath: string, snapshot: DocumentSnapshot, query: com.google.firebase.firestore.Query): firestore.Query => {
2643-
return firebase.firestore._getQuery(collectionPath, query.endAt(snapshot.android));
2645+
firebase.firestore.endAt = (collectionPath: string, snapshotOrFieldValue: DocumentSnapshot | any, fieldValues: any[], query: com.google.firebase.firestore.Query): firestore.Query => {
2646+
return firebase.firestore._getQuery(collectionPath, query.endAt(firebase.firestore._getSnapshotOrFieldValues(snapshotOrFieldValue,fieldValues)));
26442647
};
26452648

2646-
firebase.firestore.endBefore = (collectionPath: string, snapshot: DocumentSnapshot, query: com.google.firebase.firestore.Query): firestore.Query => {
2647-
return firebase.firestore._getQuery(collectionPath, query.endBefore(snapshot.android));
2649+
firebase.firestore.endBefore = (collectionPath: string, snapshotOrFieldValue: DocumentSnapshot | any, fieldValues: any[], query: com.google.firebase.firestore.Query): firestore.Query => {
2650+
return firebase.firestore._getQuery(collectionPath, query.endBefore(firebase.firestore._getSnapshotOrFieldValues(snapshotOrFieldValue,fieldValues)));
26482651
};
26492652

2653+
firebase.firestore._getSnapshotOrFieldValues = (snapshotOrFieldValue: DocumentSnapshot | any, fieldValues: any[]): any => {
2654+
if(snapshotOrFieldValue && snapshotOrFieldValue.android) {
2655+
return snapshotOrFieldValue;
2656+
} else {
2657+
const AllFieldValues = [snapshotOrFieldValue, ...fieldValues];
2658+
const javaArray = Array.create('java.lang.Object', AllFieldValues.length);
2659+
AllFieldValues.forEach((value, index) => {
2660+
// support only Number and String type
2661+
// Not sure whether other types are supported by OrderBy
2662+
let javaValue: java.lang.String | java.lang.Double;
2663+
if (isNaN(value)) {
2664+
javaValue = new java.lang.String(value.toString());
2665+
} else {
2666+
javaValue = new java.lang.Double(value);
2667+
}
2668+
javaArray[index] = javaValue;
2669+
});
2670+
return javaArray;
2671+
}
2672+
}
2673+
26502674
export type JDocumentReference = com.google.firebase.firestore.DocumentReference;
26512675
export type JCollectionReference = com.google.firebase.firestore.CollectionReference;
26522676

src/firebase.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,8 @@ export namespace firestore {
812812

813813
readonly path: string;
814814

815+
readonly firestore: firestore;
816+
815817
collection: (collectionPath: string) => CollectionReference;
816818

817819
set: (document: any, options?: SetOptions) => Promise<void>;
@@ -830,6 +832,8 @@ export namespace firestore {
830832
}
831833

832834
export interface Query {
835+
readonly firestore: firestore;
836+
833837
get(options?: GetOptions): Promise<QuerySnapshot>;
834838

835839
where(fieldPath: string, opStr: WhereFilterOp, value: any): Query;
@@ -842,11 +846,19 @@ export namespace firestore {
842846

843847
startAt(snapshot: DocumentSnapshot): Query;
844848

849+
startAt(...fieldValues: any[]): Query;
850+
845851
startAfter(snapshot: DocumentSnapshot): Query;
846852

853+
startAfter(...fieldValues: any[]): Query;
854+
847855
endAt(snapshot: DocumentSnapshot): Query;
848856

857+
endAt(...fieldValues: any[]): Query;
858+
849859
endBefore(snapshot: DocumentSnapshot): Query;
860+
861+
endBefore(...fieldValues: any[]): Query;
850862
}
851863

852864
export interface CollectionGroup {

src/firebase.ios.ts

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1938,17 +1938,18 @@ firebase.firestore._getCollectionReference = (colRef?: FIRCollectionReference):
19381938
return {
19391939
id: colRef.collectionID,
19401940
parent: firebase.firestore._getDocumentReference(colRef.parent),
1941+
firestore: firebase.firestore,
19411942
doc: (documentPath?: string) => firebase.firestore.doc(collectionPath, documentPath),
19421943
add: document => firebase.firestore.add(collectionPath, document),
19431944
get: (options?: firestore.GetOptions) => firebase.firestore.get(collectionPath, options),
19441945
where: (fieldPath: string, opStr: firestore.WhereFilterOp, value: any) => firebase.firestore.where(collectionPath, fieldPath, opStr, value),
19451946
orderBy: (fieldPath: string, directionStr: firestore.OrderByDirection): firestore.Query => firebase.firestore.orderBy(collectionPath, fieldPath, directionStr, colRef),
19461947
limit: (limit: number): firestore.Query => firebase.firestore.limit(collectionPath, limit, colRef),
19471948
onSnapshot: (optionsOrCallback: firestore.SnapshotListenOptions | ((snapshot: QuerySnapshot) => void), callbackOrOnError?: (snapshotOrError: QuerySnapshot | Error) => void, onError?: (error: Error) => void) => firebase.firestore.onCollectionSnapshot(colRef, optionsOrCallback, callbackOrOnError, onError),
1948-
startAfter: (document: DocumentSnapshot) => firebase.firestore.startAfter(collectionPath, document, colRef),
1949-
startAt: (document: DocumentSnapshot) => firebase.firestore.startAt(collectionPath, document, colRef),
1950-
endAt: (document: DocumentSnapshot) => firebase.firestore.endAt(collectionPath, document, colRef),
1951-
endBefore: (document: DocumentSnapshot) => firebase.firestore.endBefore(collectionPath, document, colRef),
1949+
startAfter: (snapshotOrFieldValue: DocumentSnapshot | any, ...fieldValues: any[]): firestore.Query => firebase.firestore.startAfter(collectionPath, snapshotOrFieldValue,fieldValues, colRef),
1950+
startAt: (snapshotOrFieldValue: DocumentSnapshot | any, ...fieldValues: any[]): firestore.Query => firebase.firestore.startAt(collectionPath, snapshotOrFieldValue,fieldValues, colRef),
1951+
endAt: (snapshotOrFieldValue: DocumentSnapshot | any, ...fieldValues: any[]): firestore.Query => firebase.firestore.endAt(collectionPath, snapshotOrFieldValue,fieldValues, colRef),
1952+
endBefore: (snapshotOrFieldValue: DocumentSnapshot | any, ...fieldValues: any[]): firestore.Query => firebase.firestore.endBefore(collectionPath, snapshotOrFieldValue,fieldValues, colRef)
19521953
};
19531954
};
19541955

@@ -1974,6 +1975,7 @@ firebase.firestore._getDocumentReference = (docRef?: FIRDocumentReference): fire
19741975
id: docRef.documentID,
19751976
parent: firebase.firestore._getCollectionReference(docRef.parent),
19761977
path: docRef.path,
1978+
firestore: firebase.firestore,
19771979
collection: cp => firebase.firestore.collection(`${collectionPath}/${docRef.documentID}/${cp}`),
19781980
set: (data: any, options?: firestore.SetOptions) => firebase.firestore.set(collectionPath, docRef.documentID, data, options),
19791981
get: (options?: firestore.GetOptions) => firebase.firestore.getDocument(collectionPath, docRef.documentID, options),
@@ -2236,10 +2238,11 @@ firebase.firestore._getQuery = (collectionPath: string, query: FIRQuery): firest
22362238
orderBy: (fp: string, directionStr: firestore.OrderByDirection): firestore.Query => firebase.firestore.orderBy(collectionPath, fp, directionStr, query),
22372239
limit: (limit: number): firestore.Query => firebase.firestore.limit(collectionPath, limit, query),
22382240
onSnapshot: (optionsOrCallback: firestore.SnapshotListenOptions | ((snapshot: QuerySnapshot) => void), callbackOrOnError?: (snapshotOrError: QuerySnapshot | Error) => void, onError?: (error: Error) => void) => firebase.firestore.onCollectionSnapshot(query, optionsOrCallback, callbackOrOnError, onError),
2239-
startAfter: (document: DocumentSnapshot) => firebase.firestore.startAfter(collectionPath, document, query),
2240-
startAt: (document: DocumentSnapshot) => firebase.firestore.startAt(collectionPath, document, query),
2241-
endAt: (document: DocumentSnapshot) => firebase.firestore.endAt(collectionPath, document, query),
2242-
endBefore: (document: DocumentSnapshot) => firebase.firestore.endBefore(collectionPath, document, query),
2241+
startAfter: (snapshotOrFieldValue: DocumentSnapshot | any, ...fieldValues: any[]): firestore.Query => firebase.firestore.startAfter(collectionPath, snapshotOrFieldValue,fieldValues,query),
2242+
startAt: (snapshotOrFieldValue: DocumentSnapshot | any, ...fieldValues: any[]): firestore.Query => firebase.firestore.startAt(collectionPath, snapshotOrFieldValue,fieldValues,query),
2243+
endAt: (snapshotOrFieldValue: DocumentSnapshot | any, ...fieldValues: any[]): firestore.Query => firebase.firestore.endAt(collectionPath, snapshotOrFieldValue,fieldValues,query),
2244+
endBefore: (snapshotOrFieldValue: DocumentSnapshot | any, ...fieldValues: any[]): firestore.Query => firebase.firestore.endBefore(collectionPath, snapshotOrFieldValue,fieldValues,query),
2245+
firestore: firebase.firestore
22432246
};
22442247
};
22452248

@@ -2288,20 +2291,36 @@ firebase.firestore.limit = (collectionPath: string, limit: number, query: FIRQue
22882291
return firebase.firestore._getQuery(collectionPath, query);
22892292
};
22902293

2291-
firebase.firestore.startAt = (collectionPath: string, document: DocumentSnapshot, query: FIRQuery) => {
2292-
return firebase.firestore._getQuery(collectionPath, query.queryStartingAtDocument(document.ios));
2294+
firebase.firestore.startAfter = (collectionPath: string, snapshotOrFieldValue: DocumentSnapshot | any, fieldValues: any[], query: FIRQuery): firestore.Query => {
2295+
if(snapshotOrFieldValue && snapshotOrFieldValue.ios){
2296+
return firebase.firestore._getQuery(collectionPath, query.queryStartingAfterDocument(snapshotOrFieldValue.ios));
2297+
}else {
2298+
return firebase.firestore._getQuery(collectionPath, query.queryStartingAfterValues([snapshotOrFieldValue, ...fieldValues]));
2299+
}
22932300
};
22942301

2295-
firebase.firestore.startAfter = (collectionPath: string, document: DocumentSnapshot, query: FIRQuery) => {
2296-
return firebase.firestore._getQuery(collectionPath, query.queryStartingAfterDocument(document.ios));
2302+
firebase.firestore.startAt = (collectionPath: string, snapshotOrFieldValue: DocumentSnapshot | any, fieldValues: any[], query: FIRQuery): firestore.Query => {
2303+
if(snapshotOrFieldValue && snapshotOrFieldValue.ios){
2304+
return firebase.firestore._getQuery(collectionPath, query.queryStartingAtDocument(snapshotOrFieldValue.ios));
2305+
}else {
2306+
return firebase.firestore._getQuery(collectionPath, query.queryStartingAtValues([snapshotOrFieldValue, ...fieldValues]));
2307+
}
22972308
};
22982309

2299-
firebase.firestore.endAt = (collectionPath: string, document: DocumentSnapshot, query: FIRQuery) => {
2300-
return firebase.firestore._getQuery(collectionPath, query.queryEndingAtDocument(document.ios));
2310+
firebase.firestore.endAt = (collectionPath: string, snapshotOrFieldValue: DocumentSnapshot | any, fieldValues: any[], query: FIRQuery): firestore.Query => {
2311+
if(snapshotOrFieldValue && snapshotOrFieldValue.ios){
2312+
return firebase.firestore._getQuery(collectionPath, query.queryEndingAtDocument(snapshotOrFieldValue.ios));
2313+
}else {
2314+
return firebase.firestore._getQuery(collectionPath, query.queryEndingAtValues([snapshotOrFieldValue, ...fieldValues]));
2315+
}
23012316
};
23022317

2303-
firebase.firestore.endBefore = (collectionPath: string, document: DocumentSnapshot, query: FIRQuery) => {
2304-
return firebase.firestore._getQuery(collectionPath, query.queryEndingBeforeDocument(document.ios));
2318+
firebase.firestore.endBefore = (collectionPath: string, snapshotOrFieldValue: DocumentSnapshot | any, fieldValues: any[], query: FIRQuery): firestore.Query => {
2319+
if(snapshotOrFieldValue && snapshotOrFieldValue.ios){
2320+
return firebase.firestore._getQuery(collectionPath, query.queryEndingBeforeDocument(snapshotOrFieldValue.ios));
2321+
}else {
2322+
return firebase.firestore._getQuery(collectionPath, query.queryEndingBeforeValues([snapshotOrFieldValue, ...fieldValues]));
2323+
}
23052324
};
23062325

23072326
class GIDSignInDelegateImpl extends NSObject implements GIDSignInDelegate {

0 commit comments

Comments
 (0)