drizzle-valibot 1.0.0-beta.6-cd45bda → 1.0.0-beta.6-a42e15a
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 +57 -0
- package/column.types.d.mts +57 -0
- package/column.types.d.ts +57 -0
- package/constants.d.cts +20 -0
- package/constants.d.mts +20 -0
- package/constants.d.ts +20 -0
- package/index.cjs +366 -332
- package/index.cjs.map +1 -1
- package/index.d.cts +6 -101
- package/index.d.mts +6 -101
- package/index.d.ts +6 -101
- package/index.mjs +345 -304
- package/index.mjs.map +1 -1
- package/package.json +3 -2
- package/schema.d.cts +4 -0
- package/schema.d.mts +4 -0
- package/schema.d.ts +4 -0
- package/schema.types.d.cts +21 -0
- package/schema.types.d.mts +21 -0
- package/schema.types.d.ts +21 -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 +28 -0
- package/utils.d.mts +28 -0
- package/utils.d.ts +28 -0
package/index.mjs
CHANGED
|
@@ -1,345 +1,386 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import * as v from
|
|
1
|
+
import { extractExtendedColumnType, getTableName, getColumnTable, isTable, getTableColumns, getViewSelectedFields, is, Column, SQL, isView } from 'drizzle-orm';
|
|
2
|
+
import * as v from 'valibot';
|
|
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
|
-
//#region src/column.ts
|
|
28
|
-
const literalSchema = v.union([
|
|
29
|
-
v.string(),
|
|
30
|
-
v.number(),
|
|
31
|
-
v.boolean(),
|
|
32
|
-
v.null()
|
|
33
|
-
]);
|
|
25
|
+
const literalSchema = v.union([v.string(), v.number(), v.boolean(), v.null()]);
|
|
34
26
|
const jsonSchema = v.union([
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
27
|
+
literalSchema,
|
|
28
|
+
v.array(v.any()),
|
|
29
|
+
v.record(v.string(), v.any()),
|
|
38
30
|
]);
|
|
39
|
-
const bufferSchema = v.custom((v
|
|
31
|
+
const bufferSchema = v.custom((v) => v instanceof Buffer);
|
|
40
32
|
function mapEnumValues(values) {
|
|
41
|
-
|
|
33
|
+
return Object.fromEntries(values.map((value) => [value, value]));
|
|
42
34
|
}
|
|
43
35
|
function columnToSchema(column) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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
|
-
|
|
36
|
+
let schema;
|
|
37
|
+
const { type, constraint } = extractExtendedColumnType(column);
|
|
38
|
+
switch (type) {
|
|
39
|
+
case 'array': {
|
|
40
|
+
schema = arrayColumnToSchema(column, constraint);
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
case 'object': {
|
|
44
|
+
schema = objectColumnToSchema(column, constraint);
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
case 'number': {
|
|
48
|
+
schema = numberColumnToSchema(column, constraint);
|
|
49
|
+
break;
|
|
50
|
+
}
|
|
51
|
+
case 'bigint': {
|
|
52
|
+
schema = bigintColumnToSchema(column, constraint);
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
case 'boolean': {
|
|
56
|
+
schema = v.boolean();
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
case 'string': {
|
|
60
|
+
schema = stringColumnToSchema(column, constraint);
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
case 'custom': {
|
|
64
|
+
schema = v.any();
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
default: {
|
|
68
|
+
schema = v.any();
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return schema;
|
|
73
72
|
}
|
|
74
73
|
function numberColumnToSchema(column, constraint) {
|
|
75
|
-
|
|
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
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
74
|
+
let min;
|
|
75
|
+
let max;
|
|
76
|
+
let integer = false;
|
|
77
|
+
switch (constraint) {
|
|
78
|
+
case 'int8': {
|
|
79
|
+
min = CONSTANTS.INT8_MIN;
|
|
80
|
+
max = CONSTANTS.INT8_MAX;
|
|
81
|
+
integer = true;
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
case 'uint8': {
|
|
85
|
+
min = 0;
|
|
86
|
+
max = CONSTANTS.INT8_UNSIGNED_MAX;
|
|
87
|
+
integer = true;
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
case 'int16': {
|
|
91
|
+
min = CONSTANTS.INT16_MIN;
|
|
92
|
+
max = CONSTANTS.INT16_MAX;
|
|
93
|
+
integer = true;
|
|
94
|
+
break;
|
|
95
|
+
}
|
|
96
|
+
case 'uint16': {
|
|
97
|
+
min = 0;
|
|
98
|
+
max = CONSTANTS.INT16_UNSIGNED_MAX;
|
|
99
|
+
integer = true;
|
|
100
|
+
break;
|
|
101
|
+
}
|
|
102
|
+
case 'int24': {
|
|
103
|
+
min = CONSTANTS.INT24_MIN;
|
|
104
|
+
max = CONSTANTS.INT24_MAX;
|
|
105
|
+
integer = true;
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
case 'uint24': {
|
|
109
|
+
min = 0;
|
|
110
|
+
max = CONSTANTS.INT24_UNSIGNED_MAX;
|
|
111
|
+
integer = true;
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
case 'int32': {
|
|
115
|
+
min = CONSTANTS.INT32_MIN;
|
|
116
|
+
max = CONSTANTS.INT32_MAX;
|
|
117
|
+
integer = true;
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
case 'uint32': {
|
|
121
|
+
min = 0;
|
|
122
|
+
max = CONSTANTS.INT32_UNSIGNED_MAX;
|
|
123
|
+
integer = true;
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
case 'int53': {
|
|
127
|
+
min = Number.MIN_SAFE_INTEGER;
|
|
128
|
+
max = Number.MAX_SAFE_INTEGER;
|
|
129
|
+
integer = true;
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
case 'uint53': {
|
|
133
|
+
min = 0;
|
|
134
|
+
max = Number.MAX_SAFE_INTEGER;
|
|
135
|
+
integer = true;
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
case 'float': {
|
|
139
|
+
min = CONSTANTS.INT24_MIN;
|
|
140
|
+
max = CONSTANTS.INT24_MAX;
|
|
141
|
+
break;
|
|
142
|
+
}
|
|
143
|
+
case 'ufloat': {
|
|
144
|
+
min = 0;
|
|
145
|
+
max = CONSTANTS.INT24_UNSIGNED_MAX;
|
|
146
|
+
break;
|
|
147
|
+
}
|
|
148
|
+
case 'double': {
|
|
149
|
+
min = CONSTANTS.INT48_MIN;
|
|
150
|
+
max = CONSTANTS.INT48_MAX;
|
|
151
|
+
break;
|
|
152
|
+
}
|
|
153
|
+
case 'udouble': {
|
|
154
|
+
min = 0;
|
|
155
|
+
max = CONSTANTS.INT48_UNSIGNED_MAX;
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
case 'year': {
|
|
159
|
+
min = 1901;
|
|
160
|
+
max = 2155;
|
|
161
|
+
integer = true;
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
164
|
+
case 'unsigned': {
|
|
165
|
+
min = 0;
|
|
166
|
+
max = Number.MAX_SAFE_INTEGER;
|
|
167
|
+
break;
|
|
168
|
+
}
|
|
169
|
+
default: {
|
|
170
|
+
min = Number.MIN_SAFE_INTEGER;
|
|
171
|
+
max = Number.MAX_SAFE_INTEGER;
|
|
172
|
+
break;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
const actions = [v.minValue(min), v.maxValue(max)];
|
|
176
|
+
if (integer) {
|
|
177
|
+
actions.push(v.integer());
|
|
178
|
+
}
|
|
179
|
+
return v.pipe(v.number(), ...actions);
|
|
162
180
|
}
|
|
163
181
|
/** @internal */
|
|
164
|
-
const bigintStringModeSchema = v.pipe(v.string(), v.regex(/^-?\d+$/),
|
|
182
|
+
const bigintStringModeSchema = v.pipe(v.string(), v.regex(/^-?\d+$/),
|
|
183
|
+
// eslint-disable-next-line unicorn/prefer-native-coercion-functions
|
|
184
|
+
v.transform((v) => BigInt(v)), v.minValue(CONSTANTS.INT64_MIN), v.maxValue(CONSTANTS.INT64_MAX), v.transform((v) => v.toString()));
|
|
165
185
|
/** @internal */
|
|
166
|
-
const unsignedBigintStringModeSchema = v.pipe(v.string(), v.regex(/^\d+$/),
|
|
186
|
+
const unsignedBigintStringModeSchema = v.pipe(v.string(), v.regex(/^\d+$/),
|
|
187
|
+
// eslint-disable-next-line unicorn/prefer-native-coercion-functions
|
|
188
|
+
v.transform((v) => BigInt(v)), v.minValue(0n), v.maxValue(CONSTANTS.INT64_MAX), v.transform((v) => v.toString()));
|
|
167
189
|
function bigintColumnToSchema(column, constraint) {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
switch (baseType) {
|
|
189
|
-
case "number":
|
|
190
|
-
baseSchema = numberColumnToSchema(column, baseConstraint);
|
|
191
|
-
break;
|
|
192
|
-
case "bigint":
|
|
193
|
-
baseSchema = bigintColumnToSchema(column, baseConstraint);
|
|
194
|
-
break;
|
|
195
|
-
case "boolean":
|
|
196
|
-
baseSchema = v.boolean();
|
|
197
|
-
break;
|
|
198
|
-
case "string":
|
|
199
|
-
baseSchema = stringColumnToSchema(column, baseConstraint);
|
|
200
|
-
break;
|
|
201
|
-
case "object":
|
|
202
|
-
baseSchema = objectColumnToSchema(column, baseConstraint);
|
|
203
|
-
break;
|
|
204
|
-
case "array":
|
|
205
|
-
baseSchema = arrayColumnToSchema(column, baseConstraint);
|
|
206
|
-
break;
|
|
207
|
-
default: baseSchema = v.any();
|
|
208
|
-
}
|
|
209
|
-
let schema = v.array(baseSchema);
|
|
210
|
-
for (let i = 1; i < dimensions; i++) schema = v.array(schema);
|
|
211
|
-
return schema;
|
|
190
|
+
let min;
|
|
191
|
+
let max;
|
|
192
|
+
switch (constraint) {
|
|
193
|
+
case 'int64': {
|
|
194
|
+
min = CONSTANTS.INT64_MIN;
|
|
195
|
+
max = CONSTANTS.INT64_MAX;
|
|
196
|
+
break;
|
|
197
|
+
}
|
|
198
|
+
case 'uint64': {
|
|
199
|
+
min = 0n;
|
|
200
|
+
max = CONSTANTS.INT64_UNSIGNED_MAX;
|
|
201
|
+
break;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
const actions = [];
|
|
205
|
+
if (min !== undefined)
|
|
206
|
+
actions.push(v.minValue(min));
|
|
207
|
+
if (max !== undefined)
|
|
208
|
+
actions.push(v.maxValue(max));
|
|
209
|
+
return actions.length > 0 ? v.pipe(v.bigint(), ...actions) : v.bigint();
|
|
212
210
|
}
|
|
213
211
|
function arrayColumnToSchema(column, constraint) {
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
212
|
+
switch (constraint) {
|
|
213
|
+
case 'geometry':
|
|
214
|
+
case 'point': {
|
|
215
|
+
return v.tuple([v.number(), v.number()]);
|
|
216
|
+
}
|
|
217
|
+
case 'line': {
|
|
218
|
+
return v.tuple([v.number(), v.number(), v.number()]);
|
|
219
|
+
}
|
|
220
|
+
case 'vector':
|
|
221
|
+
case 'halfvector': {
|
|
222
|
+
const { length } = column;
|
|
223
|
+
return length
|
|
224
|
+
? v.pipe(v.array(v.number()), v.length(length))
|
|
225
|
+
: v.array(v.number());
|
|
226
|
+
}
|
|
227
|
+
case 'int64vector': {
|
|
228
|
+
const length = column.length;
|
|
229
|
+
return length
|
|
230
|
+
? v.pipe(v.array(v.pipe(v.bigint(), v.minValue(CONSTANTS.INT64_MIN), v.maxValue(CONSTANTS.INT64_MAX))), v.length(length))
|
|
231
|
+
: v.array(v.pipe(v.bigint(), v.minValue(CONSTANTS.INT64_MIN), v.maxValue(CONSTANTS.INT64_MAX)));
|
|
232
|
+
}
|
|
233
|
+
case 'basecolumn': {
|
|
234
|
+
const { length } = column;
|
|
235
|
+
const schema = column.baseColumn
|
|
236
|
+
? v.array(columnToSchema(column.baseColumn))
|
|
237
|
+
: v.array(v.any());
|
|
238
|
+
if (length)
|
|
239
|
+
return v.pipe(schema, v.length(length));
|
|
240
|
+
return schema;
|
|
241
|
+
}
|
|
242
|
+
default: {
|
|
243
|
+
return v.array(v.any());
|
|
244
|
+
}
|
|
245
|
+
}
|
|
243
246
|
}
|
|
244
247
|
function objectColumnToSchema(column, constraint) {
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
248
|
+
switch (constraint) {
|
|
249
|
+
case 'buffer': {
|
|
250
|
+
return bufferSchema;
|
|
251
|
+
}
|
|
252
|
+
case 'date': {
|
|
253
|
+
return v.date();
|
|
254
|
+
}
|
|
255
|
+
case 'geometry':
|
|
256
|
+
case 'point': {
|
|
257
|
+
return v.object({
|
|
258
|
+
x: v.number(),
|
|
259
|
+
y: v.number(),
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
case 'json': {
|
|
263
|
+
return jsonSchema;
|
|
264
|
+
}
|
|
265
|
+
case 'line': {
|
|
266
|
+
return v.object({
|
|
267
|
+
a: v.number(),
|
|
268
|
+
b: v.number(),
|
|
269
|
+
c: v.number(),
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
default: {
|
|
273
|
+
return v.looseObject({});
|
|
274
|
+
}
|
|
275
|
+
}
|
|
261
276
|
}
|
|
262
277
|
function stringColumnToSchema(column, constraint) {
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
278
|
+
const { name: columnName, length, isLengthExact } = column;
|
|
279
|
+
let regex;
|
|
280
|
+
if (constraint === 'binary') {
|
|
281
|
+
regex = /^[01]*$/;
|
|
282
|
+
}
|
|
283
|
+
if (constraint === 'uuid')
|
|
284
|
+
return v.pipe(v.string(), v.uuid());
|
|
285
|
+
if (constraint === 'enum') {
|
|
286
|
+
const enumValues = column.enumValues;
|
|
287
|
+
if (!enumValues) {
|
|
288
|
+
throw new Error(`Column "${getTableName(getColumnTable(column))}"."${columnName}" is of 'enum' type, but lacks enum values`);
|
|
289
|
+
}
|
|
290
|
+
return v.enum(mapEnumValues(enumValues));
|
|
291
|
+
}
|
|
292
|
+
if (constraint === 'int64') {
|
|
293
|
+
return bigintStringModeSchema;
|
|
294
|
+
}
|
|
295
|
+
if (constraint === 'uint64') {
|
|
296
|
+
return unsignedBigintStringModeSchema;
|
|
297
|
+
}
|
|
298
|
+
const actions = [];
|
|
299
|
+
if (regex) {
|
|
300
|
+
actions.push(v.regex(regex));
|
|
301
|
+
}
|
|
302
|
+
if (length && isLengthExact) {
|
|
303
|
+
actions.push(v.length(length));
|
|
304
|
+
}
|
|
305
|
+
else if (length) {
|
|
306
|
+
actions.push(v.maxLength(length));
|
|
307
|
+
}
|
|
308
|
+
return actions.length > 0 ? v.pipe(v.string(), ...actions) : v.string();
|
|
279
309
|
}
|
|
280
310
|
|
|
281
|
-
//#endregion
|
|
282
|
-
//#region src/utils.ts
|
|
283
311
|
function isColumnType(column, columnTypes) {
|
|
284
|
-
|
|
312
|
+
return columnTypes.includes(column.columnType);
|
|
285
313
|
}
|
|
286
314
|
function isWithEnum(column) {
|
|
287
|
-
|
|
315
|
+
return 'enumValues' in column && Array.isArray(column.enumValues) && column.enumValues.length > 0;
|
|
288
316
|
}
|
|
289
317
|
const isPgEnum = isWithEnum;
|
|
290
318
|
|
|
291
|
-
//#endregion
|
|
292
|
-
//#region src/schema.ts
|
|
293
319
|
function getColumns(tableLike) {
|
|
294
|
-
|
|
320
|
+
return isTable(tableLike) ? getTableColumns(tableLike) : getViewSelectedFields(tableLike);
|
|
295
321
|
}
|
|
296
322
|
function handleColumns(columns, refinements, conditions) {
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
323
|
+
const columnSchemas = {};
|
|
324
|
+
for (const [key, selected] of Object.entries(columns)) {
|
|
325
|
+
if (!is(selected, Column) && !is(selected, SQL) && !is(selected, SQL.Aliased) && typeof selected === 'object') {
|
|
326
|
+
const columns = isTable(selected) || isView(selected) ? getColumns(selected) : selected;
|
|
327
|
+
columnSchemas[key] = handleColumns(columns, refinements[key] ?? {}, conditions);
|
|
328
|
+
continue;
|
|
329
|
+
}
|
|
330
|
+
const refinement = refinements[key];
|
|
331
|
+
if (refinement !== undefined && typeof refinement !== 'function') {
|
|
332
|
+
columnSchemas[key] = refinement;
|
|
333
|
+
continue;
|
|
334
|
+
}
|
|
335
|
+
const column = is(selected, Column) ? selected : undefined;
|
|
336
|
+
const schema = column ? columnToSchema(column) : v.any();
|
|
337
|
+
const refined = typeof refinement === 'function' ? refinement(schema) : schema;
|
|
338
|
+
if (conditions.never(column)) {
|
|
339
|
+
continue;
|
|
340
|
+
}
|
|
341
|
+
else {
|
|
342
|
+
columnSchemas[key] = refined;
|
|
343
|
+
}
|
|
344
|
+
if (column) {
|
|
345
|
+
if (conditions.nullable(column)) {
|
|
346
|
+
columnSchemas[key] = v.nullable(columnSchemas[key]);
|
|
347
|
+
}
|
|
348
|
+
if (conditions.optional(column)) {
|
|
349
|
+
columnSchemas[key] = v.optional(columnSchemas[key]);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
return v.object(columnSchemas);
|
|
319
354
|
}
|
|
320
355
|
const createSelectSchema = (entity, refine) => {
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
356
|
+
if (isPgEnum(entity)) {
|
|
357
|
+
return v.enum(mapEnumValues(entity.enumValues));
|
|
358
|
+
}
|
|
359
|
+
const columns = getColumns(entity);
|
|
360
|
+
return handleColumns(columns, refine ?? {}, {
|
|
361
|
+
never: () => false,
|
|
362
|
+
optional: () => false,
|
|
363
|
+
nullable: (column) => !column.notNull,
|
|
364
|
+
});
|
|
327
365
|
};
|
|
328
366
|
const createInsertSchema = (entity, refine) => {
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
367
|
+
const columns = getColumns(entity);
|
|
368
|
+
return handleColumns(columns, refine ?? {}, {
|
|
369
|
+
never: (column) => column?.generated?.type === 'always' || column?.generatedIdentity?.type === 'always'
|
|
370
|
+
|| ('identity' in (column ?? {}) && typeof column?.identity !== 'undefined'),
|
|
371
|
+
optional: (column) => !column.notNull || (column.notNull && column.hasDefault),
|
|
372
|
+
nullable: (column) => !column.notNull,
|
|
373
|
+
});
|
|
334
374
|
};
|
|
335
375
|
const createUpdateSchema = (entity, refine) => {
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
376
|
+
const columns = getColumns(entity);
|
|
377
|
+
return handleColumns(columns, refine ?? {}, {
|
|
378
|
+
never: (column) => column?.generated?.type === 'always' || column?.generatedIdentity?.type === 'always'
|
|
379
|
+
|| ('identity' in (column ?? {}) && typeof column?.identity !== 'undefined'),
|
|
380
|
+
optional: () => true,
|
|
381
|
+
nullable: (column) => !column.notNull,
|
|
382
|
+
});
|
|
341
383
|
};
|
|
342
384
|
|
|
343
|
-
//#endregion
|
|
344
385
|
export { bufferSchema, createInsertSchema, createSelectSchema, createUpdateSchema, isColumnType, isPgEnum, isWithEnum, jsonSchema, literalSchema };
|
|
345
|
-
//# sourceMappingURL=index.mjs.map
|
|
386
|
+
//# sourceMappingURL=index.mjs.map
|