Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose with NestJS - One to Many

I have an issue, I'm not able to get the relationship between 2 schemas with Mongoose, I'm a new user using this, I'm using NestJS, that's a NodeJS Framework using TypeScript.

I have these files:

user.schema.ts

import * as mongoose from 'mongoose';

export const UserSchema = new mongoose.Schema({
    name: String,
    email: String,
    role: Number,
    tokens: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Token'}]
});

token.schema.ts

import * as mongoose from 'mongoose';

export const TokenSchema = new mongoose.Schema({
    token: String,
    refreshToken: String,
    createdAt: String,
    expiresAt: String,
    isValid: Boolean,
    userId: [{ type: mongoose.Schema.Types.ObjectId, ref: 'User'}]
});

So, the relation is created, I can see it, but I don't know how to get the children of the User schema. In my ts I have this:

await this.userModel.find().populate('user', 'tokens').exec()

As I know that's to get the relation between the 2 schemas. BTW: The collections are users and tokens.

I'll appreciate any feedback or if I have to make something clear.

like image 825
Joseph Arriaza Avatar asked Sep 07 '18 22:09

Joseph Arriaza


1 Answers

I have solved my issue with ONE-TO-MANY relationship in this way:

package.json:

"@nestjs/mongoose": "7.0.2"
"mongoose": "^5.10.14"

For doing an example, I'll use the schemas from this issue(User and Token):

  1. Creating Token Schema
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Types } from 'mongoose';

@Schema({_id: false})
export class Token {

  @Prop({type: Types.ObjectId})
  _id: Types.ObjectId;

  @Prop()
  token: string;

  @Prop()
  refreshToken: string;

  @Prop()
  createdAt: Date; // You can use Date here.

  @Prop()
  expiresAt: Date;

  @Prop()
  isValid: boolean;
}

export const TokenSchema = SchemaFactory.createForClass(Token);
  1. Adding Token Schema into TokenModule
import { MongooseModule } from '@nestjs/mongoose';

@Module({
  imports: [
    MongooseModule.forFeature([
      { name: Token.name, schema: TokenSchema },
    ]),
  ],
  // ...
})
export class TokenModule {}
  1. Creating User Schema
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Types } from 'mongoose';

export type UserDocument = User & Document;

@Schema({_id: false})
export class User {

  @Prop({type: Types.ObjectId})
  _id: Types.ObjectId;

  @Prop()
  name: string;

  @Prop()
  email: string;

  @Prop()
  role: number;

  @Prop(type: [Types.ObjectId], ref: Token.name)
  tokens: Token[];
}

export const UserSchema = SchemaFactory.createForClass(User);
  1. Adding User Schema into UserModule
import { MongooseModule } from '@nestjs/mongoose';

@Module({
  imports: [
    MongooseModule.forFeature([
      { name: User.name, schema: UserSchema },
    ]),
  ],
  // ...
})
export class UserModule {}
  1. Using the populate function in the UserService

@Injectable()
export class UserService {

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

  async getUserWithPopulate(id: string) {
    // Here is the ace in the hole, you need to specify the path (tokens) and the model (Token.name).
    await this.userModel
      .find()
      .populate('tokens', null, Token.name)
      .exec()

  }
}
like image 151
Eduardo Albuquerque Avatar answered Oct 25 '22 11:10

Eduardo Albuquerque