Skip to content

Commit

Permalink
✨ Add Airtable plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
julien040 committed Jul 23, 2024
1 parent 121436e commit 9fb3918
Show file tree
Hide file tree
Showing 12 changed files with 1,333 additions and 0 deletions.
18 changes: 18 additions & 0 deletions plugins/airtable/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

# Go template downloaded with gut
*.exe
*.exe~
*.dll
*.so
*.dylib
*.test
*.out
go.work
.gut

# Dev files
*.log
devManifest.*
.init

dist/
29 changes: 29 additions & 0 deletions plugins/airtable/.goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

version: 2

before:
hooks:
# You may remove this if you don't use go modules.
- go mod tidy

builds:
- env:
- CGO_ENABLED=0
goos:
- linux
- windows
- darwin

goarch:
- amd64
- arm64

archives:
- format: binary

changelog:
sort: asc
filters:
exclude:
- "^docs:"
- "^test:"
13 changes: 13 additions & 0 deletions plugins/airtable/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

files := $(wildcard *.go)

all: $(files)
go build -o airtable.out $(files)

prod: $(files)
go build -o airtable.out -ldflags "-s -w" $(files)

clean:
rm -f notion.out

.PHONY: all clean
50 changes: 50 additions & 0 deletions plugins/airtable/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Airtable plugin

Use an [Airtable](https://airtable.com/) base as a SQL database.

## Setup

```bash
anyquery install airtable
```

The plugin will request the following information:

1. Airtable API key: Go to the [tokens page](https://airtable.com/create/tokens) and create a new token. Add the scopes:
- data.records:read
- data.records:write
- schema.bases:read
- schema.bases:write (if you want add new item in a select field with SQL, enable this scope, otherwise you can disable it)

Once done, select the bases you want to query and copy the token.
Finally, paste the token in the prompt.
2. Airtable base ID. To find it, open your base in Airtable and copy the ID from the URL. It is the string after `https://airtable.com/` and before the first `/`. Example `https://airtable.com/appWx9fD5JzAB4TIO/tblnTJJsUb8f7QjiM/viwDj8WIME?blocks=hide` the base ID is `appWx9fD5JzAB4TIO`.
3. Airtable table name. The name of the table you want to query or its ID. To find the name, open the table in Airtable and copy the name from the top left corner. Example `Table 1`.
4. Enable or disable the cache. If you enable the cache, the plugin will store the data locally for a faster response for an hour. If you disable the cache, the plugin will fetch the data from Airtable every time you run a query. Enabling the cache is recommended for better performance but might result in outdated data. You can delete the cache at any time by running `anyquery -q "SELECT clear_plugin_cache('airtable')"`

## Usage

Each column in the table will be a column in the SQL table. The plugin will automatically infer the data type of each column. References are represented as JSON arrays of record IDs. Users references are represented as JSON arrays of user IDs.

The plugin only adds two columns to the table: `id` and `createdTime`. The `id` column is the record ID in Airtable. The `createdTime` column is the time the record was created.

```sql
-- List all the records in the table
SELECT * FROM airtable_table;
-- List all the records in the table in the view "Grid view"
SELECT * FROM airtable_table('Grid view');
-- Insert a new record
INSERT INTO airtable_table (column1, column2) VALUES ('value1', 'value2');
-- Update a record
UPDATE airtable_table SET column1 = 'new_value' WHERE id = 'rec123';
-- Delete a record
DELETE FROM airtable_table WHERE id = 'rec123';
```

## Limitations

- Due to rate limits, cold runs caps at 5 requests per second. It therefore means you can read 500 records per second, and insert/modify/delete 45 records per second.

## Troubleshooting

- `failed to get records(422): {"error":{"type":"LIST_RECORDS_ITERATOR_NOT_AVAILABLE"}}`: This error occurs when the plugin tries to fetch records from Airtable after a long time they were put in the cache. To fix this, run `anyquery -q "SELECT clear_plugin_cache('airtable')"`, and restart anyquery.
43 changes: 43 additions & 0 deletions plugins/airtable/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module github.com/julien040/anyquery/plugins/airtable

go 1.22.5

require (
github.com/adrg/xdg v0.5.0
github.com/dgraph-io/badger/v4 v4.2.0
github.com/go-resty/resty/v2 v2.13.1
github.com/hashicorp/go-retryablehttp v0.7.7
github.com/julien040/anyquery v0.0.0-20240723094718-121436e52d02
go.uber.org/ratelimit v0.3.1
)

require (
github.com/benbjohnson/clock v1.3.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/dgraph-io/ristretto v0.1.1 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/fatih/color v1.17.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/glog v1.2.1 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/golang/snappy v0.0.3 // indirect
github.com/google/flatbuffers v1.12.1 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-hclog v1.6.3 // indirect
github.com/hashicorp/go-plugin v1.6.1 // indirect
github.com/hashicorp/yamux v0.1.1 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/oklog/run v1.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240722135656-d784300faade // indirect
google.golang.org/grpc v1.65.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
)
Loading

0 comments on commit 9fb3918

Please sign in to comment.