|
1 | 1 | # mongoose-field-encryption |
2 | 2 |
|
3 | | -[](https://travis-ci.org/wheresvic/mongoose-field-encryption) [](https://coveralls.io/github/wheresvic/mongoose-field-encryption?branch=master) |
| 3 | + [](https://coveralls.io/github/wheresvic/mongoose-field-encryption?branch=master) |
4 | 4 |
|
5 | 5 | A simple symmetric encryption plugin for individual fields. The goal of this plugin is to encrypt data but still allow searching over fields with string values. This plugin relies on the Node `crypto` module. Encryption and decryption happen transparently during save and find. |
6 | 6 |
|
@@ -160,6 +160,52 @@ const encrypted = fieldEncryption.encrypt('some text', 'secret')); |
160 | 160 | const decrypted = fieldEncryption.decrypt(encrypted, 'secret')); // decrypted = 'some text' |
161 | 161 | ``` |
162 | 162 |
|
| 163 | +### encryption of nested fields |
| 164 | + |
| 165 | +Note that while this plugin is designed to encrypt only top level fields, nested fields can be easily encrypted by creating a mongoose schema for the nested objects and adding the plugin to them. |
| 166 | + |
| 167 | +See comment for discussion: [https://github.com/wheresvic/mongoose-field-encryption/issues/34#issuecomment-577383776](https://github.com/wheresvic/mongoose-field-encryption/issues/34#issuecomment-577383776) |
| 168 | + |
| 169 | +Example |
| 170 | + |
| 171 | +```js |
| 172 | +const mongoose = require('mongoose'); |
| 173 | +const mongooseFieldEncryption = require("mongoose-field-encryption").fieldEncryption; |
| 174 | + |
| 175 | +const CredentialSchema = new mongoose.Schema({ |
| 176 | + type: { |
| 177 | + required: true, |
| 178 | + type: String, |
| 179 | + }, |
| 180 | + value: { |
| 181 | + required: true, |
| 182 | + type: String, |
| 183 | + }, |
| 184 | +}); |
| 185 | + |
| 186 | +CredentialSchema.plugin(mongooseFieldEncryption, { |
| 187 | + fields: ["value"], |
| 188 | + secret: process.env.MONGOOSE_ENCRYPTION_KEY, |
| 189 | +}); |
| 190 | + |
| 191 | +const accountSchema = new mongoose.Schema({ |
| 192 | + provider: { |
| 193 | + type: String, |
| 194 | + required: true, |
| 195 | + lowercase: true, |
| 196 | + trim: true, |
| 197 | + }, |
| 198 | + credentials: [CredentialSchema], |
| 199 | + owner: { |
| 200 | + required: true, |
| 201 | + type: mongoose.Schema.Types.ObjectId, |
| 202 | + ref: 'User', |
| 203 | + }, |
| 204 | +}); |
| 205 | + |
| 206 | +module.exports = mongoose.model('Account', accountSchema); |
| 207 | +``` |
| 208 | + |
163 | 209 | ## Development |
164 | 210 |
|
165 | 211 | As of version 3.0.5, one can setup a local development mongodb instance using docker: |
@@ -189,6 +235,11 @@ Feel free to make changes to the default docker configuration as required. |
189 | 235 |
|
190 | 236 | ## Changelog |
191 | 237 |
|
| 238 | +### 4.0.1 |
| 239 | + |
| 240 | +- Update documentation to add nested field encryption example |
| 241 | +- Switch from Travis to Github actions |
| 242 | + |
192 | 243 | ### 4.0.0 |
193 | 244 |
|
194 | 245 | - _FEATURE_: Add support for an optional synchronous secret function instead of a fixed string. Note that while this change should be backwards compatible, care should be taken as an issues with the secret could lead to irrecoverable documents! |
|
0 commit comments