@mikro-orm/core
Version:
TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.
26 lines (25 loc) • 1.12 kB
JavaScript
import { AbstractNamingStrategy } from './AbstractNamingStrategy.js';
/** Naming strategy that converts camelCase names to snake_case for table and column names. */
export class UnderscoreNamingStrategy extends AbstractNamingStrategy {
classToTableName(entityName, tableName) {
return tableName ?? this.underscore(entityName);
}
joinColumnName(propertyName) {
return this.underscore(propertyName) + '_' + this.referenceColumnName();
}
joinKeyColumnName(entityName, referencedColumnName, composite, tableName) {
return this.classToTableName(entityName, tableName) + '_' + (referencedColumnName || this.referenceColumnName());
}
joinTableName(sourceEntity, targetEntity, propertyName, tableName) {
return this.classToTableName(sourceEntity, tableName) + '_' + this.classToTableName(propertyName);
}
propertyToColumnName(propertyName, object) {
return this.underscore(propertyName);
}
referenceColumnName() {
return 'id';
}
underscore(name) {
return name.replace(/([a-z])([A-Z])/g, '$1_$2').toLowerCase();
}
}