2021-11-25 18:12:12 +01:00
|
|
|
import {
|
|
|
|
DatasourceFieldTypes,
|
|
|
|
Integration,
|
2021-11-25 20:12:32 +01:00
|
|
|
QueryJson,
|
2021-11-25 18:12:12 +01:00
|
|
|
QueryTypes,
|
|
|
|
} from "../definitions/datasource"
|
|
|
|
import { IntegrationBase } from "./base/IntegrationBase"
|
2022-01-06 09:08:54 +01:00
|
|
|
import { OAuth2Client } from "google-auth-library"
|
2021-11-25 18:12:12 +01:00
|
|
|
import { GoogleSpreadsheet } from "google-spreadsheet"
|
2021-11-25 20:12:32 +01:00
|
|
|
import { DatasourcePlus } from "./base/datasourcePlus"
|
|
|
|
import { Table } from "../definitions/common"
|
|
|
|
import { buildExternalTableId } from "./utils"
|
2022-01-06 09:08:54 +01:00
|
|
|
import {
|
|
|
|
DataSourceOperation,
|
|
|
|
FieldTypes,
|
|
|
|
DatasourceAuthTypes,
|
|
|
|
} from "../constants"
|
|
|
|
import env from "../environment"
|
|
|
|
import CouchDB from "../db"
|
|
|
|
import { timeStamp } from "console"
|
2021-11-25 18:12:12 +01:00
|
|
|
|
|
|
|
module GoogleSheetsModule {
|
|
|
|
interface GoogleSheetsConfig {
|
|
|
|
spreadsheetId: string
|
2022-01-06 09:08:54 +01:00
|
|
|
auth: OAuthClientConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
interface OAuthClientConfig {
|
|
|
|
appId: string
|
|
|
|
accessToken: string
|
|
|
|
refreshToken: string
|
2021-11-25 18:12:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const SCHEMA: Integration = {
|
2021-11-25 20:12:32 +01:00
|
|
|
plus: true,
|
2022-01-06 09:08:54 +01:00
|
|
|
auth: {
|
|
|
|
type: "google",
|
|
|
|
},
|
2021-11-25 18:12:12 +01:00
|
|
|
docs: "https://developers.google.com/sheets/api/quickstart/nodejs",
|
|
|
|
description:
|
|
|
|
"Create and collaborate on online spreadsheets in real-time and from any device. ",
|
|
|
|
friendlyName: "Google Sheets",
|
|
|
|
datasource: {
|
|
|
|
spreadsheetId: {
|
|
|
|
type: DatasourceFieldTypes.STRING,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
query: {
|
|
|
|
create: {
|
|
|
|
type: QueryTypes.FIELDS,
|
|
|
|
fields: {
|
|
|
|
sheet: {
|
2022-01-06 09:08:54 +01:00
|
|
|
type: DatasourceFieldTypes.STRING,
|
2021-11-25 18:12:12 +01:00
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
row: {
|
|
|
|
type: QueryTypes.JSON,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
read: {
|
|
|
|
type: QueryTypes.FIELDS,
|
|
|
|
fields: {
|
|
|
|
sheet: {
|
2022-01-06 09:08:54 +01:00
|
|
|
type: DatasourceFieldTypes.STRING,
|
2021-11-25 18:12:12 +01:00
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
update: {
|
|
|
|
type: QueryTypes.FIELDS,
|
|
|
|
fields: {
|
|
|
|
sheet: {
|
2022-01-06 09:08:54 +01:00
|
|
|
type: DatasourceFieldTypes.STRING,
|
2021-11-25 18:12:12 +01:00
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
rowIndex: {
|
2022-01-06 09:08:54 +01:00
|
|
|
type: DatasourceFieldTypes.STRING,
|
2021-11-25 18:12:12 +01:00
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
row: {
|
|
|
|
type: QueryTypes.JSON,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
delete: {
|
|
|
|
type: QueryTypes.FIELDS,
|
|
|
|
fields: {
|
|
|
|
sheet: {
|
2022-01-06 09:08:54 +01:00
|
|
|
type: DatasourceFieldTypes.STRING,
|
2021-11-25 18:12:12 +01:00
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
rowIndex: {
|
2022-01-06 09:08:54 +01:00
|
|
|
type: DatasourceFieldTypes.NUMBER,
|
2021-11-25 18:12:12 +01:00
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-11-25 20:12:32 +01:00
|
|
|
class GoogleSheetsIntegration implements DatasourcePlus {
|
2021-11-25 18:12:12 +01:00
|
|
|
private readonly config: GoogleSheetsConfig
|
|
|
|
private client: any
|
2021-11-25 20:12:32 +01:00
|
|
|
public tables: Record<string, Table> = {}
|
|
|
|
public schemaErrors: Record<string, string> = {}
|
2021-11-25 18:12:12 +01:00
|
|
|
|
|
|
|
constructor(config: GoogleSheetsConfig) {
|
|
|
|
this.config = config
|
|
|
|
this.client = new GoogleSpreadsheet(this.config.spreadsheetId)
|
|
|
|
}
|
|
|
|
|
|
|
|
async connect() {
|
|
|
|
try {
|
2022-01-06 09:08:54 +01:00
|
|
|
// Initialise oAuth client
|
|
|
|
// TODO: Move this to auth lib
|
|
|
|
const oauthClient = new OAuth2Client({
|
|
|
|
clientId: env.GOOGLE_CLIENT_ID,
|
|
|
|
clientSecret: env.GOOGLE_CLIENT_SECRET,
|
2021-11-25 18:12:12 +01:00
|
|
|
})
|
2022-01-06 09:08:54 +01:00
|
|
|
oauthClient.credentials.access_token = this.config.auth.accessToken
|
|
|
|
oauthClient.credentials.refresh_token = this.config.auth.refreshToken
|
|
|
|
this.client.useOAuth2Client(oauthClient)
|
2021-11-25 18:12:12 +01:00
|
|
|
await this.client.loadInfo()
|
|
|
|
} catch (err) {
|
|
|
|
console.error("Error connecting to google sheets", err)
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-25 20:12:32 +01:00
|
|
|
async buildSchema(datasourceId: string, entities: Record<string, Table>) {
|
|
|
|
await this.connect()
|
|
|
|
const sheets = await this.client.sheetsByIndex
|
|
|
|
const tables = {}
|
|
|
|
// tables[tableName] = {
|
|
|
|
// _id: buildExternalTableId(datasourceId, tableName),
|
|
|
|
// primary: primaryKeys,
|
|
|
|
// name: tableName,
|
|
|
|
// schema,
|
|
|
|
// }
|
|
|
|
for (let sheet of sheets) {
|
|
|
|
// must fetch rows to determine schema
|
|
|
|
await sheet.getRows()
|
|
|
|
// build schema
|
|
|
|
const schema = {}
|
|
|
|
|
|
|
|
// build schema from headers
|
|
|
|
for (let header of sheet.headerValues) {
|
|
|
|
schema[header] = {
|
|
|
|
name: header,
|
|
|
|
type: FieldTypes.STRING,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create tables
|
|
|
|
tables[sheet.title] = {
|
|
|
|
_id: buildExternalTableId(datasourceId, sheet.title),
|
|
|
|
name: sheet.title,
|
|
|
|
primary: ["rowNumber"],
|
|
|
|
schema,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.tables = tables
|
|
|
|
}
|
|
|
|
|
|
|
|
async query(json: QueryJson) {
|
|
|
|
const sheet = json.endpoint.entityId
|
|
|
|
|
|
|
|
if (json.endpoint.operation === DataSourceOperation.CREATE) {
|
|
|
|
return await this.create({
|
|
|
|
sheet,
|
|
|
|
row: json.body,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if (json.endpoint.operation === DataSourceOperation.READ) {
|
|
|
|
return await this.read({ sheet })
|
|
|
|
}
|
|
|
|
|
|
|
|
if (json.endpoint.operation === DataSourceOperation.UPDATE) {
|
|
|
|
return await this.update({
|
|
|
|
sheet,
|
|
|
|
row: json.body,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if (json.endpoint.operation === DataSourceOperation.DELETE) {
|
2022-01-06 09:08:54 +01:00
|
|
|
return await this.delete({
|
|
|
|
// TODO: complete
|
|
|
|
})
|
2021-11-25 20:12:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buildRowObject(headers: string[], values: string[], rowNumber: number) {
|
|
|
|
const rowObject = { rowNumber }
|
2021-11-25 18:12:12 +01:00
|
|
|
for (let i = 0; i < headers.length; i++) {
|
|
|
|
rowObject[headers[i]] = values[i]
|
|
|
|
}
|
|
|
|
return rowObject
|
|
|
|
}
|
|
|
|
|
2021-11-25 20:12:32 +01:00
|
|
|
async create(query: { sheet: string; row: string | object }) {
|
2021-11-25 18:12:12 +01:00
|
|
|
try {
|
|
|
|
await this.connect()
|
|
|
|
const sheet = await this.client.sheetsByTitle[query.sheet]
|
2021-11-25 20:12:32 +01:00
|
|
|
const rowToInsert =
|
|
|
|
typeof query.row === "string" ? JSON.parse(query.row) : query.row
|
2021-11-25 18:12:12 +01:00
|
|
|
const row = await sheet.addRow(rowToInsert)
|
2021-11-25 20:12:32 +01:00
|
|
|
return [
|
|
|
|
this.buildRowObject(sheet.headerValues, row._rawData, row._rowNumber),
|
|
|
|
]
|
2021-11-25 18:12:12 +01:00
|
|
|
} catch (err) {
|
|
|
|
console.error("Error writing to google sheets", err)
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async read(query: { sheet: string }) {
|
|
|
|
try {
|
|
|
|
await this.connect()
|
|
|
|
const sheet = await this.client.sheetsByTitle[query.sheet]
|
|
|
|
const rows = await sheet.getRows()
|
|
|
|
const headerValues = sheet.headerValues
|
|
|
|
const response = []
|
|
|
|
for (let row of rows) {
|
2021-11-25 20:12:32 +01:00
|
|
|
response.push(
|
|
|
|
this.buildRowObject(headerValues, row._rawData, row._rowNumber)
|
|
|
|
)
|
2021-11-25 18:12:12 +01:00
|
|
|
}
|
|
|
|
return response
|
|
|
|
} catch (err) {
|
|
|
|
console.error("Error reading from google sheets", err)
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async update(query: { sheet: string; rowIndex: number; row: string }) {
|
|
|
|
try {
|
|
|
|
await this.connect()
|
|
|
|
const sheet = await this.client.sheetsByTitle[query.sheet]
|
|
|
|
const rows = await sheet.getRows()
|
|
|
|
const row = rows[query.rowIndex]
|
|
|
|
if (row) {
|
|
|
|
const updateValues = JSON.parse(query.row)
|
|
|
|
for (let key in updateValues) {
|
|
|
|
row[key] = updateValues[key]
|
|
|
|
}
|
|
|
|
await row.save()
|
2021-11-25 20:12:32 +01:00
|
|
|
return [
|
|
|
|
this.buildRowObject(
|
|
|
|
sheet.headerValues,
|
|
|
|
row._rawData,
|
|
|
|
row._rowNumber
|
|
|
|
),
|
|
|
|
]
|
2021-11-25 18:12:12 +01:00
|
|
|
} else {
|
|
|
|
throw new Error("Row does not exist.")
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.error("Error reading from google sheets", err)
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async delete(query: { sheet: string; rowIndex: number }) {
|
|
|
|
await this.connect()
|
|
|
|
const sheet = await this.client.sheetsByTitle[query.sheet]
|
|
|
|
const rows = await sheet.getRows()
|
|
|
|
const row = rows[query.rowIndex]
|
|
|
|
if (row) {
|
|
|
|
await row.delete()
|
|
|
|
return [{ deleted: query.rowIndex }]
|
|
|
|
} else {
|
|
|
|
throw new Error("Row does not exist.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
schema: SCHEMA,
|
|
|
|
integration: GoogleSheetsIntegration,
|
|
|
|
}
|
|
|
|
}
|