drizzle-arktype 0.1.2 → 0.1.3-02522e1

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  import { type Type } from 'arktype';
2
- import type { Column } from 'drizzle-orm';
3
- export declare const literalSchema: import("arktype/internal/methods/base.ts").BaseType<string | number | boolean | null, {}>;
4
- export declare const jsonSchema: import("arktype/internal/methods/base.ts").BaseType<string | number | boolean | any[] | Record<string, any> | null, {}>;
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/variants/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 { Column } from 'drizzle-orm';
3
- export declare const literalSchema: import("arktype/internal/methods/base.ts").BaseType<string | number | boolean | null, {}>;
4
- export declare const jsonSchema: import("arktype/internal/methods/base.ts").BaseType<string | number | boolean | any[] | Record<string, any> | null, {}>;
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/variants/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 { Column } from 'drizzle-orm';
3
- export declare const literalSchema: import("arktype/internal/methods/base.ts").BaseType<string | number | boolean | null, {}>;
4
- export declare const jsonSchema: import("arktype/internal/methods/base.ts").BaseType<string | number | boolean | any[] | Record<string, any> | null, {}>;
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/variants/object.ts").ObjectType<Buffer<ArrayBufferLike>, {}>;
6
6
  export declare function columnToSchema(column: Column): Type;
