docs/versioned_docs/version-5.9/naming-strategy.md
When mapping our entities to database tables and columns, their names will be defined by naming strategy. There are 3 basic naming strategies we can choose from:
UnderscoreNamingStrategy - default of all SQL driversMongoNamingStrategy - default of MongoDriverEntityCaseNamingStrategy - uses unchanged entity and property namesYou can override this when initializing ORM. You can also provide our own naming strategy, just implement NamingStrategy interface and provide our implementation when bootstrapping ORM:
class MyCustomNamingStrategy implements NamingStrategy {
...
}
const orm = await MikroORM.init({
...
namingStrategy: MyCustomNamingStrategy,
...
});
You can also extend
AbstractNamingStrategywhich implements one method for we -getClassName()that is used to map entity file name to class name.
MongoNamingStrategy will simply use all field names as they are defined. Collection names will be translated into lower-cased dashed form:
MyCoolEntity will be translated into my-cool-entity collection name.
MySqlDriver defaults to UnderscoreNamingStrategy, which means our all our database tables and columns will be lower-cased and words divided by underscored:
CREATE TABLE `author` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`created_at` datetime(3) DEFAULT NULL,
`updated_at` datetime(3) DEFAULT NULL,
`terms_accepted` tinyint(1) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
`born` datetime DEFAULT NULL,
`favourite_book_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
NamingStrategy.getClassName(file: string, separator?: string): stringReturn a name of the class based on its file name.
NamingStrategy.classToTableName(entityName: string): stringReturn a table name for an entity class.
NamingStrategy.propertyToColumnName(propertyName: string): stringReturn a column name for a property.
NamingStrategy.referenceColumnName(): stringReturn the default reference column name.
NamingStrategy.joinColumnName(propertyName: string): stringReturn a join column name for a property.
NamingStrategy.joinTableName(sourceEntity: string, targetEntity: string, propertyName: string): stringReturn a join table name. This is used as default value for pivotTable.
NamingStrategy.joinKeyColumnName(entityName: string, referencedColumnName?: string): stringReturn the foreign key column name for the given parameters.
NamingStrategy.indexName(tableName: string, columns: string[], type: 'primary' | 'foreign' | 'unique' | 'index' | 'sequence'): stringReturns key/constraint name for given type. Some drivers might not support all the types (e.g. mysql and sqlite enforce the PK name).
NamingStrategy.aliasName(entityName: string, index: number): stringReturns alias name for given entity. The alias needs to be unique across the query, which is by default ensured via appended index parameter. It is optional to use it as long as we ensure it will be unique.