Chuyển dữ liệu JSON thành class entity của TypeORM: một class @Entity() có khóa chính, và mọi key khác thành property @Column() kèm kiểu TypeScript, khả năng null và decorator của class-validator.
Dùng nó để tạo entity đầu tiên cho TypeOrmModule.forFeature([...]) trong app NestJS + TypeORM, hoặc khi thêm DataSource của TypeORM vào dự án Express và cần entity khớp với dữ liệu bạn đã có.
Tên model (mặc định MyModel); support ticket thành SupportTicket.support-ticket.entity.ts và thêm vào mảng entities của DataSource hoặc vào TypeOrmModule.forFeature([SupportTicket]) trong module NestJS.[
{ "subject": "Login fails on Safari", "priority": 2, "assignee": "jdoe", "closed": false, "opened_at": "2024-06-03T08:12:00Z" },
{ "subject": "Export CSV is empty", "priority": 1, "closed": true, "opened_at": "2024-06-04T14:40:00Z" }
]import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';
import { IsNotEmpty, IsOptional } from 'class-validator';
@Entity()
export class SupportTicket {
@PrimaryGeneratedColumn()
id: number;
@Column()
@IsNotEmpty()
subject: string;
@Column()
@IsNotEmpty()
priority: number;
@Column({ nullable: true })
@IsOptional()
assignee: string | null;
@Column()
@IsNotEmpty()
closed: boolean;
@Column({ name: 'opened_at' })
@IsNotEmpty()
openedAt: Date;
}Ticket thứ hai không có assignee, nên cột đó là @Column({ nullable: true }) với kiểu string | null. opened_at thành property openedAt kiểu Date, với name: 'opened_at' giữ tên cột gốc.
id hoặc id là số nguyên → @PrimaryGeneratedColumn() id: number; id vượt phạm vi 32 bit → @PrimaryGeneratedColumn({ type: 'bigint' }) id: string; id dạng chuỗi như UUID → @PrimaryColumn() id: string.@Column() mặc định, số thập phân nhận type: 'double precision' để phần lẻ không bị cắt, cột bigint và cột chỉ có ngày date có kiểu string (TypeORM trả về dạng văn bản), ngày giờ là Date.simple-json với kiểu Record<string, unknown> hoặc mảng có kiểu như string[]."007" vẫn là cột string.@Column({ name: 'opened_at' }) được thêm mỗi khi key JSON khác đi.null hoặc bị thiếu nhận nullable: true, | null và @IsOptional(); số còn lại nhận @IsNotEmpty().Thay file ticket.entity.ts trống do nest g resource tickets tạo bằng output này; entity khi đó khớp với dữ liệu giả mà frontend đang dùng, và Repository<SupportTicket> sẵn sàng sử dụng.
Sau khi tạo entity, typeorm migration:generate so sánh nó với database và viết migration CREATE TABLE, kể cả tên cột và khả năng null.
simple-json lưu JSON dưới dạng văn bản, nên bạn không thể lọc theo giá trị bên trong. Nếu cần truy vấn chúng, hãy đổi sang type: 'jsonb' trên PostgreSQL hoặc type: 'json' trên MySQL.double precision là kiểu dấu phẩy động; với giá tiền hãy dùng @Column('decimal', { precision: 10, scale: 2 }), được TypeORM trả về dạng chuỗi.@ManyToOne hoặc @OneToMany.@Entity() không có tên, nên cách đặt tên mặc định của TypeORM biến tên class thành snake_case: support_ticket. Với bảng có sẵn, hãy truyền tên vào, ví dụ @Entity('tickets').
emitDecoratorMetadata bị tắt thì sao?@Column() không có option đọc kiểu từ reflection metadata, nên TypeORM sẽ báo lỗi kiểu dữ liệu. Hãy bật nó trong tsconfig hoặc ghi rõ từng kiểu, ví dụ @Column('varchar').
Date nhận kiểu gì trong database?datetime trên MySQL và timestamp without time zone trên PostgreSQL. Nếu múi giờ quan trọng trên PostgreSQL, hãy đổi sang @Column('timestamptz').