2.0.2 • Published 5 months ago

nestjs-object-id v2.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

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

Pipes

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);
  }
}

Decorators

@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: ["Invalid ObjectId for property \"authorId\": 0000000000000000",],
  error: "Bad Request",
  statusCode: 400
}

@ParseObjectId()

@ParseObjectId() is a decorator for parsing 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';
import { Types } from 'mongoose';

class CreatePostDto {
    @ParseObjectId()
    authorId: Types.ObjectId;;

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

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

{
  message: ["Invalid ObjectId for property \"authorId\": 0000000000000000",],
  error: "Bad Request",
  statusCode: 400
}

Author

Vladyslav Braslavskyi GitHub

License

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

2.0.2

5 months ago

1.4.2

8 months ago

1.3.2

8 months ago

1.2.2

9 months ago

2.0.1

8 months ago

2.0.0

8 months ago

1.2.1

1 year ago

1.2.0

1 year ago

1.0.7

1 year ago

1.1.5

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago