-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcolumn.ts
More file actions
91 lines (77 loc) · 2.85 KB
/
column.ts
File metadata and controls
91 lines (77 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { isDeepStrictEqual } from 'node:util';
import type { DataType } from '@apache-arrow/esnext-esm';
import { Field, Utf8 } from '@apache-arrow/esnext-esm';
import type { ExtensionType } from '../types/extensions.js';
import { isExtensionType } from '../types/extensions.js';
import * as arrow from './arrow.js';
import type { ClientMeta } from './meta.js';
import type { Resource } from './resource.js';
export type ColumnResolver = (meta: ClientMeta, resource: Resource, c: Column) => Promise<void>;
export type Column = {
name: string;
type: DataType;
description: string;
primaryKey: boolean;
notNull: boolean;
incrementalKey: boolean;
unique: boolean;
resolver: ColumnResolver;
ignoreInTests: boolean;
};
const emptyResolver = () => Promise.resolve();
export const createColumn = ({
name = '',
type = new Utf8(),
description = '',
incrementalKey = false,
notNull = false,
primaryKey = false,
resolver = emptyResolver,
unique = false,
ignoreInTests = false,
}: Partial<Column> = {}): Column => ({
name,
type,
description,
primaryKey,
notNull,
incrementalKey,
resolver,
unique,
ignoreInTests,
});
export const formatColumn = (column: Column): string => {
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: Column, other: unknown): boolean => {
return isDeepStrictEqual(column, other);
};
export const toArrowField = (column: Column): Field => {
const { name, type, notNull, primaryKey, unique, incrementalKey } = column;
const metadataMap = new Map<string, string>();
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 as unknown as ExtensionType;
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: Field): Column => {
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,
});
};