Performance: Entity definition
Entity definitions affect the initial database schema creation and runtime ORM operations, impacting server response time and query efficiency.
import { Entity, PrimaryGeneratedColumn, Column, OneToMany } from 'typeorm'; @Entity() export class User { @PrimaryGeneratedColumn() id: number; @Column() name: string; @OneToMany(() => Post, post => post.user, { lazy: true }) posts: Promise<Post[]>; }
import { Entity, PrimaryGeneratedColumn, Column, OneToMany } from 'typeorm'; @Entity() export class User { @PrimaryGeneratedColumn() id: number; @Column() name: string; @OneToMany(() => Post, post => post.user, { eager: true }) posts: Post[]; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Eager loading large relations in entity | N/A (server-side) | N/A | N/A | [X] Bad |
| Lazy loading relations on demand | N/A (server-side) | N/A | N/A | [OK] Good |
@Entity() decorator in a NestJS application using TypeORM?@Entity()@Entity() decorator tells NestJS and TypeORM that the class represents a database table.@Column() define table columns, but @Entity() marks the whole class as a table.@Entity() = Table marker [OK]@Entity() means 'this class is a table' [OK]@Entity() with database connection setup@Entity() defines methods or styles@Entity() with @Column()title in a NestJS entity using TypeORM?@Column() usage for a string@Column() without options assumes a string type in TypeORM.string to match the column type.@Column() = string column [OK]@Column() alone for simple string columns [OK]@Column() optionsage column in the database?
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@Column({ type: 'int' })
age: number;
}@Column() decorator on age{ type: 'int' }, which means an integer column in the database.age is a number, matching the integer database type.{ type: 'int' } means integer column [OK]{ type: 'int' } to identify integer columns [OK]int with boolean or date@Column()import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
@Entity()
export class Product {
@PrimaryGeneratedColumn()
id: number;
@Column({ type: 'varchar', length: 100 })
name: number;
}name property is typed as number but the column type is varchar, which expects a string.@Entity() is present, primary key is defined, and imports are correct.number does not match column type varchar -> Option B@Column() type options [OK]@Entity()Order with an auto-generated primary key id, a string customerName limited to 150 characters, and an optional numeric discount that can be null. Which is the correct entity definition?@PrimaryGeneratedColumn() on id: number creates an auto-incrementing integer primary key. A string type would be incorrect without additional configuration like UUID.customerName@Column({ length: 150 }) with string type creates a VARCHAR(150) column. A number type mismatches.discount@Column({ type: 'float', nullable: true }) with optional discount?: number allows null values. Missing nullable: true, wrong type like 'int', or non-optional fails.nullable: true and optional property for nullable columns [OK]Pass the test and this pattern moves unknown