({
token = import.meta.env.AIRTABLE_TOKEN,
base,
table,
queryParams,
}: AirtableLoaderOptions)
| 21 | * Loads data from an Airtable table. |
| 22 | */ |
| 23 | export function airtableLoader({ |
| 24 | token = import.meta.env.AIRTABLE_TOKEN, |
| 25 | base, |
| 26 | table, |
| 27 | queryParams, |
| 28 | }: AirtableLoaderOptions): Loader { |
| 29 | if (!token) { |
| 30 | throw new AstroError( |
| 31 | "Missing Airtable token. Set it in the AIRTABLE_TOKEN environment variable or pass it as an option.", |
| 32 | ); |
| 33 | } |
| 34 | const baseInstance = new Airtable({ apiKey: token }).base(base); |
| 35 | |
| 36 | return { |
| 37 | name: "airtable-loader", |
| 38 | load: async ({ logger, parseData, store }) => { |
| 39 | logger.info(`Loading data from table "${table}"`); |
| 40 | const records = await baseInstance(table).select(queryParams).all(); |
| 41 | for (const { id, fields } of records) { |
| 42 | const data = await parseData({ id, data: fields }); |
| 43 | store.set({ id, data }); |
| 44 | } |
| 45 | logger.info(`Loaded ${records.length} records from "${table}"`); |
| 46 | }, |
| 47 | schema: () => |
| 48 | zodSchemaFromAirtableTable({ |
| 49 | baseID: base, |
| 50 | tableIdOrName: table, |
| 51 | apiKey: token, |
| 52 | }), |
| 53 | }; |
| 54 | } |
no test coverage detected