命名策略
将实体映射到数据库表和列时,它们的名称将由命名策略定义。我们有 3 种基本命名策略可供选择:
¥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
- 所有 SQL 驱动程序的默认值¥
UnderscoreNamingStrategy
- default of all SQL drivers -
MongoNamingStrategy
-MongoDriver
的默认值¥
MongoNamingStrategy
- default ofMongoDriver
-
EntityCaseNamingStrategy
- 使用未更改的实体和属性名称¥
EntityCaseNamingStrategy
- uses unchanged entity and property names
你可以在初始化 ORM 时覆盖此行为。你还可以提供我们自己的命名策略,只需实现 NamingStrategy
接口并在引导 ORM 时提供我们的实现:
¥You 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,
...
});
你还可以扩展
AbstractNamingStrategy
,它为我们实现了一种方法 -getClassName()
用于将实体文件名映射到类名。¥You can also extend
AbstractNamingStrategy
which implements one method for we -getClassName()
that is used to map entity file name to class name.
mongo 驱动程序中的命名策略
¥Naming Strategy in mongo driver
MongoNamingStrategy
将仅使用定义的所有字段名称。集合名称将转换为小写的虚线形式:
¥MongoNamingStrategy
will simply use all field names as they are defined. Collection names will be translated into lower-cased dashed form:
MyCoolEntity
将被转换为 my-cool-entity
集合名称。
¥MyCoolEntity
will be translated into my-cool-entity
collection name.
SQL 驱动程序中的命名策略
¥Naming Strategy in SQL drivers
MySqlDriver
默认为 UnderscoreNamingStrategy
,这意味着我们所有的数据库表和列都将小写,单词之间用下划线分隔:
¥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 API
NamingStrategy.getClassName(file: string, separator?: string): string
根据文件名返回类的名称。
¥Return a name of the class based on its file name.
NamingStrategy.classToTableName(entityName: string): string
返回实体类的表名。
¥Return a table name for an entity class.
NamingStrategy.getEntityName(tableName: string, schemaName?: string): string
根据数据库表名返回实体类的名称(用于 EntityGenerator
)。
¥Return a name of the entity class based on database table name (used in EntityGenerator
).
默认实现忽略架构名称,但检测到重复时,名称将自动添加前缀。
¥Default implementation ignores the schema name, but when duplicates are detected, the name will be prefixed automatically.
NamingStrategy.propertyToColumnName(propertyName: string): string
返回属性的列名(用于 EntityGenerator
)。
¥Return a column name for a property (used in EntityGenerator
).
NamingStrategy.getEnumClassName(columnName: string, tableName: string, schemaName?: string): string
返回列的枚举类名(用于 EntityGenerator
)。
¥Return an enum class name for a column (used in EntityGenerator
).
NamingStrategy.enumValueToEnumProperty(enumValue: string, columnName: string, tableName: string, schemaName?: string): string
返回枚举值的枚举属性名称(用于 EntityGenerator
)。
¥Return an enum property name for an enum value (used in EntityGenerator
).
NamingStrategy.referenceColumnName(): string
返回默认引用列名。
¥Return the default reference column name.
NamingStrategy.joinColumnName(propertyName: string): string
返回属性的连接列名。
¥Return a join column name for a property.
NamingStrategy.joinTableName(sourceEntity: string, targetEntity: string, propertyName: string): string
返回连接表名。这用作 pivotTable
的默认值。
¥Return a join table name. This is used as default value for pivotTable
.
NamingStrategy.joinKeyColumnName(entityName: string, referencedColumnName?: string): string
返回给定参数的外键列名。
¥Return the foreign key column name for the given parameters.
NamingStrategy.indexName(tableName: string, columns: string[], type: 'primary' | 'foreign' | 'unique' | 'index' | 'sequence'): string
返回给定类型的键/约束名称。某些驱动程序可能不支持所有类型(例如,mysql 和 sqlite 强制使用 PK 名称)。
¥Returns 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): string
返回给定实体的别名。别名需要在整个查询中保持唯一,默认情况下,通过附加索引参数来确保这一点。只要我们确保它是唯一的,就可以选择使用它。
¥Returns 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.
NamingStrategy.inverseSideName(entityName: string, propertyName: string, kind: ReferenceKind): string
返回反向属性的名称。在 EntityGenerator
中使用 bidirectionalRelations
选项。默认实现将根据属性类型而有所不同:
¥Returns the name of the inverse side property. Used in the EntityGenerator
with bidirectionalRelations
option. The default implementation will vary based on the property kind:
-
M:N 关系将被命名为
${propertyName}Inverse
(属性名称是从数据透视表名称推断出来的)。¥M:N relations will be named as
${propertyName}Inverse
(the property name is inferred from pivot table name). -
其他关系类型将使用目标实体名称,第一个字符小写,如果是 1:M 集合,则附加
Collection
。¥Other relation kinds will use the target entity name, with first character lowercased, and
Collection
appended in case it's a 1:M collection.
此行为在 v6.3 中发生了变化,在此之前,所有属性都以
Inverse
后缀命名,就像现在的 M:N 关系一样。¥This behavior changed in v6.3, before that, all the properties were named with the
Inverse
suffix as the M:N relations are now.