oneuptime/CommonServer/Utils/Airtable.ts

44 lines
1.2 KiB
TypeScript
Raw Normal View History

2022-04-10 21:57:14 +00:00
import PositiveNumber from 'Common/Types/PositiveNumber';
2022-04-13 15:40:46 +00:00
import AirtableLib, { FieldSet, Records } from 'airtable';
2022-04-10 21:57:14 +00:00
import Dictionary from 'Common/Types/Dictionary';
2022-04-08 21:15:45 +00:00
import { AirtableApiKey, AirtableBaseId } from '../Config';
2022-04-13 15:40:46 +00:00
2022-04-18 14:56:45 +00:00
export type AirtableRecords = Records<FieldSet>;
2022-04-08 12:54:03 +00:00
class Airtable {
2022-04-08 21:15:45 +00:00
private static base = new AirtableLib({ apiKey: AirtableApiKey }).base(
AirtableBaseId
2022-04-08 12:54:03 +00:00
);
public static async find(
tableName: string,
airtableView: string,
limit: PositiveNumber
2022-04-18 14:56:45 +00:00
): Promise<AirtableRecords> {
2022-04-08 12:54:03 +00:00
return this.base(tableName)
.select({ view: airtableView, pageSize: limit.toNumber() })
.firstPage();
}
public static async update(
tableName: string,
2022-04-13 15:40:46 +00:00
id: string,
2022-04-08 12:54:03 +00:00
fields: Dictionary<string>
2022-04-13 15:40:46 +00:00
): Promise<void> {
await this.base(tableName).update(id, fields);
2022-04-08 12:54:03 +00:00
}
2022-04-12 12:54:51 +00:00
public static async create(
tableName: string,
fields: Dictionary<string>
2022-04-13 15:40:46 +00:00
): Promise<void> {
await this.base(tableName).create(fields);
2022-04-08 12:54:03 +00:00
}
2022-04-13 15:40:46 +00:00
public static async delete(tableName: string, id: string): Promise<void> {
await this.base(tableName).destroy(id);
2022-04-08 12:54:03 +00:00
}
}
export default Airtable;