UNPKG

@qualifyze/airtable

Version:
140 lines 5.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Table = void 0; const error_1 = require("./error"); const record_1 = require("./record"); const record_draft_1 = require("./record-draft"); const select_query_1 = require("./select-query"); const raw_types_1 = require("./raw-types"); const raw_types_2 = require("./raw-types"); const raw_types_3 = require("./raw-types"); const raw_types_4 = require("./raw-types"); class Table { constructor(base, tableName, validator) { this.base = base; this.name = tableName; this.validator = validator; } createValidation(reference) { return this.validator ? this.validator.createValidation(reference) : { isValid(input) { // If a validator is attached, we check the data, otherwise we assume data is valid as we have no means of // validating the data otherwise. return true; }, getValidationError() { return new Error("Cannot create Error from latest validation, because no real validation has taken place"); }, }; } subPath(path) { const tablePath = encodeURIComponent(this.name); return path ? `${tablePath}/${path}` : tablePath; } runTableAction(method, { path, ...options }) { return this.base.runAction(method, { path: this.subPath(path), ...options, }); } find(recordId) { return new record_draft_1.AirtableRecordDraft(this, recordId).fetch(); } async findOrNull(recordId) { try { // async/await are needed here to catch the error return await new record_draft_1.AirtableRecordDraft(this, recordId).fetch(); } catch (err) { if (err instanceof error_1.AirtableError && err.error === "NOT_FOUND") { return null; } throw err; } } select(query = {}) { return new select_query_1.SelectQuery(this, query); } async createSingleRecord(fields) { const data = await this.runTableAction("POST", { responseValidation: new raw_types_1.RecordDataValidation(this), payload: { body: { fields }, }, }); return record_1.AirtableRecord.fromRecordData(this, data); } async createMultipleRecords(records) { const data = await this.runTableAction("POST", { responseValidation: new raw_types_2.MultiRecordDataValidation(this), payload: { body: { records: records.map((record) => ({ fields: record })), }, }, }); return record_1.AirtableRecord.fromMultiRecordData(this, data); } create(data) { return Array.isArray(data) ? this.createMultipleRecords(data) : this.createSingleRecord(data); } async updateSingleRecord(data) { return new record_draft_1.AirtableRecordDraft(this, data.id).update(data.fields); } async updateMultipleRecords(data) { const response = await this.runTableAction("PATCH", { responseValidation: new raw_types_2.MultiRecordDataValidation(this), payload: { body: { records: data }, }, }); return record_1.AirtableRecord.fromMultiRecordData(this, response); } async replaceSingleRecord(data) { return new record_draft_1.AirtableRecordDraft(this, data.id).replace(data.fields); } async replaceMultipleRecords(data) { const response = await this.runTableAction("PUT", { responseValidation: new raw_types_2.MultiRecordDataValidation(this), payload: { body: { records: data }, }, }); return record_1.AirtableRecord.fromMultiRecordData(this, response); } async update(data) { return Array.isArray(data) ? this.updateMultipleRecords(data) : this.updateSingleRecord(data); } async replace(data) { return Array.isArray(data) ? this.replaceMultipleRecords(data) : this.replaceSingleRecord(data); } destroySingleRecord(id) { return new record_draft_1.AirtableRecordDraft(this, id).destroy(); } async destroyMultipleRecords(ids) { const { records } = await this.runTableAction("DELETE", { payload: { query: { records: ids }, }, responseValidation: new raw_types_4.MultiRecordResponseValidation({ createValidation: () => new raw_types_3.DeletedRecordValidation(), }), }); return records; } destroy(ids) { return Array.isArray(ids) ? this.destroyMultipleRecords(ids) : this.destroySingleRecord(ids); } } exports.Table = Table; //# sourceMappingURL=table.js.map