Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add instance/static methods to Mongoose schema when using @nestjs/mongoose? (TypeScript Issues)

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

like image 597
Greg Thomas Avatar asked Nov 07 '22 02:11

Greg Thomas


1 Answers

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

like image 98
quolpr Avatar answered Nov 12 '22 12:11

quolpr