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