Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add airtable-cache prototype #16

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft

Add airtable-cache prototype #16

wants to merge 1 commit into from

Conversation

domdomegg
Copy link
Member

No description provided.

@@ -0,0 +1,35 @@
# airtable-cache

A caching proxy to Airtable. Consumers can generally ignore the details, and just pretend this is Airtable itself for standard [Airtable API](https://airtable.com/developers/web/api/) routes. For example, with [`airtable-ts`](https://github.com/domdomegg/airtable-ts):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to explain why this exists, i.e.:

Airtable has low rate limits
Airtable is slow (sometimes)


To deploy a new version, simply commit to the master branch. GitHub Actions automatically handles CD.

## TODO
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete this TODO - has now been implemented

i.get('/v0/:baseId/:tableId', cachedGetHandler);
i.get('/v0/:baseId/:tableId/:recordId', cachedGetHandler);

// TODO: clear out base schema
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These TODOs are done

@@ -0,0 +1,74 @@
import z from 'zod';
import { sql } from 'kysely';
import { db } from '../db/client';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reckon we should probably scrap the entire database, and just use an in-memory cache.

Postgres uses up a bunch of memory in practice (~200MB) and we probably don't need all its functionality.

Maybe we want to keep Postgres if we want to set up webhooks, so we don't set up webhooks then forget about them.

@@ -0,0 +1,226 @@
import { fastify, FastifyReply, FastifyRequest } from 'fastify';
Copy link
Member Author

@domdomegg domdomegg Sep 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

File is a little messy and probably too long: tidy up and split into multiple files

@@ -3,4 +3,5 @@ import env from '../env';

export default new AirtableTs({
apiKey: env.AIRTABLE_PERSONAL_ACCESS_TOKEN,
endpointUrl: 'http://localhost:8001',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be dynamic, and also needs to work in prod

return instance;
};

const cachedGetHandler = async (req: FastifyRequest, reply: FastifyReply) => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe consider setting up webhooks on Airtable so that if the records change through other routes e.g. different API changes, or manual edits in Airtable, the cache isn't stale.

Or at least make it very easy to clear the cache - maybe a web UI or something.

await instance.register(errorHandlerPlugin);
await instance.register(fastifyCors);

await instance.register(async (i) => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the moment, this is a free proxy for any requests to Airtable, and will cache anybody's requests there.

We should instead validate the token is from some allowlisted set.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant