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