Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define varchar and enum in TypeORM?

I have database structure need to declare the variable into varchar, int, and enum using TypeORM in TypeScript. But in TypeScript doesn't have data type varchar and int. How do I declare it?

Database structure

import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";

@Entity()
export class User {
    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    account_id: varchar;

    @Column()
    email: varchar;

    @Column()
    phone_number: varchar;

    @Column()
    address: varchar;

    @Column()
    status: enum;

    @Column()
    current_id: varchar;
}
like image 464
firdaus Avatar asked Dec 17 '22 16:12

firdaus


1 Answers

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.

import {Entity, Column, PrimaryGeneratedColumn} from "typeorm";

@Entity()
export class Photo {

    @PrimaryGeneratedColumn()
    id: number;

    @Column({
        length: 100
    })
    name: string;

    @Column("text")
    description: string;

    @Column()
    filename: string;

    @Column("double")
    views: number;

    @Column()
    isPublished: boolean;
}

Since you are already using typeorm you can use types defined here: https://github.com/typeorm/typeorm/blob/master/docs/entities.md#column-types

like image 198
acesmndr Avatar answered Dec 31 '22 09:12

acesmndr