Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The @virtual is recognized in extended class #2335

Open
2 of 4 tasks
smujaddid opened this issue Dec 8, 2024 · 1 comment
Open
2 of 4 tasks

The @virtual is recognized in extended class #2335

smujaddid opened this issue Dec 8, 2024 · 1 comment
Labels

Comments

@smujaddid
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

Assume I have these classes:

@Schema()
class Asset {
  @Prop({ required: true, trim: true })
  originalFilename: string
  
  @Prop({
    type: String,
    required: false,
    trim: true,
    nullable: true,
    default: null,
  })
  url?: string | null

  @Prop({
    type: MongooseSchema.Types.ObjectId,
    ref: 'User',
    required: false,
    index: true,
    nullable: true,
    default: null,
  })
  uploadedById?: Types.ObjectId | null

  @Virtual({
    options: {
      ref: 'User',
      localField: 'uploadedById',
      foreignField: '_id',
      justOne: true,
      nullable: true,
    },
  })
  uploadedBy?: UserDocument | null
}

@Schema()
class GroupPhoto extends Asset {
  @Prop({
    type: MongooseSchema.Types.ObjectId,
    ref: 'Group',
    required: false,
    index: true,
    nullable: true,
    default: null,
  })
  groupId?: Types.ObjectId | null

  @Virtual({
    options: {
      ref: 'Group',
      localField: 'groupId',
      foreignField: '_id',
      justOne: true,
      nullable: true,
    },
  })
  group?: GroupDocument | null
}

then trying to populate uploadedBy by using

await this.groupPhotoModel
      .find({})
      .populate('uploadedBy')
      .exec()

will result in error:

StrictPopulateError: Cannot populate path `uploadedBy` because it is not in your schema. Set the `strictPopulate` option to false to override.

I don't want to set strictPopulate to false.
The only way it will be working if I re-declare it on extended class GroupPhoto such as:

class GroupPhoto extends Asset {
  // ... redacted

  @Virtual({
    options: {
      ref: 'User',
      localField: 'uploadedById',
      foreignField: '_id',
      justOne: true,
      nullable: true,
    },
  })
  declare uploadedBy?: UserDocument | null
}

The thing is, properties with @prop can still be working on extended class such as url and originalFilename. Both will be inherited along with its decorator on the extended class.

Minimum reproduction code

https://gist.github.com/smujaddid/1cb930d92fe6483f9f1a072836c43266

Steps to reproduce

No response

Expected behavior

I'm expecting properties decorated with @virtual do not need to be re-declared on extended class just like @prop

Package version

10.1.0

mongoose version

8.8.1

NestJS version

10.4.8

Node.js version

20.18.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

@smujaddid smujaddid added the bug label Dec 8, 2024
@kamilmysliwiec
Copy link
Member

Would you like to create a PR for this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants