Skip to content

Commit

Permalink
use sanitized params instead of original ones
Browse files Browse the repository at this point in the history
  • Loading branch information
Freezystem committed Aug 27, 2024
1 parent d52b9ef commit 315779f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
8 changes: 4 additions & 4 deletions packages/moleculer-db-adapter-mongoose/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,8 @@ class MongooseDbAdapter {
* @returns {Object[]}
* @memberof MongooseDbAdapter
*/
getNativeVirtualPopulateQuery(ctx) {
const fieldsToPopulate = ctx.params?.populate || [];
getNativeVirtualPopulateQuery(ctx, params) {
const fieldsToPopulate = params?.populate || [];

if (fieldsToPopulate.length === 0) return [];

Expand Down Expand Up @@ -366,8 +366,8 @@ class MongooseDbAdapter {
* @returns {Object}
* @memberof MongooseDbAdapter
*/
entityToObject(entity, ctx) {
const populate = this.useNativeMongooseVirtuals ? this.getNativeVirtualPopulateQuery(ctx) : [];
entityToObject(entity, ctx, params) {
const populate = this.useNativeMongooseVirtuals ? this.getNativeVirtualPopulateQuery(ctx, params) : [];

return Promise.resolve(populate.length > 0 ? entity.populate(populate) : entity)
.then(entity => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,48 @@ if (process.versions.node.split(".")[0] < 14) {
expect(user).toHaveProperty("lastPostWithVotes");
expect(user.lastPostWithVotes).toHaveProperty("_id", _post2.id);
});

it("Should populate virtuals using populate string param", async () => {
const _user = await User.Model.create({
firstName: "John",
lastName: "Doe",
});

const _post1 = await Post.Model.create({
title: "post_1",
content: "content 1",
author: _user._id,
});

const _post2 = await Post.Model.create({
title: "post_2",
content: "content 2",
author: _user._id,
votes: 2,
});

const user = await broker.call("users.get", {
id: _user.id,
populate: "posts, postCount, lastPost, lastPostWithVotes",
});

expect(user).toHaveProperty("firstName", "John");
expect(user).toHaveProperty("lastName", "Doe");
// virtual function without populate
expect(user).toHaveProperty("fullName", "John Doe");
// virtual populate with refPath and count option
expect(user).toHaveProperty("postCount", 2);
// virtual populate with ref
expect(user).toHaveProperty("posts");
expect(user.posts).toHaveLength(2);
expect(user.posts.map((p) => p._id)).toEqual([_post2.id, _post1.id]);
// virtual populate with justOne option set to "true"
expect(user).toHaveProperty("lastPost");
expect(user.lastPost).toHaveProperty("_id", _post2.id);
// virtual populate with match clause
expect(user).toHaveProperty("lastPostWithVotes");
expect(user.lastPostWithVotes).toHaveProperty("_id", _post2.id);
});
});

describe("Test moleculer basic virtual population", () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/moleculer-db/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ module.exports = {
return Promise.resolve(docs)

// Convert entity to JS object
.then(docs => Promise.all(docs.map(doc => this.adapter.entityToObject(doc, ctx))))
.then(docs => Promise.all(docs.map(doc => this.adapter.entityToObject(doc, ctx, params))))

// Apply idField
.then(docs => docs.map(doc => this.adapter.afterRetrieveTransformID(doc, this.settings.idField)))
Expand Down

0 comments on commit 315779f

Please sign in to comment.