mongoose-encryption
Advanced tools
Comparing version 0.9.0 to 0.10.0
31
index.js
@@ -5,2 +5,3 @@ (function() { | ||
var _ = require('underscore'); | ||
var dotty = require('dotty'); | ||
@@ -10,2 +11,6 @@ var ALGORITHM = 'aes-256-cbc'; | ||
var isEmbeddedDocument = function(doc) { | ||
return doc.constructor.name === 'EmbeddedDocument'; | ||
}; | ||
module.exports = function(schema, options) { | ||
@@ -41,4 +46,3 @@ | ||
schema.pre('init', function(next, data) { | ||
if (this.constructor.name === 'EmbeddedDocument'){ | ||
if (isEmbeddedDocument(this)) { | ||
this.decryptSync.call(data); | ||
@@ -54,3 +58,2 @@ this._doc = data; | ||
} | ||
}); | ||
@@ -65,2 +68,24 @@ | ||
schema.post('save', function(doc) { | ||
if (_.isFunction(doc.decryptSync)) doc.decryptSync(); | ||
// Mongoose doesn't trigger post save hook on EmbeddedDocuments, | ||
// instead have to call decrypt on all subDocs | ||
// ref https://github.com/LearnBoost/mongoose/issues/915 | ||
_.keys(doc.schema.paths).forEach(function(path) { | ||
if (path === '_id' || path === '__v') return; | ||
var nestedDoc = dotty.get(doc, path); | ||
if (nestedDoc && nestedDoc[0] && isEmbeddedDocument(nestedDoc[0])) { | ||
nestedDoc.forEach(function(subDoc) { | ||
if (_.isFunction(subDoc.decryptSync)) subDoc.decryptSync(); | ||
}); | ||
} | ||
}); | ||
return doc; | ||
}); | ||
schema.methods.encrypt = function(cb) { | ||
@@ -67,0 +92,0 @@ var that = this; |
{ | ||
"name": "mongoose-encryption", | ||
"description": "Simple encryption plugin for Mongoose", | ||
"version": "0.9.0", | ||
"version": "0.10.0", | ||
"author": { | ||
@@ -26,2 +26,3 @@ "name": "Joe Goldbeck" | ||
"dependencies": { | ||
"dotty": "0.0.2", | ||
"underscore": "1.5.x" | ||
@@ -33,3 +34,4 @@ }, | ||
"coffee-script": "1.7.x", | ||
"mongoose": "3.8.x" | ||
"mongoose": "3.8.x", | ||
"sinon": "~1.10.3" | ||
}, | ||
@@ -36,0 +38,0 @@ "engines": { |
@@ -50,3 +50,3 @@ mongoose-encryption | ||
And you're all set. You should be able to `find` and make `New` documents as normal, but you should not use the `lean` option on a `find` if you want the document to be decrypted. `findOne`, `findById`, etc... also all work as normal. `save` and `create` work great, but note that they both cause the document object to become encrypted (easily reversed with the `decrypt` instance method). `update` will work fine on unencrypted fields, but will not work correctly if encrypted fields are involved. | ||
And you're all set. You should be able to `find` and make `New` documents as normal, but you should not use the `lean` option on a `find` if you want the document to be decrypted. `findOne`, `findById`, etc..., as well as `save` and `create` also all work as normal. `update` will work fine on unencrypted fields, but will not work correctly if encrypted fields are involved. | ||
@@ -71,2 +71,24 @@ ### Exclude Certain Fields from Encryption | ||
### Encrypt Specific Fields of Sub Docs | ||
You can even encrypt fields of sub-documents while leaving the parent document otherwise intact, you just need to add it to the subdocument schema. | ||
``` | ||
var hidingPlaceSchema = new Schema({ | ||
latitude: Number, | ||
longitude: Number, | ||
nickname: String | ||
}); | ||
hidingPlaceSchema.plugin(encrypt, { | ||
key: process.env.KEY, | ||
exclude: ['nickname'] | ||
}); | ||
var userSchema = new Schema({ | ||
name: String, | ||
locationsOfGold: [hidingPlaceSchema] | ||
}); | ||
``` | ||
### Instance Methods | ||
@@ -92,2 +114,3 @@ | ||
``` | ||
There is also a `decryptSync` function, which executes synchronously and throws if an error is hit. | ||
@@ -94,0 +117,0 @@ ## Pros & Cons of Encrypting Multiple Fields at Once |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
42875
126
169
2
5
+ Addeddotty@0.0.2
+ Addeddotty@0.0.2(transitive)