-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
feat: implemented getPopulatedDocs() #9766
Conversation
lib/document.js
Outdated
let result = []; | ||
|
||
for (const key of index) { | ||
let value = this.get(key); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make this const
instead of let
lib/document.js
Outdated
@@ -3897,6 +3899,24 @@ Document.prototype.populate = function populate() { | |||
return this; | |||
}; | |||
|
|||
/* Returns an array of all populated documents associated with the query. */ | |||
Document.prototype.$getPopulatedDocs = function $getPopulatedDocs() { | |||
const index = (Object.keys(this.$__.populated)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's rename this variable to keys
or something that's more indicative of how this is used.
lib/document.js
Outdated
result.push(value); | ||
|
||
} | ||
console.log(result); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this console.log()
lib/document.js
Outdated
|
||
for (const key of index) { | ||
let value = this.get(key); | ||
if (Array.isArray(value)) result = result.concat(value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mongoose style guidelines generally say no one-line ifs. Curly braces {}
around if
always, so this should be:
if (Array.isArray(value)) {
result = result.concat(value);
} else if (typeof value === 'object' && value !== null) {
result.push(value);
}
test/document.test.js
Outdated
}); | ||
|
||
return co(function*() { | ||
const c = yield Child.create({ name: 'test' }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's some issues with indentation here. Run npm run lint -- --fix
and that should help fix these errors.
test/document.test.js
Outdated
|
||
p.children; // [{ _id: '...', name: 'test' }] | ||
|
||
assert.equal(p.$getPopulatedDocs().length, 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be assert.equal(p.$getPopulatedDocs().length, 2);
because both 'children' and 'child' are populated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you also add this function to index.d.ts
?
test/document.test.js
Outdated
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove the trailing whitespace
p.children; // [{ _id: '...', name: 'test' }] | ||
|
||
assert.equal(p.$getPopulatedDocs().length, 2); | ||
assert.equal(p.$getPopulatedDocs()[0], p.children[0]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's also add a test that populatedDocs[1]
is strictly equal to p.child
lib/document.js
Outdated
@@ -3897,6 +3899,23 @@ Document.prototype.populate = function populate() { | |||
return this; | |||
}; | |||
|
|||
/* Returns an array of all populated documents associated with the query. */ | |||
Document.prototype.$getPopulatedDocs = function $getPopulatedDocs() { | |||
const pathway = (Object.keys(this.$__.populated)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also not a fan of this name. Let's just use keys
test/document.test.js
Outdated
assert.equal(p.$getPopulatedDocs().length, 2); | ||
assert.equal(p.$getPopulatedDocs()[0], p.children[0]); | ||
assert.equal(p.$getPopulatedDocs()[0].name, 'test'); | ||
assert.equal(p.$getPopulatedDocs()[1],p.child); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ESLint fails here because you need a space after ,
. It's a bit pedantic, I know, but it helps with readability since code is typically read more times than it is written.
No description provided.