Removing relationship option from Googlesheets as it doesn't add value, as well as multi-select as it can't really be supported. Also fixing issues with re-fetching after adding some special column types./

This commit is contained in:
mike12345567 2023-03-13 18:04:29 +00:00
parent d3464ce166
commit 2a1f215351
2 changed files with 78 additions and 47 deletions

View File

@ -308,7 +308,7 @@
{ name: "Auto Column", type: AUTO_TYPE }, { name: "Auto Column", type: AUTO_TYPE },
] ]
} else { } else {
return [ let fields = [
FIELDS.STRING, FIELDS.STRING,
FIELDS.BARCODEQR, FIELDS.BARCODEQR,
FIELDS.LONGFORM, FIELDS.LONGFORM,
@ -316,10 +316,14 @@
FIELDS.DATETIME, FIELDS.DATETIME,
FIELDS.NUMBER, FIELDS.NUMBER,
FIELDS.BOOLEAN, FIELDS.BOOLEAN,
FIELDS.ARRAY,
FIELDS.FORMULA, FIELDS.FORMULA,
FIELDS.LINK,
] ]
// no-sql or a spreadsheet
console.log(table)
if (!external || table.sql) {
fields = [...fields, FIELDS.LINK, FIELDS.ARRAY]
}
return fields
} }
} }

View File

@ -1,19 +1,20 @@
import { import {
DatasourceFieldType, DatasourceFieldType,
DatasourcePlus, DatasourcePlus,
FieldType,
Integration, Integration,
Operation, Operation,
PaginationJson, PaginationJson,
QueryJson, QueryJson,
QueryType, QueryType,
Row,
SearchFilters, SearchFilters,
SortJson, SortJson,
Table, Table,
Row, TableRequest,
} from "@budibase/types" } from "@budibase/types"
import { OAuth2Client } from "google-auth-library" import { OAuth2Client } from "google-auth-library"
import { buildExternalTableId } from "./utils" import { buildExternalTableId, finaliseExternalTables } from "./utils"
import { FieldTypes } from "../constants"
import { GoogleSpreadsheet } from "google-spreadsheet" import { GoogleSpreadsheet } from "google-spreadsheet"
import fetch from "node-fetch" import fetch from "node-fetch"
import { configs, HTTPError } from "@budibase/backend-core" import { configs, HTTPError } from "@budibase/backend-core"
@ -40,6 +41,17 @@ interface AuthTokenResponse {
access_token: string access_token: string
} }
const ALLOWED_TYPES = [
FieldType.STRING,
FieldType.FORMULA,
FieldType.NUMBER,
FieldType.LONGFORM,
FieldType.DATETIME,
FieldType.OPTIONS,
FieldType.BOOLEAN,
FieldType.BARCODEQR,
]
const SCHEMA: Integration = { const SCHEMA: Integration = {
plus: true, plus: true,
auth: { auth: {
@ -225,13 +237,13 @@ class GoogleSheetsIntegration implements DatasourcePlus {
for (let header of headerValues) { for (let header of headerValues) {
table.schema[header] = { table.schema[header] = {
name: header, name: header,
type: FieldTypes.STRING, type: FieldType.STRING,
} }
} }
return table return table
} }
async buildSchema(datasourceId: string) { async buildSchema(datasourceId: string, entities: Record<string, Table>) {
await this.connect() await this.connect()
const sheets = this.client.sheetsByIndex const sheets = this.client.sheetsByIndex
const tables: Record<string, Table> = {} const tables: Record<string, Table> = {}
@ -246,7 +258,9 @@ class GoogleSheetsIntegration implements DatasourcePlus {
id id
) )
} }
this.tables = tables const final = finaliseExternalTables(tables, entities)
this.tables = final.tables
this.schemaErrors = final.errors
} }
async query(json: QueryJson) { async query(json: QueryJson) {
@ -274,7 +288,7 @@ class GoogleSheetsIntegration implements DatasourcePlus {
case Operation.CREATE_TABLE: case Operation.CREATE_TABLE:
return this.createTable(json?.table?.name) return this.createTable(json?.table?.name)
case Operation.UPDATE_TABLE: case Operation.UPDATE_TABLE:
return this.updateTable(json.table) return this.updateTable(json.table!)
case Operation.DELETE_TABLE: case Operation.DELETE_TABLE:
return this.deleteTable(json?.table?.name) return this.deleteTable(json?.table?.name)
default: default:
@ -296,52 +310,65 @@ class GoogleSheetsIntegration implements DatasourcePlus {
async createTable(name?: string) { async createTable(name?: string) {
try { try {
await this.connect() await this.connect()
return await this.client.addSheet({ title: name, headerValues: ["test"] }) return await this.client.addSheet({ title: name, headerValues: [] })
} catch (err) { } catch (err) {
console.error("Error creating new table in google sheets", err) console.error("Error creating new table in google sheets", err)
throw err throw err
} }
} }
async updateTable(table?: any) { async updateTable(table: TableRequest) {
try { await this.connect()
await this.connect() const sheet = this.client.sheetsByTitle[table.name]
const sheet = this.client.sheetsByTitle[table.name] await sheet.loadHeaderRow()
await sheet.loadHeaderRow()
if (table._rename) { if (table._rename) {
const headers = [] const headers = []
for (let header of sheet.headerValues) { for (let header of sheet.headerValues) {
if (header === table._rename.old) { if (header === table._rename.old) {
headers.push(table._rename.updated) headers.push(table._rename.updated)
} else { } else {
headers.push(header) headers.push(header)
}
} }
await sheet.setHeaderRow(headers)
} else {
const updatedHeaderValues = [...sheet.headerValues]
// add new column - doesn't currently exist
for (let key of Object.keys(table.schema)) {
if (!sheet.headerValues.includes(key)) {
updatedHeaderValues.push(key)
}
}
// clear out deleted columns
for (let key of sheet.headerValues) {
if (!Object.keys(table.schema).includes(key)) {
const idx = updatedHeaderValues.indexOf(key)
updatedHeaderValues.splice(idx, 1)
}
}
await sheet.setHeaderRow(updatedHeaderValues)
} }
} catch (err) { try {
console.error("Error updating table in google sheets", err) await sheet.setHeaderRow(headers)
throw err } catch (err) {
console.error("Error updating column name in google sheets", err)
throw err
}
} else {
const updatedHeaderValues = [...sheet.headerValues]
// add new column - doesn't currently exist
for (let [key, column] of Object.entries(table.schema)) {
if (!ALLOWED_TYPES.includes(column.type)) {
throw new Error(
`Column type: ${column.type} not allowed for GSheets integration.`
)
}
if (
!sheet.headerValues.includes(key) &&
column.type !== FieldType.FORMULA
) {
updatedHeaderValues.push(key)
}
}
// clear out deleted columns
for (let key of sheet.headerValues) {
if (!Object.keys(table.schema).includes(key)) {
const idx = updatedHeaderValues.indexOf(key)
updatedHeaderValues.splice(idx, 1)
}
}
try {
await sheet.setHeaderRow(updatedHeaderValues)
} catch (err) {
console.error("Error updating table in google sheets", err)
throw err
}
} }
} }