File tree Expand file tree Collapse file tree 4 files changed +93
-4
lines changed Expand file tree Collapse file tree 4 files changed +93
-4
lines changed Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ import { createColumn } from '../schema/column.js';
4
4
import { addCQIDsColumns } from '../schema/meta.js' ;
5
5
import { pathResolver , parentColumnResolver } from '../schema/resolvers.js' ;
6
6
import { createTable } from '../schema/table.js' ;
7
+ import { JSONType } from '../types/json.js' ;
7
8
8
9
export const createTables = ( ) => {
9
10
const allTables = [
@@ -12,15 +13,20 @@ export const createTables = () => {
12
13
title : 'Table 1' ,
13
14
description : 'Table 1 description' ,
14
15
resolver : ( clientMeta , parent , stream ) => {
15
- stream . write ( { id : 'id-1' } ) ;
16
- stream . write ( { id : 'id-2' } ) ;
16
+ stream . write ( { id : 'id-1' , json : '{ "a": 1 }' } ) ;
17
+ stream . write ( { id : 'id-2' , json : [ 1 , 2 , 3 ] } ) ;
17
18
return Promise . resolve ( ) ;
18
19
} ,
19
20
columns : [
20
21
createColumn ( {
21
22
name : 'id' ,
22
23
resolver : pathResolver ( 'id' ) ,
23
24
} ) ,
25
+ createColumn ( {
26
+ name : 'json' ,
27
+ resolver : pathResolver ( 'json' ) ,
28
+ type : new JSONType ( ) ,
29
+ } ) ,
24
30
] ,
25
31
} ) ,
26
32
createTable ( {
Original file line number Diff line number Diff line change
1
+ import { Utf8 as ArrowString } from '@apache-arrow/esnext-esm' ;
2
+
3
+ import { Scalar } from './scalar.js' ;
4
+ import { isInvalid , NULL_VALUE } from './util.js' ;
5
+
6
+ const validate = ( value : string ) => {
7
+ try {
8
+ JSON . parse ( value ) ;
9
+ return true ;
10
+ } catch {
11
+ return false ;
12
+ }
13
+ } ;
14
+
15
+ class JSONType implements Scalar < string > {
16
+ private _valid = false ;
17
+ private _value = '' ;
18
+
19
+ public constructor ( v ?: unknown ) {
20
+ this . value = v ;
21
+ return this ;
22
+ }
23
+
24
+ public get dataType ( ) {
25
+ return new ArrowString ( ) ;
26
+ }
27
+
28
+ public get valid ( ) : boolean {
29
+ return this . _valid ;
30
+ }
31
+
32
+ public get value ( ) : string {
33
+ return this . _value ;
34
+ }
35
+
36
+ public set value ( value : unknown ) {
37
+ if ( isInvalid ( value ) ) {
38
+ this . _valid = false ;
39
+ return ;
40
+ }
41
+
42
+ if ( typeof value === 'string' ) {
43
+ this . _value = value ;
44
+ this . _valid = validate ( value ) ;
45
+ return ;
46
+ }
47
+
48
+ if ( value instanceof Uint8Array ) {
49
+ this . _value = new TextDecoder ( ) . decode ( value ) ;
50
+ this . _valid = validate ( this . _value ) ;
51
+ return ;
52
+ }
53
+
54
+ if ( value instanceof JSONType ) {
55
+ this . _value = value . value ;
56
+ this . _valid = value . valid ;
57
+ return ;
58
+ }
59
+
60
+ try {
61
+ this . _value = JSON . stringify ( value ) ;
62
+ this . _valid = true ;
63
+ } catch {
64
+ throw new Error ( `Unable to set '${ value } ' as JSON` ) ;
65
+ }
66
+ }
67
+
68
+ public toString ( ) {
69
+ if ( this . _valid ) {
70
+ return this . _value ;
71
+ }
72
+
73
+ return NULL_VALUE ;
74
+ }
75
+ }
76
+
77
+ export { JSONType as JSON } ;
Original file line number Diff line number Diff line change 1
1
import { DataType , Precision } from '@apache-arrow/esnext-esm' ;
2
2
3
+ import { JSONType } from '../types/json.js' ;
3
4
import { UUIDType } from '../types/uuid.js' ;
4
5
5
6
import { Bool } from './bool.js' ;
@@ -9,6 +10,7 @@ import { Float64 } from './float64.js';
9
10
import { Int16 } from './int16.js' ;
10
11
import { Int32 } from './int32.js' ;
11
12
import { Int64 } from './int64.js' ;
13
+ import { JSON as JSONScalar } from './json.js' ;
12
14
import { List } from './list.js' ;
13
15
import { Text } from './text.js' ;
14
16
import { Timestamp } from './timestamp.js' ;
@@ -93,5 +95,9 @@ export const newScalar = (dataType: DataType): Scalar<Stringable> => {
93
95
return new UUID ( ) ;
94
96
}
95
97
98
+ if ( dataType instanceof JSONType ) {
99
+ return new JSONScalar ( ) ;
100
+ }
101
+
96
102
return new Text ( ) ;
97
103
} ;
Original file line number Diff line number Diff line change 1
- import { Binary , TimeNanosecond } from '@apache-arrow/esnext-esm' ;
1
+ import { Utf8 , TimeNanosecond } from '@apache-arrow/esnext-esm' ;
2
2
3
3
import { UUIDType } from '../types/uuid.js' ;
4
4
@@ -45,7 +45,7 @@ export const cqSyncTimeColumn = createColumn({
45
45
} ) ;
46
46
export const cqSourceNameColumn = createColumn ( {
47
47
name : '_cq_source_name' ,
48
- type : new Binary ( ) ,
48
+ type : new Utf8 ( ) ,
49
49
description : 'Internal CQ row that references the source plugin name data was retrieved' ,
50
50
ignoreInTests : true ,
51
51
} ) ;
You can’t perform that action at this time.
0 commit comments