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. 11 imports, 0 dependents.

File typescript DrizzleORM DatabaseDrivers 11 imports 2 functions 1 classes

Entity Profile

Dependency Diagram

graph LR
  06e0abfd_8dae_0f31_4535_349e0a684907["schema.ts"]
  cc1fbfaa_1553_bc17_cd31_5fefac6896d3["enum.ts"]
  06e0abfd_8dae_0f31_4535_349e0a684907 --> cc1fbfaa_1553_bc17_cd31_5fefac6896d3
  95f942c3_5071_5b7c_4981_1a774633963b["pgEnumObjectWithSchema"]
  06e0abfd_8dae_0f31_4535_349e0a684907 --> 95f942c3_5071_5b7c_4981_1a774633963b
  d43daaf3_6617_c211_9f91_3d5e843c8b4d["pgEnumWithSchema"]
  06e0abfd_8dae_0f31_4535_349e0a684907 --> d43daaf3_6617_c211_9f91_3d5e843c8b4d
  c67b5e95_842f_36c8_80c4_20ed982f5bfe["sequence.ts"]
  06e0abfd_8dae_0f31_4535_349e0a684907 --> c67b5e95_842f_36c8_80c4_20ed982f5bfe
  e541dd30_875d_cd87_1fd3_b43f31ab6dd8["pgSequenceWithSchema"]
  06e0abfd_8dae_0f31_4535_349e0a684907 --> e541dd30_875d_cd87_1fd3_b43f31ab6dd8
  2d5c8884_973c_561c_def6_5e394ea36d1a["table.ts"]
  06e0abfd_8dae_0f31_4535_349e0a684907 --> 2d5c8884_973c_561c_def6_5e394ea36d1a
  dae5654f_1fee_289f_bc31_bb7199ac008e["pgTableWithSchema"]
  06e0abfd_8dae_0f31_4535_349e0a684907 --> dae5654f_1fee_289f_bc31_bb7199ac008e
  a44cb974_fe5a_c572_a91a_51fe8c82034c["view.ts"]
  06e0abfd_8dae_0f31_4535_349e0a684907 --> a44cb974_fe5a_c572_a91a_51fe8c82034c
  27705a9d_afe9_57dd_8c97_e52d8a67d426["entity.ts"]
  06e0abfd_8dae_0f31_4535_349e0a684907 --> 27705a9d_afe9_57dd_8c97_e52d8a67d426
  be483a7f_d5d7_7a9b_9a13_44a4a6aafbbd["sql.ts"]
  06e0abfd_8dae_0f31_4535_349e0a684907 --> be483a7f_d5d7_7a9b_9a13_44a4a6aafbbd
  ecce3253_1e75_a87f_27b3_ca87e81a3024["utils.ts"]
  06e0abfd_8dae_0f31_4535_349e0a684907 --> ecce3253_1e75_a87f_27b3_ca87e81a3024
  style 06e0abfd_8dae_0f31_4535_349e0a684907 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { entityKind, is } from '~/entity.ts';
import { SQL, sql, type SQLWrapper } from '~/sql/sql.ts';
import type { NonArray, Writable } from '~/utils.ts';
import { type PgEnum, type PgEnumObject, pgEnumObjectWithSchema, pgEnumWithSchema } from './columns/enum.ts';
import { type pgSequence, pgSequenceWithSchema } from './sequence.ts';
import { type PgTableFn, pgTableWithSchema } from './table.ts';
import { type pgMaterializedView, pgMaterializedViewWithSchema, type pgView, pgViewWithSchema } from './view.ts';

export class PgSchema<TName extends string = string> implements SQLWrapper {
	static readonly [entityKind]: string = 'PgSchema';
	constructor(
		public readonly schemaName: TName,
	) {}

	table: PgTableFn<TName> = ((name, columns, extraConfig) => {
		return pgTableWithSchema(name, columns, extraConfig, this.schemaName);
	});

	view = ((name, columns) => {
		return pgViewWithSchema(name, columns, this.schemaName);
	}) as typeof pgView;

	materializedView = ((name, columns) => {
		return pgMaterializedViewWithSchema(name, columns, this.schemaName);
	}) as typeof pgMaterializedView;

	public enum<U extends string, T extends Readonly<[U, ...U[]]>>(
		enumName: string,
		values: T | Writable<T>,
	): PgEnum<Writable<T>>;

	public enum<E extends Record<string, string>>(
		enumName: string,
		enumObj: NonArray<E>,
	): PgEnumObject<E>;

	public enum(enumName: any, input: any): any {
		return Array.isArray(input)
			? pgEnumWithSchema(
				enumName,
				[...input] as [string, ...string[]],
				this.schemaName,
			)
			: pgEnumObjectWithSchema(enumName, input, this.schemaName);
	}

	sequence: typeof pgSequence = ((name, options) => {
		return pgSequenceWithSchema(name, options, this.schemaName);
	});

	getSQL(): SQL {
		return new SQL([sql.identifier(this.schemaName)]);
	}

	shouldOmitSQLParens(): boolean {
		return true;
	}
}

export function isPgSchema(obj: unknown): obj is PgSchema {
	return is(obj, PgSchema);
}

export function pgSchema<T extends string>(name: T) {
	if (name === 'public') {
		throw new Error(
			`You can't specify 'public' as schema name. Postgres is using public schema by default. If you want to use 'public' schema, just use pgTable() instead of creating a schema`,
		);
	}

	return new PgSchema(name);
}

Domain

Subdomains

Classes

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 DrizzleORM domain, DatabaseDrivers subdomain.
What functions are defined in schema.ts?
schema.ts defines 2 function(s): isPgSchema, pgSchema.
What does schema.ts depend on?
schema.ts imports 11 module(s): entity.ts, enum.ts, pgEnumObjectWithSchema, pgEnumWithSchema, pgSequenceWithSchema, pgTableWithSchema, sequence.ts, sql.ts, and 3 more.
Where is schema.ts in the architecture?
schema.ts is located at drizzle-orm/src/pg-core/schema.ts (domain: DrizzleORM, subdomain: DatabaseDrivers, directory: drizzle-orm/src/pg-core).

Analyze Your Own Codebase

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

Try Supermodel Free