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

schemaToTypeScript() — drizzle-orm Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  194539cf_d577_ab5e_4ea9_ec2fd5ee230b["schemaToTypeScript()"]
  1198bdc6_ac5c_88c0_dda6_b8caf0f5d9fa["introspect-mysql.ts"]
  194539cf_d577_ab5e_4ea9_ec2fd5ee230b -->|defined in| 1198bdc6_ac5c_88c0_dda6_b8caf0f5d9fa
  18b2fbda_f71a_e879_de9e_4a8232ffcaa6["prepareCasing()"]
  194539cf_d577_ab5e_4ea9_ec2fd5ee230b -->|calls| 18b2fbda_f71a_e879_de9e_4a8232ffcaa6
  66cb9854_27ce_f964_9c23_6dd6fd021ba0["createTableColumns()"]
  194539cf_d577_ab5e_4ea9_ec2fd5ee230b -->|calls| 66cb9854_27ce_f964_9c23_6dd6fd021ba0
  6f3dbbc9_96f5_0c1c_51a1_967c5c835786["isSelf()"]
  194539cf_d577_ab5e_4ea9_ec2fd5ee230b -->|calls| 6f3dbbc9_96f5_0c1c_51a1_967c5c835786
  b0c2da4f_e69e_c657_1b9f_05841197cf08["createTableIndexes()"]
  194539cf_d577_ab5e_4ea9_ec2fd5ee230b -->|calls| b0c2da4f_e69e_c657_1b9f_05841197cf08
  80a6bf84_feb5_5de0_735c_4c6a5accea61["createTableFKs()"]
  194539cf_d577_ab5e_4ea9_ec2fd5ee230b -->|calls| 80a6bf84_feb5_5de0_735c_4c6a5accea61
  ad84f619_11e3_92e1_5a33_30f5f2cbb90d["createTablePKs()"]
  194539cf_d577_ab5e_4ea9_ec2fd5ee230b -->|calls| ad84f619_11e3_92e1_5a33_30f5f2cbb90d
  eeeb2aa7_e00e_d1ed_4dd1_e5eb5a9a90d3["createTableUniques()"]
  194539cf_d577_ab5e_4ea9_ec2fd5ee230b -->|calls| eeeb2aa7_e00e_d1ed_4dd1_e5eb5a9a90d3
  e2f1309f_8662_3a55_f990_926161648340["createTableChecks()"]
  194539cf_d577_ab5e_4ea9_ec2fd5ee230b -->|calls| e2f1309f_8662_3a55_f990_926161648340
  style 194539cf_d577_ab5e_4ea9_ec2fd5ee230b fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

drizzle-kit/src/introspect-mysql.ts lines 133–354

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

	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');
			const pkImports = Object.values(it.compositePrimaryKeys).map(
				(it) => 'primaryKey',
			);
			const uniqueImports = Object.values(it.uniqueConstraints).map(
				(it) => 'unique',
			);
			const checkImports = Object.values(it.checkConstraint).map(
				(it) => 'check',
			);

			res.mysql.push(...idxImports);
			res.mysql.push(...fkImpots);
			res.mysql.push(...pkImports);
			res.mysql.push(...uniqueImports);
			res.mysql.push(...checkImports);

			const columnImports = Object.values(it.columns)
				.map((col) => {
					let patched = importsPatch[col.type] ?? col.type;
					patched = patched.startsWith('varchar(') ? 'varchar' : patched;
					patched = patched.startsWith('char(') ? 'char' : patched;
					patched = patched.startsWith('binary(') ? 'binary' : patched;
					patched = patched.startsWith('decimal(') ? 'decimal' : patched;
					patched = patched.startsWith('smallint(') ? 'smallint' : patched;
					patched = patched.startsWith('enum(') ? 'mysqlEnum' : patched;
					patched = patched.startsWith('datetime(') ? 'datetime' : patched;
					patched = patched.startsWith('varbinary(') ? 'varbinary' : patched;
					patched = patched.startsWith('int(') ? 'int' : patched;
					patched = patched.startsWith('double(') ? 'double' : patched;
					patched = patched.startsWith('float(') ? 'float' : patched;
					patched = patched.startsWith('int unsigned') ? 'int' : patched;
					patched = patched.startsWith('tinyint unsigned') ? 'tinyint' : patched;
					patched = patched.startsWith('smallint unsigned') ? 'smallint' : patched;
					patched = patched.startsWith('mediumint unsigned') ? 'mediumint' : patched;
					patched = patched.startsWith('bigint unsigned') ? 'bigint' : patched;
					return patched;
				})
				.filter((type) => {
					return mysqlImportsList.has(type);
				});

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

	Object.values(schema.views).forEach((it) => {
		imports.mysql.push('mysqlView');

		const columnImports = Object.values(it.columns)
			.map((col) => {
				let patched = importsPatch[col.type] ?? col.type;
				patched = patched.startsWith('varchar(') ? 'varchar' : patched;
				patched = patched.startsWith('char(') ? 'char' : patched;
				patched = patched.startsWith('binary(') ? 'binary' : patched;
				patched = patched.startsWith('decimal(') ? 'decimal' : patched;
				patched = patched.startsWith('smallint(') ? 'smallint' : patched;
				patched = patched.startsWith('enum(') ? 'mysqlEnum' : patched;
				patched = patched.startsWith('datetime(') ? 'datetime' : patched;
				patched = patched.startsWith('varbinary(') ? 'varbinary' : patched;
				patched = patched.startsWith('int(') ? 'int' : patched;
				patched = patched.startsWith('double(') ? 'double' : patched;
				patched = patched.startsWith('float(') ? 'float' : patched;

Domain

Subdomains

Frequently Asked Questions

What does schemaToTypeScript() do?
schemaToTypeScript() is a function in the drizzle-orm codebase, defined in drizzle-kit/src/introspect-mysql.ts.
Where is schemaToTypeScript() defined?
schemaToTypeScript() is defined in drizzle-kit/src/introspect-mysql.ts at line 133.
What does schemaToTypeScript() call?
schemaToTypeScript() calls 8 function(s): createTableChecks, createTableColumns, createTableFKs, createTableIndexes, createTablePKs, createTableUniques, isSelf, prepareCasing.

Analyze Your Own Codebase

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

Try Supermodel Free