ParseObjectIdPipe for MongoDB's ObjectID - mongodb

I want to create a NestJs API with TypeORM and MongoDB. My entity id fields are of type ObjectID. The controller routes should validate the incoming ids before passing them to the services. I know that Nest ships with the ParseIntPipe and ParseUUIDPipe but as far as I know there is nothing I can use for MongoDBs ObjectID.
So I created my own pipe for those fields as described here https://docs.nestjs.com/pipes#transformation-use-case
import { PipeTransform, Injectable, BadRequestException } from '#nestjs/common';
import { ObjectID } from 'typeorm';
#Injectable()
export class ParseObjectIdPipe implements PipeTransform<any, ObjectID> {
transform(value: any): ObjectID {
const validObjectId: boolean = ObjectID.isValid(value);
if (validObjectId) {
throw new BadRequestException('Invalid ObjectId');
}
const objectId: ObjectID = ObjectID.createFromHexString(value);
return objectId;
}
}
and hope this will do the trick, even for edge cases. I can use it for my route params like
#Get(':id')
public getUserById(#Param('id', ParseObjectIdPipe) id: ObjectID): Promise<User> {
return this.usersService.getUserById(id);
}
The problem I have is that some routes need a Body validation. I use the class-validator package as described here
https://docs.nestjs.com/techniques/validation
It seems that I have to create my own class-validator decorator for those ObjectID fields but that should be fine. Maybe I'll find something here on how to do it https://github.com/typestack/class-validator#custom-validation-classes. But how can I transform those fields to the type ObjectID? Can I use the custom pipe for that later on too?
Update:
I also tried to transform the value via class-transformer package. So the code for this is
import { ObjectID } from 'typeorm';
import { Type, Transform } from 'class-transformer';
import { BadRequestException } from '#nestjs/common';
export class FooDTO {
#Type(() => ObjectID)
#Transform(bar => {
if (ObjectID.isValid(bar)) {
throw new BadRequestException('Invalid ObjectId');
}
return ObjectID.createFromHexString(bar);
})
public bar: ObjectID;
}
Unfortunately the value bar is always undefined. But maybe this code might help for validation and transformation purposes...

I took your code and changed some parts. I tested it, It works fine.
import { PipeTransform, Injectable, BadRequestException } from '#nestjs/common';
import { Types } from 'mongoose';
#Injectable()
export class ParseObjectIdPipe implements PipeTransform<any, Types.ObjectId> {
transform(value: any): Types.ObjectId {
const validObjectId = Types.ObjectId.isValid(value);
if (!validObjectId) {
throw new BadRequestException('Invalid ObjectId');
}
return Types.ObjectId.createFromHexString(value);
}
}

