4.7 KiB
Table columns
Entity consist of columns. For each entity column, column in the database will be created.
- @Column decorator
- @PrimaryColumn decorator
- @CreateDateColumn decorator
- @UpdateDateColumn decorator
- Column types
- Column options
- Columns usage example
@Column
Column decorator simply marks an entity property to be a table column. There are several column decorator signatures:
@Column(options?: ColumnOptions)
@Column(type?: ColumnType, options?: ColumnOptions)
@PrimaryColumn
PrimaryColumn marks an entity property as a column and creates a primary key for it. There are several column decorator signatures:
@PrimaryColumn(options?: ColumnOptions)
@PrimaryColumn(type?: ColumnType, options?: ColumnOptions)
@CreateDateColumn
CreateDateColumn adds a simple datetime column to the table. During its first persistence (e.g. insertion) it sets current date as a value of the property object.
@CreateDateColumn(options?: ColumnOptions)
@UpdateDateColumn
UpdateDateColumn adds a simple datetime column to the table. Each time object is persisted, this column value is updated to the current date.
@CreateDateColumn(options?: ColumnOptions)
ColumnType
ColumnType can be one of:
stringwill be mapped to db'svarchartextwill be mapped to db'stextnumberwill be mapped to db'sdoubleintegerwill be mapped to db'sintintwill be mapped to db'sintsmallintwill be mapped to db'sintbigintwill be mapped to db'sintfloatwill be mapped to db'sfloatdoublewill be mapped to db'sdoubledecimalwill be mapped to db'sdecimaldatewill be mapped to db'sdatetimetimewill be mapped to db'stimedatetimewill be mapped to db'sdatetimebooleanwill be mapped to db'sbooleanjsonwill be mapped to db'stextsimple_arraywill be mapped to db'stext
If you omit a column type, type will be guessed automatically based on variable type:
numberwill be mapped tofloatbooleanwill be mapped tobooleanstringwill be mapped tovarcharDatewill be mapped todatetime
ColumnOptions
ColumnOptions is an object with additional column options:
name?: string- column name in the databasetype?: ColumnType- column type also can be specified via column optionslength?: string- column type's length. For example type = "string" and length = 100 means that ORM will create a column with type varchar(100).autoIncrement?: boolean- specifies if this column will use AUTO_INCREMENT or not (e.g. generated number)unique?: boolean- specifies if column's value must be unique or not.nullable?: boolean- indicates if column's value can be set to NULL.columnDefinition?: string- Extra column definition. Should be used only in emergency situations. Note that if you'll use this property auto schema generation will not work properly anymore.comment?: string- column commentprecision?: number- The precision for a decimal (exact numeric) column (applies only for decimal column), which is the maximum number of digits that are stored for the values.scale?: number- The scale for a decimal (exact numeric) column (applies only for decimal column), which represents the number of digits to the right of the decimal point and must not be greater than precision.collation?: string- Column collation. Note that not all databases support it.
Example
@Table("photo")
class Photo {
/**
* Primary column with auto increment key.
*/
@PrimaryColumn("int", { autoIncrement: true })
id: number;
/**
* Simple string column.
*/
@Column()
name: string;
/**
* Simple boolean column.
*/
@Column()
isPublished: boolean;
/**
* Simple numeric (float) column.
*/
@Column()
scale: number;
/**
* Simple numeric (integer) column.
*/
@Column("integer")
size: number;
/**
* Simple column that contains a date.
*/
@Column()
publishedDate: Date;
/**
* Simple column that contains a big text.
*/
@Column("text")
description: string;
/**
* Simple column that contains a short text.
*/
@Column({
length: 3
})
locale: string;
/**
* This column's value must be unique.
*/
@Column({
unique: true
})
slug: string;
/**
* This column's value can be nullable.
*/
@Column({
nullable: true
})
metadata: string;
}