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