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