@@ -1,14 +1,11 @@
1
- import { Type, type } from 'arktype';
2
- import type { Assume, Column } from 'drizzle-orm';
3
- import type { ColumnIsGeneratedAlwaysAs, IsEnumDefined, IsUnknown, 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 GetEnumValuesFromColumn<TColumn extends Column> = TColumn['_'] extends {
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> = ColumnIsGeneratedAlwaysAs<TColumn> extends true ? never : TColumn['_']['notNull'] extends true ? TColumn['_']['hasDefault'] extends true ? ArktypeOptional<TSchema> : TSchema : ArktypeOptional<ArktypeNullable<TSchema>>;
12
- type HandleUpdateColumn<TSchema, TColumn extends Column> = ColumnIsGeneratedAlwaysAs<TColumn> extends true ? never : TColumn['_']['notNull'] extends true ? ArktypeOptional<TSchema> : ArktypeOptional<ArktypeNullable<TSchema>>;
13
- export type HandleColumn<TType extends 'select' | 'insert' | 'update', TColumn extends Column> = GetArktypeType<TColumn['_']['data'], TColumn['_']['columnType'], GetEnumValuesFromColumn<TColumn>> extends infer TSchema ? TType extends 'select' ? HandleSelectColumn<TSchema, TColumn> : TType extends 'insert' ? HandleInsertColumn<TSchema, TColumn> : TType extends 'update' ? HandleUpdateColumn<TSchema, TColumn> : TSchema : Type;
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 {};
@@ -1,14 +1,11 @@
1
- import { Type, type } from 'arktype';
2
- import type { Assume, Column } from 'drizzle-orm';
3
- import type { ColumnIsGeneratedAlwaysAs, IsEnumDefined, IsUnknown, 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 GetEnumValuesFromColumn<TColumn extends Column> = TColumn['_'] extends {
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> = ColumnIsGeneratedAlwaysAs<TColumn> extends true ? never : TColumn['_']['notNull'] extends true ? TColumn['_']['hasDefault'] extends true ? ArktypeOptional<TSchema> : TSchema : ArktypeOptional<ArktypeNullable<TSchema>>;
12
- type HandleUpdateColumn<TSchema, TColumn extends Column> = ColumnIsGeneratedAlwaysAs<TColumn> extends true ? never : TColumn['_']['notNull'] extends true ? ArktypeOptional<TSchema> : ArktypeOptional<ArktypeNullable<TSchema>>;
13
- export type HandleColumn<TType extends 'select' | 'insert' | 'update', TColumn extends Column> = GetArktypeType<TColumn['_']['data'], TColumn['_']['columnType'], GetEnumValuesFromColumn<TColumn>> extends infer TSchema ? TType extends 'select' ? HandleSelectColumn<TSchema, TColumn> : TType extends 'insert' ? HandleInsertColumn<TSchema, TColumn> : TType extends 'update' ? HandleUpdateColumn<TSchema, TColumn> : TSchema : Type;
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 { Assume, Column } from 'drizzle-orm';
3
- import type { ColumnIsGeneratedAlwaysAs, IsEnumDefined, IsUnknown, 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 GetEnumValuesFromColumn<TColumn extends Column> = TColumn['_'] extends {
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> = ColumnIsGeneratedAlwaysAs<TColumn> extends true ? never : TColumn['_']['notNull'] extends true ? TColumn['_']['hasDefault'] extends true ? ArktypeOptional<TSchema> : TSchema : ArktypeOptional<ArktypeNullable<TSchema>>;
12
- type HandleUpdateColumn<TSchema, TColumn extends Column> = ColumnIsGeneratedAlwaysAs<TColumn> extends true ? never : TColumn['_']['notNull'] extends true ? ArktypeOptional<TSchema> : ArktypeOptional<ArktypeNullable<TSchema>>;
13
- export type HandleColumn<TType extends 'select' | 'insert' | 'update', TColumn extends Column> = GetArktypeType<TColumn['_']['data'], TColumn['_']['columnType'], GetEnumValuesFromColumn<TColumn>> extends infer TSchema ? TType extends 'select' ? HandleSelectColumn<TSchema, TColumn> : TType extends 'insert' ? HandleInsertColumn<TSchema, TColumn> : TType extends 'update' ? HandleUpdateColumn<TSchema, TColumn> : TSchema : Type;
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.instanceOf(Buffer); // eslint-disable-line no-instanceof/no-instanceof
29
+ const bufferSchema = arktype.type.unknown.narrow((value) => value instanceof Buffer).as().describe(// oxlint-disable-line drizzle-internal/no-instanceof
30
+ 'a Buffer instance');
38
31
  function columnToSchema(column) {
39
32
  let schema;
40
- if (isWithEnum(column)) {
41
- schema = column.enumValues.length ? arktype.type.enumerated(...column.enumValues) : arktype.type.string;
42
- }
43
- if (!schema) {
44
- // Handle specific types
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
- else if (isColumnType(column, ['PgGeometryObject', 'PgPointObject'])) {
49
- schema = arktype.type({
50
- x: arktype.type.number,
51
- y: arktype.type.number,
52
- });
39
+ case 'object': {
40
+ schema = objectColumnToSchema(column, constraint);
41
+ break;
53
42
  }
54
- else if (isColumnType(column, ['PgHalfVector', 'PgVector'])) {
55
- schema = column.dimensions
56
- ? arktype.type.number.array().exactlyLength(column.dimensions)
57
- : arktype.type.number.array();
43
+ case 'number': {
44
+ schema = numberColumnToSchema(column, constraint);
45
+ break;
58
46
  }
59
- else if (isColumnType(column, ['PgLine'])) {
60
- schema = arktype.type([arktype.type.number, arktype.type.number, arktype.type.number]);
47
+ case 'bigint': {
48
+ schema = bigintColumnToSchema(column, constraint);
49
+ break;
61
50
  }
62
- else if (isColumnType(column, ['PgLineABC'])) {
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
- else if (column.dataType === 'date') {
86
- schema = arktype.type.Date;
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
- else if (column.dataType === 'custom') {
59
+ case 'custom': {
95
60
  schema = arktype.type.unknown;
61
+ break;
96
62
  }
97
- else if (column.dataType === 'buffer') {
98
- schema = bufferSchema;
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
- if (isColumnType(column, ['MySqlTinyInt', 'SingleStoreTinyInt'])) {
112
- min = unsigned ? 0 : CONSTANTS.INT8_MIN;
113
- max = unsigned ? CONSTANTS.INT8_UNSIGNED_MAX : CONSTANTS.INT8_MAX;
114
- integer = true;
115
- }
116
- else if (isColumnType(column, [
117
- 'PgSmallInt',
118
- 'PgSmallSerial',
119
- 'MySqlSmallInt',
120
- 'SingleStoreSmallInt',
121
- ])) {
122
- min = unsigned ? 0 : CONSTANTS.INT16_MIN;
123
- max = unsigned ? CONSTANTS.INT16_UNSIGNED_MAX : CONSTANTS.INT16_MAX;
124
- integer = true;
125
- }
126
- else if (isColumnType(column, [
127
- 'PgReal',
128
- 'MySqlFloat',
129
- 'MySqlMediumInt',
130
- 'SingleStoreFloat',
131
- 'SingleStoreMediumInt',
132
- ])) {
133
- min = unsigned ? 0 : CONSTANTS.INT24_MIN;
134
- max = unsigned ? CONSTANTS.INT24_UNSIGNED_MAX : CONSTANTS.INT24_MAX;
135
- integer = isColumnType(column, ['MySqlMediumInt', 'SingleStoreMediumInt']);
136
- }
137
- else if (isColumnType(column, [
138
- 'PgInteger',
139
- 'PgSerial',
140
- 'MySqlInt',
141
- 'SingleStoreInt',
142
- ])) {
143
- min = unsigned ? 0 : CONSTANTS.INT32_MIN;
144
- max = unsigned ? CONSTANTS.INT32_UNSIGNED_MAX : CONSTANTS.INT32_MAX;
145
- integer = true;
146
- }
147
- else if (isColumnType(column, [
148
- 'PgDoublePrecision',
149
- 'MySqlReal',
150
- 'MySqlDouble',
151
- 'SingleStoreReal',
152
- 'SingleStoreDouble',
153
- 'SQLiteReal',
154
- ])) {
155
- min = unsigned ? 0 : CONSTANTS.INT48_MIN;
156
- max = unsigned ? CONSTANTS.INT48_UNSIGNED_MAX : CONSTANTS.INT48_MAX;
157
- }
158
- else if (isColumnType(column, [
159
- 'PgBigInt53',
160
- 'PgBigSerial53',
161
- 'MySqlBigInt53',
162
- 'MySqlSerial',
163
- 'SingleStoreBigInt53',
164
- 'SingleStoreSerial',
165
- 'SQLiteInteger',
166
- ])) {
167
- unsigned = unsigned || isColumnType(column, ['MySqlSerial', 'SingleStoreSerial']);
168
- min = unsigned ? 0 : Number.MIN_SAFE_INTEGER;
169
- max = Number.MAX_SAFE_INTEGER;
170
- integer = true;
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
- else if (isColumnType(column, ['MySqlYear', 'SingleStoreYear'])) {
173
- min = 1901;
174
- max = 2155;
175
- integer = true;
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
- else {
178
- min = Number.MIN_SAFE_INTEGER;
179
- max = Number.MAX_SAFE_INTEGER;
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
- function bigintColumnToSchema(column) {
188
- const unsigned = column.getSQLType().includes('unsigned');
189
- return arktype.type.bigint.narrow(unsigned ? unsignedBigintNarrow : bigintNarrow);
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 (isColumnType(column, ['PgBinaryVector'])) {
196
- return arktype.type(`/^[01]{${column.dimensions}}$/`)
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
- let max;
200
- let fixed = false;
201
- if (isColumnType(column, ['PgVarchar', 'SQLiteText'])) {
202
- max = column.length;
248
+ const bigint = BigInt(v);
249
+ if (bigint < CONSTANTS.INT64_MIN) {
250
+ return ctx.mustBe('greater than');
203
251
  }
204
- else if (isColumnType(column, ['MySqlVarChar', 'SingleStoreVarChar'])) {
205
- max = column.length ?? CONSTANTS.INT16_UNSIGNED_MAX;
252
+ if (bigint > CONSTANTS.INT64_MAX) {
253
+ return ctx.mustBe('less than');
206
254
  }
207
- else if (isColumnType(column, ['MySqlText', 'SingleStoreText'])) {
208
- if (column.textType === 'longtext') {
209
- max = CONSTANTS.INT32_UNSIGNED_MAX;
210
- }
211
- else if (column.textType === 'mediumtext') {
212
- max = CONSTANTS.INT24_UNSIGNED_MAX;
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
- else if (column.textType === 'text') {
215
- max = CONSTANTS.INT16_UNSIGNED_MAX;
262
+ case 'uint64': {
263
+ return arktype.type.bigint.narrow(unsignedBigintNarrow);
216
264
  }
217
- else {
218
- max = CONSTANTS.INT8_UNSIGNED_MAX;
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 (isColumnType(column, [
222
- 'PgChar',
223
- 'MySqlChar',
224
- 'SingleStoreChar',
225
- ])) {
226
- max = column.length;
227
- fixed = true;
284
+ if (constraint === 'int64') {
285
+ return bigintStringModeSchema;
228
286
  }
229
- return max && fixed ? arktype.type.string.exactlyLength(max) : max ? arktype.type.string.atMostLength(max) : arktype.type.string;
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
  });