0
0
NestJSframework~10 mins

Entity definition in NestJS - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a basic entity class with a decorator.

NestJS
import { [1] } from 'typeorm';

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

  @Column()
  name: string;
}
Drag options to blanks, or click blank then click option'
AController
BEntity
CModule
DInjectable
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Controller or @Module instead of @Entity
Forgetting to import the decorator from 'typeorm'
2fill in blank
medium

Complete the code to define a primary key column in the entity.

NestJS
import { Entity, [1], Column } from 'typeorm';

@Entity()
export class Product {
  @[1]()
  id: number;

  @Column()
  title: string;
}
Drag options to blanks, or click blank then click option'
APrimaryGeneratedColumn
BPrimaryColumn
CGenerated
DCreateDateColumn
Attempts:
3 left
💡 Hint
Common Mistakes
Using @PrimaryColumn without generation
Confusing with @CreateDateColumn which is for timestamps
3fill in blank
hard

Fix the error in the entity property decorator to correctly define a string column.

NestJS
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';

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

  @Column({ type: '[1]' })
  name: string;
}
Drag options to blanks, or click blank then click option'
Avarchar
Binteger
Cboolean
Ddate
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'integer' or 'boolean' for string fields
Leaving type undefined causing default assumptions
4fill in blank
hard

Fill both blanks to define a nullable column with a default value.

NestJS
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';

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

  @Column({ [1]: true, [2]: 'draft' })
  status: string;
}
Drag options to blanks, or click blank then click option'
Anullable
Bdefault
Cunique
Dlength
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'unique' or 'length' instead of 'nullable' or 'default'
Forgetting to set nullable true for optional fields
5fill in blank
hard

Fill all three blanks to define a one-to-many relationship between entities.

NestJS
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;
}
Drag options to blanks, or click blank then click option'
AOneToMany
BManyToOne
CJoinColumn
DManyToMany
Attempts:
3 left
💡 Hint
Common Mistakes
Using ManyToMany instead of OneToMany/ManyToOne
Forgetting to import JoinColumn or use it on the owning side