Skip to content

Commit 7a16baa

Browse files
Merge branch 'creatorden' into lean-for-post-find-findone
2 parents 40cdb16 + 280be73 commit 7a16baa

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

lib/mongoose-field-encryption.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,19 @@ const fieldEncryption = function (schema, options) {
191191
}
192192
}
193193

194+
function findHook(_next) {
195+
const next = getCompatitibleNextFunc(_next);
196+
if (this.$useProjection) {
197+
Object.keys(this._fields).forEach(key => {
198+
if (fieldsToEncrypt.indexOf(key) === -1) return;
199+
if (!this._fields[key] || (this._fields[key] !== 1 && this._fields[key] !== true)) return;
200+
this._fields[`__enc_${key}`] = 1;
201+
this._fields[`__enc_${key}_d`] = 1;
202+
})
203+
}
204+
next()
205+
}
206+
194207
function updateHook(_next) {
195208
const next = getCompatitibleNextFunc(_next);
196209
for (const field of fieldsToEncrypt) {
@@ -300,7 +313,7 @@ const fieldEncryption = function (schema, options) {
300313
next();
301314
}
302315
})
303-
316+
schema.pre(['find', 'findOne'], findHook);
304317
schema.pre("findOneAndUpdate", updateHook);
305318

306319
schema.pre("update", updateHook);

0 commit comments

Comments
 (0)