drizzle-arktype 1.0.0-beta.6-9514357 → 1.0.0-beta.6-051f3cd

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