firdaus
firdaus

Reputation: 225

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;
}

Upvotes: 10

Views: 34989

Answers (1)

acesmndr
acesmndr

Reputation: 8515

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

Upvotes: 23

Related Questions