-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(model): apply projection parameter to
hydrate()
Fix #11375
- Loading branch information
Showing
10 changed files
with
167 additions
and
34 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,77 @@ | ||
'use strict'; | ||
|
||
const hasIncludedChildren = require('./hasIncludedChildren'); | ||
const isExclusive = require('./isExclusive'); | ||
const isInclusive = require('./isInclusive'); | ||
const isPOJO = require('../../utils').isPOJO; | ||
|
||
module.exports = function applyProjection(doc, projection, _hasIncludedChildren) { | ||
if (projection == null) { | ||
return doc; | ||
} | ||
if (doc == null) { | ||
return doc; | ||
} | ||
|
||
let exclude = null; | ||
if (isInclusive(projection)) { | ||
exclude = false; | ||
} else if (isExclusive(projection)) { | ||
exclude = true; | ||
} | ||
|
||
if (exclude == null) { | ||
return doc; | ||
} else if (exclude) { | ||
_hasIncludedChildren = _hasIncludedChildren || hasIncludedChildren(projection); | ||
return applyExclusiveProjection(doc, projection, _hasIncludedChildren); | ||
} else { | ||
_hasIncludedChildren = _hasIncludedChildren || hasIncludedChildren(projection); | ||
return applyInclusiveProjection(doc, projection, _hasIncludedChildren); | ||
} | ||
}; | ||
|
||
function applyExclusiveProjection(doc, projection, hasIncludedChildren, projectionLimb, prefix) { | ||
if (doc == null || typeof doc !== 'object') { | ||
return doc; | ||
} | ||
const ret = { ...doc }; | ||
projectionLimb = prefix ? (projectionLimb || {}) : projection; | ||
|
||
for (const key of Object.keys(ret)) { | ||
const fullPath = prefix ? prefix + '.' + key : key; | ||
if (projection.hasOwnProperty(fullPath) || projectionLimb.hasOwnProperty(key)) { | ||
if (isPOJO(projection[fullPath]) || isPOJO(projectionLimb[key])) { | ||
ret[key] = applyExclusiveProjection(ret[key], projection, hasIncludedChildren, projectionLimb[key], fullPath); | ||
} else { | ||
delete ret[key]; | ||
} | ||
} else if (hasIncludedChildren[fullPath]) { | ||
ret[key] = applyExclusiveProjection(ret[key], projection, hasIncludedChildren, projectionLimb[key], fullPath); | ||
} | ||
} | ||
return ret; | ||
} | ||
|
||
function applyInclusiveProjection(doc, projection, hasIncludedChildren, projectionLimb, prefix) { | ||
if (doc == null || typeof doc !== 'object') { | ||
return doc; | ||
} | ||
const ret = { ...doc }; | ||
projectionLimb = prefix ? (projectionLimb || {}) : projection; | ||
|
||
for (const key of Object.keys(ret)) { | ||
const fullPath = prefix ? prefix + '.' + key : key; | ||
if (projection.hasOwnProperty(fullPath) || projectionLimb.hasOwnProperty(key)) { | ||
if (isPOJO(projection[fullPath]) || isPOJO(projectionLimb[key])) { | ||
ret[key] = applyInclusiveProjection(ret[key], projection, hasIncludedChildren, projectionLimb[key], fullPath); | ||
} | ||
continue; | ||
} else if (hasIncludedChildren[fullPath]) { | ||
ret[key] = applyInclusiveProjection(ret[key], projection, hasIncludedChildren, projectionLimb[key], fullPath); | ||
} else { | ||
delete ret[key]; | ||
} | ||
} | ||
return ret; | ||
} |
This file contains 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,36 @@ | ||
'use strict'; | ||
|
||
/*! | ||
* Creates an object that precomputes whether a given path has child fields in | ||
* the projection. | ||
* | ||
* ####Example: | ||
* const res = hasIncludedChildren({ 'a.b.c': 0 }); | ||
* res.a; // 1 | ||
* res['a.b']; // 1 | ||
* res['a.b.c']; // 1 | ||
* res['a.c']; // undefined | ||
*/ | ||
|
||
module.exports = function hasIncludedChildren(fields) { | ||
const hasIncludedChildren = {}; | ||
const keys = Object.keys(fields); | ||
|
||
for (const key of keys) { | ||
if (key.indexOf('.') === -1) { | ||
hasIncludedChildren[key] = 1; | ||
continue; | ||
} | ||
const parts = key.split('.'); | ||
let c = parts[0]; | ||
|
||
for (let i = 0; i < parts.length; ++i) { | ||
hasIncludedChildren[c] = 1; | ||
if (i + 1 < parts.length) { | ||
c = c + '.' + parts[i + 1]; | ||
} | ||
} | ||
} | ||
|
||
return hasIncludedChildren; | ||
}; |
This file contains 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 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 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 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,22 @@ | ||
'use strict'; | ||
|
||
const applyProjection = require('../../lib/helpers/projection/applyProjection'); | ||
const assert = require('assert'); | ||
|
||
describe('applyProjection', function() { | ||
it('handles deep inclusive projections', function() { | ||
const obj = { str: 'test', nested: { str2: 'test2', num3: 42 } }; | ||
|
||
assert.deepEqual(applyProjection(obj, { str: 1 }), { str: 'test' }); | ||
assert.deepEqual(applyProjection(obj, { 'nested.str2': 1 }), { nested: { str2: 'test2' } }); | ||
assert.deepEqual(applyProjection(obj, { str: 1, nested: { num3: 1 } }), { str: 'test', nested: { num3: 42 } }); | ||
}); | ||
|
||
it('handles deep exclusive projections', function() { | ||
const obj = { str: 'test', nested: { str2: 'test2', num3: 42 } }; | ||
|
||
assert.deepEqual(applyProjection(obj, { nested: 0 }), { str: 'test' }); | ||
assert.deepEqual(applyProjection(obj, { 'nested.str2': 0 }), { str: 'test', nested: { num3: 42 } }); | ||
assert.deepEqual(applyProjection(obj, { nested: { num3: 0 } }), { str: 'test', nested: { str2: 'test2' } }); | ||
}); | ||
}); |
This file contains 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