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

Fix: populate param parsing #394

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 24 additions & 19 deletions packages/moleculer-db-adapter-mongoose/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 @@ -294,8 +294,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 @@ -352,8 +352,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
10 changes: 6 additions & 4 deletions packages/moleculer-db-adapter-mongoose/test/unit/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -814,8 +814,9 @@ if (process.versions.node.split(".")[0] < 14) {
},
});
adapter.init(broker, service);
const ctx = { service, params: { populate: ["firstVirtual", "secondVirtual", "thirdVirtual", "fourthVirtual"]}};
const res = adapter.getNativeVirtualPopulateQuery(ctx);
const params = { populate: ["firstVirtual", "secondVirtual", "thirdVirtual", "fourthVirtual"]};
const ctx = { service, params };
const res = adapter.getNativeVirtualPopulateQuery(ctx, params);

expect(res).toHaveLength(3);

Expand Down Expand Up @@ -877,8 +878,9 @@ if (process.versions.node.split(".")[0] < 14) {
},
});
adapter.init(broker, service);
const ctx = { service, params: { populate: ["firstVirtual", "secondVirtual", "thirdVirtual"]}};
const res = adapter.getNativeVirtualPopulateQuery(ctx);
const params = { populate: ["firstVirtual", "secondVirtual", "thirdVirtual"]};
const ctx = { service, params };
const res = adapter.getNativeVirtualPopulateQuery(ctx, params);

expect(res).toHaveLength(3);

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 @@ -531,7 +531,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
8 changes: 4 additions & 4 deletions packages/moleculer-db/test/unit/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ describe("Test transformDocuments method", () => {
expect(res).toBe(doc);

expect(mockAdapter.entityToObject).toHaveBeenCalledTimes(1);
expect(mockAdapter.entityToObject).toHaveBeenCalledWith(doc, ctx);
expect(mockAdapter.entityToObject).toHaveBeenCalledWith(doc, ctx, ctx.params);

expect(service.encodeID).toHaveBeenCalledTimes(1);
expect(service.encodeID).toHaveBeenCalledWith(doc._id);
Expand All @@ -566,7 +566,7 @@ describe("Test transformDocuments method", () => {
expect(res).toBe(doc);

expect(mockAdapter.entityToObject).toHaveBeenCalledTimes(1);
expect(mockAdapter.entityToObject).toHaveBeenCalledWith(doc, ctx);
expect(mockAdapter.entityToObject).toHaveBeenCalledWith(doc, ctx, ctx.params);

expect(service.encodeID).toHaveBeenCalledTimes(1);
expect(service.encodeID).toHaveBeenCalledWith(doc._id);
Expand Down Expand Up @@ -603,8 +603,8 @@ describe("Test transformDocuments method", () => {
expect(res).toEqual(docs);

expect(mockAdapter.entityToObject).toHaveBeenCalledTimes(2);
expect(mockAdapter.entityToObject).toHaveBeenCalledWith(docs[0], ctx);
expect(mockAdapter.entityToObject).toHaveBeenCalledWith(docs[1], ctx);
expect(mockAdapter.entityToObject).toHaveBeenCalledWith(docs[0], ctx, ctx.params);
expect(mockAdapter.entityToObject).toHaveBeenCalledWith(docs[1], ctx, ctx.params);

expect(service.encodeID).toHaveBeenCalledTimes(2);
expect(service.encodeID).toHaveBeenCalledWith(docs[0]._id);
Expand Down