Home / File/ schema.ts — drizzle-orm Source File

schema.ts — drizzle-orm Source File

Architecture documentation for schema.ts, a typescript file in the drizzle-orm codebase. 8 imports, 0 dependents.

Entity Profile

Dependency Diagram

graph LR
  91122f81_e9f3_fa04_26c5_a8235e0646d0["schema.ts"]
  d3d3a2d1_4197_a39b_12f9_f9c7be54febc["column.ts"]
  91122f81_e9f3_fa04_26c5_a8235e0646d0 --> d3d3a2d1_4197_a39b_12f9_f9c7be54febc
  85adcc4b_0e8e_a2e6_a538_afbe19c96304["columnToSchema"]
  91122f81_e9f3_fa04_26c5_a8235e0646d0 --> 85adcc4b_0e8e_a2e6_a538_afbe19c96304
  acfeaa81_8bd9_605a_74e7_52a36a49babb["schema.types.internal.ts"]
  91122f81_e9f3_fa04_26c5_a8235e0646d0 --> acfeaa81_8bd9_605a_74e7_52a36a49babb
  37a75c34_5ecc_3525_ecb4_d53dfebc43bb["schema.types.ts"]
  91122f81_e9f3_fa04_26c5_a8235e0646d0 --> 37a75c34_5ecc_3525_ecb4_d53dfebc43bb
  90807ef2_6e28_df2f_8c6b_9bcb1bab99ec["utils.ts"]
  91122f81_e9f3_fa04_26c5_a8235e0646d0 --> 90807ef2_6e28_df2f_8c6b_9bcb1bab99ec
  690f7dfc_0aea_9ee8_d6e7_26bbb3689031["drizzle-orm"]
  91122f81_e9f3_fa04_26c5_a8235e0646d0 --> 690f7dfc_0aea_9ee8_d6e7_26bbb3689031
  53497908_16e7_977d_e97d_7414884a88a6["pg-core"]
  91122f81_e9f3_fa04_26c5_a8235e0646d0 --> 53497908_16e7_977d_e97d_7414884a88a6
  2eba3e52_10ef_4b48_38fc_0e1540a5fc3e["v4"]
  91122f81_e9f3_fa04_26c5_a8235e0646d0 --> 2eba3e52_10ef_4b48_38fc_0e1540a5fc3e
  style 91122f81_e9f3_fa04_26c5_a8235e0646d0 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { Column, getTableColumns, getViewSelectedFields, is, isTable, isView, SQL } from 'drizzle-orm';
import type { Table, View } from 'drizzle-orm';
import type { PgEnum } from 'drizzle-orm/pg-core';
import { z } from 'zod/v4';
import { columnToSchema } from './column.ts';
import type { Conditions } from './schema.types.internal.ts';
import type {
	CreateInsertSchema,
	CreateSchemaFactoryOptions,
	CreateSelectSchema,
	CreateUpdateSchema,
} from './schema.types.ts';
import { isPgEnum } from './utils.ts';

function getColumns(tableLike: Table | View) {
	return isTable(tableLike) ? getTableColumns(tableLike) : getViewSelectedFields(tableLike);
}

function handleColumns(
	columns: Record<string, any>,
	refinements: Record<string, any>,
	conditions: Conditions,
	factory?: CreateSchemaFactoryOptions<
		Partial<Record<'bigint' | 'boolean' | 'date' | 'number' | 'string', true>> | true | undefined
	>,
): z.ZodType {
	const columnSchemas: Record<string, z.ZodType> = {};

	for (const [key, selected] of Object.entries(columns)) {
		if (!is(selected, Column) && !is(selected, SQL) && !is(selected, SQL.Aliased) && typeof selected === 'object') {
			const columns = isTable(selected) || isView(selected) ? getColumns(selected) : selected;
			columnSchemas[key] = handleColumns(columns, refinements[key] ?? {}, conditions, factory);
			continue;
		}

		const refinement = refinements[key];
		if (refinement !== undefined && typeof refinement !== 'function') {
			columnSchemas[key] = refinement;
			continue;
		}

		const column = is(selected, Column) ? selected : undefined;
		const schema = column ? columnToSchema(column, factory) : z.any();
		const refined = typeof refinement === 'function' ? refinement(schema) : schema;

		if (conditions.never(column)) {
			continue;
		} else {
			columnSchemas[key] = refined;
		}

		if (column) {
			if (conditions.nullable(column)) {
				columnSchemas[key] = columnSchemas[key]!.nullable();
			}

			if (conditions.optional(column)) {
				columnSchemas[key] = columnSchemas[key]!.optional();
			}
		}
// ... (93 more lines)

Subdomains

Frequently Asked Questions

What does schema.ts do?
schema.ts is a source file in the drizzle-orm codebase, written in typescript. It belongs to the ValidationAdapters domain, SchemaRefinement subdomain.
What functions are defined in schema.ts?
schema.ts defines 16 function(s): createInsertSchema, createSchemaFactory, createSelectSchema, createUpdateSchema, getColumns, handleColumns, handleEnum, insertConditions.never, insertConditions.nullable, insertConditions.optional, and 6 more.
What does schema.ts depend on?
schema.ts imports 8 module(s): column.ts, columnToSchema, drizzle-orm, pg-core, schema.types.internal.ts, schema.types.ts, utils.ts, v4.
Where is schema.ts in the architecture?
schema.ts is located at drizzle-zod/src/schema.ts (domain: ValidationAdapters, subdomain: SchemaRefinement, directory: drizzle-zod/src).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free