From 9e383bfd49fa06babc2202595230eb8277265960 Mon Sep 17 00:00:00 2001 From: Doguhan Okumus Date: Thu, 17 Jun 2021 18:29:23 +0300 Subject: [PATCH] .select for .find and .findOne --- lib/mongoose-field-encryption.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/mongoose-field-encryption.js b/lib/mongoose-field-encryption.js index 40c6b4a..ccd103b 100644 --- a/lib/mongoose-field-encryption.js +++ b/lib/mongoose-field-encryption.js @@ -172,6 +172,19 @@ const fieldEncryption = function (schema, options) { } } + function findHook(_next) { + const next = getCompatitibleNextFunc(_next); + if (this.$useProjection) { + Object.keys(this._fields).forEach(key => { + if (fieldsToEncrypt.indexOf(key) === -1) return; + if (!this._fields[key] || (this._fields[key] !== 1 && this._fields[key] !== true)) return; + this._fields[`__enc_${key}`] = 1; + this._fields[`__enc_${key}_d`] = 1; + }) + } + next() + } + function updateHook(_next) { const next = getCompatitibleNextFunc(_next); for (const field of fieldsToEncrypt) { @@ -249,6 +262,7 @@ const fieldEncryption = function (schema, options) { } }); + schema.pre(['find', 'findOne'], findHook); schema.pre("findOneAndUpdate", updateHook); schema.pre("update", updateHook);