Skip to content

Commit

Permalink
Fixing filtering plus pagination in googlesheets, the pagination prev…
Browse files Browse the repository at this point in the history
…iously was first which broke the filtering.
  • Loading branch information
mike12345567 committed Jun 5, 2023
1 parent 171b284 commit e745cca
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
21 changes: 13 additions & 8 deletions packages/server/src/integrations/googlesheets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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) {
Expand Down
16 changes: 16 additions & 0 deletions packages/shared-core/src/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit e745cca

Please sign in to comment.