Home / Function/ schemaToTypeScript() — drizzle-orm Function Reference

schemaToTypeScript() — drizzle-orm Function Reference

Architecture documentation for the schemaToTypeScript() function in introspect-pg.ts from the drizzle-orm codebase.

Entity Profile

Dependency Diagram

graph TD
  ae4bc88a_1e24_ce7b_ea80_728e39b829ef["schemaToTypeScript()"]
  71516551_23e3_bf30_27c9_000fb046ef71["introspect-pg.ts"]
  ae4bc88a_1e24_ce7b_ea80_728e39b829ef -->|defined in| 71516551_23e3_bf30_27c9_000fb046ef71
  968467a1_c4a6_11cb_5fbc_8854071f9af4["withCasing()"]
  ae4bc88a_1e24_ce7b_ea80_728e39b829ef -->|calls| 968467a1_c4a6_11cb_5fbc_8854071f9af4
  7920a2b5_4791_abeb_cbd1_adae59f54b93["isCyclic()"]
  ae4bc88a_1e24_ce7b_ea80_728e39b829ef -->|calls| 7920a2b5_4791_abeb_cbd1_adae59f54b93
  1e52ac37_0af2_3896_4314_dd91a737f67d["isSelf()"]
  ae4bc88a_1e24_ce7b_ea80_728e39b829ef -->|calls| 1e52ac37_0af2_3896_4314_dd91a737f67d
  9033c8cf_1cd9_c420_437a_01e7a84a9869["paramNameFor()"]
  ae4bc88a_1e24_ce7b_ea80_728e39b829ef -->|calls| 9033c8cf_1cd9_c420_437a_01e7a84a9869
  b79b7789_7dc7_8d59_d31c_04fd15a06eee["unescapeSingleQuotes()"]
  ae4bc88a_1e24_ce7b_ea80_728e39b829ef -->|calls| b79b7789_7dc7_8d59_d31c_04fd15a06eee
  d060c9b2_d1df_37e7_df71_422307a42ed6["createTableColumns()"]
  ae4bc88a_1e24_ce7b_ea80_728e39b829ef -->|calls| d060c9b2_d1df_37e7_df71_422307a42ed6
  6654cdae_b135_ed6d_5743_767fc7ed226e["createTableIndexes()"]
  ae4bc88a_1e24_ce7b_ea80_728e39b829ef -->|calls| 6654cdae_b135_ed6d_5743_767fc7ed226e
  da670fa1_7aef_55a6_f6b5_d56dded0eade["createTableFKs()"]
  ae4bc88a_1e24_ce7b_ea80_728e39b829ef -->|calls| da670fa1_7aef_55a6_f6b5_d56dded0eade
  0750c42c_a22a_1019_9840_e8ee6ab9031c["createTablePKs()"]
  ae4bc88a_1e24_ce7b_ea80_728e39b829ef -->|calls| 0750c42c_a22a_1019_9840_e8ee6ab9031c
  4bf3de5e_42f3_e8a0_a804_e18a3ea1cb6a["createTableUniques()"]
  ae4bc88a_1e24_ce7b_ea80_728e39b829ef -->|calls| 4bf3de5e_42f3_e8a0_a804_e18a3ea1cb6a
  cdb1f2e5_6be0_6765_9367_ec66357c1205["createTablePolicies()"]
  ae4bc88a_1e24_ce7b_ea80_728e39b829ef -->|calls| cdb1f2e5_6be0_6765_9367_ec66357c1205
  7ebe671e_34b5_c576_afb8_b091963fb48f["createTableChecks()"]
  ae4bc88a_1e24_ce7b_ea80_728e39b829ef -->|calls| 7ebe671e_34b5_c576_afb8_b091963fb48f
  28b894c7_c809_8ecd_6165_49b642904255["trimChar()"]
  ae4bc88a_1e24_ce7b_ea80_728e39b829ef -->|calls| 28b894c7_c809_8ecd_6165_49b642904255
  style ae4bc88a_1e24_ce7b_ea80_728e39b829ef fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

