drizzle-typebox 0.3.3-84d9a79 → 0.3.3-860c2f2

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,5 +1,5 @@
1
1
  import type { TSchema, Type as typebox } from '@sinclair/typebox';
2
- import type { Column } from 'drizzle-orm';
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 { Column } from 'drizzle-orm';
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 { Column } from 'drizzle-orm';
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;
@@ -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, IsEnumDefined, IsNever, JsonSchema } from "./utils.cjs";
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> = TColumn['_']['columnType'] extends 'MySqlTinyInt' | 'SingleStoreTinyInt' | 'PgSmallInt' | 'PgSmallSerial' | 'MySqlSmallInt' | 'MySqlMediumInt' | 'SingleStoreSmallInt' | 'SingleStoreMediumInt' | 'PgInteger' | 'PgSerial' | 'MySqlInt' | 'SingleStoreInt' | 'PgBigInt53' | 'PgBigSerial53' | 'MySqlBigInt53' | 'MySqlSerial' | 'SingleStoreBigInt53' | 'SingleStoreSerial' | 'SQLiteInteger' | 'MySqlYear' | 'SingleStoreYear' ? t.TInteger : TColumn['_']['columnType'] extends 'PgBinaryVector' ? t.TRegExp : HasBaseColumn<TColumn> extends true ? t.TArray<GetTypeboxType<Assume<TColumn['_']['baseColumn'], Column>>> : IsEnumDefined<TColumn['_']['enumValues']> extends true ? t.TEnum<{
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
- }> : TColumn['_']['columnType'] extends 'PgGeometry' | 'PgPointTuple' ? t.TTuple<[t.TNumber, t.TNumber]> : TColumn['_']['columnType'] extends 'PgLine' ? t.TTuple<[t.TNumber, t.TNumber, t.TNumber]> : TColumn['_']['data'] extends Date ? t.TDate : TColumn['_']['data'] extends Buffer ? BufferSchema : TColumn['_']['dataType'] extends 'array' ? t.TArray<GetTypeboxPrimitiveType<Assume<TColumn['_']['data'], any[]>[number]>> : TColumn['_']['data'] extends Record<string, any> ? TColumn['_']['columnType'] extends 'PgJson' | 'PgJsonb' | 'MySqlJson' | 'SingleStoreJson' | 'SQLiteTextJson' | 'SQLiteBlobJson' ? GenericSchema<TColumn['_']['data']> : t.TObject<{
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]>>;
@@ -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, IsEnumDefined, IsNever, JsonSchema } from "./utils.mjs";
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> = TColumn['_']['columnType'] extends 'MySqlTinyInt' | 'SingleStoreTinyInt' | 'PgSmallInt' | 'PgSmallSerial' | 'MySqlSmallInt' | 'MySqlMediumInt' | 'SingleStoreSmallInt' | 'SingleStoreMediumInt' | 'PgInteger' | 'PgSerial' | 'MySqlInt' | 'SingleStoreInt' | 'PgBigInt53' | 'PgBigSerial53' | 'MySqlBigInt53' | 'MySqlSerial' | 'SingleStoreBigInt53' | 'SingleStoreSerial' | 'SQLiteInteger' | 'MySqlYear' | 'SingleStoreYear' ? t.TInteger : TColumn['_']['columnType'] extends 'PgBinaryVector' ? t.TRegExp : HasBaseColumn<TColumn> extends true ? t.TArray<GetTypeboxType<Assume<TColumn['_']['baseColumn'], Column>>> : IsEnumDefined<TColumn['_']['enumValues']> extends true ? t.TEnum<{
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
- }> : TColumn['_']['columnType'] extends 'PgGeometry' | 'PgPointTuple' ? t.TTuple<[t.TNumber, t.TNumber]> : TColumn['_']['columnType'] extends 'PgLine' ? t.TTuple<[t.TNumber, t.TNumber, t.TNumber]> : TColumn['_']['data'] extends Date ? t.TDate : TColumn['_']['data'] extends Buffer ? BufferSchema : TColumn['_']['dataType'] extends 'array' ? t.TArray<GetTypeboxPrimitiveType<Assume<TColumn['_']['data'], any[]>[number]>> : TColumn['_']['data'] extends Record<string, any> ? TColumn['_']['columnType'] extends 'PgJson' | 'PgJsonb' | 'MySqlJson' | 'SingleStoreJson' | 'SQLiteTextJson' | 'SQLiteBlobJson' ? GenericSchema<TColumn['_']['data']> : t.TObject<{
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, IsEnumDefined, IsNever, JsonSchema } from './utils.js';
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> = TColumn['_']['columnType'] extends 'MySqlTinyInt' | 'SingleStoreTinyInt' | 'PgSmallInt' | 'PgSmallSerial' | 'MySqlSmallInt' | 'MySqlMediumInt' | 'SingleStoreSmallInt' | 'SingleStoreMediumInt' | 'PgInteger' | 'PgSerial' | 'MySqlInt' | 'SingleStoreInt' | 'PgBigInt53' | 'PgBigSerial53' | 'MySqlBigInt53' | 'MySqlSerial' | 'SingleStoreBigInt53' | 'SingleStoreSerial' | 'SQLiteInteger' | 'MySqlYear' | 'SingleStoreYear' ? t.TInteger : TColumn['_']['columnType'] extends 'PgBinaryVector' ? t.TRegExp : HasBaseColumn<TColumn> extends true ? t.TArray<GetTypeboxType<Assume<TColumn['_']['baseColumn'], Column>>> : IsEnumDefined<TColumn['_']['enumValues']> extends true ? t.TEnum<{
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
- }> : TColumn['_']['columnType'] extends 'PgGeometry' | 'PgPointTuple' ? t.TTuple<[t.TNumber, t.TNumber]> : TColumn['_']['columnType'] extends 'PgLine' ? t.TTuple<[t.TNumber, t.TNumber, t.TNumber]> : TColumn['_']['data'] extends Date ? t.TDate : TColumn['_']['data'] extends Buffer ? BufferSchema : TColumn['_']['dataType'] extends 'array' ? t.TArray<GetTypeboxPrimitiveType<Assume<TColumn['_']['data'], any[]>[number]>> : TColumn['_']['data'] extends Record<string, any> ? TColumn['_']['columnType'] extends 'PgJson' | 'PgJsonb' | 'MySqlJson' | 'SingleStoreJson' | 'SQLiteTextJson' | 'SQLiteBlobJson' ? GenericSchema<TColumn['_']['data']> : t.TObject<{
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
- if (isWithEnum(column)) {
45
- schema = column.enumValues.length ? t.Enum(mapEnumValues(column.enumValues)) : t.String();
46
- }
47
- if (!schema) {
48
- // Handle specific types
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
- else if (isColumnType(column, ['PgLine'])) {
64
- schema = t.Tuple([t.Number(), t.Number(), t.Number()]);
42
+ case 'object': {
43
+ schema = objectColumnToSchema(column, constraint, t);
44
+ break;
65
45
  }
66
- else if (isColumnType(column, ['PgLineABC'])) {
67
- schema = t.Object({
68
- a: t.Number(),
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
- else if (column.dataType === 'number') {
85
- schema = numberColumnToSchema(column, t);
50
+ case 'bigint': {
51
+ schema = bigintColumnToSchema(column, constraint, t);
52
+ break;
86
53
  }
87
- else if (column.dataType === 'bigint') {
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
- else if (column.dataType === 'date') {
94
- schema = t.Date();
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
- else if (column.dataType === 'custom') {
62
+ case 'custom': {
103
63
  schema = t.Any();
64
+ break;
104
65
  }
105
- else if (column.dataType === 'buffer') {
106
- schema = bufferSchema;
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
- if (isColumnType(column, ['MySqlTinyInt', 'SingleStoreTinyInt'])) {
120
- min = unsigned ? 0 : CONSTANTS.INT8_MIN;
121
- max = unsigned ? CONSTANTS.INT8_UNSIGNED_MAX : CONSTANTS.INT8_MAX;
122
- integer = true;
123
- }
124
- else if (isColumnType(column, [
125
- 'PgSmallInt',
126
- 'PgSmallSerial',
127
- 'MySqlSmallInt',
128
- 'SingleStoreSmallInt',
129
- ])) {
130
- min = unsigned ? 0 : CONSTANTS.INT16_MIN;
131
- max = unsigned ? CONSTANTS.INT16_UNSIGNED_MAX : CONSTANTS.INT16_MAX;
132
- integer = true;
133
- }
134
- else if (isColumnType(column, [
135
- 'PgReal',
136
- 'MySqlFloat',
137
- 'MySqlMediumInt',
138
- 'SingleStoreFloat',
139
- 'SingleStoreMediumInt',
140
- ])) {
141
- min = unsigned ? 0 : CONSTANTS.INT24_MIN;
142
- max = unsigned ? CONSTANTS.INT24_UNSIGNED_MAX : CONSTANTS.INT24_MAX;
143
- integer = isColumnType(column, ['MySqlMediumInt', 'SingleStoreMediumInt']);
144
- }
145
- else if (isColumnType(column, [
146
- 'PgInteger',
147
- 'PgSerial',
148
- 'MySqlInt',
149
- 'SingleStoreInt',
150
- ])) {
151
- min = unsigned ? 0 : CONSTANTS.INT32_MIN;
152
- max = unsigned ? CONSTANTS.INT32_UNSIGNED_MAX : CONSTANTS.INT32_MAX;
153
- integer = true;
154
- }
155
- else if (isColumnType(column, [
156
- 'PgDoublePrecision',
157
- 'MySqlReal',
158
- 'MySqlDouble',
159
- 'SingleStoreReal',
160
- 'SingleStoreDouble',
161
- 'SQLiteReal',
162
- ])) {
163
- min = unsigned ? 0 : CONSTANTS.INT48_MIN;
164
- max = unsigned ? CONSTANTS.INT48_UNSIGNED_MAX : CONSTANTS.INT48_MAX;
165
- }
166
- else if (isColumnType(column, [
167
- 'PgBigInt53',
168
- 'PgBigSerial53',
169
- 'MySqlBigInt53',
170
- 'MySqlSerial',
171
- 'SingleStoreBigInt53',
172
- 'SingleStoreSerial',
173
- 'SQLiteInteger',
174
- ])) {
175
- unsigned = unsigned || isColumnType(column, ['MySqlSerial', 'SingleStoreSerial']);
176
- min = unsigned ? 0 : Number.MIN_SAFE_INTEGER;
177
- max = Number.MAX_SAFE_INTEGER;
178
- integer = true;
179
- }
180
- else if (isColumnType(column, ['MySqlYear', 'SingleStoreYear'])) {
181
- min = 1901;
182
- max = 2155;
183
- integer = true;
184
- }
185
- else {
186
- min = Number.MIN_SAFE_INTEGER;
187
- max = Number.MAX_SAFE_INTEGER;
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 bigintColumnToSchema(column, t) {
196
- const unsigned = column.getSQLType().includes('unsigned');
197
- const min = unsigned ? 0n : CONSTANTS.INT64_MIN;
198
- const max = unsigned ? CONSTANTS.INT64_UNSIGNED_MAX : CONSTANTS.INT64_MAX;
199
- return t.BigInt({
200
- minimum: min,
201
- maximum: max,
202
- });
203
- }
204
- function stringColumnToSchema(column, t) {
205
- if (isColumnType(column, ['PgUUID'])) {
206
- return t.String({ format: 'uuid' });
207
- }
208
- else if (isColumnType(column, [
209
- 'PgBinaryVector',
210
- ])) {
211
- return t.RegExp(/^[01]+$/, column.dimensions ? { maxLength: column.dimensions } : undefined);
212
- }
213
- let max;
214
- let fixed = false;
215
- if (isColumnType(column, ['PgVarchar', 'SQLiteText'])) {
216
- max = column.length;
217
- }
218
- else if (isColumnType(column, ['MySqlVarChar', 'SingleStoreVarChar'])) {
219
- max = column.length ?? CONSTANTS.INT16_UNSIGNED_MAX;
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
- else if (isColumnType(column, ['MySqlText', 'SingleStoreText'])) {
222
- if (column.textType === 'longtext') {
223
- max = CONSTANTS.INT32_UNSIGNED_MAX;
211
+ }
212
+ function objectColumnToSchema(column, constraint, t) {
213
+ switch (constraint) {
214
+ case 'buffer': {
215
+ return bufferSchema;
224
216
  }
225
- else if (column.textType === 'mediumtext') {
226
- max = CONSTANTS.INT24_UNSIGNED_MAX;
217
+ case 'date': {
218
+ return t.Date();
227
219
  }
228
- else if (column.textType === 'text') {
229
- max = CONSTANTS.INT16_UNSIGNED_MAX;
220
+ case 'geometry':
221
+ case 'point': {
222
+ return t.Object({
223
+ x: t.Number(),
224
+ y: t.Number(),
225
+ });
230
226
  }
231
- else {
232
- max = CONSTANTS.INT8_UNSIGNED_MAX;
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 (isColumnType(column, [
236
- 'PgChar',
237
- 'MySqlChar',
238
- 'SingleStoreChar',
239
- ])) {
240
- max = column.length;
241
- fixed = true;
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 (max !== undefined && fixed) {
245
- options.minLength = max;
246
- options.maxLength = max;
285
+ if (length !== undefined && isLengthExact) {
286
+ options.minLength = length;
287
+ options.maxLength = length;
247
288
  }
248
- else if (max !== undefined) {
249
- options.maxLength = max;
289
+ else if (length !== undefined) {
290
+ options.maxLength = length;
250
291
  }
251
- return t.String(Object.keys(options).length > 0 ? options : undefined);
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
  }