2021-06-24 19:17:26 +02:00
|
|
|
import {
|
2023-05-05 16:47:55 +02:00
|
|
|
DatasourceFeature,
|
2022-08-11 14:50:05 +02:00
|
|
|
DatasourceFieldType,
|
2023-05-05 16:47:55 +02:00
|
|
|
Integration,
|
2022-08-11 12:48:58 +02:00
|
|
|
IntegrationBase,
|
2023-05-05 16:47:55 +02:00
|
|
|
QueryType,
|
2022-08-11 12:48:58 +02:00
|
|
|
} from "@budibase/types"
|
2021-06-24 19:16:48 +02:00
|
|
|
|
2022-08-12 18:03:06 +02:00
|
|
|
const Airtable = require("airtable")
|
2021-06-24 19:16:48 +02:00
|
|
|
|
2022-08-12 18:03:06 +02:00
|
|
|
interface AirtableConfig {
|
|
|
|
apiKey: string
|
|
|
|
base: string
|
|
|
|
}
|
2021-06-24 19:16:48 +02:00
|
|
|
|
2022-08-12 18:03:06 +02:00
|
|
|
const SCHEMA: Integration = {
|
|
|
|
docs: "https://airtable.com/api",
|
|
|
|
description:
|
|
|
|
"Airtable is a spreadsheet-database hybrid, with the features of a database but applied to a spreadsheet.",
|
|
|
|
friendlyName: "Airtable",
|
|
|
|
type: "Spreadsheet",
|
2023-05-15 18:36:16 +02:00
|
|
|
features: [],
|
2022-08-12 18:03:06 +02:00
|
|
|
datasource: {
|
|
|
|
apiKey: {
|
|
|
|
type: DatasourceFieldType.PASSWORD,
|
|
|
|
default: "enter api key",
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
base: {
|
|
|
|
type: DatasourceFieldType.STRING,
|
|
|
|
default: "mybase",
|
|
|
|
required: true,
|
2021-06-24 19:16:48 +02:00
|
|
|
},
|
2022-08-12 18:03:06 +02:00
|
|
|
},
|
|
|
|
query: {
|
|
|
|
create: {
|
|
|
|
type: QueryType.FIELDS,
|
|
|
|
customisable: true,
|
|
|
|
fields: {
|
|
|
|
table: {
|
|
|
|
type: DatasourceFieldType.STRING,
|
|
|
|
required: true,
|
2021-06-24 19:16:48 +02:00
|
|
|
},
|
|
|
|
},
|
2022-08-12 18:03:06 +02:00
|
|
|
},
|
|
|
|
read: {
|
|
|
|
type: QueryType.FIELDS,
|
|
|
|
fields: {
|
|
|
|
table: {
|
|
|
|
type: DatasourceFieldType.STRING,
|
|
|
|
required: true,
|
2021-06-24 19:16:48 +02:00
|
|
|
},
|
2022-08-12 18:03:06 +02:00
|
|
|
view: {
|
|
|
|
type: DatasourceFieldType.STRING,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
numRecords: {
|
|
|
|
type: DatasourceFieldType.NUMBER,
|
|
|
|
default: 10,
|
2021-06-24 19:16:48 +02:00
|
|
|
},
|
|
|
|
},
|
2022-08-12 18:03:06 +02:00
|
|
|
},
|
|
|
|
update: {
|
|
|
|
type: QueryType.FIELDS,
|
|
|
|
customisable: true,
|
|
|
|
fields: {
|
|
|
|
id: {
|
|
|
|
display: "Record ID",
|
|
|
|
type: DatasourceFieldType.STRING,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
table: {
|
|
|
|
type: DatasourceFieldType.STRING,
|
|
|
|
required: true,
|
|
|
|
},
|
2021-06-24 19:16:48 +02:00
|
|
|
},
|
|
|
|
},
|
2022-08-12 18:03:06 +02:00
|
|
|
delete: {
|
|
|
|
type: QueryType.JSON,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-06-24 19:16:48 +02:00
|
|
|
|
2022-08-12 18:03:06 +02:00
|
|
|
class AirtableIntegration implements IntegrationBase {
|
|
|
|
private config: AirtableConfig
|
|
|
|
private client: any
|
2021-06-24 19:16:48 +02:00
|
|
|
|
2022-08-12 18:03:06 +02:00
|
|
|
constructor(config: AirtableConfig) {
|
|
|
|
this.config = config
|
|
|
|
this.client = new Airtable(config).base(config.base)
|
|
|
|
}
|
2021-06-24 19:16:48 +02:00
|
|
|
|
2022-08-12 18:03:06 +02:00
|
|
|
async create(query: { table: any; json: any }) {
|
|
|
|
const { table, json } = query
|
2021-06-24 19:16:48 +02:00
|
|
|
|
2022-08-12 18:03:06 +02:00
|
|
|
try {
|
|
|
|
return await this.client(table).create([
|
|
|
|
{
|
|
|
|
fields: json,
|
|
|
|
},
|
|
|
|
])
|
|
|
|
} catch (err) {
|
|
|
|
console.error("Error writing to airtable", err)
|
|
|
|
throw err
|
2021-06-24 19:16:48 +02:00
|
|
|
}
|
2022-08-12 18:03:06 +02:00
|
|
|
}
|
2021-06-24 19:16:48 +02:00
|
|
|
|
2022-08-12 18:03:06 +02:00
|
|
|
async read(query: { table: any; numRecords: any; view: any }) {
|
|
|
|
try {
|
|
|
|
const records = await this.client(query.table)
|
|
|
|
.select({ maxRecords: query.numRecords || 10, view: query.view })
|
|
|
|
.firstPage()
|
|
|
|
// @ts-ignore
|
|
|
|
return records.map(({ fields }) => fields)
|
|
|
|
} catch (err) {
|
|
|
|
console.error("Error writing to airtable", err)
|
|
|
|
return []
|
2021-06-24 19:16:48 +02:00
|
|
|
}
|
2022-08-12 18:03:06 +02:00
|
|
|
}
|
2021-06-24 19:16:48 +02:00
|
|
|
|
2022-08-12 18:03:06 +02:00
|
|
|
async update(query: { table: any; id: any; json: any }) {
|
|
|
|
const { table, id, json } = query
|
2021-06-24 19:16:48 +02:00
|
|
|
|
2022-08-12 18:03:06 +02:00
|
|
|
try {
|
|
|
|
return await this.client(table).update([
|
|
|
|
{
|
|
|
|
id,
|
|
|
|
fields: json,
|
|
|
|
},
|
|
|
|
])
|
|
|
|
} catch (err) {
|
|
|
|
console.error("Error writing to airtable", err)
|
|
|
|
throw err
|
2021-06-24 19:16:48 +02:00
|
|
|
}
|
2022-08-12 18:03:06 +02:00
|
|
|
}
|
2021-06-24 19:16:48 +02:00
|
|
|
|
2022-08-12 18:03:06 +02:00
|
|
|
async delete(query: { table: any; ids: any }) {
|
|
|
|
try {
|
|
|
|
return await this.client(query.table).destroy(query.ids)
|
|
|
|
} catch (err) {
|
|
|
|
console.error("Error writing to airtable", err)
|
|
|
|
throw err
|
2021-06-24 19:16:48 +02:00
|
|
|
}
|
|
|
|
}
|
2022-08-12 18:03:06 +02:00
|
|
|
}
|
2021-06-24 19:16:48 +02:00
|
|
|
|
2022-08-12 18:03:06 +02:00
|
|
|
export default {
|
|
|
|
schema: SCHEMA,
|
|
|
|
integration: AirtableIntegration,
|
2021-06-24 19:16:48 +02:00
|
|
|
}
|