nestjs / mongoose

Mongoose module for Nest framework (node.js) 🍸
https://nestjs.com
MIT License
528 stars 118 forks source link

Please enlighten documentation for virtuals and methods on schema definition #732

Closed NitnekB closed 3 years ago

NitnekB commented 3 years ago

I'm submitting a...


[ ] Regression 
[ ] Bug report
[ ] Feature request
[x] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

Cannot set properly virtual attribute on schema definition

Expected behavior

Easy way to concat string attributes on new field using mongoose virtual feature

Ex: see Nest documentation with fullName on User schema

Minimal reproduction of the problem with instructions

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

export type UserDocument = User & Document;

@Schema()
export class User {
  @Prop({ required: true, min: 2, max: 64 })
  firstName: string;

  @Prop({ required: true, min: 2, max: 64 })
  lastName: string;

  @Prop({ required: true, lowercase: true })
  email: string;

  @Prop({ required: true, select: false })
  password: string;

  @Prop({ default: false })
  hasBeenValidated: boolean;

  @Prop({ virtual: 'fullName' }) // THIS IS NOT THE PROPER WAY I GUESS
  fullName = `${this.firstName} ${this.lastName}`;
}

export const UserSchema = SchemaFactory.createForClass(User);

What is the motivation / use case for changing the behavior?

Just need to understand how virtual mongoose feature works with @nestjs/mongoose (and also how to declare some methods will be nice ;) )

No documentation about those features :(

Environment

none

Tony133 commented 3 years ago

try like this:

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

@Schema({autoIndex: true, toJSON: {virtuals: true}})
export class User extends Document {
   @Prop ({required: true, min: 2, max: 64})
   firstName: string;

   @Prop({required: true, min: 2, max: 64})
   lastName: string;

   @Prop({required: true, lowercase: true})
   email: string;

   @Prop({required: true, select: false})
   password: string;

   @Prop({default: false})
   hasBeenValidated: boolean;
}

export const UserSchema = SchemaFactory.createForClass(User);

UserSchema.virtual('fullName').get(function() {
   return `${this.firstName} ${this.lastName}`;
});

for the addition of this part concerning the virtuals I had talked about it here with Kamil, I had created a pull. see discussion here https://github.com/nestjs/docs.nestjs.com/pull/1646

I still have the branch in the fork, if in the future you change your mind I can do the pull again, no problem for me :-)

if you have difficulty use the discord channel for support

kamilmysliwiec commented 3 years ago

You can define virtuals in the same way as described in the official mongoose docs.

NitnekB commented 3 years ago

Thanks a lot Tomy and Kamil, works perfectly fine! I guess it was to obvious for me haha

cheezone commented 2 years ago

You can try to use Mongoose's loadClass, when the second parameter is true, you can only load virtual properties.