装饰器参考手册
本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →
实体装饰器
@Entity
将模型标记为实体。实体是会被转换为数据库表的类。 你可以在实体中指定表名:
@Entity("users")
export class User {}
这段代码将创建名为 "users" 的数据库表。
你还可以指定其他实体选项:
-
name- 表名。如果未指定,则根据实体类名生成。 -
database- 所选 DB 服务器中的数据库名称。 -
schema- 模式名称。 -
engine- 建表时使用的数据库引擎(仅部分数据库支持)。 -
synchronize- 标记为false的实体将跳过模式更新。 -
orderBy- 为find操作和QueryBuilder指定实体默认排序方式。
示例:
@Entity({
name: "users",
engine: "MyISAM",
database: "example_dev",
schema: "schema_with_best_tables",
synchronize: false,
orderBy: {
name: "ASC",
id: "DESC",
},
})
export class User {}
了解更多关于实体的内容。
@ViewEntity
视图实体是映射到数据库视图的类。
@ViewEntity() 接受以下选项:
-
name- 视图名称。如果未指定,则根据实体类名生成。 -
database- 所选 DB 服务器中的数据库名称。 -
schema- 模式名称。 -
expression- 视图定义。必填参数。
expression 可以是正确转义列名和表名的字符串(以 postgres 为例):
@ViewEntity({
expression: `
SELECT "post"."id" "id", "post"."name" AS "name", "category"."name" AS "categoryName"
FROM "post" "post"
LEFT JOIN "category" "category" ON "post"."categoryId" = "category"."id"
`,
})
export class PostCategory {}
或是 QueryBuilder 的实例
@ViewEntity({
expression: (dataSource: DataSource) =>
dataSource
.createQueryBuilder()
.select("post.id", "id")
.addSelect("post.name", "name")
.addSelect("category.name", "categoryName")
.from(Post, "post")
.leftJoin(Category, "category", "category.id = post.categoryId"),
})
export class PostCategory {}
注意: 由于驱动程序限制,不支持参数绑定。请改用字面量参数。
@ViewEntity({
expression: (dataSource: DataSource) =>
dataSource
.createQueryBuilder()
.select("post.id", "id")
.addSelect("post.name", "name")
.addSelect("category.name", "categoryName")
.from(Post, "post")
.leftJoin(Category, "category", "category.id = post.categoryId")
.where("category.name = :name", { name: "Cars" }) // <-- this is wrong
.where("category.name = 'Cars'"), // <-- and this is right
})
export class PostCategory {}
了解更多关于视图实体的内容。
列装饰器
@Column
将实体中的属性标记为表列。 示例:
@Entity("users")
export class User {
@Column({ primary: true })
id: number
@Column({ type: "varchar", length: 200, unique: true })
firstName: string
@Column({ nullable: true })
lastName: string
@Column({ default: false })
isActive: boolean
}
@Column 接受多个可用选项:
-
type: ColumnType- 列类型,支持所有列类型。 -
name: string- 数据库表中的列名。默认根据属性名生成,可自定义覆盖。 -
length: string|number- 列类型长度。例如创建varchar(150)需同时指定类型和长度选项。 -
width: number- 列类型的显示宽度(仅适用于 MySQL 整数类型)。新版本 MySQL 已弃用,TypeORM 将在后续版本移除该选项。 -
onUpdate: string-ON UPDATE触发器(仅 MySQL 支持) -
nullable: boolean- 决定列是否允许为NULL(默认nullable: false表示必须为NOT NULL)。 -
update: boolean- 指示列值是否通过 "save" 操作更新。设为 false 时,该值仅在首次插入对象时可写(默认为true)。 -
insert: boolean- 指示列值是否在首次插入对象时设置。默认值true。 -
select: boolean- 定义默认查询时是否隐藏该列。设为false时,标准查询将不返回此列数据。默认为select: true。 -
default: string- 数据库级别的列默认值(DEFAULT)。 -
primary: boolean- 将列标记为主键(功能等同于@PrimaryColumn)。 -
unique: boolean- 将列标记为唯一列(创建唯一约束)。默认值为 false。 -
comment: string- 数据库列注释(并非所有数据库类型都支持)。 -
precision: number- 十进制(精确数值)列的精度(仅适用于 decimal 列),表示存储数值的最大位数。用于某些列类型。 -
scale: number- 十进制(精确数值)列的小数位数(仅适用于 decimal 列),表示小数点右侧的位数且不得超过精度值。用于某些列类型。 -
zerofill: boolean- 为数值列添加ZEROFILL属性(仅 MySQL 支持)。设为true时 MySQL 会自动添加UNSIGNED属性。新版本 MySQL 已弃用,TypeORM 将在后续版本移除该选项。建议改用字符列和LPAD函数。 -
unsigned: boolean- 为数值列添加UNSIGNED属性(仅 MySQL 支持)。 -
charset: string- 定义列字符集(并非所有数据库类型都支持)。 -
collation: string- 定义列排序规则。 -
enum: string[]|AnyEnum- 用于enum列类型,指定允许的枚举值列表(可传入值数组或枚举类)。 -
enumName: string- 生成的枚举类型名称。未指定时 TypeORM 会根据实体和列名生成,如需跨表复用相同枚举类 型则必须指定。 -
primaryKeyConstraintName: string- 主键约束名称。未指定时根据表名和相关列名生成。 -
generatedType: "VIRTUAL"|"STORED"- 生成列类型(仅支持 MySQL 和 Postgres(仅 "STORED"))。 -
hstoreType: "object"|"string"-HSTORE列返回类型(返回字符串或对象,仅 Postgres 支持)。 -
array: boolean- 用于支持数组类型的列(如 int[]),适用于 postgres 和 cockroachdb。 -
transformer: ValueTransformer|ValueTransformer[]- 指定用于读写数据库时对该列进行值(解)编组的转换器(或转换器数组)。对于数组,值转换器将按自然顺序从 entityValue 到 databaseValue 应用,并按相反顺序从 databaseValue 到 entityValue 应用。 -
spatialFeatureType: string- 可选的要素类型(Point、Polygon、LineString、Geometry),用作空间列的约束。如果未指定,其行为将如同提供 了Geometry。仅 PostgreSQL 和 CockroachDB 支持。 -
srid: number- 可选的空间参考标识符,用作空间列的约束。如果未指定,则默认为0。标准地理坐标(WGS84 基准面中的纬度/经度)对应 EPSG 4326。仅 PostgreSQL 和 CockroachDB 支持。
深入了解实体列。
@PrimaryColumn
将实体属性标记为表主列。功能与 @Column 装饰器相同,但会自动设置 primary 选项为 true。
示例:
@Entity()
export class User {
@PrimaryColumn()
id: number
}
@PrimaryColumn() 支持自定义主键约束名称:
@Entity()
export class User {
@PrimaryColumn({ primaryKeyConstraintName: "pk_user_id" })
id: number
}
注意:当多个主列使用
primaryKeyConstraintName时,所有主列必须使用相同的约束名称。
深入了解实体列。