For the class-transformer approach this worked for me
import { IsNotEmpty } from "class-validator";
import { Type, Transform } from 'class-transformer';
import { Types } from "mongoose"
export class CreateCommentDto {
#IsNotEmpty()
#Type(() => Types.ObjectId)
#Transform(toMongoObjectId)
articleId: Types.ObjectId
with this definition for 'toMongoObjectId':
export function toMongoObjectId({ value, key }): Types.ObjectId {
if ( Types.ObjectId.isValid(value) && ( Types.ObjectId(value).toString() === value)) {
return Types.ObjectId(value);
} else {
throw new BadRequestException(`${key} is not a valid MongoId`);
}
}

You are getting undefined because you are importing from wrong lib.
you need to change this:
import { ObjectID } from 'typeorm';
for this:
import { ObjectID } from 'mongodb';

Just create on pipe of object id in code by using below code :
import { PipeTransform, Injectable, BadRequestException } from
'#nestjs/common';
import { ObjectID } from 'mongodb';
#Injectable()
export class ParseObjectIdPipe implements PipeTransform<any, ObjectID> {
public transform(value: any): ObjectID {
try {
const transformedObjectId: ObjectID = ObjectID.createFromHexString(value);
return transformedObjectId;
} catch (error) {
throw new BadRequestException('Validation failed (ObjectId is expected)');
}
}
}
and add piple in controller method :
#Post('cat/:id')
async cat(
#Param('id', ParseObjectIdPipe) id: ObjectId,
#Res() res,
#Body() cat: catDTO[],)

Related

Nest JS user authentication issue with parameter name

I am just learning nestjs for about a day and I came across this strange bug, probably has something to do with me not understanding what Im doing and rushing the project so please bear with me. My main issue is that while using JWT authentication, JSON coming from body is "username" and I can't change it. I want to log in using {"email":"test#gmail.com", "password": "password123"}, but instead it only accepts {"username":"test#gmail.com", "password": "password123"}. The word "username" is not defined or mentioned anywhere in my codebase
users.controller.ts
import { Controller, Get, Post, Body, Param, UseGuards } from '#nestjs/common';
import { UsersService} from './users.service';
import { CreateUserDto} from './dto/create-user.dto';
import { AuthGuard} from '#nestjs/passport';
#Controller('/users')
export class UsersController {
// constructor(private readonly usersService: UsersService) {}
constructor(private readonly userService: UsersService) {}
#UseGuards(AuthGuard('jwt'))
#Get('username')
getUserByEmail(#Param() param) {
return this.userService.getUserByEmail(param.email);
}
#Post('register')
registerUser(#Body() createUserDto: CreateUserDto) {
return this.userService.registerUser(createUserDto);
}
}
users.service.ts
import { Injectable, BadRequestException } from '#nestjs/common';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { Model } from 'mongoose';
import { InjectModel } from '#nestjs/mongoose';
import { HashService } from './hash.service';
import { User, UserDocument} from '../schemas/user.schema'
#Injectable()
export class UsersService {
constructor(#InjectModel(User.name) private userModel: Model < UserDocument > , private hashService: HashService) {}
async getUserByEmail(email: string) {
return this.userModel.findOne({
email
})
.exec();
}
async registerUser(createUserDto: CreateUserDto) {
// validate DTO
const createUser = new this.userModel(createUserDto);
// check if user exists
const user = await this.getUserByEmail(createUser.email);
if (user) {
throw new BadRequestException();
}
// Hash Password
createUser.password = await this.hashService.hashPassword(createUser.password);
return createUser.save();
}
}
auth.controller.ts
import { AuthService} from './auth.service';
import { Controller, Request, UseGuards, Post} from '#nestjs/common';
import { AuthGuard } from '#nestjs/passport';
#Controller('auth')
export class AuthController {
constructor(private authService: AuthService) {}
#UseGuards(AuthGuard('local'))
#Post(`/login`)
async login(#Request() req) {
console.log(req.user, "here")
return this.authService.login(req.user);
}
}
Here is the source code https://github.com/networkdavit/pillicam_test
Any help or suggestion is highly appreciated!
I tried changing all the parameter names, user schemas, adding a DTO, I googled how to add a custom parameter name or override it, tried to find if "default username param" actually exists. Nothing has worked for me so far
It's in there username in your code. https://github.com/networkdavit/pillicam_test/blob/main/src/users/entities/user.entity.ts#:~:text=class%20User%20%7B-,username%3A%20string%3B,-password%3A%20string
You can change it.
Or you can refer to this article for JWT implementation in nest.js
Just in case anyone ever gets this problem, I found a solution.
All I had to do was to add this to my local.strategy.ts file in constructor
super({
usernameField: 'email',
passwordField: 'password'
});
The default expects a username and password, so have to modify it manually

NestJS handle config service dynamically

The following code:
import { HttpModuleOptions, HttpModuleOptionsFactory, Inject, Injectable } from '#nestjs/common';
import { ConfigType } from '#nestjs/config';
import { DotEnv } from '../../../common/enums/dotenv.enum';
import { DotEnvError } from '../../../common/errors/dot-env.error';
import zendeskConfig from '../../../config/zendesk.config';
#Injectable()
export class ZendeskHttpConfigService implements HttpModuleOptionsFactory {
constructor(#Inject(zendeskConfig.KEY) private zendesk: ConfigType<typeof zendeskConfig>) {}
createHttpOptions(): HttpModuleOptions {
if (!this.zendesk.url || !this.zendesk.username || !this.zendesk.password)
throw new DotEnvError(DotEnv.ZENDESK_URL, DotEnv.ZENDESK_USERNAME, DotEnv.ZENDESK_PASSWORD);
return {
baseURL: this.zendesk.url,
auth: {
username: this.zendesk.username,
password: this.zendesk.password
}
};
}
}
I would like to handle this.zendesk.url dynamically.
In my controller I call an endpoint with a query param.
For example if the query param is "a" I would like to set this.zendesk.urlA and
if "b" I would like to set this.zendesk.urlB
Does NestJs allow that?

TypeError: Cannot read properties of undefined (reading 'create') + TypeORM Repository

I'm working with typeORM and Postgres in Nest.js and I'm separating my Repository from Service.
The problem is the entityRepository() decorator is not registering one of my repositories and everything points to it being fine as the same code is creating another entity's repository fine.
I've really searched and deleted the module twice thinking I might have missed something along the way, but nothing is working. When I do console.log(this) in the repository I get an empty object {}...
Below is the (module, repository, and entity) for the stock-analysis module
stock-analysis.module.ts
import { Module } from '#nestjs/common';
import { TypeOrmModule } from '#nestjs/typeorm';
import { StockAnalysisController } from './stock-analysis.controller';
import { StockAnalysisRepository } from './stock-analysis.repository';
import { StockAnalysisService } from './stock-analysis.service';
#Module({
imports: [TypeOrmModule.forFeature([StockAnalysisRepository])],
controllers: [StockAnalysisController],
providers: [StockAnalysisService, StockAnalysisRepository],
exports: [TypeOrmModule],
})
export class AnalysisModule {}
stock-analysis.repository.ts
import { EntityRepository, Repository } from 'typeorm';
import { StockAnalysisDto } from '../dtos/analysis/stock-analysis-dto';
import { StockAnalysis } from './stock-analysis.entity';
import { User } from '../user/user.entity';
#EntityRepository(StockAnalysis)
export class StockAnalysisRepository extends Repository<StockAnalysis> {
async analyseStock(user: User, stockAnalysisDto: StockAnalysisDto) {
const { stock } = stockAnalysisDto;
console.log({ this: this });
// console.log({ stock });
// console.log(user);
const stocksToAnalyse = this.create();
}
}
stock-analysis.entity.ts
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
#Entity()
export class StockAnalysis {
#PrimaryGeneratedColumn('uuid')
Id: string;
#Column('text', { array: true })
stocks: string[];
#Column()
userId: string;
}
I'm really stuck, ran out of ideas. Any form of help will be deeply appreciated
Can you try Injectable() instead of #EntityRepository(StockAnalysis)? And if that doesn't work, try to add this constructor in the repository:
constructor(private dataSource: DataSource) {
super(StockAnalysis, dataSource.createEntityManager());
}

Type 'Task' does not satisfy the constraint 'Document'

I'm creating API using Nestjs and Mongodb. tasks.service.ts, tried to create a getAll endpoint and got typescript error:
Type 'Task' does not satisfy the constraint 'Document'. Type 'Task' is missing the following properties from type 'Document': increment, model, $isDeleted, remove, and 51 more.
tasks.service.ts
import { Injectable, HttpStatus } from '#nestjs/common';
import { InjectModel } from '#nestjs/mongoose';
import { Model } from 'mongoose';
import { Task } from './dto/task.inferface';
#Injectable()
export class TasksService {
private readonly Tasks: Task[] = [];
constructor(#InjectModel('Task') private readonly TaskModel: Model<Task>) {}
async getAll(): Promise<Task> {
const tasks = await this.TaskModel.find().exec();
return tasks;
}
}
Extend the document class from the interface.
import { Document } from 'mongoose';
export interface Task extends Document {
//Task info ...
}
Task shouldn't be a DTO, it should be entity like:
tasks.entity.ts
import { Schema, Prop, SchemaFactory } from '#nestjs/mongoose';
import { Document } from 'mongoose';
#Schema()
export class Task extends Document {
#Prop()
name: string;
}
export const TaskSchema = SchemaFactory.createForClass(Task);
You also need to register that model in your module:
#Module({
imports: [
MongooseModule.forFeature([
{
name: Task.name,
schema: TaskSchema,
},
]),
],
})
So your code will be something like
import { Injectable, HttpStatus } from '#nestjs/common';
import { InjectModel } from '#nestjs/mongoose';
import { Model } from 'mongoose';
import { Task } from './entities/task.entity';
#Injectable()
export class TasksService {
constructor(#InjectModel(Task.name) private readonly TaskModel: Model<Task>) {}
async getAll(): Promise<Task> {
const tasks = await this.TaskModel.find().exec();
return tasks;
}
}

What is the proper way to do seed mongoDB in NestJS, using mongoose and taking advantage of my already defined schmas

We are using NestJS with mongoose and want to seed mongoDB.
Wondering what is the proper way to seed the database, and use the db schemas already defined to ensure the data seeded is valid and properly maintained.
Seeding at the module level (just before the definition of the Module) feels hacky and ends in threadpool being destroyed, and therefore all following mongo operations fail
I've done using the nestjs-command library like that.
1. Install the library:
https://www.npmjs.com/package/nestjs-command
2. Then I've created a command to seed my userService like:
src/modules/user/seeds/user.seed.ts
import { Command, Positional } from 'nestjs-command';
import { Injectable } from '#nestjs/common';
import { UserService } from '../../../shared/services/user.service';
#Injectable()
export class UserSeed {
constructor(
private readonly userService: UserService,
) { }
#Command({ command: 'create:user', describe: 'create a user', autoExit: true })
async create() {
const user = await this.userService.create({
firstName: 'First name',
lastName: 'Last name',
mobile: 999999999,
email: 'test#test.com',
password: 'foo_b#r',
});
console.log(user);
}
}
3. Add that seed command into your module. I've created a SeedsModule in a shared folder to add more seeds in future
src/shared/seeds.module.ts
import { Module } from '#nestjs/common';
import { CommandModule } from 'nestjs-command';
import { UserSeed } from '../modules/user/seeds/user.seed';
import { SharedModule } from './shared.module';
#Module({
imports: [CommandModule, SharedModule],
providers: [UserSeed],
exports: [UserSeed],
})
export class SeedsModule {}
Btw I'm importing my userService into my SharedModule
4. Add the SeedsModule into your AppModule
On your AppModule usually at src/app.module.ts add the SeedsModule into imports
Final
If you followed the steps in the nestjs-command repo you should be able to run
npx nestjs-command create:user
That will bootstrap a new application and run that command and then seed to your mongo/mongoose
Hope that help others too.
actually you can do it easily with onModuleInit(), here i'm using Mongoose ORM. This all done with zero dependencies, hope it helps
import { Injectable, OnModuleInit } from '#nestjs/common';
import { UserRepository } from './repositories/user.repository';
#Injectable()
export class UserService implements OnModuleInit {
constructor(private readonly userRepository: UserRepository) {}
// onModuleInit() is executed before the app bootstraped
async onModuleInit() {
try {
const res = await this.userRepository.findAll(); // this method returns user data exist in database (if any)
// checks if any user data exist
if (res['data'] == 0) {
const newUser = {
name: 'yourname',
email: 'youremail#gmail.com',
username: 'yourusername',
};
const user = await this.userRepository.create(newUser); // this method creates new user in database
console.log(user);
}
} catch (error) {
throw error;
}
}
// your other methods
}
For my case, I needed to insert seed during the tests, the best I could find is to create a seed service, imported and used only during tests.
Here is my base class using the schema model, all is needed is to extend and pass the model.
// # base.seed.service.ts
import { Model, Document } from 'mongoose';
import { forceArray, toJson } from 'src/utils/code';
export abstract class BaseSeedService<D extends Document> {
constructor(protected entityModel: Model<D>) {}
async insert<T = any>(data: T | T[]): Promise<any[]> {
const docs = await this.entityModel.insertMany(forceArray(data));
return toJson(docs);
}
}
// # utils
const toJson = (arg: any) => JSON.parse(JSON.stringify(arg));
function forceArray<T = any>(instance: T | T[]): T[] {
if (instance instanceof Array) return instance;
return [instance];
}
// # dummy.seed.service.ts
import { Injectable } from '#nestjs/common';
import { InjectModel } from '#nestjs/mongoose';
import { Model } from 'mongoose';
import { DummyDocument } from './dummy.schema';
#Injectable()
export class DummySeedService extends BaseSeedService<DummyDocument> {
constructor(
#InjectModel(Dummy.name)
protected model: Model<DummyDocument>,
) {
super(model);
}
}
Then inside the tests
describe('Dymmy Seeds', () => {
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [DummySeedService],
imports: [
MongooseModule.forRoot(__connect_to_your_mongodb_test_db__),
MongooseModule.forFeature([
{
name: Dummy.name,
schema: DummySchema,
},
]),
],
}).compile();
const seeder = module.get<DummySeedService>(DummySeedService);
const initData = [__seed_data_here__];
const entities: Dummy[] = await seeder.insert(initData);
expect(entities.length > 0).toBeTruthy();
});
});