Skip to content

Commit

Permalink
Add resource name to storage key
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamFilipek92 committed May 22, 2020
1 parent b4e6500 commit 6fc7eb7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "react-admin-firebase-test",
"description": "A firebase data provider for the React Admin framework",
"version": "3.2.9",
"version": "3.2.11",
"peerDependencies": {
"firebase": "^7.9.x",
"react": "^16.x",
Expand Down
28 changes: 19 additions & 9 deletions src/providers/database/FirebaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ export class FirebaseClient implements IFirebaseClient {
if (page === 1) {
query = query.limit(perPage);
} else {
let queryCursor = await this.getQueryCursor(r.collection, params);
let queryCursor = await this.getQueryCursor(r.collection, params, resourceName);
if (!queryCursor) {
queryCursor = await this.findLastQueryCursor(r.collection, query, fullParams);
queryCursor = await this.findLastQueryCursor(r.collection, query, fullParams, resourceName);
}
query = query.startAfter(queryCursor).limit(perPage);
}
Expand All @@ -116,7 +116,8 @@ export class FirebaseClient implements IFirebaseClient {
const data = snapshots.docs.map(doc => parseFireStoreDocument(doc));
this.setQueryCursor(
snapshots.docs[snapshots.docs.length - 1],
fullParams
fullParams,
resourceName
);
log("apiGetListLazy - result", { data });
return { data, total: 100000 };
Expand Down Expand Up @@ -411,8 +412,8 @@ export class FirebaseClient implements IFirebaseClient {
return query;
}

private setQueryCursor(doc: DocumentSnapshot, params: messageTypes.IParamsGetList) {
const key = btoa(JSON.stringify(params));
private setQueryCursor(doc: DocumentSnapshot, params: messageTypes.IParamsGetList, resourceName: string) {
const key = btoa(JSON.stringify({ ...params, resourceName }));
localStorage.setItem(key, doc.id);

const localCursorKeys = localStorage.getItem('ra-firebase-cursor-keys');
Expand All @@ -425,8 +426,12 @@ export class FirebaseClient implements IFirebaseClient {
}
}

private async getQueryCursor(collection: CollectionReference, params: messageTypes.IParamsGetList): Promise<DocumentSnapshot | boolean> {
const key = btoa(JSON.stringify(params));
private async getQueryCursor(
collection: CollectionReference,
params: messageTypes.IParamsGetList,
resourceName: string
): Promise<DocumentSnapshot | boolean> {
const key = btoa(JSON.stringify({ ...params, resourceName }));
const docId = localStorage.getItem(key);
if (!docId) {
return false;
Expand All @@ -445,7 +450,12 @@ export class FirebaseClient implements IFirebaseClient {
}
}

private async findLastQueryCursor(collection: CollectionReference, query: Query, params: messageTypes.IParamsGetList) {
private async findLastQueryCursor(
collection: CollectionReference,
query: Query,
params: messageTypes.IParamsGetList,
resourceName: string
) {
const { page, perPage } = params.pagination;
const previousPage = page - 1;

Expand All @@ -458,7 +468,7 @@ export class FirebaseClient implements IFirebaseClient {
}
};

const currentPageQueryCursor = await this.getQueryCursor(collection, currentPageParams);
const currentPageQueryCursor = await this.getQueryCursor(collection, currentPageParams, resourceName);
if (currentPageQueryCursor) {
return currentPageQueryCursor;
}
Expand Down

0 comments on commit 6fc7eb7

Please sign in to comment.