5
votes

When using vanilla Mongoose, it's straight-forward to add methods to Mongoose schemas. It's well-addressed by the Mongoose documentation and several examples can be found.

But what about when you're using Mongoose within the context of a Nest app? I want my Mongoose schemas to be more "Nest-like", so I'm using the Nest wrapper for Mongoose (@nestjs/mongoose). However, the documentation for @nestjs/mongoose seems to be lacking. The closest thing I can find to any documentation is the guide for using MongoDB in a Nest app, and that only includes the most absolute basic use case for Mongoose.

To me, it looks like the way that Mongoose is used in the Nest world is very different from how vanilla Mongoose used. Maybe this is just a lack of TypeScript or Nest familiarity, but I can't seem to really navigate the differences, and the lack of examples isn't helping that.

I see a couple of answers for how to achieve this on StackOverflow, like:

  • Solution 1 - example solution that adds a method to MySchema.methods
    • This solution isn't working for me: TypeScript still tells me that the property does not exist on that type.
  • Solution 2 - example solution using an interface that extends Model
    • While this solution of adding a new interface with the method(s) I need does make TypeScript recognize that the method is valid for that type, I'm not sure how to actually implement it. I can't write a class that implements that interface because there's over 60 Mongoose model-methods it needs to implement, and any other place I try to write an implementation it isn't working for me.

How can I do something like this?

Schema

import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';

export type CatDocument = Cat & Document;

@Schema()
export class Cat {
  @Prop()
  name: string;

  @Prop()
  age: number;

  @Prop()
  breed: string;
}

export const CatSchema = SchemaFactory.createForClass(Cat);

// ** Add methods here? **

Service

import { Model } from 'mongoose';
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Cat, CatDocument } from './schemas/cat.schema';

@Injectable()
export class CatsService {
  constructor(@InjectModel(Cat.name) private catModel: Model<CatDocument>) {}

  async findAll(): Promise<Cat[]> {
    // Call our custom method here:
    return this.catModel.doSomething();
  }
}

2

2 Answers

0
votes

Here is what I managed to do:

export type UserDocument = User & Document;

@Schema()
export class User extends Document {
  @Prop({ required: true, unique: true })
  email!: string;
  @Prop({ required: true })
  passwordHash!: string;

  toGraphql!: () => UserType;
}

export const UserSchema = SchemaFactory.createForClass(User);

UserSchema.methods.toGraphql = function (this: User) {
  const user = new UserType();

  user.id = this._id;
  user.email = this.email;

  return user;
};

Just added

toGraphql!: () => UserType;

to class

0
votes

Actually you are calling method on model, you have to call the method on created document, or returned document by model.

Method on schema.

UserSchema.methods.comparePassword = async function(candidatePassword: string) {
  return await bcrypt.compare(candidatePassword, this.password);
};

Interface containing defined function.

export interface User {
  comparePassword(candidatePassword: string): Promise<boolean>;
}

In my case I had already been using UserDocument which had User & Document. I add it when injecting model.

constructor(
    @InjectModel(User.name) private readonly userModel: Model<UserDocument, UserFunction>
  ) {
  }

@Injectable()
export class AuthService {
  constructor(
    private readonly usersService: UsersService,
    private readonly jwtService: JwtService,
) { }

async signIn({ email, password }: SignInDto): Promise<LoginResponse> {

const user = await this.usersService.findByEmail(email); // Document found

if (!user) { throw new UnauthorizedException('Invalid Username or Password'); }

if (await user.comparePassword(password)) // <- Calling Schema method here {

  const tokenPayload: JwtPayload = { userId: user.id };
  const token = this.jwtService.sign(tokenPayload);

  return ({ token, userId: user.id, status: LoginStatus.success });
} else {
  throw new UnauthorizedException('Invalid Username or Password');
}

I had been calling the methods on userModel itself instead of calling it on it's document. Thanks to @Pantera answer I spotted the mistake.