Types
This commit is contained in:
parent
30fde61d4d
commit
abe06a127a
|
@ -13,7 +13,7 @@ import { DataSourceOperation, FieldTypes } from "../constants"
|
||||||
import { GoogleSpreadsheet } from "google-spreadsheet"
|
import { GoogleSpreadsheet } from "google-spreadsheet"
|
||||||
import env from "../environment"
|
import env from "../environment"
|
||||||
import { tenancy, db as dbCore, constants } from "@budibase/backend-core"
|
import { tenancy, db as dbCore, constants } from "@budibase/backend-core"
|
||||||
const fetch = require("node-fetch")
|
import fetch from "node-fetch"
|
||||||
|
|
||||||
interface GoogleSheetsConfig {
|
interface GoogleSheetsConfig {
|
||||||
spreadsheetId: string
|
spreadsheetId: string
|
||||||
|
@ -112,7 +112,7 @@ const SCHEMA: Integration = {
|
||||||
|
|
||||||
class GoogleSheetsIntegration implements DatasourcePlus {
|
class GoogleSheetsIntegration implements DatasourcePlus {
|
||||||
private readonly config: GoogleSheetsConfig
|
private readonly config: GoogleSheetsConfig
|
||||||
private client: any
|
private client: GoogleSpreadsheet
|
||||||
public tables: Record<string, Table> = {}
|
public tables: Record<string, Table> = {}
|
||||||
public schemaErrors: Record<string, string> = {}
|
public schemaErrors: Record<string, string> = {}
|
||||||
|
|
||||||
|
@ -211,7 +211,7 @@ class GoogleSheetsIntegration implements DatasourcePlus {
|
||||||
|
|
||||||
async buildSchema(datasourceId: string) {
|
async buildSchema(datasourceId: string) {
|
||||||
await this.connect()
|
await this.connect()
|
||||||
const sheets = await this.client.sheetsByIndex
|
const sheets = this.client.sheetsByIndex
|
||||||
const tables: Record<string, Table> = {}
|
const tables: Record<string, Table> = {}
|
||||||
for (let sheet of sheets) {
|
for (let sheet of sheets) {
|
||||||
// must fetch rows to determine schema
|
// must fetch rows to determine schema
|
||||||
|
@ -294,7 +294,7 @@ class GoogleSheetsIntegration implements DatasourcePlus {
|
||||||
async updateTable(table?: any) {
|
async updateTable(table?: any) {
|
||||||
try {
|
try {
|
||||||
await this.connect()
|
await this.connect()
|
||||||
const sheet = await this.client.sheetsByTitle[table.name]
|
const sheet = this.client.sheetsByTitle[table.name]
|
||||||
await sheet.loadHeaderRow()
|
await sheet.loadHeaderRow()
|
||||||
|
|
||||||
if (table._rename) {
|
if (table._rename) {
|
||||||
|
@ -329,7 +329,7 @@ class GoogleSheetsIntegration implements DatasourcePlus {
|
||||||
async deleteTable(sheet: any) {
|
async deleteTable(sheet: any) {
|
||||||
try {
|
try {
|
||||||
await this.connect()
|
await this.connect()
|
||||||
const sheetToDelete = await this.client.sheetsByTitle[sheet]
|
const sheetToDelete = this.client.sheetsByTitle[sheet]
|
||||||
return await sheetToDelete.delete()
|
return await sheetToDelete.delete()
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("Error deleting table in google sheets", err)
|
console.error("Error deleting table in google sheets", err)
|
||||||
|
@ -340,7 +340,7 @@ class GoogleSheetsIntegration implements DatasourcePlus {
|
||||||
async create(query: { sheet: string; row: any }) {
|
async create(query: { sheet: string; row: any }) {
|
||||||
try {
|
try {
|
||||||
await this.connect()
|
await this.connect()
|
||||||
const sheet = await this.client.sheetsByTitle[query.sheet]
|
const sheet = this.client.sheetsByTitle[query.sheet]
|
||||||
const rowToInsert =
|
const rowToInsert =
|
||||||
typeof query.row === "string" ? JSON.parse(query.row) : query.row
|
typeof query.row === "string" ? JSON.parse(query.row) : query.row
|
||||||
const row = await sheet.addRow(rowToInsert)
|
const row = await sheet.addRow(rowToInsert)
|
||||||
|
@ -356,7 +356,7 @@ class GoogleSheetsIntegration implements DatasourcePlus {
|
||||||
async read(query: { sheet: string }) {
|
async read(query: { sheet: string }) {
|
||||||
try {
|
try {
|
||||||
await this.connect()
|
await this.connect()
|
||||||
const sheet = await this.client.sheetsByTitle[query.sheet]
|
const sheet = this.client.sheetsByTitle[query.sheet]
|
||||||
const rows = await sheet.getRows()
|
const rows = await sheet.getRows()
|
||||||
const headerValues = sheet.headerValues
|
const headerValues = sheet.headerValues
|
||||||
const response = []
|
const response = []
|
||||||
|
@ -375,7 +375,7 @@ class GoogleSheetsIntegration implements DatasourcePlus {
|
||||||
async update(query: { sheet: string; rowIndex: number; row: any }) {
|
async update(query: { sheet: string; rowIndex: number; row: any }) {
|
||||||
try {
|
try {
|
||||||
await this.connect()
|
await this.connect()
|
||||||
const sheet = await this.client.sheetsByTitle[query.sheet]
|
const sheet = this.client.sheetsByTitle[query.sheet]
|
||||||
const rows = await sheet.getRows()
|
const rows = await sheet.getRows()
|
||||||
const row = rows[query.rowIndex]
|
const row = rows[query.rowIndex]
|
||||||
if (row) {
|
if (row) {
|
||||||
|
@ -399,7 +399,7 @@ class GoogleSheetsIntegration implements DatasourcePlus {
|
||||||
|
|
||||||
async delete(query: { sheet: string; rowIndex: number }) {
|
async delete(query: { sheet: string; rowIndex: number }) {
|
||||||
await this.connect()
|
await this.connect()
|
||||||
const sheet = await this.client.sheetsByTitle[query.sheet]
|
const sheet = this.client.sheetsByTitle[query.sheet]
|
||||||
const rows = await sheet.getRows()
|
const rows = await sheet.getRows()
|
||||||
const row = rows[query.rowIndex]
|
const row = rows[query.rowIndex]
|
||||||
if (row) {
|
if (row) {
|
||||||
|
|
Loading…
Reference in New Issue