reshuffle-airtable-connector

A Reshuffle Airtable connector

Usage no npm install needed!

<script type="module">
  import reshuffleAirtableConnector from 'https://cdn.skypack.dev/reshuffle-airtable-connector';
</script>

README

reshuffle-airtable-connector

Code | npm | Code sample

npm install reshuffle-airtable-connector

Reshuffle Airtable Connector

This package contains a Reshuffle connector to connect Airtable APIs.

The following example exposes an endpoint to return the first page of Airtable tab 'Design projects' with view 'All projects'. After running the example go to http://localhost:8000/projects to view the results.

const { HttpConnector, Reshuffle } = require('reshuffle')
const { AirtableConnector } = require('reshuffle-airtable-connector')

const app = new Reshuffle()

const airtableConnector = new AirtableConnector(app, {
  endpointUrl: 'AIRPOINT_ENDPOINT_URL', // 'https://api.airtable.com'
  apiKey: 'YOUR_API_KEY',
  base: 'YOUR_BASE'
})

const httpConnector = new HttpConnector(app)
const base = airtableConnector.base()

httpConnector.on({ method: 'GET', path: '/projects' }, async (event, app) => {
  base('Design projects').select({
    view: 'All projects'
  }).firstPage(function(err, records) {
    if (err) {
      event.res.json(err)
      return
    }
    event.res.json(records.map(record => record.get('Name')))
  })
})

app.start()

The official Airtable JavaScript library can be found [here]](https://github.com/Airtable/airtable.js)

Table of Contents

Configuration Options

Connector Events

Listening to Airtable events

Connector Actions

Base - Retrieve a base Airtable object

SDK - Retrieve a full Airtable sdk object

Configuration options

const app = new Reshuffle()
const airtableConnector = new AirtableConnector(app, {
  endpointUrl: 'AIRPOINT_ENDPOINT_URL',
  apiKey: 'YOUR_API_KEY',
  base: 'YOUR_BASE'
})

endpointUrlis optional, the default is https://api.airtable.com.

Get your apiKey by following the steps in this article.

More details about the APIs are described in Airtable API documentation.

Events

Listening to Airtable events

In order to listen to events happening in Airtable, you'll need to capture them with the connector's on function, providing an AirtableConnectorEventOptions to it.

Events are specific to an Airtable table/tab, for example in order to define all three events (added, modified, deleted) on two tables you have to define six events.

interface AirtableConnectorEventOptions {
  type: AirtableEventType // See bellow 
  table: string           // Airtable table/tab name
  fireWhileTyping?: boolean // How to manage `RecordModified` events for text fields, check the explanation below. The default value is false.
}

// Where...
type AirtableEventType =
  | 'RecordAdded'
  | 'RecordModified'
  | 'RecordDeleted'

* 'RecordAdded' - For a Grid View, when adding a new record there is no 'Create New' button that opens a dialog that enables populating and saving the record's fields. Instead Airtable stores the new record at the time you click the create the new record, at this stage all the record's fields are empty. Due to that, a 'RecordAdded' event for a Grid View will end up having an empty record.

** fireWhileTyping - Airtable persists every keystroke as the user is typing text. This may result in several 'RecordModified' events firing for a single field change. When fireWhileTyping is false (the default value) the connector will fire an event only when the user stops typing. When fireWhileTyping is true there will be number of events, similar to the way that Airtable persisted the change.

Example:

airtableConnector.on({ type: 'RecordModified', table: 'Design projects' }, async (event, app) => {
  console.log('RecordModified on Design projects table event')
  console.log(event.id)
  console.log(event.fields)
})

Actions

base

Returns a base object providing an access to the Airtable APIs. Usually you will use base in order to execute all the Airtable APIs.

const base = airtableConnector.base()

Example:

const base = airtableConnector.base()

base('Design projects').select({
    view: 'All projects'
  }).firstPage(function(err, records) {
    if (err) {
      event.res.json(err)
      return
    }
    event.res.json(records.map(record => record.get('Name')))
  })

sdk

Usually base will be the main access to the Airtable APIs but if you need the SDK it is available by using this action.

const sdk = airtableConnector.sdk()

Example:

const base = airtableConnector.sdk().base('YOUR_BASE')

base('Design projects').select({
    view: 'All projects'
  }).firstPage(function(err, records) {
    if (err) {
      event.res.json(err)
      return
    }
    event.res.json(records.map(record => record.get('Name')))
  })