Skip to main content
The ModelOptions interface specifies the entity/model class that the CRUD controller will operate on. This is the only required option when configuring a CRUD controller.

Interface Definition

export interface ModelOptions {
  type: any;
}

Properties

type
any
required
The entity class that represents your data model.This should be a TypeORM entity, Mongoose model, or any other ORM entity class that your service layer can work with.
model: { type: User }

Usage Examples

Basic TypeORM Entity

import { Controller } from '@nestjs/common';
import { Crud } from '@nestjsx/crud';
import { User } from './user.entity';
import { UsersService } from './users.service';

@Crud({
  model: {
    type: User,
  },
})
@Controller('users')
export class UsersController {
  constructor(public service: UsersService) {}
}

With TypeORM Entity Definition

user.entity.ts
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';

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

  @Column()
  name: string;

  @Column({ unique: true })
  email: string;

  @Column()
  password: string;

  @Column({ default: true })
  isActive: boolean;
}
users.controller.ts
import { Controller } from '@nestjs/common';
import { Crud } from '@nestjsx/crud';
import { User } from './user.entity';
import { UsersService } from './users.service';

@Crud({
  model: {
    type: User, // Reference to the entity class
  },
  query: {
    exclude: ['password'], // Don't expose password field
  },
})
@Controller('users')
export class UsersController {
  constructor(public service: UsersService) {}
}

With Complete CRUD Configuration

import { Controller } from '@nestjs/common';
import { Crud } from '@nestjsx/crud';
import { Hero } from './hero.entity';
import { HeroesService } from './heroes.service';
import { CreateHeroDto } from './dto/create-hero.dto';
import { UpdateHeroDto } from './dto/update-hero.dto';

@Crud({
  model: {
    type: Hero, // The entity class
  },
  dto: {
    create: CreateHeroDto,
    update: UpdateHeroDto,
  },
  query: {
    limit: 25,
    alwaysPaginate: true,
    join: {
      powers: { eager: true },
    },
  },
})
@Controller('heroes')
export class HeroesController {
  constructor(public service: HeroesService) {}
}

Entity Requirements

The entity class specified in model.type should:
  1. Be decorated with ORM decorators (e.g., @Entity() for TypeORM)
  2. Have a primary key field (e.g., @PrimaryGeneratedColumn() or @PrimaryColumn())
  3. Have columns/properties that map to database fields
  4. Be registered in your ORM module configuration

TypeORM Entity Example

import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn } from 'typeorm';

@Entity('heroes')
export class Hero {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column()
  name: string;

  @Column({ nullable: true })
  description: string;

  @Column({ default: true })
  isActive: boolean;

  @CreateDateColumn()
  createdAt: Date;

  @UpdateDateColumn()
  updatedAt: Date;
}

Mongoose Model Example

import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';

@Schema()
export class Hero extends Document {
  @Prop({ required: true })
  name: string;

  @Prop()
  description: string;

  @Prop({ default: true })
  isActive: boolean;

  @Prop({ default: Date.now })
  createdAt: Date;
}

export const HeroSchema = SchemaFactory.createForClass(Hero);

Notes

The model.type property is the only required field in the entire CrudOptions configuration. All other options have sensible defaults.
Make sure the entity class is properly registered in your ORM module (e.g., TypeOrmModule.forFeature([User]) for TypeORM) before using it in a CRUD controller.

CrudOptions

Main CRUD configuration

ParamsOptions

Configure entity parameters

Build docs developers (and LLMs) love