// Importing necessary modules and packages.
import { TriggerClient, eventTrigger } from "@trigger.dev/sdk";
import { Airtable } from "@trigger.dev/airtable";
import { z } from "zod";
// Creating an instance of the Airtable client with personal access token obtained from an environment variable.
const airtable = new Airtable({
id: "airtable",
apiKey: process.env.AIRTABLE_PERSONAL_ACCESS_TOKEN!,
});
type LaunchGoalsAndOkRs = {
"Launch goals"?: string;
DRI?: Collaborator;
Team?: string;
Status?: "On track" | "In progress" | "At risk";
"Key results"?: Array<string>;
"Features (from 💻 Features table)"?: Array<string>;
"Status (from 💻 Features)": Array<
"Live" | "Complete" | "In progress" | "Planning" | "In reviews"
>;
};
client.defineJob({
id: "airtable-example",
name: "Airtable Example: getRecords",
version: "0.1.0",
trigger: eventTrigger({
name: "airtable.example",
}),
integrations: {
// Make sure to add the integration here
airtable,
},
run: async (payload, io, ctx) => {
// Adding the type to table<YourTableType>("<your table name>") gives you nice type inference and errors
// You can leave it out as well table("<your table name>")
const table = io.airtable.base("<your base id>").table<LaunchGoalsAndOkRs>("<your table name>");
// Gets multiple records from the table. Here we only get the Status fields (columns)
const records = await table.getRecords("multiple records", {
fields: ["Status"],
});
await io.logger.log(records[0].fields.Status ?? "no status");
// Get a single record
const aRecord = await table.getRecord("single", records[0].id);
// Create a new record
const newRecords = await table.createRecords("create records", [
{
fields: {
"Launch goals": "Created from Trigger.dev",
Status: "In progress",
},
},
]);
// Update the record we just created
const updatedRecords = await table.updateRecords(
"update records",
newRecords.map((record) => ({
id: record.id,
fields: { Status: "At risk" },
}))
);
// Wait for 5 seconds
await io.wait("5 secs", 5);
// Delete the record we just created + updated
const deletedRecords = await table.deleteRecords(
"delete records",
updatedRecords.map((record) => record.id)
);
},
});