2021-01-11 18:18:22 +01:00
|
|
|
const Airtable = require("airtable")
|
2021-01-13 17:39:47 +01:00
|
|
|
const { FIELD_TYPES, QUERY_TYPES } = require("./Integration")
|
2021-01-11 18:18:22 +01:00
|
|
|
|
|
|
|
const SCHEMA = {
|
|
|
|
datasource: {
|
|
|
|
apiKey: {
|
2021-01-13 17:39:47 +01:00
|
|
|
type: FIELD_TYPES.STRING,
|
2021-01-11 18:18:22 +01:00
|
|
|
default: "enter api key",
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
base: {
|
2021-01-13 17:39:47 +01:00
|
|
|
type: FIELD_TYPES.STRING,
|
2021-01-11 18:18:22 +01:00
|
|
|
default: "mybase",
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
},
|
2021-01-12 18:45:43 +01:00
|
|
|
query: {
|
2021-01-13 15:11:53 +01:00
|
|
|
create: {
|
|
|
|
"Airtable Record": {
|
|
|
|
type: "fields",
|
|
|
|
customisable: true,
|
|
|
|
fields: {
|
|
|
|
table: {
|
2021-01-13 17:39:47 +01:00
|
|
|
type: FIELD_TYPES.STRING,
|
2021-01-13 15:11:53 +01:00
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
read: {
|
|
|
|
Table: {
|
2021-01-13 17:39:47 +01:00
|
|
|
type: QUERY_TYPES.FIELDS,
|
2021-01-13 15:11:53 +01:00
|
|
|
fields: {
|
|
|
|
table: {
|
2021-01-13 17:39:47 +01:00
|
|
|
type: FIELD_TYPES.STRING,
|
2021-01-13 15:11:53 +01:00
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
view: {
|
2021-01-13 17:39:47 +01:00
|
|
|
type: FIELD_TYPES.STRING,
|
2021-01-13 15:11:53 +01:00
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
update: {
|
|
|
|
Fields: {
|
2021-01-13 17:39:47 +01:00
|
|
|
type: QUERY_TYPES.FIELDS,
|
2021-01-13 15:11:53 +01:00
|
|
|
customisable: true,
|
|
|
|
fields: {
|
|
|
|
id: {
|
2021-01-13 17:39:47 +01:00
|
|
|
type: FIELD_TYPES.STRING,
|
2021-01-13 15:11:53 +01:00
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-01-12 18:45:43 +01:00
|
|
|
},
|
2021-01-13 15:11:53 +01:00
|
|
|
delete: {
|
|
|
|
"Airtable Ids": {
|
2021-01-13 17:39:47 +01:00
|
|
|
type: FIELD_TYPES.LIST,
|
2021-01-13 15:11:53 +01:00
|
|
|
},
|
2021-01-12 18:45:43 +01:00
|
|
|
},
|
|
|
|
},
|
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-13 15:11:53 +01:00
|
|
|
async create(query) {
|
|
|
|
const { table, ...rest } = query
|
|
|
|
|
2021-01-12 18:45:43 +01:00
|
|
|
try {
|
2021-01-13 15:11:53 +01:00
|
|
|
const records = await this.client(table).create([
|
2021-01-12 18:45:43 +01:00
|
|
|
{
|
2021-01-13 15:11:53 +01:00
|
|
|
fields: rest,
|
2021-01-12 18:45:43 +01:00
|
|
|
},
|
|
|
|
])
|
|
|
|
return records
|
|
|
|
} catch (err) {
|
|
|
|
console.error("Error writing to airtable", err)
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}
|
2021-01-11 18:18:22 +01:00
|
|
|
|
2021-01-13 15:11:53 +01:00
|
|
|
async read(query) {
|
|
|
|
try {
|
|
|
|
const records = await this.client(query.table)
|
|
|
|
.select({ maxRecords: 10, view: query.view })
|
|
|
|
.firstPage()
|
|
|
|
return records.map(({ fields }) => fields)
|
|
|
|
} catch (err) {
|
|
|
|
console.error("Error writing to airtable", err)
|
|
|
|
return []
|
|
|
|
}
|
2021-01-11 18:18:22 +01:00
|
|
|
}
|
2021-01-12 18:45:43 +01:00
|
|
|
|
2021-01-13 15:11:53 +01:00
|
|
|
async update(query) {
|
|
|
|
const { table, id, ...rest } = query
|
2021-01-12 18:45:43 +01:00
|
|
|
|
|
|
|
try {
|
2021-01-13 15:11:53 +01:00
|
|
|
const records = await this.client(table).update([
|
2021-01-12 18:45:43 +01:00
|
|
|
{
|
|
|
|
id,
|
|
|
|
fields: rest,
|
|
|
|
},
|
|
|
|
])
|
|
|
|
return records
|
|
|
|
} catch (err) {
|
|
|
|
console.error("Error writing to airtable", err)
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-13 15:11:53 +01:00
|
|
|
async delete(query) {
|
2021-01-12 18:45:43 +01:00
|
|
|
try {
|
2021-01-13 15:11:53 +01:00
|
|
|
const records = await this.client(query.table).destroy(query.ids)
|
2021-01-12 18:45:43 +01:00
|
|
|
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,
|
|
|
|
}
|