drizzle-typebox 0.3.3-57a367f → 0.3.3-59e351d
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 +12 -13
- package/column.types.d.mts +12 -13
- package/column.types.d.ts +12 -13
- package/index.cjs +183 -234
- package/index.cjs.map +1 -1
- package/index.mjs +184 -235
- package/index.mjs.map +1 -1
- package/package.json +1 -1
- 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 +3 -7
- package/utils.d.mts +3 -7
- package/utils.d.ts +3 -7
package/column.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { TSchema, Type as typebox } from '@sinclair/typebox';
|
|
2
|
-
import {
|
|
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 {
|
|
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 {
|
|
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,24 +1,23 @@
|
|
|
1
1
|
import type * as t from '@sinclair/typebox';
|
|
2
|
-
import type { Assume, Column
|
|
3
|
-
import type { BufferSchema, JsonSchema } from "./utils.cjs";
|
|
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;
|
|
4
9
|
export type EnumValuesToEnum<TEnumValues extends [string, ...string[]]> = {
|
|
5
10
|
[K in TEnumValues[number]]: K;
|
|
6
11
|
};
|
|
7
12
|
export interface GenericSchema<T> extends t.TSchema {
|
|
8
13
|
static: T;
|
|
9
14
|
}
|
|
10
|
-
export type GetTypeboxType<TColumn extends Column
|
|
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<{
|
|
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<{
|
|
20
16
|
[K in Assume<TColumn['_']['enumValues'], string[]>[number]]: K;
|
|
21
|
-
}> : t.
|
|
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;
|
|
22
21
|
type HandleSelectColumn<TSchema extends t.TSchema, TColumn extends Column> = TColumn['_']['notNull'] extends true ? TSchema : t.Union<[TSchema, t.TNull]>;
|
|
23
22
|
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]>>;
|
|
24
23
|
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,24 +1,23 @@
|
|
|
1
1
|
import type * as t from '@sinclair/typebox';
|
|
2
|
-
import type { Assume, Column
|
|
3
|
-
import type { BufferSchema, JsonSchema } from "./utils.mjs";
|
|
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;
|
|
4
9
|
export type EnumValuesToEnum<TEnumValues extends [string, ...string[]]> = {
|
|
5
10
|
[K in TEnumValues[number]]: K;
|
|
6
11
|
};
|
|
7
12
|
export interface GenericSchema<T> extends t.TSchema {
|
|
8
13
|
static: T;
|
|
9
14
|
}
|
|
10
|
-
export type GetTypeboxType<TColumn extends Column
|
|
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<{
|
|
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<{
|
|
20
16
|
[K in Assume<TColumn['_']['enumValues'], string[]>[number]]: K;
|
|
21
|
-
}> : t.
|
|
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;
|
|
22
21
|
type HandleSelectColumn<TSchema extends t.TSchema, TColumn extends Column> = TColumn['_']['notNull'] extends true ? TSchema : t.Union<[TSchema, t.TNull]>;
|
|
23
22
|
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]>>;
|
|
24
23
|
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,24 +1,23 @@
|
|
|
1
1
|
import type * as t from '@sinclair/typebox';
|
|
2
|
-
import type { Assume, Column
|
|
3
|
-
import type { BufferSchema, JsonSchema } from './utils.js';
|
|
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;
|
|
4
9
|
export type EnumValuesToEnum<TEnumValues extends [string, ...string[]]> = {
|
|
5
10
|
[K in TEnumValues[number]]: K;
|
|
6
11
|
};
|
|
7
12
|
export interface GenericSchema<T> extends t.TSchema {
|
|
8
13
|
static: T;
|
|
9
14
|
}
|
|
10
|
-
export type GetTypeboxType<TColumn extends Column
|
|
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<{
|
|
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<{
|
|
20
16
|
[K in Assume<TColumn['_']['enumValues'], string[]>[number]]: K;
|
|
21
|
-
}> : t.
|
|
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;
|
|
22
21
|
type HandleSelectColumn<TSchema extends t.TSchema, TColumn extends Column> = TColumn['_']['notNull'] extends true ? TSchema : t.Union<[TSchema, t.TNull]>;
|
|
23
22
|
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]>>;
|
|
24
23
|
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,6 +24,14 @@ 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
|
+
|
|
27
35
|
const literalSchema = typebox.Type.Union([typebox.Type.String(), typebox.Type.Number(), typebox.Type.Boolean(), typebox.Type.Null()]);
|
|
28
36
|
const jsonSchema = typebox.Type.Union([literalSchema, typebox.Type.Array(typebox.Type.Any()), typebox.Type.Record(typebox.Type.String(), typebox.Type.Any())]);
|
|
29
37
|
typebox.TypeRegistry.Set('Buffer', (_, value) => value instanceof Buffer); // eslint-disable-line no-instanceof/no-instanceof
|
|
@@ -33,138 +41,150 @@ function mapEnumValues(values) {
|
|
|
33
41
|
}
|
|
34
42
|
function columnToSchema(column, t) {
|
|
35
43
|
let schema;
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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);
|
|
41
62
|
}
|
|
42
|
-
|
|
43
|
-
schema =
|
|
44
|
-
break;
|
|
63
|
+
else if (isColumnType(column, ['PgLine'])) {
|
|
64
|
+
schema = t.Tuple([t.Number(), t.Number(), t.Number()]);
|
|
45
65
|
}
|
|
46
|
-
|
|
47
|
-
schema =
|
|
48
|
-
|
|
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());
|
|
49
83
|
}
|
|
50
|
-
|
|
51
|
-
schema =
|
|
52
|
-
break;
|
|
84
|
+
else if (column.dataType === 'number') {
|
|
85
|
+
schema = numberColumnToSchema(column, t);
|
|
53
86
|
}
|
|
54
|
-
|
|
87
|
+
else if (column.dataType === 'bigint') {
|
|
88
|
+
schema = bigintColumnToSchema(column, t);
|
|
89
|
+
}
|
|
90
|
+
else if (column.dataType === 'boolean') {
|
|
55
91
|
schema = t.Boolean();
|
|
56
|
-
break;
|
|
57
92
|
}
|
|
58
|
-
|
|
59
|
-
schema =
|
|
60
|
-
break;
|
|
93
|
+
else if (column.dataType === 'date') {
|
|
94
|
+
schema = t.Date();
|
|
61
95
|
}
|
|
62
|
-
|
|
63
|
-
schema = t
|
|
64
|
-
|
|
96
|
+
else if (column.dataType === 'string') {
|
|
97
|
+
schema = stringColumnToSchema(column, t);
|
|
98
|
+
}
|
|
99
|
+
else if (column.dataType === 'json') {
|
|
100
|
+
schema = jsonSchema;
|
|
65
101
|
}
|
|
66
|
-
|
|
102
|
+
else if (column.dataType === 'custom') {
|
|
67
103
|
schema = t.Any();
|
|
68
104
|
}
|
|
105
|
+
else if (column.dataType === 'buffer') {
|
|
106
|
+
schema = bufferSchema;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
if (!schema) {
|
|
110
|
+
schema = t.Any();
|
|
69
111
|
}
|
|
70
112
|
return schema;
|
|
71
113
|
}
|
|
72
|
-
function numberColumnToSchema(column,
|
|
114
|
+
function numberColumnToSchema(column, t) {
|
|
115
|
+
let unsigned = column.getSQLType().includes('unsigned');
|
|
73
116
|
let min;
|
|
74
117
|
let max;
|
|
75
118
|
let integer = false;
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
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
|
-
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
|
-
}
|
|
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;
|
|
168
188
|
}
|
|
169
189
|
const key = integer ? 'Integer' : 'Number';
|
|
170
190
|
return t[key]({
|
|
@@ -172,136 +192,65 @@ function numberColumnToSchema(column, constraint, t) {
|
|
|
172
192
|
maximum: max,
|
|
173
193
|
});
|
|
174
194
|
}
|
|
175
|
-
function
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
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
|
-
}
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
function objectColumnToSchema(column, constraint, t) {
|
|
213
|
-
switch (constraint) {
|
|
214
|
-
case 'buffer': {
|
|
215
|
-
return bufferSchema;
|
|
216
|
-
}
|
|
217
|
-
case 'date': {
|
|
218
|
-
return t.Date();
|
|
219
|
-
}
|
|
220
|
-
case 'geometry':
|
|
221
|
-
case 'point': {
|
|
222
|
-
return t.Object({
|
|
223
|
-
x: t.Number(),
|
|
224
|
-
y: t.Number(),
|
|
225
|
-
});
|
|
226
|
-
}
|
|
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
|
-
});
|
|
236
|
-
}
|
|
237
|
-
default: {
|
|
238
|
-
return t.Object({});
|
|
239
|
-
}
|
|
240
|
-
}
|
|
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
|
+
});
|
|
241
203
|
}
|
|
242
|
-
function
|
|
243
|
-
|
|
244
|
-
|
|
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;
|
|
204
|
+
function stringColumnToSchema(column, t) {
|
|
205
|
+
if (isColumnType(column, ['PgUUID'])) {
|
|
206
|
+
return t.String({ format: 'uuid' });
|
|
260
207
|
}
|
|
261
|
-
if (
|
|
262
|
-
|
|
208
|
+
else if (isColumnType(column, [
|
|
209
|
+
'PgBinaryVector',
|
|
210
|
+
])) {
|
|
211
|
+
return t.RegExp(/^[01]+$/, column.dimensions ? { maxLength: column.dimensions } : undefined);
|
|
263
212
|
}
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
let regex;
|
|
269
|
-
if (constraint === 'binary') {
|
|
270
|
-
regex = /^[01]*$/;
|
|
213
|
+
let max;
|
|
214
|
+
let fixed = false;
|
|
215
|
+
if (isColumnType(column, ['PgVarchar', 'SQLiteText'])) {
|
|
216
|
+
max = column.length;
|
|
271
217
|
}
|
|
272
|
-
if (
|
|
273
|
-
|
|
274
|
-
format: 'uuid',
|
|
275
|
-
});
|
|
218
|
+
else if (isColumnType(column, ['MySqlVarChar', 'SingleStoreVarChar'])) {
|
|
219
|
+
max = column.length ?? CONSTANTS.INT16_UNSIGNED_MAX;
|
|
276
220
|
}
|
|
277
|
-
if (
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
221
|
+
else if (isColumnType(column, ['MySqlText', 'SingleStoreText'])) {
|
|
222
|
+
if (column.textType === 'longtext') {
|
|
223
|
+
max = CONSTANTS.INT32_UNSIGNED_MAX;
|
|
224
|
+
}
|
|
225
|
+
else if (column.textType === 'mediumtext') {
|
|
226
|
+
max = CONSTANTS.INT24_UNSIGNED_MAX;
|
|
227
|
+
}
|
|
228
|
+
else if (column.textType === 'text') {
|
|
229
|
+
max = CONSTANTS.INT16_UNSIGNED_MAX;
|
|
230
|
+
}
|
|
231
|
+
else {
|
|
232
|
+
max = CONSTANTS.INT8_UNSIGNED_MAX;
|
|
281
233
|
}
|
|
282
|
-
|
|
234
|
+
}
|
|
235
|
+
if (isColumnType(column, [
|
|
236
|
+
'PgChar',
|
|
237
|
+
'MySqlChar',
|
|
238
|
+
'SingleStoreChar',
|
|
239
|
+
])) {
|
|
240
|
+
max = column.length;
|
|
241
|
+
fixed = true;
|
|
283
242
|
}
|
|
284
243
|
const options = {};
|
|
285
|
-
if (
|
|
286
|
-
options.minLength =
|
|
287
|
-
options.maxLength =
|
|
244
|
+
if (max !== undefined && fixed) {
|
|
245
|
+
options.minLength = max;
|
|
246
|
+
options.maxLength = max;
|
|
288
247
|
}
|
|
289
|
-
else if (
|
|
290
|
-
options.maxLength =
|
|
248
|
+
else if (max !== undefined) {
|
|
249
|
+
options.maxLength = max;
|
|
291
250
|
}
|
|
292
|
-
return
|
|
293
|
-
? t.RegExp(regex, Object.keys(options).length > 0 ? options : undefined)
|
|
294
|
-
: t.String(Object.keys(options).length > 0 ? options : undefined);
|
|
251
|
+
return t.String(Object.keys(options).length > 0 ? options : undefined);
|
|
295
252
|
}
|
|
296
253
|
|
|
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
|
-
|
|
305
254
|
function getColumns(tableLike) {
|
|
306
255
|
return drizzleOrm.isTable(tableLike) ? drizzleOrm.getTableColumns(tableLike) : drizzleOrm.getViewSelectedFields(tableLike);
|
|
307
256
|
}
|