@cloudquery/plugin-sdk-javascript
Version:
This is the high-level package to use for developing CloudQuery plugins in JavaScript
52 lines • 2.33 kB
JavaScript
import { isDeepStrictEqual } from 'node:util';
import { Field, Utf8 } from '@apache-arrow/esnext-esm';
import { isExtensionType } from '../types/extensions.js';
import * as arrow from './arrow.js';
const emptyResolver = () => Promise.resolve();
export const createColumn = ({ name = '', type = new Utf8(), description = '', incrementalKey = false, notNull = false, primaryKey = false, resolver = emptyResolver, unique = false, ignoreInTests = false, } = {}) => ({
name,
type,
description,
primaryKey,
notNull,
incrementalKey,
resolver,
unique,
ignoreInTests,
});
export const formatColumn = (column) => {
const { name, type, description, primaryKey, notNull, incrementalKey, unique } = column;
return `Column(name=${name}, type=${type}, description=${description}, primary_key=${primaryKey}, not_null=${notNull}, incremental_key=${incrementalKey}, unique=${unique})`;
};
export const equals = (column, other) => {
return isDeepStrictEqual(column, other);
};
export const toArrowField = (column) => {
const { name, type, notNull, primaryKey, unique, incrementalKey } = column;
const metadataMap = new Map();
metadataMap.set(arrow.METADATA_PRIMARY_KEY, primaryKey ? arrow.METADATA_TRUE : arrow.METADATA_FALSE);
metadataMap.set(arrow.METADATA_UNIQUE, unique ? arrow.METADATA_TRUE : arrow.METADATA_FALSE);
metadataMap.set(arrow.METADATA_INCREMENTAL, incrementalKey ? arrow.METADATA_TRUE : arrow.METADATA_FALSE);
if (isExtensionType(type)) {
const { name, metadata } = type;
metadataMap.set(arrow.METADATA_ARROW_EXTENSION_NAME, name);
metadataMap.set(arrow.METADATA_ARROW_EXTENSION_METADATA, metadata);
}
return new Field(name, type, !notNull, metadataMap);
};
export const fromArrowField = (field) => {
const { name, type, nullable } = field;
const metadata = field.metadata;
const primaryKey = metadata.get(arrow.METADATA_PRIMARY_KEY) === arrow.METADATA_TRUE;
const unique = metadata.get(arrow.METADATA_UNIQUE) === arrow.METADATA_TRUE;
const incrementalKey = metadata.get(arrow.METADATA_INCREMENTAL) === arrow.METADATA_TRUE;
return createColumn({
name,
type,
primaryKey,
notNull: !nullable,
unique,
incrementalKey,
});
};
//# sourceMappingURL=column.js.map