ministry-platform-provider
Version:
TypeScript client library for Ministry Platform API integration
72 lines (71 loc) • 2.53 kB
JavaScript
export class TableService {
client;
constructor(client) {
this.client = client;
}
/**
* Returns the list of records from the specified table satisfying the provided search criteria.
*/
async getTableRecords(table, params) {
try {
await this.client.ensureValidToken();
console.log('Fetching records from table:', table);
console.log('Query Params:', params);
const endpoint = `/tables/${encodeURIComponent(table)}`;
const data = await this.client.getHttpClient().get(endpoint, params);
console.log('Fetched records:', data);
return data;
}
catch (error) {
console.error(`Error fetching records from table ${table}:`, error);
throw error;
}
}
/**
* Creates new records in the specified table.
*/
async createTableRecords(table, records, params) {
try {
await this.client.ensureValidToken();
const endpoint = `/tables/${encodeURIComponent(table)}`;
const result = await this.client.getHttpClient().post(endpoint, { records }, params);
return result;
}
catch (error) {
console.error(`Error creating records in table ${table}:`, error);
throw error;
}
}
/**
* Updates provided records in the specified table.
*/
async updateTableRecords(table, records, params) {
try {
await this.client.ensureValidToken();
const endpoint = `/tables/${encodeURIComponent(table)}`;
const result = await this.client.getHttpClient().put(endpoint, { records }, params);
return result;
}
catch (error) {
console.error(`Error updating records in table ${table}:`, error);
throw error;
}
}
/**
* Deletes multiple records from the specified table.
*/
async deleteTableRecords(table, ids, params) {
try {
await this.client.ensureValidToken();
// Combine the ids and other params
const queryParams = { ...params, id: ids };
const endpoint = `/tables/${encodeURIComponent(table)}`;
const result = await this.client.getHttpClient().delete(endpoint, queryParams);
return result;
}
catch (error) {
console.error(`Error deleting records from table ${table}:`, error);
throw error;
}
}
}