Fix Google Sheets pagination.

This commit is contained in:
Sam Rose 2024-09-16 16:15:09 +01:00
parent 949c31d760
commit 26ad987072
No known key found for this signature in database
2 changed files with 47 additions and 5 deletions

View File

@ -551,11 +551,16 @@ export class GoogleSheetsIntegration implements DatasourcePlus {
await this.connect() await this.connect()
const hasFilters = dataFilters.hasFilters(query.filters) const hasFilters = dataFilters.hasFilters(query.filters)
const limit = query.paginate?.limit || 100 const limit = query.paginate?.limit || 100
const page: number = let offset = query.paginate?.offset || 0
typeof query.paginate?.page === "number"
? query.paginate.page let page = query.paginate?.page
: parseInt(query.paginate?.page || "1") if (typeof page === "string") {
const offset = (page - 1) * limit page = parseInt(page)
}
if (page !== undefined) {
offset = page * limit
}
const sheet = this.client.sheetsByTitle[query.sheet] const sheet = this.client.sheetsByTitle[query.sheet]
let rows: GoogleSpreadsheetRow[] = [] let rows: GoogleSpreadsheetRow[] = []
if (query.paginate && !hasFilters) { if (query.paginate && !hasFilters) {

View File

@ -5,6 +5,7 @@ import TestConfiguration from "../../tests/utilities/TestConfiguration"
import { import {
Datasource, Datasource,
FieldType, FieldType,
Row,
SourceName, SourceName,
Table, Table,
TableSourceType, TableSourceType,
@ -208,6 +209,42 @@ describe("Google Sheets Integration", () => {
expect(row2.name).toEqual("Test Contact 2") expect(row2.name).toEqual("Test Contact 2")
expect(row2.description).toEqual("original description 2") expect(row2.description).toEqual("original description 2")
}) })
it("can paginate correctly", async () => {
await config.api.row.bulkImport(table._id!, {
rows: Array.from({ length: 248 }, (_, i) => ({
name: `${i}`,
description: "",
})),
})
let resp = await config.api.row.search(table._id!, {
tableId: table._id!,
query: {},
paginate: true,
limit: 10,
})
let rows = resp.rows
while (resp.hasNextPage) {
resp = await config.api.row.search(table._id!, {
tableId: table._id!,
query: {},
paginate: true,
limit: 10,
bookmark: resp.bookmark,
})
rows = rows.concat(resp.rows)
if (rows.length > 250) {
throw new Error("Too many rows returned")
}
}
expect(rows.length).toEqual(250)
expect(rows.map(row => row.name)).toEqual(
expect.arrayContaining(Array.from({ length: 248 }, (_, i) => `${i}`))
)
})
}) })
describe("update", () => { describe("update", () => {