Adding table columns
To add database columns, you simply need to decorate an entity’s properties you want to make into a columnwith a @Column
decorator.
import {Entity, Column} from "typeorm";
@Entity()
export class Photo {
@Column()
id: number;
@Column()
name: string;
@Column()
description: string;
@Column()
filename: string;
@Column()
views: number;
@Column()
isPublished: boolean;
}
Now id
, name
, description
, filename
, views
and isPublished
columns will be added to the photo
table.Column types in the database are inferred from the property types you used, e.g.number
will be converted into integer
, string
into varchar
, boolean
into bool
, etc.But you can use any column type your database supports by implicitly specifying a column type into the @Column
decorator.
We generated a database table with columns, but there is one thing left.Each database table must have a column with a primary key.