New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

mongoose-hidden

Package Overview
Dependencies
Maintainers
1
Versions
42
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongoose-hidden - npm Package Compare versions

Comparing version

to
1.9.0

.github/FUNDING.yml

5

CHANGELOG.md
# Changelog
1.9.0
- feat: support for nested documents (issue #83)
- chore: remove dependency on 'debug'
1.8.1

@@ -4,0 +9,0 @@

26

lib/mongoose-hidden.js
'use strict'
const mpath = require('./mpath.js')
const log = require('debug')('mongoose-hidden')
const applyRecursively = 'applyRecursively'
const hide = 'hide'

@@ -180,2 +180,3 @@ const hideObject = 'hideObject'

options = {
applyRecursively: ensure(applyRecursively, defaults.applyRecursively),
hide: ensure(hide, defaults.autoHide),

@@ -205,2 +206,3 @@ hideJSON: ensure(hideJSON, defaults.autoHideJSON),

{
applyRecursively: false,
autoHide: true,

@@ -215,12 +217,9 @@ autoHideJSON: true,

return function(schema, options) {
const plugin = function(schema, options) {
options = prepOptions(options, _defaults)
log(options)
let paths = getPathnames(options, schema)
log(paths)
let transformer = function(target, prevTransform) {
return function(doc, transformed, opt) {
log(transformed)

@@ -230,13 +229,9 @@ // Apply existing transformer

if (typeof prevTransform === 'function') {
log('%s: running existing transform function', target)
finalTransform = prevTransform(doc, transformed, opt)
log(finalTransform)
}
// Copy real values
log('REAL AND NESTED VALUES')
paths.forEach(function(pathname) {
let schemaType = schema.path(pathname)
if (shouldHide(schemaType, options, target, doc, transformed, pathname)) {
log('%s: hiding "%s"', target, pathname)
mpath.unset(pathname, finalTransform)

@@ -246,3 +241,2 @@ } else {

if (typeof value !== 'undefined') {
log('%s: copy "%s"', target, pathname)
mpath.set(pathname, value, finalTransform)

@@ -254,6 +248,4 @@ }

// Copy virtual values
log('VIRTUAL VALUES')
for (let key in transformed) {
if (shouldCopyVirtual(schema, key, options, target)) {
log('%s: copy virtual "%s"', target, key)
mpath.set(key, mpath.get(key, transformed), finalTransform)

@@ -263,4 +255,2 @@ }

log('FINAL TRANSFORM')
log(finalTransform)
return finalTransform

@@ -285,3 +275,11 @@ }

})
if (options.applyRecursively) {
schema.childSchemas.forEach(child => {
plugin(child.schema, options)
})
}
}
return plugin
}
{
"name": "mongoose-hidden",
"version": "1.8.1",
"version": "1.9.0",
"author": "Michael Bøcker-Larsen <m19n@pm.me>",

@@ -29,4 +29,3 @@ "description": "Hides certain model properties when invoking toJSON or toObject.",

"dependencies": {
"debug": "^4.1.1",
"mpath": "^0.6.0"
"mpath": "^0.7.0"
},

@@ -33,0 +32,0 @@ "devDependencies": {

@@ -22,3 +22,3 @@ # mongoose-hidden

A simple example the hides passwords:
A simple example that hides passwords:

@@ -140,5 +140,10 @@ ```javascript

### Option: `applyRecursively`
Off by default, but when turned on the plugin will attach itself to any child
schemas as well.
### Transform
The `mongoose-hidden` is written as a transform function. If you implement your own transform functions be sure to add them to prior to applying the plugin. The plugin will then invoke that function before hiding properties.
The `mongoose-hidden` is written as a transform function. If you implement your own transform functions be sure to add them prior to applying the plugin. The plugin will then invoke that function before hiding properties.

@@ -172,3 +177,1 @@ ```javascript

```
* Recursive use of hide not supported, but nested documents/objects are supported.

@@ -170,2 +170,40 @@ 'use strict'

})
// Github issue https://github.com/mblarsen/mongoose-hidden/issues/82
describe('Bug: elements of array is visible', function() {
it('password should be hidden', function(done) {
const AuthSchema = new mongoose.Schema(
{
login: String,
password: {
type: String,
hide: true,
}
}, { _id: false }
)
const HostSchema = new mongoose.Schema(
{
name: String,
credentials: [AuthSchema],
},
{
timestamps: { createdAt: 'created', updatedAt: 'modified' },
}
)
HostSchema.set('toObject', { virtuals: true })
HostSchema.set('toJSON', { virtuals: true })
HostSchema.plugin(mongooseHidden, { applyRecursively: true })
const Host = mongoose.model('Host', HostSchema)
Host.create({ name: 'Main', credentials: [{ login: 'root', password: '123456' }] }).then(() => {
Host.findOne({ name: 'Main' }).then(host => {
const obj = host.toObject();
for (const cred of obj.credentials) {
Object.keys(cred).should.deepEqual(['login'])
}
done()
}).catch(done)
})
})
})
})