drizzle-typebox 1.0.0-beta.6-cefee57 → 1.0.0-beta.6-7419dcb
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 +10 -0
- package/column.d.mts +10 -0
- package/column.d.ts +10 -0
- package/column.types.d.cts +27 -0
- package/column.types.d.mts +27 -0
- package/column.types.d.ts +27 -0
- package/constants.d.cts +20 -0
- package/constants.d.mts +20 -0
- package/constants.d.ts +20 -0
- package/index.cjs +402 -356
- package/index.cjs.map +1 -1
- package/index.d.cts +6 -122
- package/index.d.mts +6 -122
- package/index.d.ts +6 -122
- package/index.mjs +394 -350
- package/index.mjs.map +1 -1
- package/package.json +3 -2
- package/schema.d.cts +18 -0
- package/schema.d.mts +18 -0
- package/schema.d.ts +18 -0
- package/schema.types.d.cts +24 -0
- package/schema.types.d.mts +24 -0
- package/schema.types.d.ts +24 -0
- package/schema.types.internal.d.cts +22 -0
- package/schema.types.internal.d.mts +22 -0
- package/schema.types.internal.d.ts +22 -0
- package/utils.d.cts +39 -0
- package/utils.d.mts +39 -0
- package/utils.d.ts +39 -0
package/index.mjs
CHANGED
|
@@ -1,405 +1,449 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { Type, TypeRegistry, Kind } from '@sinclair/typebox';
|
|
2
|
+
import { extractExtendedColumnType, getTableName, getColumnTable, isTable, getTableColumns, getViewSelectedFields, is, Column, SQL, isView } from 'drizzle-orm';
|
|
3
3
|
|
|
4
|
-
//#region src/constants.ts
|
|
5
4
|
const CONSTANTS = {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
5
|
+
INT8_MIN: -128,
|
|
6
|
+
INT8_MAX: 127,
|
|
7
|
+
INT8_UNSIGNED_MAX: 255,
|
|
8
|
+
INT16_MIN: -32768,
|
|
9
|
+
INT16_MAX: 32767,
|
|
10
|
+
INT16_UNSIGNED_MAX: 65535,
|
|
11
|
+
INT24_MIN: -8388608,
|
|
12
|
+
INT24_MAX: 8388607,
|
|
13
|
+
INT24_UNSIGNED_MAX: 16777215,
|
|
14
|
+
INT32_MIN: -2147483648,
|
|
15
|
+
INT32_MAX: 2147483647,
|
|
16
|
+
INT32_UNSIGNED_MAX: 4294967295,
|
|
17
|
+
INT48_MIN: -140737488355328,
|
|
18
|
+
INT48_MAX: 140737488355327,
|
|
19
|
+
INT48_UNSIGNED_MAX: 281474976710655,
|
|
20
|
+
INT64_MIN: -9223372036854775808n,
|
|
21
|
+
INT64_MAX: 9223372036854775807n,
|
|
22
|
+
INT64_UNSIGNED_MAX: 18446744073709551615n,
|
|
24
23
|
};
|
|
25
24
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
Type.Number(),
|
|
31
|
-
Type.Boolean(),
|
|
32
|
-
Type.Null()
|
|
33
|
-
]);
|
|
34
|
-
const jsonSchema = Type.Union([
|
|
35
|
-
literalSchema,
|
|
36
|
-
Type.Array(Type.Any()),
|
|
37
|
-
Type.Record(Type.String(), Type.Any())
|
|
38
|
-
]);
|
|
39
|
-
TypeRegistry.Set("Buffer", (_, value) => value instanceof Buffer);
|
|
40
|
-
const bufferSchema = {
|
|
41
|
-
[Kind]: "Buffer",
|
|
42
|
-
type: "buffer"
|
|
43
|
-
};
|
|
25
|
+
const literalSchema = Type.Union([Type.String(), Type.Number(), Type.Boolean(), Type.Null()]);
|
|
26
|
+
const jsonSchema = Type.Union([literalSchema, Type.Array(Type.Any()), Type.Record(Type.String(), Type.Any())]);
|
|
27
|
+
TypeRegistry.Set('Buffer', (_, value) => value instanceof Buffer);
|
|
28
|
+
const bufferSchema = { [Kind]: 'Buffer', type: 'buffer' };
|
|
44
29
|
function mapEnumValues(values) {
|
|
45
|
-
|
|
30
|
+
return Object.fromEntries(values.map((value) => [value, value]));
|
|
46
31
|
}
|
|
47
32
|
function columnToSchema(column, t) {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
33
|
+
let schema;
|
|
34
|
+
const { type, constraint } = extractExtendedColumnType(column);
|
|
35
|
+
switch (type) {
|
|
36
|
+
case 'array': {
|
|
37
|
+
schema = arrayColumnToSchema(column, constraint, t);
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
case 'object': {
|
|
41
|
+
schema = objectColumnToSchema(column, constraint, t);
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
case 'number': {
|
|
45
|
+
schema = numberColumnToSchema(column, constraint, t);
|
|
46
|
+
break;
|
|
47
|
+
}
|
|
48
|
+
case 'bigint': {
|
|
49
|
+
schema = bigintColumnToSchema(column, constraint, t);
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
case 'boolean': {
|
|
53
|
+
schema = t.Boolean();
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
case 'string': {
|
|
57
|
+
schema = stringColumnToSchema(column, constraint, t);
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
case 'custom': {
|
|
61
|
+
schema = t.Any();
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
default: {
|
|
65
|
+
schema = t.Any();
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return schema;
|
|
77
69
|
}
|
|
78
70
|
function numberColumnToSchema(column, constraint, t) {
|
|
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
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
71
|
+
let min;
|
|
72
|
+
let max;
|
|
73
|
+
let integer = false;
|
|
74
|
+
switch (constraint) {
|
|
75
|
+
case 'int8': {
|
|
76
|
+
min = CONSTANTS.INT8_MIN;
|
|
77
|
+
max = CONSTANTS.INT8_MAX;
|
|
78
|
+
integer = true;
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
case 'uint8': {
|
|
82
|
+
min = 0;
|
|
83
|
+
max = CONSTANTS.INT8_UNSIGNED_MAX;
|
|
84
|
+
integer = true;
|
|
85
|
+
break;
|
|
86
|
+
}
|
|
87
|
+
case 'int16': {
|
|
88
|
+
min = CONSTANTS.INT16_MIN;
|
|
89
|
+
max = CONSTANTS.INT16_MAX;
|
|
90
|
+
integer = true;
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
case 'uint16': {
|
|
94
|
+
min = 0;
|
|
95
|
+
max = CONSTANTS.INT16_UNSIGNED_MAX;
|
|
96
|
+
integer = true;
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
case 'int24': {
|
|
100
|
+
min = CONSTANTS.INT24_MIN;
|
|
101
|
+
max = CONSTANTS.INT24_MAX;
|
|
102
|
+
integer = true;
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
case 'uint24': {
|
|
106
|
+
min = 0;
|
|
107
|
+
max = CONSTANTS.INT24_UNSIGNED_MAX;
|
|
108
|
+
integer = true;
|
|
109
|
+
break;
|
|
110
|
+
}
|
|
111
|
+
case 'int32': {
|
|
112
|
+
min = CONSTANTS.INT32_MIN;
|
|
113
|
+
max = CONSTANTS.INT32_MAX;
|
|
114
|
+
integer = true;
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
case 'uint32': {
|
|
118
|
+
min = 0;
|
|
119
|
+
max = CONSTANTS.INT32_UNSIGNED_MAX;
|
|
120
|
+
integer = true;
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
case 'int53': {
|
|
124
|
+
min = Number.MIN_SAFE_INTEGER;
|
|
125
|
+
max = Number.MAX_SAFE_INTEGER;
|
|
126
|
+
integer = true;
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
case 'uint53': {
|
|
130
|
+
min = 0;
|
|
131
|
+
max = Number.MAX_SAFE_INTEGER;
|
|
132
|
+
integer = true;
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
case 'float': {
|
|
136
|
+
min = CONSTANTS.INT24_MIN;
|
|
137
|
+
max = CONSTANTS.INT24_MAX;
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
case 'ufloat': {
|
|
141
|
+
min = 0;
|
|
142
|
+
max = CONSTANTS.INT24_UNSIGNED_MAX;
|
|
143
|
+
break;
|
|
144
|
+
}
|
|
145
|
+
case 'double': {
|
|
146
|
+
min = CONSTANTS.INT48_MIN;
|
|
147
|
+
max = CONSTANTS.INT48_MAX;
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
case 'udouble': {
|
|
151
|
+
min = 0;
|
|
152
|
+
max = CONSTANTS.INT48_UNSIGNED_MAX;
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
case 'year': {
|
|
156
|
+
min = 1901;
|
|
157
|
+
max = 2155;
|
|
158
|
+
integer = true;
|
|
159
|
+
break;
|
|
160
|
+
}
|
|
161
|
+
case 'unsigned': {
|
|
162
|
+
min = 0;
|
|
163
|
+
max = Number.MAX_SAFE_INTEGER;
|
|
164
|
+
break;
|
|
165
|
+
}
|
|
166
|
+
default: {
|
|
167
|
+
min = Number.MIN_SAFE_INTEGER;
|
|
168
|
+
max = Number.MAX_SAFE_INTEGER;
|
|
169
|
+
break;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
const key = integer ? 'Integer' : 'Number';
|
|
173
|
+
return t[key]({
|
|
174
|
+
minimum: min,
|
|
175
|
+
maximum: max,
|
|
176
|
+
});
|
|
167
177
|
}
|
|
168
|
-
TypeRegistry.Set(
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
178
|
+
TypeRegistry.Set('BigIntStringMode', (_, value) => {
|
|
179
|
+
if (typeof value !== 'string' || !(/^-?\d+$/.test(value))) {
|
|
180
|
+
return false;
|
|
181
|
+
}
|
|
182
|
+
const bigint = BigInt(value);
|
|
183
|
+
if (bigint < CONSTANTS.INT64_MIN || bigint > CONSTANTS.INT64_MAX) {
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
return true;
|
|
173
187
|
});
|
|
174
|
-
TypeRegistry.Set(
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
188
|
+
TypeRegistry.Set('UnsignedBigIntStringMode', (_, value) => {
|
|
189
|
+
if (typeof value !== 'string' || !(/^\d+$/.test(value))) {
|
|
190
|
+
return false;
|
|
191
|
+
}
|
|
192
|
+
const bigint = BigInt(value);
|
|
193
|
+
if (bigint < 0 || bigint > CONSTANTS.INT64_MAX) {
|
|
194
|
+
return false;
|
|
195
|
+
}
|
|
196
|
+
return true;
|
|
179
197
|
});
|
|
180
198
|
/** @internal */
|
|
181
199
|
const bigintStringModeSchema = {
|
|
182
|
-
|
|
183
|
-
|
|
200
|
+
[Kind]: 'BigIntStringMode',
|
|
201
|
+
type: 'string',
|
|
184
202
|
};
|
|
185
203
|
/** @internal */
|
|
186
204
|
const unsignedBigintStringModeSchema = {
|
|
187
|
-
|
|
188
|
-
|
|
205
|
+
[Kind]: 'UnsignedBigIntStringMode',
|
|
206
|
+
type: 'string',
|
|
189
207
|
};
|
|
190
|
-
function pgArrayColumnToSchema(column, dimensions, t) {
|
|
191
|
-
const [baseType, baseConstraint] = column.dataType.split(" ");
|
|
192
|
-
let baseSchema;
|
|
193
|
-
switch (baseType) {
|
|
194
|
-
case "number":
|
|
195
|
-
baseSchema = numberColumnToSchema(column, baseConstraint, t);
|
|
196
|
-
break;
|
|
197
|
-
case "bigint":
|
|
198
|
-
baseSchema = bigintColumnToSchema(column, baseConstraint, t);
|
|
199
|
-
break;
|
|
200
|
-
case "boolean":
|
|
201
|
-
baseSchema = t.Boolean();
|
|
202
|
-
break;
|
|
203
|
-
case "string":
|
|
204
|
-
baseSchema = stringColumnToSchema(column, baseConstraint, t);
|
|
205
|
-
break;
|
|
206
|
-
case "object":
|
|
207
|
-
baseSchema = objectColumnToSchema(column, baseConstraint, t);
|
|
208
|
-
break;
|
|
209
|
-
case "array":
|
|
210
|
-
baseSchema = arrayColumnToSchema(column, baseConstraint, t);
|
|
211
|
-
break;
|
|
212
|
-
default: baseSchema = t.Any();
|
|
213
|
-
}
|
|
214
|
-
let schema = t.Array(baseSchema);
|
|
215
|
-
for (let i = 1; i < dimensions; i++) schema = t.Array(schema);
|
|
216
|
-
return schema;
|
|
217
|
-
}
|
|
218
208
|
function arrayColumnToSchema(column, constraint, t) {
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
209
|
+
switch (constraint) {
|
|
210
|
+
case 'geometry':
|
|
211
|
+
case 'point': {
|
|
212
|
+
return t.Tuple([t.Number(), t.Number()]);
|
|
213
|
+
}
|
|
214
|
+
case 'line': {
|
|
215
|
+
return t.Tuple([t.Number(), t.Number(), t.Number()]);
|
|
216
|
+
}
|
|
217
|
+
case 'vector':
|
|
218
|
+
case 'halfvector': {
|
|
219
|
+
const length = column.length;
|
|
220
|
+
const sizeParam = length
|
|
221
|
+
? {
|
|
222
|
+
minItems: length,
|
|
223
|
+
maxItems: length,
|
|
224
|
+
}
|
|
225
|
+
: undefined;
|
|
226
|
+
return t.Array(t.Number(), sizeParam);
|
|
227
|
+
}
|
|
228
|
+
case 'int64vector': {
|
|
229
|
+
const length = column.length;
|
|
230
|
+
const sizeParam = length
|
|
231
|
+
? {
|
|
232
|
+
minItems: length,
|
|
233
|
+
maxItems: length,
|
|
234
|
+
}
|
|
235
|
+
: undefined;
|
|
236
|
+
return t.Array(t.BigInt({
|
|
237
|
+
minimum: CONSTANTS.INT64_MIN,
|
|
238
|
+
maximum: CONSTANTS.INT64_MAX,
|
|
239
|
+
}), sizeParam);
|
|
240
|
+
}
|
|
241
|
+
case 'basecolumn': {
|
|
242
|
+
const size = column.length;
|
|
243
|
+
const sizeParam = size
|
|
244
|
+
? {
|
|
245
|
+
minItems: size,
|
|
246
|
+
maxItems: size,
|
|
247
|
+
}
|
|
248
|
+
: undefined;
|
|
249
|
+
return column.baseColumn
|
|
250
|
+
? t.Array(columnToSchema(column.baseColumn, t), sizeParam)
|
|
251
|
+
: t.Array(t.Any(), sizeParam);
|
|
252
|
+
}
|
|
253
|
+
default: {
|
|
254
|
+
return t.Array(t.Any());
|
|
255
|
+
}
|
|
256
|
+
}
|
|
261
257
|
}
|
|
262
258
|
function objectColumnToSchema(column, constraint, t) {
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
259
|
+
switch (constraint) {
|
|
260
|
+
case 'buffer': {
|
|
261
|
+
return bufferSchema;
|
|
262
|
+
}
|
|
263
|
+
case 'date': {
|
|
264
|
+
return t.Date();
|
|
265
|
+
}
|
|
266
|
+
case 'geometry':
|
|
267
|
+
case 'point': {
|
|
268
|
+
return t.Object({
|
|
269
|
+
x: t.Number(),
|
|
270
|
+
y: t.Number(),
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
case 'json': {
|
|
274
|
+
return jsonSchema;
|
|
275
|
+
}
|
|
276
|
+
case 'line': {
|
|
277
|
+
return t.Object({
|
|
278
|
+
a: t.Number(),
|
|
279
|
+
b: t.Number(),
|
|
280
|
+
c: t.Number(),
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
default: {
|
|
284
|
+
return t.Object({});
|
|
285
|
+
}
|
|
286
|
+
}
|
|
279
287
|
}
|
|
280
288
|
function bigintColumnToSchema(column, constraint, t) {
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
289
|
+
let min;
|
|
290
|
+
let max;
|
|
291
|
+
switch (constraint) {
|
|
292
|
+
case 'int64': {
|
|
293
|
+
min = CONSTANTS.INT64_MIN;
|
|
294
|
+
max = CONSTANTS.INT64_MAX;
|
|
295
|
+
break;
|
|
296
|
+
}
|
|
297
|
+
case 'uint64': {
|
|
298
|
+
min = 0n;
|
|
299
|
+
max = CONSTANTS.INT64_UNSIGNED_MAX;
|
|
300
|
+
break;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
const options = {};
|
|
304
|
+
if (min !== undefined) {
|
|
305
|
+
options.minimum = min;
|
|
306
|
+
}
|
|
307
|
+
if (max !== undefined) {
|
|
308
|
+
options.maximum = max;
|
|
309
|
+
}
|
|
310
|
+
return t.BigInt(Object.keys(options).length > 0 ? options : undefined);
|
|
297
311
|
}
|
|
298
312
|
function stringColumnToSchema(column, constraint, t) {
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
313
|
+
const { name: columnName, length, isLengthExact } = column;
|
|
314
|
+
let regex;
|
|
315
|
+
if (constraint === 'binary') {
|
|
316
|
+
regex = /^[01]*$/;
|
|
317
|
+
}
|
|
318
|
+
if (constraint === 'uuid') {
|
|
319
|
+
return t.String({
|
|
320
|
+
format: 'uuid',
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
if (constraint === 'enum') {
|
|
324
|
+
const enumValues = column.enumValues;
|
|
325
|
+
if (!enumValues) {
|
|
326
|
+
throw new Error(`Column "${getTableName(getColumnTable(column))}"."${columnName}" is of 'enum' type, but lacks enum values`);
|
|
327
|
+
}
|
|
328
|
+
return t.Enum(mapEnumValues(enumValues));
|
|
329
|
+
}
|
|
330
|
+
if (constraint === 'int64') {
|
|
331
|
+
return bigintStringModeSchema;
|
|
332
|
+
}
|
|
333
|
+
if (constraint === 'uint64') {
|
|
334
|
+
return unsignedBigintStringModeSchema;
|
|
335
|
+
}
|
|
336
|
+
const options = {};
|
|
337
|
+
if (length !== undefined && isLengthExact) {
|
|
338
|
+
options.minLength = length;
|
|
339
|
+
options.maxLength = length;
|
|
340
|
+
}
|
|
341
|
+
else if (length !== undefined) {
|
|
342
|
+
options.maxLength = length;
|
|
343
|
+
}
|
|
344
|
+
return regex
|
|
345
|
+
? t.RegExp(regex, Object.keys(options).length > 0 ? options : undefined)
|
|
346
|
+
: t.String(Object.keys(options).length > 0 ? options : undefined);
|
|
316
347
|
}
|
|
317
348
|
|
|
318
|
-
//#endregion
|
|
319
|
-
//#region src/utils.ts
|
|
320
349
|
function isColumnType(column, columnTypes) {
|
|
321
|
-
|
|
350
|
+
return columnTypes.includes(column.columnType);
|
|
322
351
|
}
|
|
323
352
|
function isWithEnum(column) {
|
|
324
|
-
|
|
353
|
+
return 'enumValues' in column && Array.isArray(column.enumValues) && column.enumValues.length > 0;
|
|
325
354
|
}
|
|
326
355
|
const isPgEnum = isWithEnum;
|
|
327
356
|
|
|
328
|
-
//#endregion
|
|
329
|
-
//#region src/schema.ts
|
|
330
357
|
function getColumns(tableLike) {
|
|
331
|
-
|
|
358
|
+
return isTable(tableLike) ? getTableColumns(tableLike) : getViewSelectedFields(tableLike);
|
|
332
359
|
}
|
|
333
360
|
function handleColumns(columns, refinements, conditions, factory) {
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
361
|
+
const columnSchemas = {};
|
|
362
|
+
for (const [key, selected] of Object.entries(columns)) {
|
|
363
|
+
if (!is(selected, Column) && !is(selected, SQL) && !is(selected, SQL.Aliased) && typeof selected === 'object') {
|
|
364
|
+
const columns = isTable(selected) || isView(selected) ? getColumns(selected) : selected;
|
|
365
|
+
columnSchemas[key] = handleColumns(columns, refinements[key] ?? {}, conditions, factory);
|
|
366
|
+
continue;
|
|
367
|
+
}
|
|
368
|
+
const refinement = refinements[key];
|
|
369
|
+
if (refinement !== undefined && typeof refinement !== 'function') {
|
|
370
|
+
columnSchemas[key] = refinement;
|
|
371
|
+
continue;
|
|
372
|
+
}
|
|
373
|
+
const column = is(selected, Column) ? selected : undefined;
|
|
374
|
+
const schema = column ? columnToSchema(column, factory?.typeboxInstance ?? Type) : Type.Any();
|
|
375
|
+
const refined = typeof refinement === 'function' ? refinement(schema) : schema;
|
|
376
|
+
if (conditions.never(column)) {
|
|
377
|
+
continue;
|
|
378
|
+
}
|
|
379
|
+
else {
|
|
380
|
+
columnSchemas[key] = refined;
|
|
381
|
+
}
|
|
382
|
+
if (column) {
|
|
383
|
+
if (conditions.nullable(column)) {
|
|
384
|
+
columnSchemas[key] = Type.Union([columnSchemas[key], Type.Null()]);
|
|
385
|
+
}
|
|
386
|
+
if (conditions.optional(column)) {
|
|
387
|
+
columnSchemas[key] = Type.Optional(columnSchemas[key]);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
return Type.Object(columnSchemas);
|
|
356
392
|
}
|
|
357
393
|
function handleEnum(enum_, factory) {
|
|
358
|
-
|
|
394
|
+
const typebox = factory?.typeboxInstance ?? Type;
|
|
395
|
+
return typebox.Enum(mapEnumValues(enum_.enumValues));
|
|
359
396
|
}
|
|
360
397
|
const selectConditions = {
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
398
|
+
never: () => false,
|
|
399
|
+
optional: () => false,
|
|
400
|
+
nullable: (column) => !column.notNull,
|
|
364
401
|
};
|
|
365
402
|
const insertConditions = {
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
403
|
+
never: (column) => column?.generated?.type === 'always' || column?.generatedIdentity?.type === 'always'
|
|
404
|
+
|| ('identity' in (column ?? {}) && typeof column?.identity !== 'undefined'),
|
|
405
|
+
optional: (column) => !column.notNull || (column.notNull && column.hasDefault),
|
|
406
|
+
nullable: (column) => !column.notNull,
|
|
369
407
|
};
|
|
370
408
|
const updateConditions = {
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
409
|
+
never: (column) => column?.generated?.type === 'always' || column?.generatedIdentity?.type === 'always'
|
|
410
|
+
|| ('identity' in (column ?? {}) && typeof column?.identity !== 'undefined'),
|
|
411
|
+
optional: () => true,
|
|
412
|
+
nullable: (column) => !column.notNull,
|
|
374
413
|
};
|
|
375
414
|
const createSelectSchema = (entity, refine) => {
|
|
376
|
-
|
|
377
|
-
|
|
415
|
+
if (isPgEnum(entity)) {
|
|
416
|
+
return handleEnum(entity);
|
|
417
|
+
}
|
|
418
|
+
const columns = getColumns(entity);
|
|
419
|
+
return handleColumns(columns, refine ?? {}, selectConditions);
|
|
378
420
|
};
|
|
379
421
|
const createInsertSchema = (entity, refine) => {
|
|
380
|
-
|
|
422
|
+
const columns = getColumns(entity);
|
|
423
|
+
return handleColumns(columns, refine ?? {}, insertConditions);
|
|
381
424
|
};
|
|
382
425
|
const createUpdateSchema = (entity, refine) => {
|
|
383
|
-
|
|
426
|
+
const columns = getColumns(entity);
|
|
427
|
+
return handleColumns(columns, refine ?? {}, updateConditions);
|
|
384
428
|
};
|
|
385
429
|
function createSchemaFactory(options) {
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
430
|
+
const createSelectSchema = (entity, refine) => {
|
|
431
|
+
if (isPgEnum(entity)) {
|
|
432
|
+
return handleEnum(entity, options);
|
|
433
|
+
}
|
|
434
|
+
const columns = getColumns(entity);
|
|
435
|
+
return handleColumns(columns, refine ?? {}, selectConditions, options);
|
|
436
|
+
};
|
|
437
|
+
const createInsertSchema = (entity, refine) => {
|
|
438
|
+
const columns = getColumns(entity);
|
|
439
|
+
return handleColumns(columns, refine ?? {}, insertConditions, options);
|
|
440
|
+
};
|
|
441
|
+
const createUpdateSchema = (entity, refine) => {
|
|
442
|
+
const columns = getColumns(entity);
|
|
443
|
+
return handleColumns(columns, refine ?? {}, updateConditions, options);
|
|
444
|
+
};
|
|
445
|
+
return { createSelectSchema, createInsertSchema, createUpdateSchema };
|
|
401
446
|
}
|
|
402
447
|
|
|
403
|
-
//#endregion
|
|
404
448
|
export { bufferSchema, createInsertSchema, createSchemaFactory, createSelectSchema, createUpdateSchema, getColumns, handleColumns, handleEnum, isColumnType, isPgEnum, isWithEnum, jsonSchema, literalSchema };
|
|
405
|
-
//# sourceMappingURL=index.mjs.map
|
|
449
|
+
//# sourceMappingURL=index.mjs.map
|