Fixing filtering plus pagination in googlesheets, the pagination previously was first which broke the filtering.

This commit is contained in:
mike12345567 2023-06-05 10:03:47 +01:00
parent 770f7c1dc6
commit 0506096e72
2 changed files with 29 additions and 8 deletions

View File

@ -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) {

View File

@ -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
}