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,
|
|
|
|
},
|
|
|
|
},
|
2021-01-12 18:45:43 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-01-12 18:45:43 +01:00
|
|
|
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)
|
|
|
|
}
|
2021-01-12 18:45:43 +01:00
|
|
|
|
|
|
|
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,
|
|
|
|
}
|