Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUDI-8579] Create more realistic tests for Google Sheets #14535

Merged
merged 15 commits into from
Sep 10, 2024
Merged
46 changes: 18 additions & 28 deletions packages/server/src/integrations/googlesheets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { cache, configs, context, HTTPError } from "@budibase/backend-core"
import { dataFilters, utils } from "@budibase/shared-core"
import { GOOGLE_SHEETS_PRIMARY_KEY } from "../constants"

interface GoogleSheetsConfig {
export interface GoogleSheetsConfig {
spreadsheetId: string
auth: OAuthClientConfig
continueSetupId?: string
Expand Down Expand Up @@ -157,7 +157,7 @@ const SCHEMA: Integration = {
},
}

class GoogleSheetsIntegration implements DatasourcePlus {
export class GoogleSheetsIntegration implements DatasourcePlus {
private readonly config: GoogleSheetsConfig
private readonly spreadsheetId: string
private client: GoogleSpreadsheet = undefined!
Expand Down Expand Up @@ -378,6 +378,8 @@ class GoogleSheetsIntegration implements DatasourcePlus {
return this.create({ sheet, row: json.body as Row })
case Operation.BULK_CREATE:
return this.createBulk({ sheet, rows: json.body as Row[] })
case Operation.BULK_UPSERT:
return this.createBulk({ sheet, rows: json.body as Row[] })
case Operation.READ:
return this.read({ ...json, sheet })
case Operation.UPDATE:
Expand All @@ -395,7 +397,12 @@ class GoogleSheetsIntegration implements DatasourcePlus {
sheet,
})
case Operation.CREATE_TABLE:
return this.createTable(json?.table?.name)
if (json.table === undefined) {
samwho marked this conversation as resolved.
Show resolved Hide resolved
throw new Error(
"attempted to create a table without specifying the table to create"
)
}
return this.createTable(json.table)
case Operation.UPDATE_TABLE:
return this.updateTable(json.table!)
case Operation.DELETE_TABLE:
Expand All @@ -422,13 +429,13 @@ class GoogleSheetsIntegration implements DatasourcePlus {
return rowObject
}

private async createTable(name?: string) {
if (!name) {
throw new Error("Must provide name for new sheet.")
}
private async createTable(table: Table) {
try {
await this.connect()
await this.client.addSheet({ title: name, headerValues: [name] })
await this.client.addSheet({
title: table.name,
headerValues: Object.keys(table.schema),
})
} catch (err) {
console.error("Error creating new table in google sheets", err)
throw err
Expand Down Expand Up @@ -552,32 +559,15 @@ class GoogleSheetsIntegration implements DatasourcePlus {
} else {
rows = await sheet.getRows()
}
// this is a special case - need to handle the _id, it doesn't exist
// we cannot edit the returned structure from google, it does not have
// setter functions and is immutable, easier to update the filters
// to look for the _rowNumber property rather than rowNumber
if (query.filters?.equal) {
const idFilterKeys = Object.keys(query.filters.equal).filter(filter =>
filter.includes(GOOGLE_SHEETS_PRIMARY_KEY)
)
for (let idFilterKey of idFilterKeys) {
const id = query.filters.equal[idFilterKey]
delete query.filters.equal[idFilterKey]
query.filters.equal[`_${GOOGLE_SHEETS_PRIMARY_KEY}`] = id
}
}

if (hasFilters && query.paginate) {
rows = rows.slice(offset, offset + limit)
}
const headerValues = sheet.headerValues
let response = []
for (let row of rows) {
response.push(
this.buildRowObject(headerValues, row.toObject(), row.rowNumber)
)
}

let response = rows.map(row =>
this.buildRowObject(headerValues, row.toObject(), row.rowNumber)
)
response = dataFilters.runQuery(response, query.filters || {})

if (query.sort) {
Expand Down
Loading
Loading