budibase/packages/server/src/integrations/airtable.js

91 lines
1.7 KiB
JavaScript
Raw Normal View History

2021-01-11 18:18:22 +01:00
const Airtable = require("airtable")
const SCHEMA = {
datasource: {
apiKey: {
type: "string",
default: "enter api key",
required: true,
},
base: {
type: "string",
default: "mybase",
required: true,
},
table: {
type: "string",
default: "mytable",
required: true,
},
},
query: {
Custom: {
type: "fields",
custom: true,
},
"Airtable Ids": {
type: "list",
},
},
2021-01-11 18:18:22 +01:00
}
class AirtableIntegration {
constructor(config) {
this.config = config
this.client = new Airtable(config).base(config.base)
}
async create(record) {
try {
const records = await this.client(this.config.table).create([
{
fields: record,
},
])
return records
} catch (err) {
console.error("Error writing to airtable", err)
throw err
}
}
2021-01-11 18:18:22 +01:00
2021-01-12 17:49:11 +01:00
async read() {
2021-01-11 18:18:22 +01:00
const records = await this.client(this.config.table)
2021-01-12 17:49:11 +01:00
.select({ maxRecords: this.query.records, view: this.query.view })
2021-01-11 18:18:22 +01:00
.firstPage()
return records.map(({ fields }) => fields)
}
async update(document) {
const { id, ...rest } = document
try {
const records = await this.client(this.config.table).update([
{
id,
fields: rest,
},
])
return records
} catch (err) {
console.error("Error writing to airtable", err)
throw err
}
}
async delete(id) {
try {
const records = await this.client(this.config.table).destroy([id])
return records
} catch (err) {
console.error("Error writing to airtable", err)
throw err
}
}
2021-01-11 18:18:22 +01:00
}
module.exports = {
schema: SCHEMA,
integration: AirtableIntegration,
}