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

casing.ts — drizzle-orm Source File

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

File typescript DrizzleORM RelationalQuery 6 imports 3 functions 1 classes

Entity Profile

Dependency Diagram

graph LR
  f75147d3_9ada_e5d8_9b05_204b10aba05f["casing.ts"]
  bc6d807f_7198_da43_c1e6_911af80c80ee["entity.ts"]
  f75147d3_9ada_e5d8_9b05_204b10aba05f --> bc6d807f_7198_da43_c1e6_911af80c80ee
  ddbb35ab_7e67_d2b6_96ec_1f37678fcb67["table.ts"]
  f75147d3_9ada_e5d8_9b05_204b10aba05f --> ddbb35ab_7e67_d2b6_96ec_1f37678fcb67
  6f4580c8_dbd3_725b_5126_f06988ed43ab["Table"]
  f75147d3_9ada_e5d8_9b05_204b10aba05f --> 6f4580c8_dbd3_725b_5126_f06988ed43ab
  84d0ea35_e4ab_0d94_802f_66e4bc7f77eb["Table"]
  f75147d3_9ada_e5d8_9b05_204b10aba05f --> 84d0ea35_e4ab_0d94_802f_66e4bc7f77eb
  99737bc3_a631_a054_9291_f966c791930f["utils.ts"]
  f75147d3_9ada_e5d8_9b05_204b10aba05f --> 99737bc3_a631_a054_9291_f966c791930f
  05f0a280_d0c9_693a_a4bf_83cc671012d2["column.ts"]
  f75147d3_9ada_e5d8_9b05_204b10aba05f --> 05f0a280_d0c9_693a_a4bf_83cc671012d2
  style f75147d3_9ada_e5d8_9b05_204b10aba05f fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import type { Column } from '~/column.ts';
import { entityKind } from './entity.ts';
import { Table } from './table.ts';
import type { Casing } from './utils.ts';

export function toSnakeCase(input: string) {
	const words = input
		.replace(/['\u2019]/g, '')
		.match(/[\da-z]+|[A-Z]+(?![a-z])|[A-Z][\da-z]+/g) ?? [];

	return words.map((word) => word.toLowerCase()).join('_');
}

export function toCamelCase(input: string) {
	const words = input
		.replace(/['\u2019]/g, '')
		.match(/[\da-z]+|[A-Z]+(?![a-z])|[A-Z][\da-z]+/g) ?? [];

	return words.reduce((acc, word, i) => {
		const formattedWord = i === 0 ? word.toLowerCase() : `${word[0]!.toUpperCase()}${word.slice(1)}`;
		return acc + formattedWord;
	}, '');
}

function noopCase(input: string) {
	return input;
}

export class CasingCache {
	static readonly [entityKind]: string = 'CasingCache';

	/** @internal */
	cache: Record<string, string> = {};
	private cachedTables: Record<string, true> = {};
	private convert: (input: string) => string;

	constructor(casing?: Casing) {
		this.convert = casing === 'snake_case'
			? toSnakeCase
			: casing === 'camelCase'
			? toCamelCase
			: noopCase;
	}

	getColumnCasing(column: Column): string {
		if (!column.keyAsName) return column.name;

		const schema = column.table[Table.Symbol.Schema] ?? 'public';
		const tableName = column.table[Table.Symbol.OriginalName];
		const key = `${schema}.${tableName}.${column.name}`;

		if (!this.cache[key]) {
			this.cacheTable(column.table);
		}
		return this.cache[key]!;
	}

	private cacheTable(table: Table) {
		const schema = table[Table.Symbol.Schema] ?? 'public';
		const tableName = table[Table.Symbol.OriginalName];
		const tableKey = `${schema}.${tableName}`;

		if (!this.cachedTables[tableKey]) {
			for (const column of Object.values(table[Table.Symbol.Columns])) {
				const columnKey = `${tableKey}.${column.name}`;
				this.cache[columnKey] = this.convert(column.name);
			}
			this.cachedTables[tableKey] = true;
		}
	}

	clearCache() {
		this.cache = {};
		this.cachedTables = {};
	}
}

Domain

Subdomains

Classes

Dependencies

Frequently Asked Questions

What does casing.ts do?
casing.ts is a source file in the drizzle-orm codebase, written in typescript. It belongs to the DrizzleORM domain, RelationalQuery subdomain.
What functions are defined in casing.ts?
casing.ts defines 3 function(s): noopCase, toCamelCase, toSnakeCase.
What does casing.ts depend on?
casing.ts imports 6 module(s): Table, Table, column.ts, entity.ts, table.ts, utils.ts.
Where is casing.ts in the architecture?
casing.ts is located at drizzle-orm/src/casing.ts (domain: DrizzleORM, subdomain: RelationalQuery, directory: drizzle-orm/src).

Analyze Your Own Codebase

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

Try Supermodel Free