drizzle-typebox 0.3.3-6baabce → 0.3.3-7722e6a
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/column.d.cts +1 -1
- package/column.d.mts +1 -1
- package/column.d.ts +1 -1
- package/column.types.d.cts +13 -12
- package/column.types.d.mts +13 -12
- package/column.types.d.ts +13 -12
- package/index.cjs +234 -183
- package/index.cjs.map +1 -1
- package/index.mjs +235 -184
- package/index.mjs.map +1 -1
- package/package.json +2 -2
- package/schema.types.d.cts +4 -4
- package/schema.types.d.mts +4 -4
- package/schema.types.d.ts +4 -4
- package/utils.d.cts +7 -3
- package/utils.d.mts +7 -3
- package/utils.d.ts +7 -3
package/column.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { TSchema, Type as typebox } from '@sinclair/typebox';
|
|
2
|
-
import type
|
|
2
|
+
import { type Column } from 'drizzle-orm';
|
|
3
3
|
import type { BufferSchema, JsonSchema } from "./utils.cjs";
|
|
4
4
|
export declare const literalSchema: import("@sinclair/typebox").TUnion<[import("@sinclair/typebox").TString, import("@sinclair/typebox").TNumber, import("@sinclair/typebox").TBoolean, import("@sinclair/typebox").TNull]>;
|
|
5
5
|
export declare const jsonSchema: JsonSchema;
|
package/column.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { TSchema, Type as typebox } from '@sinclair/typebox';
|
|
2
|
-
import type
|
|
2
|
+
import { type Column } from 'drizzle-orm';
|
|
3
3
|
import type { BufferSchema, JsonSchema } from "./utils.mjs";
|
|
4
4
|
export declare const literalSchema: import("@sinclair/typebox").TUnion<[import("@sinclair/typebox").TString, import("@sinclair/typebox").TNumber, import("@sinclair/typebox").TBoolean, import("@sinclair/typebox").TNull]>;
|
|
5
5
|
export declare const jsonSchema: JsonSchema;
|
package/column.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { TSchema, Type as typebox } from '@sinclair/typebox';
|
|
2
|
-
import type
|
|
2
|
+
import { type Column } from 'drizzle-orm';
|
|
3
3
|
import type { BufferSchema, JsonSchema } from './utils.js';
|
|
4
4
|
export declare const literalSchema: import("@sinclair/typebox").TUnion<[import("@sinclair/typebox").TString, import("@sinclair/typebox").TNumber, import("@sinclair/typebox").TBoolean, import("@sinclair/typebox").TNull]>;
|
|
5
5
|
export declare const jsonSchema: JsonSchema;
|
package/column.types.d.cts
CHANGED
|
@@ -1,23 +1,24 @@
|
|
|
1
1
|
import type * as t from '@sinclair/typebox';
|
|
2
|
-
import type { Assume, Column } from 'drizzle-orm';
|
|
3
|
-
import type { BufferSchema,
|
|
4
|
-
type HasBaseColumn<TColumn> = TColumn extends {
|
|
5
|
-
_: {
|
|
6
|
-
baseColumn: Column | undefined;
|
|
7
|
-
};
|
|
8
|
-
} ? IsNever<TColumn['_']['baseColumn']> extends false ? true : false : false;
|
|
2
|
+
import type { Assume, Column, ColumnTypeData, ExtractColumnTypeData } from 'drizzle-orm';
|
|
3
|
+
import type { BufferSchema, JsonSchema } from "./utils.cjs";
|
|
9
4
|
export type EnumValuesToEnum<TEnumValues extends [string, ...string[]]> = {
|
|
10
5
|
[K in TEnumValues[number]]: K;
|
|
11
6
|
};
|
|
12
7
|
export interface GenericSchema<T> extends t.TSchema {
|
|
13
8
|
static: T;
|
|
14
9
|
}
|
|
15
|
-
export type GetTypeboxType<TColumn extends Column
|
|
10
|
+
export type GetTypeboxType<TColumn extends Column, TType extends ColumnTypeData = ExtractColumnTypeData<TColumn['_']['dataType']>> = TType['type'] extends 'array' ? TType['constraint'] extends 'basecolumn' ? t.TArray<GetTypeboxType<Assume<TColumn['_'], {
|
|
11
|
+
baseColumn: Column;
|
|
12
|
+
}>['baseColumn']>> : TType['constraint'] extends 'geometry' | 'point' ? t.TTuple<[t.TNumber, t.TNumber]> : TType['constraint'] extends 'line' ? t.TTuple<[t.TNumber, t.TNumber, t.TNumber]> : TType['constraint'] extends 'vector' | 'halfvector' ? t.TArray<t.TNumber> : t.TArray<t.TAny> : TType['type'] extends 'object' ? TType['constraint'] extends 'geometry' | 'point' ? t.TObject<{
|
|
13
|
+
x: t.TNumber;
|
|
14
|
+
y: t.TNumber;
|
|
15
|
+
}> : TType['constraint'] extends 'line' ? t.TObject<{
|
|
16
|
+
a: t.TNumber;
|
|
17
|
+
b: t.TNumber;
|
|
18
|
+
c: t.TNumber;
|
|
19
|
+
}> : TType['constraint'] extends 'date' ? t.TDate : TType['constraint'] extends 'buffer' ? BufferSchema : TType['constraint'] extends 'json' ? TColumn['_']['data'] extends Record<string, any> ? GenericSchema<TColumn['_']['data']> : JsonSchema : t.TObject : TType['type'] extends 'custom' ? t.TAny : TType['type'] extends 'number' ? TType['constraint'] extends 'int8' | 'int16' | 'int24' | 'int32' | 'int53' | 'uint8' | 'uint16' | 'uint24' | 'uint32' | 'uint53' | 'year' ? t.TInteger : t.TNumber : TType['type'] extends 'bigint' ? t.TBigInt : TType['type'] extends 'boolean' ? t.TBoolean : TType['type'] extends 'string' ? TType['constraint'] extends 'binary' | 'varbinary' ? t.TRegExp : TType['constraint'] extends 'enum' ? t.TEnum<{
|
|
16
20
|
[K in Assume<TColumn['_']['enumValues'], string[]>[number]]: K;
|
|
17
|
-
}> :
|
|
18
|
-
[K in keyof TColumn['_']['data']]: GetTypeboxPrimitiveType<TColumn['_']['data'][K]>;
|
|
19
|
-
}> : TColumn['_']['dataType'] extends 'json' ? JsonSchema : GetTypeboxPrimitiveType<TColumn['_']['data']>;
|
|
20
|
-
type GetTypeboxPrimitiveType<TData> = TData extends number ? t.TNumber : TData extends bigint ? t.TBigInt : TData extends boolean ? t.TBoolean : TData extends string ? t.TString : t.TAny;
|
|
21
|
+
}> : t.TString : t.TAny;
|
|
21
22
|
type HandleSelectColumn<TSchema extends t.TSchema, TColumn extends Column> = TColumn['_']['notNull'] extends true ? TSchema : t.Union<[TSchema, t.TNull]>;
|
|
22
23
|
type HandleInsertColumn<TSchema extends t.TSchema, TColumn extends Column> = TColumn['_']['notNull'] extends true ? TColumn['_']['hasDefault'] extends true ? t.TOptional<TSchema> : TSchema : t.TOptional<t.Union<[TSchema, t.TNull]>>;
|
|
23
24
|
type HandleUpdateColumn<TSchema extends t.TSchema, TColumn extends Column> = TColumn['_']['notNull'] extends true ? t.TOptional<TSchema> : t.TOptional<t.Union<[TSchema, t.TNull]>>;
|
package/column.types.d.mts
CHANGED
|
@@ -1,23 +1,24 @@
|
|
|
1
1
|
import type * as t from '@sinclair/typebox';
|
|
2
|
-
import type { Assume, Column } from 'drizzle-orm';
|
|
3
|
-
import type { BufferSchema,
|
|
4
|
-
type HasBaseColumn<TColumn> = TColumn extends {
|
|
5
|
-
_: {
|
|
6
|
-
baseColumn: Column | undefined;
|
|
7
|
-
};
|
|
8
|
-
} ? IsNever<TColumn['_']['baseColumn']> extends false ? true : false : false;
|
|
2
|
+
import type { Assume, Column, ColumnTypeData, ExtractColumnTypeData } from 'drizzle-orm';
|
|
3
|
+
import type { BufferSchema, JsonSchema } from "./utils.mjs";
|
|
9
4
|
export type EnumValuesToEnum<TEnumValues extends [string, ...string[]]> = {
|
|
10
5
|
[K in TEnumValues[number]]: K;
|
|
11
6
|
};
|
|
12
7
|
export interface GenericSchema<T> extends t.TSchema {
|
|
13
8
|
static: T;
|
|
14
9
|
}
|
|
15
|
-
export type GetTypeboxType<TColumn extends Column
|
|
10
|
+
export type GetTypeboxType<TColumn extends Column, TType extends ColumnTypeData = ExtractColumnTypeData<TColumn['_']['dataType']>> = TType['type'] extends 'array' ? TType['constraint'] extends 'basecolumn' ? t.TArray<GetTypeboxType<Assume<TColumn['_'], {
|
|
11
|
+
baseColumn: Column;
|
|
12
|
+
}>['baseColumn']>> : TType['constraint'] extends 'geometry' | 'point' ? t.TTuple<[t.TNumber, t.TNumber]> : TType['constraint'] extends 'line' ? t.TTuple<[t.TNumber, t.TNumber, t.TNumber]> : TType['constraint'] extends 'vector' | 'halfvector' ? t.TArray<t.TNumber> : t.TArray<t.TAny> : TType['type'] extends 'object' ? TType['constraint'] extends 'geometry' | 'point' ? t.TObject<{
|
|
13
|
+
x: t.TNumber;
|
|
14
|
+
y: t.TNumber;
|
|
15
|
+
}> : TType['constraint'] extends 'line' ? t.TObject<{
|
|
16
|
+
a: t.TNumber;
|
|
17
|
+
b: t.TNumber;
|
|
18
|
+
c: t.TNumber;
|
|
19
|
+
}> : TType['constraint'] extends 'date' ? t.TDate : TType['constraint'] extends 'buffer' ? BufferSchema : TType['constraint'] extends 'json' ? TColumn['_']['data'] extends Record<string, any> ? GenericSchema<TColumn['_']['data']> : JsonSchema : t.TObject : TType['type'] extends 'custom' ? t.TAny : TType['type'] extends 'number' ? TType['constraint'] extends 'int8' | 'int16' | 'int24' | 'int32' | 'int53' | 'uint8' | 'uint16' | 'uint24' | 'uint32' | 'uint53' | 'year' ? t.TInteger : t.TNumber : TType['type'] extends 'bigint' ? t.TBigInt : TType['type'] extends 'boolean' ? t.TBoolean : TType['type'] extends 'string' ? TType['constraint'] extends 'binary' | 'varbinary' ? t.TRegExp : TType['constraint'] extends 'enum' ? t.TEnum<{
|
|
16
20
|
[K in Assume<TColumn['_']['enumValues'], string[]>[number]]: K;
|
|
17
|
-
}> :
|
|
18
|
-
[K in keyof TColumn['_']['data']]: GetTypeboxPrimitiveType<TColumn['_']['data'][K]>;
|
|
19
|
-
}> : TColumn['_']['dataType'] extends 'json' ? JsonSchema : GetTypeboxPrimitiveType<TColumn['_']['data']>;
|
|
20
|
-
type GetTypeboxPrimitiveType<TData> = TData extends number ? t.TNumber : TData extends bigint ? t.TBigInt : TData extends boolean ? t.TBoolean : TData extends string ? t.TString : t.TAny;
|
|
21
|
+
}> : t.TString : t.TAny;
|
|
21
22
|
type HandleSelectColumn<TSchema extends t.TSchema, TColumn extends Column> = TColumn['_']['notNull'] extends true ? TSchema : t.Union<[TSchema, t.TNull]>;
|
|
22
23
|
type HandleInsertColumn<TSchema extends t.TSchema, TColumn extends Column> = TColumn['_']['notNull'] extends true ? TColumn['_']['hasDefault'] extends true ? t.TOptional<TSchema> : TSchema : t.TOptional<t.Union<[TSchema, t.TNull]>>;
|
|
23
24
|
type HandleUpdateColumn<TSchema extends t.TSchema, TColumn extends Column> = TColumn['_']['notNull'] extends true ? t.TOptional<TSchema> : t.TOptional<t.Union<[TSchema, t.TNull]>>;
|
package/column.types.d.ts
CHANGED
|
@@ -1,23 +1,24 @@
|
|
|
1
1
|
import type * as t from '@sinclair/typebox';
|
|
2
|
-
import type { Assume, Column } from 'drizzle-orm';
|
|
3
|
-
import type { BufferSchema,
|
|
4
|
-
type HasBaseColumn<TColumn> = TColumn extends {
|
|
5
|
-
_: {
|
|
6
|
-
baseColumn: Column | undefined;
|
|
7
|
-
};
|
|
8
|
-
} ? IsNever<TColumn['_']['baseColumn']> extends false ? true : false : false;
|
|
2
|
+
import type { Assume, Column, ColumnTypeData, ExtractColumnTypeData } from 'drizzle-orm';
|
|
3
|
+
import type { BufferSchema, JsonSchema } from './utils.js';
|
|
9
4
|
export type EnumValuesToEnum<TEnumValues extends [string, ...string[]]> = {
|
|
10
5
|
[K in TEnumValues[number]]: K;
|
|
11
6
|
};
|
|
12
7
|
export interface GenericSchema<T> extends t.TSchema {
|
|
13
8
|
static: T;
|
|
14
9
|
}
|
|
15
|
-
export type GetTypeboxType<TColumn extends Column
|
|
10
|
+
export type GetTypeboxType<TColumn extends Column, TType extends ColumnTypeData = ExtractColumnTypeData<TColumn['_']['dataType']>> = TType['type'] extends 'array' ? TType['constraint'] extends 'basecolumn' ? t.TArray<GetTypeboxType<Assume<TColumn['_'], {
|
|
11
|
+
baseColumn: Column;
|
|
12
|
+
}>['baseColumn']>> : TType['constraint'] extends 'geometry' | 'point' ? t.TTuple<[t.TNumber, t.TNumber]> : TType['constraint'] extends 'line' ? t.TTuple<[t.TNumber, t.TNumber, t.TNumber]> : TType['constraint'] extends 'vector' | 'halfvector' ? t.TArray<t.TNumber> : t.TArray<t.TAny> : TType['type'] extends 'object' ? TType['constraint'] extends 'geometry' | 'point' ? t.TObject<{
|
|
13
|
+
x: t.TNumber;
|
|
14
|
+
y: t.TNumber;
|
|
15
|
+
}> : TType['constraint'] extends 'line' ? t.TObject<{
|
|
16
|
+
a: t.TNumber;
|
|
17
|
+
b: t.TNumber;
|
|
18
|
+
c: t.TNumber;
|
|
19
|
+
}> : TType['constraint'] extends 'date' ? t.TDate : TType['constraint'] extends 'buffer' ? BufferSchema : TType['constraint'] extends 'json' ? TColumn['_']['data'] extends Record<string, any> ? GenericSchema<TColumn['_']['data']> : JsonSchema : t.TObject : TType['type'] extends 'custom' ? t.TAny : TType['type'] extends 'number' ? TType['constraint'] extends 'int8' | 'int16' | 'int24' | 'int32' | 'int53' | 'uint8' | 'uint16' | 'uint24' | 'uint32' | 'uint53' | 'year' ? t.TInteger : t.TNumber : TType['type'] extends 'bigint' ? t.TBigInt : TType['type'] extends 'boolean' ? t.TBoolean : TType['type'] extends 'string' ? TType['constraint'] extends 'binary' | 'varbinary' ? t.TRegExp : TType['constraint'] extends 'enum' ? t.TEnum<{
|
|
16
20
|
[K in Assume<TColumn['_']['enumValues'], string[]>[number]]: K;
|
|
17
|
-
}> :
|
|
18
|
-
[K in keyof TColumn['_']['data']]: GetTypeboxPrimitiveType<TColumn['_']['data'][K]>;
|
|
19
|
-
}> : TColumn['_']['dataType'] extends 'json' ? JsonSchema : GetTypeboxPrimitiveType<TColumn['_']['data']>;
|
|
20
|
-
type GetTypeboxPrimitiveType<TData> = TData extends number ? t.TNumber : TData extends bigint ? t.TBigInt : TData extends boolean ? t.TBoolean : TData extends string ? t.TString : t.TAny;
|
|
21
|
+
}> : t.TString : t.TAny;
|
|
21
22
|
type HandleSelectColumn<TSchema extends t.TSchema, TColumn extends Column> = TColumn['_']['notNull'] extends true ? TSchema : t.Union<[TSchema, t.TNull]>;
|
|
22
23
|
type HandleInsertColumn<TSchema extends t.TSchema, TColumn extends Column> = TColumn['_']['notNull'] extends true ? TColumn['_']['hasDefault'] extends true ? t.TOptional<TSchema> : TSchema : t.TOptional<t.Union<[TSchema, t.TNull]>>;
|
|
23
24
|
type HandleUpdateColumn<TSchema extends t.TSchema, TColumn extends Column> = TColumn['_']['notNull'] extends true ? t.TOptional<TSchema> : t.TOptional<t.Union<[TSchema, t.TNull]>>;
|
package/index.cjs
CHANGED
|
@@ -24,14 +24,6 @@ const CONSTANTS = {
|
|
|
24
24
|
INT64_UNSIGNED_MAX: 18446744073709551615n,
|
|
25
25
|
};
|
|
26
26
|
|
|
27
|
-
function isColumnType(column, columnTypes) {
|
|
28
|
-
return columnTypes.includes(column.columnType);
|
|
29
|
-
}
|
|
30
|
-
function isWithEnum(column) {
|
|
31
|
-
return 'enumValues' in column && Array.isArray(column.enumValues) && column.enumValues.length > 0;
|
|
32
|
-
}
|
|
33
|
-
const isPgEnum = isWithEnum;
|
|
34
|
-
|
|
35
27
|
const literalSchema = typebox.Type.Union([typebox.Type.String(), typebox.Type.Number(), typebox.Type.Boolean(), typebox.Type.Null()]);
|
|
36
28
|
const jsonSchema = typebox.Type.Union([literalSchema, typebox.Type.Array(typebox.Type.Any()), typebox.Type.Record(typebox.Type.String(), typebox.Type.Any())]);
|
|
37
29
|
typebox.TypeRegistry.Set('Buffer', (_, value) => value instanceof Buffer); // eslint-disable-line no-instanceof/no-instanceof
|
|
@@ -41,150 +33,138 @@ function mapEnumValues(values) {
|
|
|
41
33
|
}
|
|
42
34
|
function columnToSchema(column, t) {
|
|
43
35
|
let schema;
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
if (isColumnType(column, ['PgGeometry', 'PgPointTuple'])) {
|
|
50
|
-
schema = t.Tuple([t.Number(), t.Number()]);
|
|
51
|
-
}
|
|
52
|
-
else if (isColumnType(column, ['PgGeometryObject', 'PgPointObject'])) {
|
|
53
|
-
schema = t.Object({ x: t.Number(), y: t.Number() });
|
|
54
|
-
}
|
|
55
|
-
else if (isColumnType(column, ['PgHalfVector', 'PgVector'])) {
|
|
56
|
-
schema = t.Array(t.Number(), column.dimensions
|
|
57
|
-
? {
|
|
58
|
-
minItems: column.dimensions,
|
|
59
|
-
maxItems: column.dimensions,
|
|
60
|
-
}
|
|
61
|
-
: undefined);
|
|
36
|
+
const { type, constraint } = drizzleOrm.extractExtendedColumnType(column);
|
|
37
|
+
switch (type) {
|
|
38
|
+
case 'array': {
|
|
39
|
+
schema = arrayColumnToSchema(column, constraint, t);
|
|
40
|
+
break;
|
|
62
41
|
}
|
|
63
|
-
|
|
64
|
-
schema =
|
|
42
|
+
case 'object': {
|
|
43
|
+
schema = objectColumnToSchema(column, constraint, t);
|
|
44
|
+
break;
|
|
65
45
|
}
|
|
66
|
-
|
|
67
|
-
schema = t
|
|
68
|
-
|
|
69
|
-
b: t.Number(),
|
|
70
|
-
c: t.Number(),
|
|
71
|
-
});
|
|
72
|
-
} // Handle other types
|
|
73
|
-
else if (isColumnType(column, ['PgArray'])) {
|
|
74
|
-
schema = t.Array(columnToSchema(column.baseColumn, t), column.size
|
|
75
|
-
? {
|
|
76
|
-
minItems: column.size,
|
|
77
|
-
maxItems: column.size,
|
|
78
|
-
}
|
|
79
|
-
: undefined);
|
|
80
|
-
}
|
|
81
|
-
else if (column.dataType === 'array') {
|
|
82
|
-
schema = t.Array(t.Any());
|
|
46
|
+
case 'number': {
|
|
47
|
+
schema = numberColumnToSchema(column, constraint, t);
|
|
48
|
+
break;
|
|
83
49
|
}
|
|
84
|
-
|
|
85
|
-
schema =
|
|
50
|
+
case 'bigint': {
|
|
51
|
+
schema = bigintColumnToSchema(column, constraint, t);
|
|
52
|
+
break;
|
|
86
53
|
}
|
|
87
|
-
|
|
88
|
-
schema = bigintColumnToSchema(column, t);
|
|
89
|
-
}
|
|
90
|
-
else if (column.dataType === 'boolean') {
|
|
54
|
+
case 'boolean': {
|
|
91
55
|
schema = t.Boolean();
|
|
56
|
+
break;
|
|
92
57
|
}
|
|
93
|
-
|
|
94
|
-
schema = t
|
|
95
|
-
|
|
96
|
-
else if (column.dataType === 'string') {
|
|
97
|
-
schema = stringColumnToSchema(column, t);
|
|
98
|
-
}
|
|
99
|
-
else if (column.dataType === 'json') {
|
|
100
|
-
schema = jsonSchema;
|
|
58
|
+
case 'string': {
|
|
59
|
+
schema = stringColumnToSchema(column, constraint, t);
|
|
60
|
+
break;
|
|
101
61
|
}
|
|
102
|
-
|
|
62
|
+
case 'custom': {
|
|
103
63
|
schema = t.Any();
|
|
64
|
+
break;
|
|
104
65
|
}
|
|
105
|
-
|
|
106
|
-
schema =
|
|
66
|
+
default: {
|
|
67
|
+
schema = t.Any();
|
|
107
68
|
}
|
|
108
69
|
}
|
|
109
|
-
if (!schema) {
|
|
110
|
-
schema = t.Any();
|
|
111
|
-
}
|
|
112
70
|
return schema;
|
|
113
71
|
}
|
|
114
|
-
function numberColumnToSchema(column, t) {
|
|
115
|
-
let unsigned = column.getSQLType().includes('unsigned');
|
|
72
|
+
function numberColumnToSchema(column, constraint, t) {
|
|
116
73
|
let min;
|
|
117
74
|
let max;
|
|
118
75
|
let integer = false;
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
'
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
'
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
'
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
'
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
76
|
+
switch (constraint) {
|
|
77
|
+
case 'int8': {
|
|
78
|
+
min = CONSTANTS.INT8_MIN;
|
|
79
|
+
max = CONSTANTS.INT8_MAX;
|
|
80
|
+
integer = true;
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
case 'uint8': {
|
|
84
|
+
min = 0;
|
|
85
|
+
max = CONSTANTS.INT8_UNSIGNED_MAX;
|
|
86
|
+
integer = true;
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
case 'int16': {
|
|
90
|
+
min = CONSTANTS.INT16_MIN;
|
|
91
|
+
max = CONSTANTS.INT16_MAX;
|
|
92
|
+
integer = true;
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
case 'uint16': {
|
|
96
|
+
min = 0;
|
|
97
|
+
max = CONSTANTS.INT16_UNSIGNED_MAX;
|
|
98
|
+
integer = true;
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
case 'int24': {
|
|
102
|
+
min = CONSTANTS.INT24_MIN;
|
|
103
|
+
max = CONSTANTS.INT24_MAX;
|
|
104
|
+
integer = true;
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
case 'uint24': {
|
|
108
|
+
min = 0;
|
|
109
|
+
max = CONSTANTS.INT24_UNSIGNED_MAX;
|
|
110
|
+
integer = true;
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
case 'int32': {
|
|
114
|
+
min = CONSTANTS.INT32_MIN;
|
|
115
|
+
max = CONSTANTS.INT32_MAX;
|
|
116
|
+
integer = true;
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
case 'uint32': {
|
|
120
|
+
min = 0;
|
|
121
|
+
max = CONSTANTS.INT32_UNSIGNED_MAX;
|
|
122
|
+
integer = true;
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
125
|
+
case 'int53': {
|
|
126
|
+
min = Number.MIN_SAFE_INTEGER;
|
|
127
|
+
max = Number.MAX_SAFE_INTEGER;
|
|
128
|
+
integer = true;
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
case 'uint53': {
|
|
132
|
+
min = 0;
|
|
133
|
+
max = Number.MAX_SAFE_INTEGER;
|
|
134
|
+
integer = true;
|
|
135
|
+
break;
|
|
136
|
+
}
|
|
137
|
+
case 'float': {
|
|
138
|
+
min = CONSTANTS.INT24_MIN;
|
|
139
|
+
max = CONSTANTS.INT24_MAX;
|
|
140
|
+
break;
|
|
141
|
+
}
|
|
142
|
+
case 'ufloat': {
|
|
143
|
+
min = 0;
|
|
144
|
+
max = CONSTANTS.INT24_UNSIGNED_MAX;
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
case 'double': {
|
|
148
|
+
min = CONSTANTS.INT48_MIN;
|
|
149
|
+
max = CONSTANTS.INT48_MAX;
|
|
150
|
+
break;
|
|
151
|
+
}
|
|
152
|
+
case 'udouble': {
|
|
153
|
+
min = 0;
|
|
154
|
+
max = CONSTANTS.INT48_UNSIGNED_MAX;
|
|
155
|
+
break;
|
|
156
|
+
}
|
|
157
|
+
case 'year': {
|
|
158
|
+
min = 1901;
|
|
159
|
+
max = 2155;
|
|
160
|
+
integer = true;
|
|
161
|
+
break;
|
|
162
|
+
}
|
|
163
|
+
default: {
|
|
164
|
+
min = Number.MIN_SAFE_INTEGER;
|
|
165
|
+
max = Number.MAX_SAFE_INTEGER;
|
|
166
|
+
break;
|
|
167
|
+
}
|
|
188
168
|
}
|
|
189
169
|
const key = integer ? 'Integer' : 'Number';
|
|
190
170
|
return t[key]({
|
|
@@ -192,65 +172,136 @@ function numberColumnToSchema(column, t) {
|
|
|
192
172
|
maximum: max,
|
|
193
173
|
});
|
|
194
174
|
}
|
|
195
|
-
function
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
175
|
+
function arrayColumnToSchema(column, constraint, t) {
|
|
176
|
+
switch (constraint) {
|
|
177
|
+
case 'geometry':
|
|
178
|
+
case 'point': {
|
|
179
|
+
return t.Tuple([t.Number(), t.Number()]);
|
|
180
|
+
}
|
|
181
|
+
case 'line': {
|
|
182
|
+
return t.Tuple([t.Number(), t.Number(), t.Number()]);
|
|
183
|
+
}
|
|
184
|
+
case 'vector':
|
|
185
|
+
case 'halfvector': {
|
|
186
|
+
const length = column.length;
|
|
187
|
+
const sizeParam = length
|
|
188
|
+
? {
|
|
189
|
+
minItems: length,
|
|
190
|
+
maxItems: length,
|
|
191
|
+
}
|
|
192
|
+
: undefined;
|
|
193
|
+
return t.Array(t.Number(), sizeParam);
|
|
194
|
+
}
|
|
195
|
+
case 'basecolumn': {
|
|
196
|
+
const size = column.length;
|
|
197
|
+
const sizeParam = size
|
|
198
|
+
? {
|
|
199
|
+
minItems: size,
|
|
200
|
+
maxItems: size,
|
|
201
|
+
}
|
|
202
|
+
: undefined;
|
|
203
|
+
return column.baseColumn
|
|
204
|
+
? t.Array(columnToSchema(column.baseColumn, t), sizeParam)
|
|
205
|
+
: t.Array(t.Any(), sizeParam);
|
|
206
|
+
}
|
|
207
|
+
default: {
|
|
208
|
+
return t.Array(t.Any());
|
|
209
|
+
}
|
|
220
210
|
}
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
211
|
+
}
|
|
212
|
+
function objectColumnToSchema(column, constraint, t) {
|
|
213
|
+
switch (constraint) {
|
|
214
|
+
case 'buffer': {
|
|
215
|
+
return bufferSchema;
|
|
224
216
|
}
|
|
225
|
-
|
|
226
|
-
|
|
217
|
+
case 'date': {
|
|
218
|
+
return t.Date();
|
|
227
219
|
}
|
|
228
|
-
|
|
229
|
-
|
|
220
|
+
case 'geometry':
|
|
221
|
+
case 'point': {
|
|
222
|
+
return t.Object({
|
|
223
|
+
x: t.Number(),
|
|
224
|
+
y: t.Number(),
|
|
225
|
+
});
|
|
230
226
|
}
|
|
231
|
-
|
|
232
|
-
|
|
227
|
+
case 'json': {
|
|
228
|
+
return jsonSchema;
|
|
229
|
+
}
|
|
230
|
+
case 'line': {
|
|
231
|
+
return t.Object({
|
|
232
|
+
a: t.Number(),
|
|
233
|
+
b: t.Number(),
|
|
234
|
+
c: t.Number(),
|
|
235
|
+
});
|
|
233
236
|
}
|
|
237
|
+
default: {
|
|
238
|
+
return t.Object({});
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
function bigintColumnToSchema(column, constraint, t) {
|
|
243
|
+
let min;
|
|
244
|
+
let max;
|
|
245
|
+
switch (constraint) {
|
|
246
|
+
case 'int64': {
|
|
247
|
+
min = CONSTANTS.INT64_MIN;
|
|
248
|
+
max = CONSTANTS.INT64_MAX;
|
|
249
|
+
break;
|
|
250
|
+
}
|
|
251
|
+
case 'uint64': {
|
|
252
|
+
min = 0n;
|
|
253
|
+
max = CONSTANTS.INT64_UNSIGNED_MAX;
|
|
254
|
+
break;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
const options = {};
|
|
258
|
+
if (min !== undefined) {
|
|
259
|
+
options.minimum = min;
|
|
234
260
|
}
|
|
235
|
-
if (
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
261
|
+
if (max !== undefined) {
|
|
262
|
+
options.maximum = max;
|
|
263
|
+
}
|
|
264
|
+
return t.BigInt(Object.keys(options).length > 0 ? options : undefined);
|
|
265
|
+
}
|
|
266
|
+
function stringColumnToSchema(column, constraint, t) {
|
|
267
|
+
const { name: columnName, length, isLengthExact } = column;
|
|
268
|
+
let regex;
|
|
269
|
+
if (constraint === 'binary') {
|
|
270
|
+
regex = /^[01]*$/;
|
|
271
|
+
}
|
|
272
|
+
if (constraint === 'uuid') {
|
|
273
|
+
return t.String({
|
|
274
|
+
format: 'uuid',
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
if (constraint === 'enum') {
|
|
278
|
+
const enumValues = column.enumValues;
|
|
279
|
+
if (!enumValues) {
|
|
280
|
+
throw new Error(`Column "${drizzleOrm.getTableName(drizzleOrm.getColumnTable(column))}"."${columnName}" is of 'enum' type, but lacks enum values`);
|
|
281
|
+
}
|
|
282
|
+
return t.Enum(mapEnumValues(enumValues));
|
|
242
283
|
}
|
|
243
284
|
const options = {};
|
|
244
|
-
if (
|
|
245
|
-
options.minLength =
|
|
246
|
-
options.maxLength =
|
|
285
|
+
if (length !== undefined && isLengthExact) {
|
|
286
|
+
options.minLength = length;
|
|
287
|
+
options.maxLength = length;
|
|
247
288
|
}
|
|
248
|
-
else if (
|
|
249
|
-
options.maxLength =
|
|
289
|
+
else if (length !== undefined) {
|
|
290
|
+
options.maxLength = length;
|
|
250
291
|
}
|
|
251
|
-
return
|
|
292
|
+
return regex
|
|
293
|
+
? t.RegExp(regex, Object.keys(options).length > 0 ? options : undefined)
|
|
294
|
+
: t.String(Object.keys(options).length > 0 ? options : undefined);
|
|
252
295
|
}
|
|
253
296
|
|
|
297
|
+
function isColumnType(column, columnTypes) {
|
|
298
|
+
return columnTypes.includes(column.columnType);
|
|
299
|
+
}
|
|
300
|
+
function isWithEnum(column) {
|
|
301
|
+
return 'enumValues' in column && Array.isArray(column.enumValues) && column.enumValues.length > 0;
|
|
302
|
+
}
|
|
303
|
+
const isPgEnum = isWithEnum;
|
|
304
|
+
|
|
254
305
|
function getColumns(tableLike) {
|
|
255
306
|
return drizzleOrm.isTable(tableLike) ? drizzleOrm.getTableColumns(tableLike) : drizzleOrm.getViewSelectedFields(tableLike);
|
|
256
307
|
}
|