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

Does not work on nested schemas on discriminated models #42

Open
DesignByOnyx opened this issue Jul 18, 2024 · 0 comments · May be fixed by #43
Open

Does not work on nested schemas on discriminated models #42

DesignByOnyx opened this issue Jul 18, 2024 · 0 comments · May be fixed by #43

Comments

@DesignByOnyx
Copy link

Do you want to request a feature or report a bug?

Bug

What is the current behavior?

We have a shared schema which gets referenced by several models. Whenever this schema is part of a discriminated model, the getters do not get called for lean queries.

If the current behavior is a bug, please provide the steps to reproduce.

const nestedSchema = new mongoose.Schema({
  num: {
    type: mongoose.Types.Decimal128,
    get: (val) => `${val}`
  }
});

const rootSchema = new mongoose.Schema({});
rootSchema.plugin(mongooseLeanGetters);

const discriminatedSchema = new mongoose.Schema({
  discriminatedProp: { type: nestedSchema },
  discriminatedArray: { type: [nestedSchema] },
});

const RootModel = mongoose.model('Root', rootSchema);
const DiscriminatedModel = RootModel.discriminator('Discriminated', discriminatedSchema);

const entry = await DiscriminatedModel.create({
  discriminatedProp: { num: -0.222222222222222222 },
  discriminatedArray: [{ num: -0.333333333333333333 }],
});

const found = await DiscriminatedModel.findById(entry._id).lean({ getters: true }).exec();

assert.equal(typeof found.discriminatedProp.num, 'string');  // fail
assert.equal(typeof found.discriminatedArray[0].num, 'string');  // fail

What is the expected behavior?

The getters should be called

What are the versions of Node.js, mongoose-lean-getters, and Mongoose are you are using? Note that "latest" is not a version.

Node: v20.12.2
mongoose: v8.5.0 (also v7.7.0)
mongoose-lean-getters: v2.1.0

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

Successfully merging a pull request may close this issue.

1 participant