UNPKG

@qualifyze/airtable

Version:
91 lines 2.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SelectQuery = void 0; const record_1 = require("./record"); const raw_types_1 = require("./raw-types"); const airtable_formulator_1 = require("@qualifyze/airtable-formulator"); class SelectQuery { constructor(table, param) { this.table = table; this.params = param; } createQueryPayload(offset) { const { filterByFormula: formula, ...params } = this.params; return { query: { ...params, filterByFormula: formula && (0, airtable_formulator_1.compile)(formula), offset, }, }; } async fetchRecords(payload) { const { offset, records } = await this.table.runTableAction("GET", { payload, responseValidation: new raw_types_1.QueryPageResultValidation(this.table), }); return { records: records ? record_1.AirtableRecord.fromMultiRecordData(this.table, { records }) : [], offset, }; } /** * For cross-compatiblity with the offiical client. * * @deprecated */ firstPage() { return this.getPage(); } pageIterable() { const load = (offset) => this.fetchRecords(this.createQueryPayload(offset)); return { async *[Symbol.asyncIterator]() { let offset; do { const { records, offset: nextOffset } = await load(offset); yield records; offset = nextOffset; } while (offset); }, }; } /** * For cross-compatibility with the official client. * * @deprecated * @param handler */ async eachPage(handler) { for await (const records of this.pageIterable()) { handler(records); } } async getPage(offset) { const { records } = await this.fetchRecords(this.createQueryPayload(offset)); return records; } async *[Symbol.asyncIterator]() { for await (const records of this.pageIterable()) { for (const record of records) { yield record; } } } /** * For cross-compatibility with the official client. * * @deprecated */ async all() { const records = []; for await (const page of this.pageIterable()) { records.push(...page); } return records; } } exports.SelectQuery = SelectQuery; //# sourceMappingURL=select-query.js.map