UNPKG

@cloudquery/plugin-sdk-javascript

Version:

This is the high-level package to use for developing CloudQuery plugins in JavaScript

60 lines 2.21 kB
import { tableToIPC, Table as ArrowTable, RecordBatch, vectorFromArray } from '@apache-arrow/esnext-esm'; import { ResourceError } from '../errors/errors.js'; import { newScalar } from '../scalar/scalar.js'; import { cqIDColumn } from './meta.js'; import { toArrowSchema } from './table.js'; export class Resource { item; parent; table; data; constructor(table, parent, item) { this.table = table; this.parent = parent; this.item = item; this.data = table.columns.map((column) => newScalar(column.type)); } getColumnData(columnName) { const columnIndex = this.table.columns.findIndex((c) => c.name === columnName); if (columnIndex === undefined) { throw new ResourceError(`Column '${columnName}' not found`, { props: { resource: this } }); } return this.data[columnIndex]; } setColumData(columnName, value) { const columnIndex = this.table.columns.findIndex((c) => c.name === columnName); if (columnIndex === undefined) { throw new ResourceError(`Column '${columnName}' not found`, { props: { resource: this } }); } this.data[columnIndex].value = value; } setCqId(value) { const columnIndex = this.table.columns.findIndex((c) => c.name === cqIDColumn.name); if (columnIndex === -1) { return; } this.data[columnIndex].value = value; } getItem() { return this.item; } setItem(item) { this.item = item; } } export const encodeResource = (resource) => { const { table } = resource; const schema = toArrowSchema(table); // TODO: Check if this can be simplified let batch = new RecordBatch(schema, undefined); for (let index = 0; index < table.columns.length; index++) { const column = table.columns[index]; const data = resource.getColumnData(column.name).value; const vector = vectorFromArray([data], column.type); batch = batch.setChildAt(index, vector); } const arrowTable = new ArrowTable(schema, batch); const bytes = tableToIPC(arrowTable); return bytes; }; //# sourceMappingURL=resource.js.map