drizzle-kit/src/introspect-pg.ts lines 309–636

export const schemaToTypeScript = (schema: PgSchemaInternal, casing: Casing) => {
	// collectFKs
	Object.values(schema.tables).forEach((table) => {
		Object.values(table.foreignKeys).forEach((fk) => {
			const relation = `${fk.tableFrom}-${fk.tableTo}`;
			relations.add(relation);
		});
	});

	const schemas = Object.fromEntries(
		Object.entries(schema.schemas).map((it) => {
			return [it[0], withCasing(it[1], casing)];
		}),
	);

	const enumTypes = Object.values(schema.enums).reduce((acc, cur) => {
		acc.add(`${cur.schema}.${cur.name}`);
		return acc;
	}, new Set<string>());

	const imports = Object.values(schema.tables).reduce(
		(res, it) => {
			const idxImports = Object.values(it.indexes).map((idx) => (idx.isUnique ? 'uniqueIndex' : 'index'));
			const fkImpots = Object.values(it.foreignKeys).map((it) => 'foreignKey');
			if (Object.values(it.foreignKeys).some((it) => isCyclic(it) && !isSelf(it))) {
				res.pg.push('type AnyPgColumn');
			}
			const pkImports = Object.values(it.compositePrimaryKeys).map((it) => 'primaryKey');
			const uniqueImports = Object.values(it.uniqueConstraints).map((it) => 'unique');

			const checkImports = Object.values(it.checkConstraints).map(
				(it) => 'check',
			);

			const policiesImports = Object.values(it.policies).map(
				(it) => 'pgPolicy',
			);

			if (it.schema && it.schema !== 'public' && it.schema !== '') {
				res.pg.push('pgSchema');
			}

			res.pg.push(...idxImports);
			res.pg.push(...fkImpots);
			res.pg.push(...pkImports);
			res.pg.push(...uniqueImports);
			res.pg.push(...policiesImports);
			res.pg.push(...checkImports);

			const columnImports = Object.values(it.columns)
				.map((col) => {
					let patched: string = (importsPatch[col.type] || col.type).replace('[]', '');
					patched = patched === 'double precision' ? 'doublePrecision' : patched;
					patched = patched.startsWith('varchar(') ? 'varchar' : patched;
					patched = patched.startsWith('char(') ? 'char' : patched;
					patched = patched.startsWith('numeric(') ? 'numeric' : patched;
					patched = patched.startsWith('time(') ? 'time' : patched;
					patched = patched.startsWith('timestamp(') ? 'timestamp' : patched;
					patched = patched.startsWith('vector(') ? 'vector' : patched;
					patched = patched.startsWith('geometry(') ? 'geometry' : patched;
					return patched;
				})
				.filter((type) => {
					return pgImportsList.has(type);
				});

			res.pg.push(...columnImports);
			return res;
		},
		{ pg: [] as string[] },
	);

	Object.values(schema.views).forEach((it) => {
		if (it.schema && it.schema !== 'public' && it.schema !== '') {
			imports.pg.push('pgSchema');
		} else if (it.schema === 'public') {
			it.materialized ? imports.pg.push('pgMaterializedView') : imports.pg.push('pgView');
		}

		Object.values(it.columns).forEach(() => {
			const columnImports = Object.values(it.columns)

Domain

Subdomains

Frequently Asked Questions

What does schemaToTypeScript() do?
schemaToTypeScript() is a function in the drizzle-orm codebase, defined in drizzle-kit/src/introspect-pg.ts.
Where is schemaToTypeScript() defined?
schemaToTypeScript() is defined in drizzle-kit/src/introspect-pg.ts at line 309.
What does schemaToTypeScript() call?
schemaToTypeScript() calls 13 function(s): createTableChecks, createTableColumns, createTableFKs, createTableIndexes, createTablePKs, createTablePolicies, createTableUniques, isCyclic, and 5 more.

Analyze Your Own Codebase

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

Try Supermodel Free