|
1 |
| -import { DataType, Field } from '@apache-arrow/esnext-esm'; |
| 1 | +import { isDeepStrictEqual } from 'node:util'; |
| 2 | + |
| 3 | +import { DataType, Field, Bool } from '@apache-arrow/esnext-esm'; |
2 | 4 |
|
3 | 5 | import * as arrow from './arrow.js';
|
4 | 6 |
|
5 |
| -export class Column { |
| 7 | +export type Column = { |
6 | 8 | name: string;
|
7 | 9 | type: DataType;
|
8 | 10 | description: string;
|
9 | 11 | primaryKey: boolean;
|
10 | 12 | notNull: boolean;
|
11 | 13 | incrementalKey: boolean;
|
12 | 14 | unique: boolean;
|
| 15 | +}; |
13 | 16 |
|
14 |
| - constructor( |
15 |
| - name: string, |
16 |
| - type: DataType, |
17 |
| - description: string = '', |
18 |
| - primaryKey: boolean = false, |
19 |
| - notNull: boolean = false, |
20 |
| - incrementalKey: boolean = false, |
21 |
| - unique: boolean = false, |
22 |
| - ) { |
23 |
| - this.name = name; |
24 |
| - this.type = type; |
25 |
| - this.description = description; |
26 |
| - this.primaryKey = primaryKey; |
27 |
| - this.notNull = notNull; |
28 |
| - this.incrementalKey = incrementalKey; |
29 |
| - this.unique = unique; |
30 |
| - } |
31 |
| - |
32 |
| - toString(): string { |
33 |
| - return `Column(name=${this.name}, type=${this.type}, description=${this.description}, primary_key=${this.primaryKey}, not_null=${this.notNull}, incremental_key=${this.incrementalKey}, unique=${this.unique})`; |
34 |
| - } |
| 17 | +export const createColumn = ({ |
| 18 | + name = '', |
| 19 | + type = new Bool(), |
| 20 | + description = '', |
| 21 | + incrementalKey = false, |
| 22 | + notNull = false, |
| 23 | + primaryKey = false, |
| 24 | + unique = false, |
| 25 | +}: Partial<Column> = {}): Column => ({ |
| 26 | + name, |
| 27 | + type, |
| 28 | + description, |
| 29 | + primaryKey, |
| 30 | + notNull, |
| 31 | + incrementalKey, |
| 32 | + unique, |
| 33 | +}); |
35 | 34 |
|
36 |
| - // JavaScript (and TypeScript) uses a single method for both string representation and debugging output |
37 |
| - toJSON(): string { |
38 |
| - return this.toString(); |
39 |
| - } |
| 35 | +export const formatColumn = (column: Column): string => { |
| 36 | + const { name, type, description, primaryKey, notNull, incrementalKey, unique } = column; |
| 37 | + return `Column(name=${name}, type=${type}, description=${description}, primary_key=${primaryKey}, not_null=${notNull}, incremental_key=${incrementalKey}, unique=${unique})`; |
| 38 | +}; |
40 | 39 |
|
41 |
| - equals(value: object): boolean { |
42 |
| - if (value instanceof Column) { |
43 |
| - return ( |
44 |
| - this.name === value.name && |
45 |
| - this.type === value.type && |
46 |
| - this.description === value.description && |
47 |
| - this.primaryKey === value.primaryKey && |
48 |
| - this.notNull === value.notNull && |
49 |
| - this.incrementalKey === value.incrementalKey && |
50 |
| - this.unique === value.unique |
51 |
| - ); |
52 |
| - } |
53 |
| - return false; |
54 |
| - } |
| 40 | +export const equals = (column: Column, other: unknown): boolean => { |
| 41 | + return isDeepStrictEqual(column, other); |
| 42 | +}; |
55 | 43 |
|
56 |
| - toArrowField(): Field { |
57 |
| - const metadataMap = new Map<string, string>(); |
58 |
| - metadataMap.set(arrow.METADATA_PRIMARY_KEY, this.primaryKey ? arrow.METADATA_TRUE : arrow.METADATA_FALSE); |
59 |
| - metadataMap.set(arrow.METADATA_UNIQUE, this.unique ? arrow.METADATA_TRUE : arrow.METADATA_FALSE); |
60 |
| - metadataMap.set(arrow.METADATA_INCREMENTAL, this.incrementalKey ? arrow.METADATA_TRUE : arrow.METADATA_FALSE); |
| 44 | +export const toArrowField = (column: Column): Field => { |
| 45 | + const { name, type, notNull, primaryKey, unique, incrementalKey } = column; |
| 46 | + const metadataMap = new Map<string, string>(); |
| 47 | + metadataMap.set(arrow.METADATA_PRIMARY_KEY, primaryKey ? arrow.METADATA_TRUE : arrow.METADATA_FALSE); |
| 48 | + metadataMap.set(arrow.METADATA_UNIQUE, unique ? arrow.METADATA_TRUE : arrow.METADATA_FALSE); |
| 49 | + metadataMap.set(arrow.METADATA_INCREMENTAL, incrementalKey ? arrow.METADATA_TRUE : arrow.METADATA_FALSE); |
61 | 50 |
|
62 |
| - return new Field(this.name, this.type, /*nullable=*/ !this.notNull, metadataMap); |
63 |
| - } |
| 51 | + return new Field(name, type, /*nullable=*/ !notNull, metadataMap); |
| 52 | +}; |
64 | 53 |
|
65 |
| - static fromArrowField(field: Field): Column { |
66 |
| - const metadata = field.metadata; |
67 |
| - const primaryKey = metadata.get(arrow.METADATA_PRIMARY_KEY) === arrow.METADATA_TRUE; |
68 |
| - const unique = metadata.get(arrow.METADATA_UNIQUE) === arrow.METADATA_TRUE; |
69 |
| - const incrementalKey = metadata.get(arrow.METADATA_INCREMENTAL) === arrow.METADATA_TRUE; |
| 54 | +export const fromArrowField = (field: Field): Column => { |
| 55 | + const { name, type, nullable } = field; |
| 56 | + const metadata = field.metadata; |
| 57 | + const primaryKey = metadata.get(arrow.METADATA_PRIMARY_KEY) === arrow.METADATA_TRUE; |
| 58 | + const unique = metadata.get(arrow.METADATA_UNIQUE) === arrow.METADATA_TRUE; |
| 59 | + const incrementalKey = metadata.get(arrow.METADATA_INCREMENTAL) === arrow.METADATA_TRUE; |
70 | 60 |
|
71 |
| - return new Column(field.name, field.type, '', primaryKey, !field.nullable, unique, incrementalKey); |
72 |
| - } |
73 |
| -} |
| 61 | + return { |
| 62 | + name, |
| 63 | + type, |
| 64 | + description: '', |
| 65 | + primaryKey, |
| 66 | + notNull: !nullable, |
| 67 | + unique, |
| 68 | + incrementalKey, |
| 69 | + }; |
| 70 | +}; |
0 commit comments