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:
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();
}
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With