-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
feat(model): add Model.applyVirtuals() to apply virtuals to a POJO
#14905
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
50b7bc9
feat(model): add `Model.applyVirtuals()` to apply virtuals to a POJO
vkarpov15 bcbc9e0
Update lib/model.js
vkarpov15 a199929
Update lib/model.js
vkarpov15 c143d8e
remove duplicate test
vkarpov15 660aad0
docs(applyVirtuals): add jsdoc comments and clean up some unnecessary…
vkarpov15 fdfb385
refactor: rename doc to obj in applyVirtuals helper
vkarpov15 54844e3
types: add applyVirtuals() to types
vkarpov15 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| 'use strict'; | ||
|
|
||
| const mpath = require('mpath'); | ||
|
|
||
| module.exports = applyVirtuals; | ||
|
|
||
| /** | ||
| * Apply a given schema's virtuals to a given POJO | ||
| * | ||
| * @param {Schema} schema | ||
| * @param {Object} obj | ||
| * @param {Array<string>} [virtuals] optional whitelist of virtuals to apply | ||
| * @returns | ||
| */ | ||
|
|
||
| function applyVirtuals(schema, obj, virtuals) { | ||
| if (obj == null) { | ||
| return obj; | ||
| } | ||
|
|
||
| let virtualsForChildren = virtuals; | ||
| let toApply = null; | ||
|
|
||
| if (Array.isArray(virtuals)) { | ||
| virtualsForChildren = []; | ||
| toApply = []; | ||
| for (const virtual of virtuals) { | ||
| if (virtual.length === 1) { | ||
| toApply.push(virtual[0]); | ||
| } else { | ||
| virtualsForChildren.push(virtual); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| applyVirtualsToChildren(schema, obj, virtualsForChildren); | ||
| return applyVirtualsToDoc(schema, obj, toApply); | ||
| } | ||
|
|
||
| /** | ||
| * Apply virtuals to any subdocuments | ||
| * | ||
| * @param {Schema} schema subdocument schema | ||
| * @param {Object} res subdocument | ||
| * @param {Array<String>} [virtuals] optional whitelist of virtuals to apply | ||
| */ | ||
|
|
||
| function applyVirtualsToChildren(schema, res, virtuals) { | ||
| let attachedVirtuals = false; | ||
| for (const childSchema of schema.childSchemas) { | ||
| const _path = childSchema.model.path; | ||
| const _schema = childSchema.schema; | ||
| if (!_path) { | ||
| continue; | ||
| } | ||
| const _obj = mpath.get(_path, res); | ||
| if (_obj == null || (Array.isArray(_obj) && _obj.flat(Infinity).length === 0)) { | ||
| continue; | ||
| } | ||
|
|
||
| let virtualsForChild = null; | ||
| if (Array.isArray(virtuals)) { | ||
| virtualsForChild = []; | ||
| for (const virtual of virtuals) { | ||
| if (virtual[0] == _path) { | ||
| virtualsForChild.push(virtual.slice(1)); | ||
| } | ||
| } | ||
|
|
||
| if (virtualsForChild.length === 0) { | ||
| continue; | ||
| } | ||
| } | ||
|
|
||
| applyVirtuals(_schema, _obj, virtualsForChild); | ||
| attachedVirtuals = true; | ||
| } | ||
|
|
||
| if (virtuals && virtuals.length && !attachedVirtuals) { | ||
| applyVirtualsToDoc(schema, res, virtuals); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Apply virtuals to a given document. Does not apply virtuals to subdocuments: use `applyVirtualsToChildren` instead | ||
| * | ||
| * @param {Schema} schema | ||
| * @param {Object} doc | ||
| * @param {Array<String>} [virtuals] optional whitelist of virtuals to apply | ||
| * @returns | ||
| */ | ||
|
|
||
| function applyVirtualsToDoc(schema, obj, virtuals) { | ||
| if (obj == null || typeof obj !== 'object') { | ||
| return; | ||
| } | ||
| if (Array.isArray(obj)) { | ||
| for (const el of obj) { | ||
| applyVirtualsToDoc(schema, el, virtuals); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| if (schema.discriminators && Object.keys(schema.discriminators).length > 0) { | ||
| for (const discriminatorKey of Object.keys(schema.discriminators)) { | ||
| const discriminator = schema.discriminators[discriminatorKey]; | ||
| const key = discriminator.discriminatorMapping.key; | ||
| const value = discriminator.discriminatorMapping.value; | ||
| if (obj[key] == value) { | ||
| schema = discriminator; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (virtuals == null) { | ||
| virtuals = Object.keys(schema.virtuals); | ||
| } | ||
| for (const virtual of virtuals) { | ||
| if (schema.virtuals[virtual] == null) { | ||
| continue; | ||
| } | ||
| const virtualType = schema.virtuals[virtual]; | ||
| const sp = Array.isArray(virtual) | ||
| ? virtual | ||
| : virtual.indexOf('.') === -1 | ||
| ? [virtual] | ||
| : virtual.split('.'); | ||
| let cur = obj; | ||
| for (let i = 0; i < sp.length - 1; ++i) { | ||
| cur[sp[i]] = sp[i] in cur ? cur[sp[i]] : {}; | ||
| cur = cur[sp[i]]; | ||
| } | ||
| let val = virtualType.applyGetters(cur[sp[sp.length - 1]], obj); | ||
| const isPopulateVirtual = | ||
| virtualType.options && (virtualType.options.ref || virtualType.options.refPath); | ||
| if (isPopulateVirtual && val === undefined) { | ||
| if (virtualType.options.justOne) { | ||
| val = null; | ||
| } else { | ||
| val = []; | ||
| } | ||
| } | ||
| cur[sp[sp.length - 1]] = val; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.