drizzle-arktype 0.1.2 → 0.1.3-08e4e66
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 +4 -4
- package/column.d.mts +4 -4
- package/column.d.ts +4 -4
- package/column.types.d.cts +9 -12
- package/column.types.d.mts +9 -12
- package/column.types.d.ts +9 -12
- package/index.cjs +240 -168
- package/index.cjs.map +1 -1
- package/index.d.cts +0 -1
- package/index.d.mts +0 -1
- package/index.d.ts +0 -1
- package/index.mjs +241 -169
- package/index.mjs.map +1 -1
- package/package.json +9 -6
- package/schema.types.d.cts +6 -5
- package/schema.types.d.mts +6 -5
- package/schema.types.d.ts +6 -5
- package/schema.types.internal.d.cts +11 -13
- package/schema.types.internal.d.mts +11 -13
- package/schema.types.internal.d.ts +11 -13
- package/utils.d.cts +7 -9
- package/utils.d.mts +7 -9
- package/utils.d.ts +7 -9
package/column.d.cts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type Type } from 'arktype';
|
|
2
|
-
import type
|
|
3
|
-
export declare const literalSchema: import("arktype
|
|
4
|
-
export declare const jsonSchema: import("arktype
|
|
5
|
-
export declare const bufferSchema: import("arktype/internal/methods/object.ts").ObjectType<Buffer
|
|
2
|
+
import { type Column } from 'drizzle-orm';
|
|
3
|
+
export declare const literalSchema: import("arktype").BaseType<string | number | boolean | null, {}>;
|
|
4
|
+
export declare const jsonSchema: import("arktype").BaseType<string | number | boolean | any[] | Record<string, any> | null, {}>;
|
|
5
|
+
export declare const bufferSchema: import("arktype/internal/methods/object.ts").ObjectType<Buffer<ArrayBufferLike>, {}>;
|
|
6
6
|
export declare function columnToSchema(column: Column): Type;
|
package/column.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type Type } from 'arktype';
|
|
2
|
-
import type
|
|
3
|
-
export declare const literalSchema: import("arktype
|
|
4
|
-
export declare const jsonSchema: import("arktype
|
|
5
|
-
export declare const bufferSchema: import("arktype/internal/methods/object.ts").ObjectType<Buffer
|
|
2
|
+
import { type Column } from 'drizzle-orm';
|
|
3
|
+
export declare const literalSchema: import("arktype").BaseType<string | number | boolean | null, {}>;
|
|
4
|
+
export declare const jsonSchema: import("arktype").BaseType<string | number | boolean | any[] | Record<string, any> | null, {}>;
|
|
5
|
+
export declare const bufferSchema: import("arktype/internal/methods/object.ts").ObjectType<Buffer<ArrayBufferLike>, {}>;
|
|
6
6
|
export declare function columnToSchema(column: Column): Type;
|
package/column.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type Type } from 'arktype';
|
|
2
|
-
import type
|
|
3
|
-
export declare const literalSchema: import("arktype
|
|
4
|
-
export declare const jsonSchema: import("arktype
|
|
5
|
-
export declare const bufferSchema: import("arktype/internal/methods/object.ts").ObjectType<Buffer
|
|
2
|
+
import { type Column } from 'drizzle-orm';
|
|
3
|
+
export declare const literalSchema: import("arktype").BaseType<string | number | boolean | null, {}>;
|
|
4
|
+
export declare const jsonSchema: import("arktype").BaseType<string | number | boolean | any[] | Record<string, any> | null, {}>;
|
|
5
|
+
export declare const bufferSchema: import("arktype/internal/methods/object.ts").ObjectType<Buffer<ArrayBufferLike>, {}>;
|
|
6
6
|
export declare function columnToSchema(column: Column): Type;
|
package/column.types.d.cts
CHANGED
|
@@ -1,14 +1,11 @@
|
|
|
1
|
-
import { Type, type } from 'arktype';
|
|
2
|
-
import type {
|
|
3
|
-
import type {
|
|
4
|
-
export type ArktypeNullable<TSchema> = Type<type.infer<TSchema> | null
|
|
5
|
-
export type ArktypeOptional<TSchema> = [Type<type.infer<TSchema
|
|
6
|
-
export type
|
|
7
|
-
enumValues: [string, ...string[]];
|
|
8
|
-
} ? TColumn['_']['enumValues'] : undefined;
|
|
9
|
-
export type GetArktypeType<TData, TColumnType extends string, TEnumValues extends [string, ...string[]] | undefined> = IsEnumDefined<TEnumValues> extends true ? Type<Assume<TEnumValues, any[]>[number]> : TColumnType extends 'PgJson' | 'PgJsonb' | 'MySqlJson' | 'SingleStoreJson' | 'SQLiteTextJson' | 'SQLiteBlobJson' ? IsUnknown<TData> extends true ? Type<Json> : Type<TData> : Type<TData>;
|
|
1
|
+
import type { Type, type } from 'arktype';
|
|
2
|
+
import type { Column, ColumnTypeData, ExtractColumnTypeData } from 'drizzle-orm';
|
|
3
|
+
import type { Json } from "./utils.cjs";
|
|
4
|
+
export type ArktypeNullable<TSchema> = Type<type.infer<TSchema> | null>;
|
|
5
|
+
export type ArktypeOptional<TSchema> = [Type<type.infer<TSchema>>, '?'];
|
|
6
|
+
export type GetArktypeType<TColumn extends Column, TType extends ColumnTypeData = ExtractColumnTypeData<TColumn['_']['dataType']>> = TType['constraint'] extends 'json' ? unknown extends TColumn['_']['data'] ? Type<Json> : Type<TColumn['_']['data']> : TType['type'] extends 'custom' ? Type<any> : Type<TColumn['_']['data']>;
|
|
10
7
|
type HandleSelectColumn<TSchema, TColumn extends Column> = TColumn['_']['notNull'] extends true ? TSchema : ArktypeNullable<TSchema>;
|
|
11
|
-
type HandleInsertColumn<TSchema, TColumn extends Column> =
|
|
12
|
-
type HandleUpdateColumn<TSchema, TColumn extends Column> =
|
|
13
|
-
export type HandleColumn<TType extends 'select' | 'insert' | 'update', TColumn extends Column> =
|
|
8
|
+
type HandleInsertColumn<TSchema, TColumn extends Column> = TColumn['_']['notNull'] extends true ? TColumn['_']['hasDefault'] extends true ? ArktypeOptional<TSchema> : TSchema : ArktypeOptional<ArktypeNullable<TSchema>>;
|
|
9
|
+
type HandleUpdateColumn<TSchema, TColumn extends Column> = TColumn['_']['notNull'] extends true ? ArktypeOptional<TSchema> : ArktypeOptional<ArktypeNullable<TSchema>>;
|
|
10
|
+
export type HandleColumn<TType extends 'select' | 'insert' | 'update', TColumn extends Column> = TType extends 'select' ? HandleSelectColumn<GetArktypeType<TColumn>, TColumn> : TType extends 'insert' ? HandleInsertColumn<GetArktypeType<TColumn>, TColumn> : TType extends 'update' ? HandleUpdateColumn<GetArktypeType<TColumn>, TColumn> : GetArktypeType<TColumn>;
|
|
14
11
|
export {};
|
package/column.types.d.mts
CHANGED
|
@@ -1,14 +1,11 @@
|
|
|
1
|
-
import { Type, type } from 'arktype';
|
|
2
|
-
import type {
|
|
3
|
-
import type {
|
|
4
|
-
export type ArktypeNullable<TSchema> = Type<type.infer<TSchema> | null
|
|
5
|
-
export type ArktypeOptional<TSchema> = [Type<type.infer<TSchema
|
|
6
|
-
export type
|
|
7
|
-
enumValues: [string, ...string[]];
|
|
8
|
-
} ? TColumn['_']['enumValues'] : undefined;
|
|
9
|
-
export type GetArktypeType<TData, TColumnType extends string, TEnumValues extends [string, ...string[]] | undefined> = IsEnumDefined<TEnumValues> extends true ? Type<Assume<TEnumValues, any[]>[number]> : TColumnType extends 'PgJson' | 'PgJsonb' | 'MySqlJson' | 'SingleStoreJson' | 'SQLiteTextJson' | 'SQLiteBlobJson' ? IsUnknown<TData> extends true ? Type<Json> : Type<TData> : Type<TData>;
|
|
1
|
+
import type { Type, type } from 'arktype';
|
|
2
|
+
import type { Column, ColumnTypeData, ExtractColumnTypeData } from 'drizzle-orm';
|
|
3
|
+
import type { Json } from "./utils.mjs";
|
|
4
|
+
export type ArktypeNullable<TSchema> = Type<type.infer<TSchema> | null>;
|
|
5
|
+
export type ArktypeOptional<TSchema> = [Type<type.infer<TSchema>>, '?'];
|
|
6
|
+
export type GetArktypeType<TColumn extends Column, TType extends ColumnTypeData = ExtractColumnTypeData<TColumn['_']['dataType']>> = TType['constraint'] extends 'json' ? unknown extends TColumn['_']['data'] ? Type<Json> : Type<TColumn['_']['data']> : TType['type'] extends 'custom' ? Type<any> : Type<TColumn['_']['data']>;
|
|
10
7
|
type HandleSelectColumn<TSchema, TColumn extends Column> = TColumn['_']['notNull'] extends true ? TSchema : ArktypeNullable<TSchema>;
|
|
11
|
-
type HandleInsertColumn<TSchema, TColumn extends Column> =
|
|
12
|
-
type HandleUpdateColumn<TSchema, TColumn extends Column> =
|
|
13
|
-
export type HandleColumn<TType extends 'select' | 'insert' | 'update', TColumn extends Column> =
|
|
8
|
+
type HandleInsertColumn<TSchema, TColumn extends Column> = TColumn['_']['notNull'] extends true ? TColumn['_']['hasDefault'] extends true ? ArktypeOptional<TSchema> : TSchema : ArktypeOptional<ArktypeNullable<TSchema>>;
|
|
9
|
+
type HandleUpdateColumn<TSchema, TColumn extends Column> = TColumn['_']['notNull'] extends true ? ArktypeOptional<TSchema> : ArktypeOptional<ArktypeNullable<TSchema>>;
|
|
10
|
+
export type HandleColumn<TType extends 'select' | 'insert' | 'update', TColumn extends Column> = TType extends 'select' ? HandleSelectColumn<GetArktypeType<TColumn>, TColumn> : TType extends 'insert' ? HandleInsertColumn<GetArktypeType<TColumn>, TColumn> : TType extends 'update' ? HandleUpdateColumn<GetArktypeType<TColumn>, TColumn> : GetArktypeType<TColumn>;
|
|
14
11
|
export {};
|
package/column.types.d.ts
CHANGED
|
@@ -1,14 +1,11 @@
|
|
|
1
|
-
import { Type, type } from 'arktype';
|
|
2
|
-
import type {
|
|
3
|
-
import type {
|
|
4
|
-
export type ArktypeNullable<TSchema> = Type<type.infer<TSchema> | null
|
|
5
|
-
export type ArktypeOptional<TSchema> = [Type<type.infer<TSchema
|
|
6
|
-
export type
|
|
7
|
-
enumValues: [string, ...string[]];
|
|
8
|
-
} ? TColumn['_']['enumValues'] : undefined;
|
|
9
|
-
export type GetArktypeType<TData, TColumnType extends string, TEnumValues extends [string, ...string[]] | undefined> = IsEnumDefined<TEnumValues> extends true ? Type<Assume<TEnumValues, any[]>[number]> : TColumnType extends 'PgJson' | 'PgJsonb' | 'MySqlJson' | 'SingleStoreJson' | 'SQLiteTextJson' | 'SQLiteBlobJson' ? IsUnknown<TData> extends true ? Type<Json> : Type<TData> : Type<TData>;
|
|
1
|
+
import type { Type, type } from 'arktype';
|
|
2
|
+
import type { Column, ColumnTypeData, ExtractColumnTypeData } from 'drizzle-orm';
|
|
3
|
+
import type { Json } from './utils.js';
|
|
4
|
+
export type ArktypeNullable<TSchema> = Type<type.infer<TSchema> | null>;
|
|
5
|
+
export type ArktypeOptional<TSchema> = [Type<type.infer<TSchema>>, '?'];
|
|
6
|
+
export type GetArktypeType<TColumn extends Column, TType extends ColumnTypeData = ExtractColumnTypeData<TColumn['_']['dataType']>> = TType['constraint'] extends 'json' ? unknown extends TColumn['_']['data'] ? Type<Json> : Type<TColumn['_']['data']> : TType['type'] extends 'custom' ? Type<any> : Type<TColumn['_']['data']>;
|
|
10
7
|
type HandleSelectColumn<TSchema, TColumn extends Column> = TColumn['_']['notNull'] extends true ? TSchema : ArktypeNullable<TSchema>;
|
|
11
|
-
type HandleInsertColumn<TSchema, TColumn extends Column> =
|
|
12
|
-
type HandleUpdateColumn<TSchema, TColumn extends Column> =
|
|
13
|
-
export type HandleColumn<TType extends 'select' | 'insert' | 'update', TColumn extends Column> =
|
|
8
|
+
type HandleInsertColumn<TSchema, TColumn extends Column> = TColumn['_']['notNull'] extends true ? TColumn['_']['hasDefault'] extends true ? ArktypeOptional<TSchema> : TSchema : ArktypeOptional<ArktypeNullable<TSchema>>;
|
|
9
|
+
type HandleUpdateColumn<TSchema, TColumn extends Column> = TColumn['_']['notNull'] extends true ? ArktypeOptional<TSchema> : ArktypeOptional<ArktypeNullable<TSchema>>;
|
|
10
|
+
export type HandleColumn<TType extends 'select' | 'insert' | 'update', TColumn extends Column> = TType extends 'select' ? HandleSelectColumn<GetArktypeType<TColumn>, TColumn> : TType extends 'insert' ? HandleInsertColumn<GetArktypeType<TColumn>, TColumn> : TType extends 'update' ? HandleUpdateColumn<GetArktypeType<TColumn>, TColumn> : GetArktypeType<TColumn>;
|
|
14
11
|
export {};
|
package/index.cjs
CHANGED
|
@@ -24,210 +24,280 @@ 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 = arktype.type.string.or(arktype.type.number).or(arktype.type.boolean).or(arktype.type.null);
|
|
36
28
|
const jsonSchema = literalSchema.or(arktype.type.unknown.as().array()).or(arktype.type.object.as());
|
|
37
|
-
const bufferSchema = arktype.type.
|
|
29
|
+
const bufferSchema = arktype.type.unknown.narrow((value) => value instanceof Buffer).as().describe(// eslint-disable-line no-instanceof/no-instanceof
|
|
30
|
+
'a Buffer instance');
|
|
38
31
|
function columnToSchema(column) {
|
|
39
32
|
let schema;
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
if (isColumnType(column, ['PgGeometry', 'PgPointTuple'])) {
|
|
46
|
-
schema = arktype.type([arktype.type.number, arktype.type.number]);
|
|
33
|
+
const { type: columnType, constraint } = drizzleOrm.extractExtendedColumnType(column);
|
|
34
|
+
switch (columnType) {
|
|
35
|
+
case 'array': {
|
|
36
|
+
schema = arrayColumnToSchema(column, constraint);
|
|
37
|
+
break;
|
|
47
38
|
}
|
|
48
|
-
|
|
49
|
-
schema =
|
|
50
|
-
|
|
51
|
-
y: arktype.type.number,
|
|
52
|
-
});
|
|
39
|
+
case 'object': {
|
|
40
|
+
schema = objectColumnToSchema(column, constraint);
|
|
41
|
+
break;
|
|
53
42
|
}
|
|
54
|
-
|
|
55
|
-
schema = column
|
|
56
|
-
|
|
57
|
-
: arktype.type.number.array();
|
|
43
|
+
case 'number': {
|
|
44
|
+
schema = numberColumnToSchema(column, constraint);
|
|
45
|
+
break;
|
|
58
46
|
}
|
|
59
|
-
|
|
60
|
-
schema =
|
|
47
|
+
case 'bigint': {
|
|
48
|
+
schema = bigintColumnToSchema(column, constraint);
|
|
49
|
+
break;
|
|
61
50
|
}
|
|
62
|
-
|
|
63
|
-
schema = arktype.type({
|
|
64
|
-
a: arktype.type.number,
|
|
65
|
-
b: arktype.type.number,
|
|
66
|
-
c: arktype.type.number,
|
|
67
|
-
});
|
|
68
|
-
} // Handle other types
|
|
69
|
-
else if (isColumnType(column, ['PgArray'])) {
|
|
70
|
-
const arraySchema = columnToSchema(column.baseColumn).array();
|
|
71
|
-
schema = column.size ? arraySchema.exactlyLength(column.size) : arraySchema;
|
|
72
|
-
}
|
|
73
|
-
else if (column.dataType === 'array') {
|
|
74
|
-
schema = arktype.type.unknown.array();
|
|
75
|
-
}
|
|
76
|
-
else if (column.dataType === 'number') {
|
|
77
|
-
schema = numberColumnToSchema(column);
|
|
78
|
-
}
|
|
79
|
-
else if (column.dataType === 'bigint') {
|
|
80
|
-
schema = bigintColumnToSchema(column);
|
|
81
|
-
}
|
|
82
|
-
else if (column.dataType === 'boolean') {
|
|
51
|
+
case 'boolean': {
|
|
83
52
|
schema = arktype.type.boolean;
|
|
53
|
+
break;
|
|
84
54
|
}
|
|
85
|
-
|
|
86
|
-
schema =
|
|
87
|
-
|
|
88
|
-
else if (column.dataType === 'string') {
|
|
89
|
-
schema = stringColumnToSchema(column);
|
|
90
|
-
}
|
|
91
|
-
else if (column.dataType === 'json') {
|
|
92
|
-
schema = jsonSchema;
|
|
55
|
+
case 'string': {
|
|
56
|
+
schema = stringColumnToSchema(column, constraint);
|
|
57
|
+
break;
|
|
93
58
|
}
|
|
94
|
-
|
|
59
|
+
case 'custom': {
|
|
95
60
|
schema = arktype.type.unknown;
|
|
61
|
+
break;
|
|
96
62
|
}
|
|
97
|
-
|
|
98
|
-
schema =
|
|
63
|
+
default: {
|
|
64
|
+
schema = arktype.type.unknown;
|
|
99
65
|
}
|
|
100
66
|
}
|
|
101
|
-
if (!schema) {
|
|
102
|
-
schema = arktype.type.unknown;
|
|
103
|
-
}
|
|
104
67
|
return schema;
|
|
105
68
|
}
|
|
106
|
-
function numberColumnToSchema(column) {
|
|
107
|
-
let unsigned = column.getSQLType().includes('unsigned');
|
|
69
|
+
function numberColumnToSchema(column, constraint) {
|
|
108
70
|
let min;
|
|
109
71
|
let max;
|
|
110
72
|
let integer = false;
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
'
|
|
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
|
-
|
|
73
|
+
switch (constraint) {
|
|
74
|
+
case 'int8': {
|
|
75
|
+
min = CONSTANTS.INT8_MIN;
|
|
76
|
+
max = CONSTANTS.INT8_MAX;
|
|
77
|
+
integer = true;
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
case 'uint8': {
|
|
81
|
+
min = 0;
|
|
82
|
+
max = CONSTANTS.INT8_UNSIGNED_MAX;
|
|
83
|
+
integer = true;
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
case 'int16': {
|
|
87
|
+
min = CONSTANTS.INT16_MIN;
|
|
88
|
+
max = CONSTANTS.INT16_MAX;
|
|
89
|
+
integer = true;
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
92
|
+
case 'uint16': {
|
|
93
|
+
min = 0;
|
|
94
|
+
max = CONSTANTS.INT16_UNSIGNED_MAX;
|
|
95
|
+
integer = true;
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
case 'int24': {
|
|
99
|
+
min = CONSTANTS.INT24_MIN;
|
|
100
|
+
max = CONSTANTS.INT24_MAX;
|
|
101
|
+
integer = true;
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
case 'uint24': {
|
|
105
|
+
min = 0;
|
|
106
|
+
max = CONSTANTS.INT24_UNSIGNED_MAX;
|
|
107
|
+
integer = true;
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
case 'int32': {
|
|
111
|
+
min = CONSTANTS.INT32_MIN;
|
|
112
|
+
max = CONSTANTS.INT32_MAX;
|
|
113
|
+
integer = true;
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
case 'uint32': {
|
|
117
|
+
min = 0;
|
|
118
|
+
max = CONSTANTS.INT32_UNSIGNED_MAX;
|
|
119
|
+
integer = true;
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
case 'int53': {
|
|
123
|
+
min = Number.MIN_SAFE_INTEGER;
|
|
124
|
+
max = Number.MAX_SAFE_INTEGER;
|
|
125
|
+
integer = true;
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
case 'uint53': {
|
|
129
|
+
min = 0;
|
|
130
|
+
max = Number.MAX_SAFE_INTEGER;
|
|
131
|
+
integer = true;
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
134
|
+
case 'float': {
|
|
135
|
+
min = CONSTANTS.INT24_MIN;
|
|
136
|
+
max = CONSTANTS.INT24_MAX;
|
|
137
|
+
break;
|
|
138
|
+
}
|
|
139
|
+
case 'ufloat': {
|
|
140
|
+
min = 0;
|
|
141
|
+
max = CONSTANTS.INT24_UNSIGNED_MAX;
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
case 'double': {
|
|
145
|
+
min = CONSTANTS.INT48_MIN;
|
|
146
|
+
max = CONSTANTS.INT48_MAX;
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
case 'udouble': {
|
|
150
|
+
min = 0;
|
|
151
|
+
max = CONSTANTS.INT48_UNSIGNED_MAX;
|
|
152
|
+
break;
|
|
153
|
+
}
|
|
154
|
+
case 'year': {
|
|
155
|
+
min = 1901;
|
|
156
|
+
max = 2155;
|
|
157
|
+
integer = true;
|
|
158
|
+
break;
|
|
159
|
+
}
|
|
160
|
+
case 'unsigned': {
|
|
161
|
+
min = 0;
|
|
162
|
+
max = Number.MAX_SAFE_INTEGER;
|
|
163
|
+
break;
|
|
164
|
+
}
|
|
165
|
+
default: {
|
|
166
|
+
min = Number.MIN_SAFE_INTEGER;
|
|
167
|
+
max = Number.MAX_SAFE_INTEGER;
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
171
170
|
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
171
|
+
return (integer ? arktype.type.keywords.number.integer : arktype.type.number).atLeast(min).atMost(max);
|
|
172
|
+
}
|
|
173
|
+
function arrayColumnToSchema(column, constraint) {
|
|
174
|
+
switch (constraint) {
|
|
175
|
+
case 'geometry':
|
|
176
|
+
case 'point': {
|
|
177
|
+
return arktype.type([arktype.type.number, arktype.type.number]);
|
|
178
|
+
}
|
|
179
|
+
case 'line': {
|
|
180
|
+
return arktype.type([arktype.type.number, arktype.type.number, arktype.type.number]);
|
|
181
|
+
}
|
|
182
|
+
case 'vector':
|
|
183
|
+
case 'halfvector': {
|
|
184
|
+
const length = column.length;
|
|
185
|
+
return length ? arktype.type.number.array().exactlyLength(length) : arktype.type.number.array();
|
|
186
|
+
}
|
|
187
|
+
case 'int64vector': {
|
|
188
|
+
const length = column.length;
|
|
189
|
+
// TODO - INT64 number range
|
|
190
|
+
return length ? arktype.type.bigint.array().exactlyLength(length) : arktype.type.bigint.array();
|
|
191
|
+
}
|
|
192
|
+
case 'basecolumn': {
|
|
193
|
+
const length = column.length;
|
|
194
|
+
const schema = column.baseColumn
|
|
195
|
+
? columnToSchema(column.baseColumn).array()
|
|
196
|
+
: arktype.type.unknown.array();
|
|
197
|
+
if (length)
|
|
198
|
+
return schema.exactlyLength(length);
|
|
199
|
+
return schema;
|
|
200
|
+
}
|
|
201
|
+
default: {
|
|
202
|
+
return arktype.type.unknown.array();
|
|
203
|
+
}
|
|
176
204
|
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
205
|
+
}
|
|
206
|
+
function objectColumnToSchema(column, constraint) {
|
|
207
|
+
switch (constraint) {
|
|
208
|
+
case 'buffer': {
|
|
209
|
+
return bufferSchema;
|
|
210
|
+
}
|
|
211
|
+
case 'date': {
|
|
212
|
+
return arktype.type.Date;
|
|
213
|
+
}
|
|
214
|
+
case 'geometry':
|
|
215
|
+
case 'point': {
|
|
216
|
+
return arktype.type({
|
|
217
|
+
x: arktype.type.number,
|
|
218
|
+
y: arktype.type.number,
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
case 'json': {
|
|
222
|
+
return jsonSchema;
|
|
223
|
+
}
|
|
224
|
+
case 'line': {
|
|
225
|
+
return arktype.type({
|
|
226
|
+
a: arktype.type.number,
|
|
227
|
+
b: arktype.type.number,
|
|
228
|
+
c: arktype.type.number,
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
default: {
|
|
232
|
+
return arktype.type({});
|
|
233
|
+
}
|
|
180
234
|
}
|
|
181
|
-
return (integer ? arktype.type.keywords.number.integer : arktype.type.number).atLeast(min).atMost(max);
|
|
182
235
|
}
|
|
183
236
|
/** @internal */
|
|
184
237
|
const unsignedBigintNarrow = (v, ctx) => v < 0n ? ctx.mustBe('greater than') : v > CONSTANTS.INT64_UNSIGNED_MAX ? ctx.mustBe('less than') : true;
|
|
185
238
|
/** @internal */
|
|
186
239
|
const bigintNarrow = (v, ctx) => v < CONSTANTS.INT64_MIN ? ctx.mustBe('greater than') : v > CONSTANTS.INT64_MAX ? ctx.mustBe('less than') : true;
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
function stringColumnToSchema(column) {
|
|
192
|
-
if (isColumnType(column, ['PgUUID'])) {
|
|
193
|
-
return arktype.type(/^[\da-f]{8}(?:-[\da-f]{4}){3}-[\da-f]{12}$/iu).describe('a RFC-4122-compliant UUID');
|
|
240
|
+
/** @internal */
|
|
241
|
+
const bigintStringModeSchema = arktype.type.string.narrow((v, ctx) => {
|
|
242
|
+
if (typeof v !== 'string') {
|
|
243
|
+
return ctx.mustBe('a string');
|
|
194
244
|
}
|
|
195
|
-
if (
|
|
196
|
-
return
|
|
197
|
-
.describe(`a string containing ones or zeros while being ${column.dimensions} characters long`);
|
|
245
|
+
if (!(/^-?\d+$/.test(v))) {
|
|
246
|
+
return ctx.mustBe('a string representing a number');
|
|
198
247
|
}
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
max = column.length;
|
|
248
|
+
const bigint = BigInt(v);
|
|
249
|
+
if (bigint < CONSTANTS.INT64_MIN) {
|
|
250
|
+
return ctx.mustBe('greater than');
|
|
203
251
|
}
|
|
204
|
-
|
|
205
|
-
|
|
252
|
+
if (bigint > CONSTANTS.INT64_MAX) {
|
|
253
|
+
return ctx.mustBe('less than');
|
|
206
254
|
}
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
255
|
+
return true;
|
|
256
|
+
});
|
|
257
|
+
function bigintColumnToSchema(column, constraint) {
|
|
258
|
+
switch (constraint) {
|
|
259
|
+
case 'int64': {
|
|
260
|
+
return arktype.type.bigint.narrow(bigintNarrow);
|
|
213
261
|
}
|
|
214
|
-
|
|
215
|
-
|
|
262
|
+
case 'uint64': {
|
|
263
|
+
return arktype.type.bigint.narrow(unsignedBigintNarrow);
|
|
216
264
|
}
|
|
217
|
-
|
|
218
|
-
|
|
265
|
+
}
|
|
266
|
+
return arktype.type.bigint;
|
|
267
|
+
}
|
|
268
|
+
function stringColumnToSchema(column, constraint) {
|
|
269
|
+
const { name: columnName, length, isLengthExact } = column;
|
|
270
|
+
if (constraint === 'binary') {
|
|
271
|
+
return arktype.type(`/^[01]${length ? `{${isLengthExact ? length : `0,${length}`}}` : '*'}$/`)
|
|
272
|
+
.describe(`a string containing ones or zeros${length ? ` while being ${isLengthExact ? '' : 'up to '}${length} characters long` : ''}`);
|
|
273
|
+
}
|
|
274
|
+
if (constraint === 'uuid') {
|
|
275
|
+
return arktype.type(/^[\da-f]{8}(?:-[\da-f]{4}){3}-[\da-f]{12}$/iu).describe('a RFC-4122-compliant UUID');
|
|
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`);
|
|
219
281
|
}
|
|
282
|
+
return arktype.type.enumerated(...enumValues);
|
|
220
283
|
}
|
|
221
|
-
if (
|
|
222
|
-
|
|
223
|
-
'MySqlChar',
|
|
224
|
-
'SingleStoreChar',
|
|
225
|
-
])) {
|
|
226
|
-
max = column.length;
|
|
227
|
-
fixed = true;
|
|
284
|
+
if (constraint === 'int64') {
|
|
285
|
+
return bigintStringModeSchema;
|
|
228
286
|
}
|
|
229
|
-
return
|
|
287
|
+
return length && isLengthExact
|
|
288
|
+
? arktype.type.string.exactlyLength(length)
|
|
289
|
+
: length
|
|
290
|
+
? arktype.type.string.atMostLength(length)
|
|
291
|
+
: arktype.type.string;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
function isColumnType(column, columnTypes) {
|
|
295
|
+
return columnTypes.includes(column.columnType);
|
|
296
|
+
}
|
|
297
|
+
function isWithEnum(column) {
|
|
298
|
+
return 'enumValues' in column && Array.isArray(column.enumValues) && column.enumValues.length > 0;
|
|
230
299
|
}
|
|
300
|
+
const isPgEnum = isWithEnum;
|
|
231
301
|
|
|
232
302
|
function getColumns(tableLike) {
|
|
233
303
|
return drizzleOrm.isTable(tableLike) ? drizzleOrm.getTableColumns(tableLike) : drizzleOrm.getViewSelectedFields(tableLike);
|
|
@@ -280,7 +350,8 @@ const createSelectSchema = ((entity, refine) => {
|
|
|
280
350
|
const createInsertSchema = ((entity, refine) => {
|
|
281
351
|
const columns = getColumns(entity);
|
|
282
352
|
return handleColumns(columns, refine ?? {}, {
|
|
283
|
-
never: (column) => column?.generated?.type === 'always' || column?.generatedIdentity?.type === 'always'
|
|
353
|
+
never: (column) => column?.generated?.type === 'always' || column?.generatedIdentity?.type === 'always'
|
|
354
|
+
|| ('identity' in (column ?? {}) && typeof column?.identity !== 'undefined'),
|
|
284
355
|
optional: (column) => !column.notNull || (column.notNull && column.hasDefault),
|
|
285
356
|
nullable: (column) => !column.notNull,
|
|
286
357
|
});
|
|
@@ -288,7 +359,8 @@ const createInsertSchema = ((entity, refine) => {
|
|
|
288
359
|
const createUpdateSchema = ((entity, refine) => {
|
|
289
360
|
const columns = getColumns(entity);
|
|
290
361
|
return handleColumns(columns, refine ?? {}, {
|
|
291
|
-
never: (column) => column?.generated?.type === 'always' || column?.generatedIdentity?.type === 'always'
|
|
362
|
+
never: (column) => column?.generated?.type === 'always' || column?.generatedIdentity?.type === 'always'
|
|
363
|
+
|| ('identity' in (column ?? {}) && typeof column?.identity !== 'undefined'),
|
|
292
364
|
optional: () => true,
|
|
293
365
|
nullable: (column) => !column.notNull,
|
|
294
366
|
});
|