From 0506096e721e6f68542b0e4589a7473bb3819cb1 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Mon, 5 Jun 2023 10:03:47 +0100 Subject: [PATCH] Fixing filtering plus pagination in googlesheets, the pagination previously was first which broke the filtering. --- .../server/src/integrations/googlesheets.ts | 21 ++++++++++++------- packages/shared-core/src/filters.ts | 16 ++++++++++++++ 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/packages/server/src/integrations/googlesheets.ts b/packages/server/src/integrations/googlesheets.ts index 59d47a0afa..8863aa0b3a 100644 --- a/packages/server/src/integrations/googlesheets.ts +++ b/packages/server/src/integrations/googlesheets.ts @@ -457,17 +457,19 @@ class GoogleSheetsIntegration implements DatasourcePlus { }) { try { await this.connect() + const hasFilters = dataFilters.hasFilters(query.filters) + const limit = query.paginate?.limit || 100 + const page: number = + typeof query.paginate?.page === "number" + ? query.paginate.page + : parseInt(query.paginate?.page || "1") + const offset = (page - 1) * limit const sheet = this.client.sheetsByTitle[query.sheet] let rows: GoogleSpreadsheetRow[] = [] - if (query.paginate) { - const limit = query.paginate.limit || 100 - let page: number = - typeof query.paginate.page === "number" - ? query.paginate.page - : parseInt(query.paginate.page || "1") + if (query.paginate && !hasFilters) { rows = await sheet.getRows({ limit, - offset: (page - 1) * limit, + offset, }) } else { rows = await sheet.getRows() @@ -486,7 +488,10 @@ class GoogleSheetsIntegration implements DatasourcePlus { query.filters.equal[`_${GOOGLE_SHEETS_PRIMARY_KEY}`] = id } } - const filtered = dataFilters.runLuceneQuery(rows, query.filters) + let filtered = dataFilters.runLuceneQuery(rows, query.filters) + if (hasFilters && query.paginate) { + filtered = filtered.slice(offset, offset + limit) + } const headerValues = sheet.headerValues let response = [] for (let row of filtered) { diff --git a/packages/shared-core/src/filters.ts b/packages/shared-core/src/filters.ts index 1ffcebdd2b..7c2c34bf6b 100644 --- a/packages/shared-core/src/filters.ts +++ b/packages/shared-core/src/filters.ts @@ -455,3 +455,19 @@ export const luceneLimit = (docs: any[], limit: string) => { } return docs.slice(0, numLimit) } + +export const hasFilters = (query?: Query) => { + if (!query) { + return false + } + const skipped = ["allOr"] + for (let [key, value] of Object.entries(query)) { + if (skipped.includes(key) || typeof value !== "object") { + continue + } + if (Object.keys(value).length !== 0) { + return true + } + } + return false +}