|
1 |
| -# [**@tsmx/mongoose-encryptedstring**](https://github.com/tsmx/mongoose-encryptedstring) |
| 1 | +# [**@tsmx/mongoose-encrypted-string**](https://github.com/tsmx/mongoose-encrypted-string) |
2 | 2 |
|
3 | 3 | [](https://opensource.org/licenses/MIT)
|
4 |
| - |
5 |
| - |
6 |
| -[](https://travis-ci.org/tsmx/mongoose-encryptedstring) |
7 |
| -[](https://coveralls.io/github/tsmx/mongoose-encryptedstring?branch=master) |
| 4 | + |
| 5 | + |
| 6 | +[](https://travis-ci.org/tsmx/mongoose-encrypted-string) |
| 7 | +[](https://coveralls.io/github/tsmx/mongoose-encrypted-string?branch=master) |
8 | 8 |
|
9 |
| -> `EncryptedString` type for Mongoose schemas. |
10 |
| -
|
11 |
| -AES-256-CBC encryption-at-rest for strings. |
| 9 | +> `EncryptedString` type for Mongoose schemas. Provides AES-256-CBC encryption-at-rest for strings. |
12 | 10 |
|
13 | 11 | ## Usage
|
14 | 12 |
|
15 | 13 | ```js
|
16 | 14 | var mongoose = require('mongoose');
|
17 |
| -const encryptedString = require('@tsmx/mongoose-encryptedstring'); |
| 15 | +const mes = require('@tsmx/mongoose-encrypted-string'); |
18 | 16 | const key = 'YOUR KEY HERE';
|
19 | 17 |
|
20 | 18 | // register the new type EncryptedString
|
21 |
| -encryptedString.registerEncryptedString(mongoose, key); |
| 19 | +mes.registerEncryptedString(mongoose, key); |
22 | 20 |
|
23 | 21 | // use EncryptedString in your schemas
|
24 | 22 | Person = mongoose.model('Person', {
|
@@ -64,6 +62,24 @@ The mongoose instance where `EncryptedString` should be registered.
|
64 | 62 |
|
65 | 63 | The key used for encryption/decryption. Length must be 32 bytes. See [notes](#notes) for details.
|
66 | 64 |
|
| 65 | +## Use with lean() queries |
| 66 | + |
| 67 | +For performance reasons it maybe useful to use Mongoose's `lean()` queries. Doing so, the query will return the raw JSON objects from the MongoDB database where all properties of type `EncryptedString` are encrypted. |
| 68 | + |
| 69 | +To get the clear text values back you can directly use [@tsmx/string-crypto](https://www.npmjs.com/package/@tsmx/string-crypto) which is also used internally in this package for encryption and decryption. |
| 70 | + |
| 71 | +```js |
| 72 | +const key = 'YOUR KEY HERE'; |
| 73 | +const sc = require('@tsmx/string-crypto'); |
| 74 | + |
| 75 | +// query raw objects with encrypted string values |
| 76 | +let person = await Person.findOne({ id: 'id-test' }).lean(); |
| 77 | + |
| 78 | +// decrypt using string-crypto |
| 79 | +let firstName = sc.decrypt(person.firstName, { key: key }); |
| 80 | +let lastName = sc.decrypt(person.lastName, { key: key }); |
| 81 | +``` |
| 82 | + |
67 | 83 | ## Notes
|
68 | 84 |
|
69 | 85 | - Encryption/decryption is done via the package [@tsmx/string-crypto](https://www.npmjs.com/package/@tsmx/string-crypto).
|
|
0 commit comments