Skip to main content
The routes configuration option allows you to customize or exclude individual CRUD routes.

Routes interface

interface RoutesOptions {
  exclude?: BaseRouteName[];
  only?: BaseRouteName[];
  getManyBase?: GetManyRouteOptions;
  getOneBase?: GetOneRouteOptions;
  createOneBase?: CreateOneRouteOptions;
  createManyBase?: CreateManyRouteOptions;
  updateOneBase?: UpdateOneRouteOptions;
  replaceOneBase?: ReplaceOneRouteOptions;
  deleteOneBase?: DeleteOneRouteOptions;
  recoverOneBase?: RecoverOneRouteOptions;
}

Excluding routes

Use exclude to prevent specific routes from being generated:
@Crud({
  model: { type: User },
  routes: {
    exclude: ['createManyBase', 'replaceOneBase'],
  },
})
@Controller('users')
export class UsersController {
  constructor(public service: UsersService) {}
}
This generates all routes except bulk create and replace.

Only specific routes

Use only to generate only specific routes:
@Crud({
  model: { type: User },
  routes: {
    only: ['getOneBase', 'updateOneBase'],
  },
})
@Controller('me')
export class MeController {
  constructor(public service: UsersService) {}
}
This generates only GET and PATCH routes, useful for user profile endpoints.
The exclude and only options are mutually exclusive. Use one or the other, not both.

Available route names

All route names use the Base suffix:
  • getManyBase - GET collection
  • getOneBase - GET single entity
  • createOneBase - POST single entity
  • createManyBase - POST bulk create
  • updateOneBase - PATCH update
  • replaceOneBase - PUT replace
  • deleteOneBase - DELETE
  • recoverOneBase - PATCH recover (soft delete)

Base route options

All route types support these common options:
interceptors
any[]
Array of NestJS interceptors to apply to this route.
decorators
(PropertyDecorator | MethodDecorator)[]
Array of decorators to apply to this route.

Example: Adding interceptors

import { CacheInterceptor } from '@nestjs/cache-manager';
import { LoggingInterceptor } from './logging.interceptor';

@Crud({
  model: { type: User },
  routes: {
    getManyBase: {
      interceptors: [CacheInterceptor],
    },
    createOneBase: {
      interceptors: [LoggingInterceptor],
    },
  },
})

Example: Adding decorators

import { UseGuards } from '@nestjs/common';
import { AdminGuard } from './admin.guard';

@Crud({
  model: { type: User },
  routes: {
    deleteOneBase: {
      decorators: [UseGuards(AdminGuard)],
    },
  },
})

Create route options

createOneBase

createOneBase.returnShallow
boolean
default:false
When true, returns only the created entity without relations.
@Crud({
  model: { type: User },
  routes: {
    createOneBase: {
      returnShallow: true,
    },
  },
})

createManyBase

createManyBase.interceptors
any[]
Interceptors for bulk create operations.
createManyBase.decorators
(PropertyDecorator | MethodDecorator)[]
Decorators for bulk create operations.

Update route options

updateOneBase

updateOneBase.allowParamsOverride
boolean
default:false
When true, allows request body to override URL parameters.
updateOneBase.returnShallow
boolean
default:false
When true, returns only the updated entity without relations.
@Crud({
  model: { type: User },
  routes: {
    updateOneBase: {
      allowParamsOverride: false,
      returnShallow: true,
    },
  },
})
Setting allowParamsOverride: true can be a security risk. It allows clients to modify path parameter fields like IDs through the request body.

replaceOneBase

replaceOneBase.allowParamsOverride
boolean
default:false
When true, allows request body to override URL parameters.
replaceOneBase.returnShallow
boolean
default:false
When true, returns only the replaced entity without relations.

Delete route options

deleteOneBase

deleteOneBase.returnDeleted
boolean
default:false
When true, returns the deleted entity in the response.
@Crud({
  model: { type: Device },
  routes: {
    deleteOneBase: {
      returnDeleted: true,
    },
  },
})
Without returnDeleted:
{}
With returnDeleted: true:
{
  "id": 123,
  "name": "My Device",
  "deviceKey": "550e8400-e29b-41d4-a716-446655440000"
}

Recover route options

recoverOneBase

recoverOneBase.returnRecovered
boolean
default:false
When true, returns the recovered entity in the response.
@Crud({
  model: { type: User },
  query: {
    softDelete: true,
  },
  routes: {
    recoverOneBase: {
      returnRecovered: true,
    },
  },
})
The recoverOneBase route is only available when query.softDelete is enabled.

Get route options

getManyBase and getOneBase

These routes support the base options (interceptors and decorators) but have no additional specific options.
@Crud({
  model: { type: User },
  routes: {
    getManyBase: {
      interceptors: [CacheInterceptor],
      decorators: [UseGuards(AuthGuard)],
    },
    getOneBase: {
      decorators: [UseGuards(AuthGuard)],
    },
  },
})

Complete example

import { Controller, UseGuards, UseInterceptors } from '@nestjs/common';
import { Crud } from '@nestjsx/crud';
import { AuthGuard } from './auth.guard';
import { AdminGuard } from './admin.guard';
import { LoggingInterceptor } from './logging.interceptor';
import { User } from './user.entity';
import { UsersService } from './users.service';

@Crud({
  model: { type: User },
  routes: {
    // Exclude bulk operations
    exclude: ['createManyBase', 'replaceOneBase'],
    
    // Add logging to all creates
    createOneBase: {
      interceptors: [LoggingInterceptor],
      returnShallow: true,
    },
    
    // Secure updates
    updateOneBase: {
      decorators: [UseGuards(AuthGuard)],
      allowParamsOverride: false,
      returnShallow: false,
    },
    
    // Admin-only deletes
    deleteOneBase: {
      decorators: [UseGuards(AuthGuard, AdminGuard)],
      returnDeleted: true,
    },
  },
})
@Controller('users')
export class UsersController {
  constructor(public service: UsersService) {}
}

Route HTTP methods and paths

Here’s how each route maps to HTTP methods:
Route NameHTTP MethodPathDescription
getManyBaseGET/Get multiple entities
getOneBaseGET/:idGet single entity
createOneBasePOST/Create one entity
createManyBasePOST/bulkCreate multiple entities
updateOneBasePATCH/:idPartial update
replaceOneBasePUT/:idFull replacement
deleteOneBaseDELETE/:idDelete entity
recoverOneBasePATCH/:id/recoverRecover soft-deleted entity

Use cases

routes: {
  only: ['getManyBase', 'getOneBase'],
}
routes: {
  exclude: ['createManyBase'],
}
routes: {
  only: ['getOneBase', 'updateOneBase'],
},
params: {
  id: {
    primary: true,
    disabled: true,  // ID from auth context
  },
}
routes: {
  deleteOneBase: {
    decorators: [UseGuards(AdminGuard)],
    returnDeleted: true,
  },
}