mongoose-hidden
Advanced tools
Comparing version
# Changelog | ||
1.8.0 | ||
- reveret to old setPath of pre-1.7 but still use the mpath.set patching approach to make the client compatible with mpath.set | ||
1.7.0 | ||
@@ -4,0 +8,0 @@ |
const mpath = require('mpath') | ||
const _originalSet = mpath.set | ||
/** | ||
* Convert path parts into a nested object | ||
* | ||
* @access private | ||
* @param {array} parts an array of parts on the path | ||
* @param {mixed} value the value to set at the end of the path | ||
* @returns {object} an object corresponding to the path that the parts represents | ||
*/ | ||
const partsToValue = function(parts, value) { | ||
if (parts.length === 0) { | ||
return value | ||
} | ||
let obj = {} | ||
obj[parts[0]] = partsToValue(parts.slice(1), value) | ||
return obj | ||
} | ||
mpath.set = function(path, value, obj) { | ||
_originalSet(path, value, obj) | ||
// mpath will not set a value undefined keys | ||
// so we traverse backwards to the last known | ||
// key recursively | ||
if (mpath.get(path, obj) !== value) { | ||
const pos = path.lastIndexOf('.') | ||
const key = path.substr(pos + 1) | ||
if (key === path) { | ||
obj[key] = value | ||
} else { | ||
mpath.set(path.substr(0, pos), { [key]: value }, obj) | ||
} | ||
/** | ||
* Set a value in object denoted by dot-path | ||
* | ||
* @access private | ||
* @param {object} obj source object | ||
* @param {string} path a dot-path | ||
* @param {mixed} value the value to set at the end of the path | ||
*/ | ||
const setPath = function(obj, path, value) { | ||
const parts = path.split('.') | ||
/* traverse existing path to nearest object */ | ||
while (parts.length > 1 && typeof obj[parts[0]] === 'object') { | ||
obj = obj[parts.shift()] | ||
} | ||
/* set value */ | ||
obj[parts[0]] = partsToValue(parts.slice(1), value) | ||
} | ||
// Map setPath arguments to mpath.set | ||
mpath.set = function(path, value, obj) { | ||
setPath(obj, path, value) | ||
} | ||
module.exports = mpath |
{ | ||
"name": "mongoose-hidden", | ||
"version": "1.7.1", | ||
"version": "1.8.0", | ||
"author": "Michael Bøcker-Larsen <m19n@pm.me>", | ||
@@ -5,0 +5,0 @@ "description": "Hides certain model properties when invoking toJSON or toObject.", |
@@ -695,2 +695,108 @@ 'use strict' | ||
// https://github.com/mblarsen/mongoose-hidden/issues/73 | ||
describe('A document with nested documents when hiding', function() { | ||
it("shouldn't remove its nested documents", function(done) { | ||
mongoose.modelSchemas = {} | ||
mongoose.models = {} | ||
let Company = defineModel( | ||
'Company', | ||
{ | ||
name: String, | ||
code: String, | ||
}, | ||
{ hideObject: false }, | ||
{} | ||
) | ||
let User = defineModel('User', { | ||
name: String, | ||
email: String, | ||
companies: [{ | ||
description: { type: String }, | ||
link: { type: Schema.ObjectId, ref: 'Company' } | ||
}], | ||
password: { | ||
type: String, | ||
hide: true, | ||
}, | ||
}) | ||
let company = new Company(testCompany) | ||
let user = new User(testUser) | ||
company.save(function(err, freshCompany) { | ||
user.companies = { description: 'mock-description', link: company._id} | ||
user.save(function() { | ||
User.findOne() | ||
.populate('companies.link') | ||
.exec(function(err, freshUser) { | ||
should.exist(freshUser.name) | ||
should.exist(freshUser.email) | ||
should.exist(freshUser.password) | ||
should.exist(freshUser.companies) | ||
should.exist(freshUser.companies[0]) | ||
should.exist(freshUser.companies[0].description) // failed | ||
should.exist(freshUser.companies[0].link) // failed | ||
let userJson = freshUser.toJSON() | ||
should.not.exist(userJson.password) | ||
should.exist(userJson.name) | ||
should.exist(userJson.email) | ||
should.exist(userJson.companies) | ||
should.exist(userJson.companies[0].description) // failed | ||
should.exist(userJson.companies[0].link) // failed | ||
done() | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
describe('A document with nested documents (array refs) when hiding', function() { | ||
it("shouldn't remove its nested documents", function(done) { | ||
mongoose.modelSchemas = {} | ||
mongoose.models = {} | ||
let Company = defineModel( | ||
'Company', | ||
{ | ||
name: String, | ||
code: String, | ||
}, | ||
{ hideObject: false }, | ||
{} | ||
) | ||
let User = defineModel('User', { | ||
name: String, | ||
email: String, | ||
companies: [ | ||
{ | ||
type: Schema.ObjectId, | ||
ref: 'Company', | ||
}, | ||
], | ||
password: { | ||
type: String, | ||
hide: true, | ||
}, | ||
}) | ||
let company = new Company(testCompany) | ||
let user = new User(testUser) | ||
company.save(function(err, freshCompany) { | ||
user.companies.push(company._id) | ||
user.save(function() { | ||
User.findOne() | ||
.populate('companies') | ||
.exec(function(err, freshUser) { | ||
should.exist(freshUser.companies) | ||
should.equal(1, freshUser.companies.length) | ||
freshUser.companies[0].name.should.equal('GOGGLE') | ||
let userJson = freshUser.toJSON() | ||
should.equal(1, userJson.companies.length) | ||
should.equal('GOGGLE', userJson.companies[0].name) | ||
done() | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
describe('A document with a collection of nested documents', function() { | ||
@@ -955,2 +1061,29 @@ it("shouldn't remove its nested documents", function(done) { | ||
}) | ||
describe('mpath patch', () => { | ||
it('should throw if not string', () => { | ||
const set = () => mpath.set(null, 'Jane', {}) | ||
set.should.throw(TypeError) | ||
}) | ||
it('shoud set key if path = key', () => { | ||
const result = {} | ||
mpath.set('name', 'Jane', result) | ||
result.name.should.equal('Jane') | ||
}) | ||
it('should set key at depth', () => { | ||
const result = {} | ||
mpath.set('my.name.is', 'Jane', result) | ||
result.my.name.is.should.equal('Jane') | ||
}) | ||
it('should set key at depth and preserve other keys', () => { | ||
const result = { my: { dog: { Kenny: true } } } | ||
mpath.set('my.name.is', 'Jane', result) | ||
result.my.name.is.should.equal('Jane') | ||
result.my.dog.Kenny.should.equal(true) | ||
}) | ||
it('should be able to set arrays', () => { | ||
const result = {} | ||
mpath.set('my.name', ['Jane', 'Doe'], result) | ||
result.my.name.should.deepEqual(['Jane', 'Doe']) | ||
}) | ||
}) | ||
}) |
62548
8.8%18
5.88%1588
10.35%