TypeORM Basics

Preview — 3 of 10 questions

Which method do you call to configure the global TypeORM connection at the root module of a NestJS application?

javascript
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { User } from './users/user.entity';

@Module({
  imports: [
    TypeOrmModule.forRoot({
      type: 'postgres',
      host: 'localhost',
      port: 5432,
      username: 'postgres',
      password: 'secret',
      database: 'codejump',
      entities: [User],
      synchronize: true, // dev only
    }),
  ],
})
export class AppModule {}
ATypeOrmModule.forFeature()
BDatabaseModule.register()
CTypeOrmModule.connect()
DTypeOrmModule.forRoot()

Which decorator turns a plain TypeScript class into a TypeORM database table mapping?

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

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

  @Column()
  email: string;
}
A@Entity()
B@Model()
C@Table()
D@Schema()

What does @PrimaryGeneratedColumn() do on an entity property?

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

@Entity()
export class Challenge {
  @PrimaryGeneratedColumn() // integer auto-increment
  id: number;

  @PrimaryGeneratedColumn('uuid') // alternative: uuid strategy
  // uuid: string;

  @Column()
  title: string;
}
AMarks the column as a foreign key
BCreates a plain column that must always be set manually
CCreates a column whose value is auto-generated by the database (e.g. auto-increment integer)
DCreates a UUID only

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.