Complete the code to define a basic entity class with a decorator.
import { [1] } from 'typeorm'; @Entity() export class User { @PrimaryGeneratedColumn() id: number; @Column() name: string; }
The @Entity() decorator marks the class as a database entity in TypeORM, which NestJS uses for database models.
Complete the code to define a primary key column in the entity.
import { Entity, [1], Column } from 'typeorm'; @Entity() export class Product { @[1]() id: number; @Column() title: string; }
@PrimaryGeneratedColumn() creates a primary key column with auto-generated values, commonly used for IDs.
Fix the error in the entity property decorator to correctly define a string column.
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm'; @Entity() export class Category { @PrimaryGeneratedColumn() id: number; @Column({ type: '[1]' }) name: string; }
The varchar type is used for string columns in SQL databases, which matches the string type in TypeScript.
Fill both blanks to define a nullable column with a default value.
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm'; @Entity() export class Post { @PrimaryGeneratedColumn() id: number; @Column({ [1]: true, [2]: 'draft' }) status: string; }
nullable: true allows the column to have null values. default: 'draft' sets the default value when none is provided.
Fill all three blanks to define a one-to-many relationship between entities.
import { Entity, PrimaryGeneratedColumn, Column, [1], [2], [3] } from 'typeorm'; @Entity() export class Author { @PrimaryGeneratedColumn() id: number; @Column() name: string; @[1](() => Post, post => post.author) posts: Post[]; } @Entity() export class Post { @PrimaryGeneratedColumn() id: number; @Column() title: string; @[2](() => Author, author => author.posts) @[3]() author: Author; }
OneToMany and ManyToOne decorators define the two sides of a one-to-many relationship. JoinColumn is used on the many-to-one side to specify the owning side of the relation.