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