nestjs-object-id
TypeScript icon, indicating that this package has built-in type declarations

1.2.1 • Public • Published

nestjs-object-id

npm license npm downloads

Description

This package provides an efficient way to validate and parse ObjectIds for NestJS applications that interact with MongoDB and supports a wide range of architectures and patterns, including REST APIs, GraphQL, DTOs and Microservices.

Table of Contents

Installation

npm install nestjs-object-id

Usage

@IsObjectId()

@IsObjectId() is a decorator for validating MongoDB ObjectIds in DTOs.
Here is an example along with commonly used IsString and IsNotEmpty from class-validator package.

import { IsObjectId } from 'nestjs-object-id';
import { IsString, IsNotEmpty } from 'class-validator';

class CreatePostDto {
    @IsObjectId()
    authorId: string;

    @IsString()  
    @IsNotEmpty()
    title: string;
}

If an invalid 'authorId' is received, an error will be thrown:

{
  message: ["authorId must be an MongoDB ObjectId instance"],
  error: "Bad Request",
  statusCode: 400
}

IsObjectIdPipe

IsObjectIdPipe is a pipe for validating MongoDB ObjectIds in route parameters.

import { IsObjectIdPipe } from 'nestjs-object-id';

@Controller('posts')
export class PostsController {
  constructor(private readonly postsService: PostsService) {}

  @Get(':id')
  findOne(@Param('id', IsObjectIdPipe) id: string) {
    return this.postsService.findOne(id);
  }
}

WARNING To work with Pipes correctly, make sure you have @nestjs/common@10.2.7 or higher installed.

If an invalid 'id' is received, an error will be thrown:

{
    "message": "Invalid ObjectId",
    "error": "Bad Request",
    "statusCode": 400
}

ParseObjectIdPipe

ParseObjectIdPipe extends the functionality of IsObjectIdPipe by converting string parameters into Types.ObjectId instances.

import { ParseObjectIdPipe } from 'nestjs-object-id';
import { Types } from 'mongoose';

@Controller('posts')
export class PostsController {
  constructor(private readonly postsService: PostsService) {}

  @Get(':id')
  findOne(@Param('id', ParseObjectIdPipe) id: Types.ObjectId) {
    return this.postsService.findOne(id);
  }
}

HINT To log request and response activity clearly and efficiently, you can install nesjs-http-logger.

GraphQL

You can use IsObjectIdPipe and ParseObjectIdPipe with GraphQL.
Example of using IsObjectIdPipe with GraphQL:

import { IsObjectIdPipe } from 'nestjs-object-id';
import { Args, Query, Resolver } from '@nestjs/graphql';

@Resolver(() => Post)
export class PostsResolver {
  constructor(private readonly postsService: PostsService) {}

  @Query(() => Post)
  post(@Args('id', IsObjectIdPipe) id: string) {
    return this.postsService.findOne(id);
  }
}

Microservices

You can use IsObjectIdPipe and ParseObjectIdPipe with Microservices.
Example of using IsObjectIdPipe with Microservices:

import { IsObjectIdPipe } from 'nestjs-object-id';
import { MessagePattern, Payload } from '@nestjs/microservices';

@Controller('posts')
export class PostsController {
  constructor(private readonly postsService: PostsService) {}

  @MessagePattern('find_post')
  findOne(@Payload('id', IsObjectIdPipe) id: string) {
    return this.postsService.findOne(id);
  }
}

Author

Vladyslav Braslavskyi GitHub

License

Licensed under the MIT License - see the LICENSE file for details.

Package Sidebar

Install

npm i nestjs-object-id

Weekly Downloads

150

Version

1.2.1

License

MIT

Unpacked Size

13.8 kB

Total Files

15

Last publish

Collaborators

  • vlbras