返回
Featured image of post TypeORM - Entites

TypeORM - Entites

Entites 是類型 Mapping to DB Table 的實體。

20220930-備註更新 此篇文章為 0.2.x


在實作架構中的 Model 可以將所需的資料寫成 Class 存取

範例 - ORM 對應 DB 存取

Models Class

export class Photo {
    id: number;
    name:string;
    description: string;
    filename: string;
    views: number;
    isPublished: boolean;
}

導入 Entity 和 Table Column

import { Entity } from "typeorm";

@Entity()
export class Photo {

    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    name: string;

    @Column()
    description: string;

    @Column()
    filename: string;

    @Column()
    views: number;

    @Column()
    isPublished: boolean;
}

Column 寫法

    @Column({ comment: '欄位註解',type: "int", width: 200 , default: 0 })

Column types for mysql / mariadb

bit, int, integer, tinyint, smallint, mediumint, bigint, float, double,
double precision, dec, decimal, numeric, fixed, bool, boolean, date,
datetime, timestamp, time, year, char, nchar, national char, varchar,
nvarchar, national varchar, text, tinytext, mediumtext, blob, longtext,
tinyblob, mediumblob, longblob, enum, set, json, binary, varbinary,
geometry, point, linestring, polygon, multipoint, multilinestring,
multipolygon, geometrycollection
Licensed under CC BY-NC-SA 4.0
comments powered by Disqus