MCPcopy
hub / github.com/drizzle-team/drizzle-orm / handleColumns

Function handleColumns

drizzle-zod/src/schema.ts:19–64  ·  view source on GitHub ↗
(
	columns: Record<string, any>,
	refinements: Record<string, any>,
	conditions: Conditions,
	factory?: CreateSchemaFactoryOptions<
		Partial<Record<'bigint' | 'boolean' | 'date' | 'number' | 'string', true>> | true | undefined
	>,
)

Source from the content-addressed store, hash-verified

17}
18
19function handleColumns(
20 columns: Record<string, any>,
21 refinements: Record<string, any>,
22 conditions: Conditions,
23 factory?: CreateSchemaFactoryOptions<
24 Partial<Record<'bigint' | 'boolean' | 'date' | 'number' | 'string', true>> | true | undefined
25 >,
26): z.ZodType {
27 const columnSchemas: Record<string, z.ZodType> = {};
28
29 for (const [key, selected] of Object.entries(columns)) {
30 if (!is(selected, Column) && !is(selected, SQL) && !is(selected, SQL.Aliased) && typeof selected === 'object') {
31 const columns = isTable(selected) || isView(selected) ? getColumns(selected) : selected;
32 columnSchemas[key] = handleColumns(columns, refinements[key] ?? {}, conditions, factory);
33 continue;
34 }
35
36 const refinement = refinements[key];
37 if (refinement !== undefined && typeof refinement !== 'function') {
38 columnSchemas[key] = refinement;
39 continue;
40 }
41
42 const column = is(selected, Column) ? selected : undefined;
43 const schema = column ? columnToSchema(column, factory) : z.any();
44 const refined = typeof refinement === 'function' ? refinement(schema) : schema;
45
46 if (conditions.never(column)) {
47 continue;
48 } else {
49 columnSchemas[key] = refined;
50 }
51
52 if (column) {
53 if (conditions.nullable(column)) {
54 columnSchemas[key] = columnSchemas[key]!.nullable();
55 }
56
57 if (conditions.optional(column)) {
58 columnSchemas[key] = columnSchemas[key]!.optional();
59 }
60 }
61 }
62
63 return z.object(columnSchemas) as any;
64}
65
66function handleEnum(
67 enum_: PgEnum<any>,

Callers 3

createSelectSchemaFunction · 0.70
createInsertSchemaFunction · 0.70
createUpdateSchemaFunction · 0.70

Calls 5

isFunction · 0.90
isTableFunction · 0.90
isViewFunction · 0.90
columnToSchemaFunction · 0.90
getColumnsFunction · 0.70

Tested by

no test coverage detected