A service designed for dependency injection (using typedi) to interact with Airtable. It extends BaseService and exposes its CRUD methods publicly. Inject this service into other services or controllers to access Airtable data.

import { Service } from 'typedi';
import { DataService, BaseModel } from '@thomascsd/stools';

// Define your Airtable model
export class Contact extends BaseModel {
name: string;
email: string;
}

const AIRTABLE_TOKEN = process.env.AIRTABLE_TOKEN!;
const BASE_ID = 'appXXXXXXXXXXXXXX';
const TABLE_NAME = 'Contacts';

@Service()
export class ContactService {
constructor(private db: DataService) {}

async getContacts(): Promise<Contact[]> {
return await this.db.getDatas<Contact>(AIRTABLE_TOKEN, BASE_ID, TABLE_NAME);
}

async saveContact(contact: Contact) {
return await this.db.saveData<Contact>(AIRTABLE_TOKEN, BASE_ID, TABLE_NAME, contact);
}

async updateContact(contact: Contact) {
return await this.db.updateData<Contact>(AIRTABLE_TOKEN, BASE_ID, TABLE_NAME, contact);
}

async deleteContact(contact: Contact) {
return await this.db.deleteData<Contact>(AIRTABLE_TOKEN, BASE_ID, TABLE_NAME, contact);
}
}

DataService

Hierarchy (View Summary)

Constructors

Methods

  • Protected

    Deletes a single record from the specified Airtable table using its record ID.

    Type Parameters

    • T extends BaseModel

      The type extending BaseModel representing the record to delete. Must include the id property.

    Parameters

    • token: string

      The Airtable Personal Access Token for authentication.

    • baseId: string

      The ID of the target Airtable base.

    • tableName: string

      The name or ID of the target table.

    • model: T

      The object representing the record to delete. It must include the id property.

    Returns Promise<AirtableDeletion>

    A promise that resolves to the Airtable API response confirming the deletion.

    BaseService

  • Deletes a record from the specified Airtable table. Publicly exposes the delete method from BaseService.

    Type Parameters

    • T extends BaseModel

      The type extending BaseModel representing the record to delete (must include id).

    Parameters

    • token: string

      The Airtable Personal Access Token.

    • baseId: string

      The ID of the Airtable base.

    • tableName: string

      The name or ID of the table.

    • model: T

      An object containing the id of the record to delete.

    Returns Promise<AirtableDeletion>

    A promise resolving to the Airtable API response.

    DataService

  • Protected

    Retrieves multiple records from a specified Airtable table, optionally applying query parameters. Maps the Airtable record structure to the provided generic type T.

    Type Parameters

    • T extends BaseModel

      The type extending BaseModel that represents the structure of the records.

    Parameters

    • token: string

      The Airtable Personal Access Token for authentication.

    • baseId: string

      The ID of the target Airtable base.

    • tableName: string

      The name or ID of the target table within the base.

    • Optionaloptions: SelectOptions

      Optional parameters for filtering, sorting, pagination, etc. See Airtable API documentation for details.

    Returns Promise<T[]>

    A promise that resolves to an array of records matching the query, cast to type T.

    BaseService

  • Retrieves multiple records from the specified Airtable table. Publicly exposes the get method from BaseService.

    Type Parameters

    • T extends BaseModel

      The type extending BaseModel representing the record structure.

    Parameters

    • token: string

      The Airtable Personal Access Token.

    • baseId: string

      The ID of the Airtable base.

    • tableName: string

      The name or ID of the table.

    • Optionaloptions: SelectOptions

      Optional query parameters.

    Returns Promise<T[]>

    A promise resolving to an array of records.

    DataService

  • Protected

    Creates a single new record in the specified Airtable table.

    Type Parameters

    • T extends BaseModel

      The type extending BaseModel representing the record to create.

    Parameters

    • token: string

      The Airtable Personal Access Token for authentication.

    • baseId: string

      The ID of the target Airtable base.

    • tableName: string

      The name or ID of the target table.

    • model: T

      The data object representing the record to be created. The id property, if present, will be ignored by Airtable.

    Returns Promise<AirtableResult>

    A promise that resolves to the Airtable API response for the created record.

    BaseService

  • Creates a new record in the specified Airtable table. Publicly exposes the save method from BaseService.

    Type Parameters

    • T extends BaseModel

      The type extending BaseModel representing the record to create.

    Parameters

    • token: string

      The Airtable Personal Access Token.

    • baseId: string

      The ID of the Airtable base.

    • tableName: string

      The name or ID of the table.

    • model: T

      The data for the new record.

    Returns Promise<AirtableResult>

    A promise resolving to the Airtable API response.

    DataService

  • Protected

    Updates an existing record in the specified Airtable table using its record ID.

    Type Parameters

    • T extends BaseModel

      The type extending BaseModel representing the record to update. Must include the id property.

    Parameters

    • token: string

      The Airtable Personal Access Token for authentication.

    • baseId: string

      The ID of the target Airtable base.

    • tableName: string

      The name or ID of the target table.

    • model: T

      The data object containing the fields to update. It must include the id property of the record to update.

    Returns Promise<AirtableUpdateResult>

    A promise that resolves to the Airtable API response for the updated record.

    BaseService

  • Updates an existing record in the specified Airtable table. Publicly exposes the update method from BaseService.

    Type Parameters

    • T extends BaseModel

      The type extending BaseModel representing the record to update (must include id).

    Parameters

    • token: string

      The Airtable Personal Access Token.

    • baseId: string

      The ID of the Airtable base.

    • tableName: string

      The name or ID of the table.

    • model: T

      The data containing updates, including the record id.

    Returns Promise<AirtableUpdateResult>

    A promise resolving to the Airtable API response.

    DataService