NestJS is a TypeScript Node.js framework with first-class TypeORM and Prisma support. This guide covers UUID v7 configuration across both ORMs, request validation with ParseUUIDPipe, and Swagger documentation. Background: UUID in Databases explains why UUID v7 outperforms v4 as a primary key.

TypeORM — UUID v7 entity

// order.entity.ts
import { Entity, PrimaryColumn, Column, BeforeInsert } from 'typeorm';
import { v7 as uuidv7 } from 'uuid';

@Entity('orders')
export class Order {
  @PrimaryColumn('uuid')
  id: string;

  @Column()
  status: string;

  @BeforeInsert()
  generateId() {
    if (!this.id) this.id = uuidv7();
  }
}

@BeforeInsert() assigns UUID v7 before every INSERT — works on any database version. On PostgreSQL 18, you can alternatively set the column default in a migration:

// migration — PostgreSQL 18 only
await queryRunner.query(
  `ALTER TABLE orders ALTER COLUMN id SET DEFAULT uuidv7()`
);

Then replace @PrimaryColumn('uuid') with @PrimaryGeneratedColumn('uuid') and the DB handles generation. See PostgreSQL 18 Ships Native UUID v7.

Prisma — UUID v7 schema

// schema.prisma
model Order {
  id     String @id @default(dbgenerated("uuidv7()")) @db.Uuid
  status String
}

Requires PostgreSQL 18+ for uuidv7(). For earlier versions or other databases:

model Order {
  id     String @id @db.Uuid
  status String
}
// orders.service.ts
import { v7 as uuidv7 } from 'uuid';

@Injectable()
export class OrdersService {
  constructor(private prisma: PrismaService) {}

  async create(data: CreateOrderDto) {
    return this.prisma.order.create({
      data: { id: uuidv7(), ...data },
    });
  }
}

See UUID v7 ORM Support in 2026 for the full Prisma v7 configuration.

Request validation with ParseUUIDPipe

// orders.controller.ts
import { Controller, Get, Param, ParseUUIDPipe } from '@nestjs/common';

@Controller('orders')
export class OrdersController {
  @Get(':id')
  findOne(
    @Param('id', new ParseUUIDPipe({ version: '7' })) id: string,
  ) {
    return this.ordersService.findOne(id);
  }
}

ParseUUIDPipe validates format and version. Pass version: '4' or omit the option to accept any version. For routes that accept both v4 and v7 IDs during a migration period, use class-validator’s @IsUUID('all'). See UUID Testing Strategies.

DTO validation with class-validator

import { IsUUID, IsString } from 'class-validator';

export class CreateOrderDto {
  @IsUUID('7')
  customerId: string; // enforce UUID v7 from callers

  @IsString()
  status: string;
}

Swagger / OpenAPI decoration

import { ApiProperty } from '@nestjs/swagger';

export class OrderResponseDto {
  @ApiProperty({
    example: '018fbe3a-4c5d-7b12-8abc-0123456789ab',
    description: 'UUID v7 — time-sortable primary key',
    format: 'uuid',
  })
  id: string;
}

This surfaces correctly in Swagger UI and generates format: uuid in the OpenAPI spec.

Further reading

External references

Frequently asked questions

How do I use UUID v7 as a primary key in NestJS with TypeORM?

TypeORM's @PrimaryGeneratedColumn('uuid') generates UUID v4 by default. To use UUID v7, replace it with @PrimaryColumn('uuid') and set the default in the entity's @BeforeInsert() hook: this.id = uuidv7(). Alternatively, set the PostgreSQL column default to uuidv7() on PostgreSQL 18 using a migration. See UUID in Spring Boot for the equivalent Java/Hibernate pattern.

How do I validate a UUID route parameter in NestJS?

Use NestJS's built-in ParseUUIDPipe on the @Param() decorator: @Param('id', new ParseUUIDPipe({ version: '7' })). This validates the format and optionally the version digit. For custom validation, use class-validator's @IsUUID() decorator with version 'all' to accept both v4 and v7. See UUID Testing Strategies for fixture patterns.