// Gets multiple records from the table.await table.getRecords("multiple records", { fields: ["Status"], // Here we only get the Status fields (columns)});
// Create newRecords by adding a record to the table.await table.createRecords("create records", [ { // Define the new record to be created fields: { "Launch goals": "Created from Trigger.dev", Status: "In progress", }, },]);
// Update the record that was just created.await table.updateRecords( // Specify the table's name as "update records." "update records", // Use the `map` method to create an array of records to be updated. newRecords.map((record) => ({ // For each record, specify the: ID and the field to update ("Status" to "At risk"). id: record.id, fields: { Status: "At risk", }, })));
In this demo we will use the Airtable to get records from a table, create a new record, update it and then delete it.
Copy
Ask AI
// 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) ); },});