Skip to content

Commit 9e383bf

Browse files
committed
.select for .find and .findOne
1 parent 036917d commit 9e383bf

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

lib/mongoose-field-encryption.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,19 @@ const fieldEncryption = function (schema, options) {
172172
}
173173
}
174174

175+
function findHook(_next) {
176+
const next = getCompatitibleNextFunc(_next);
177+
if (this.$useProjection) {
178+
Object.keys(this._fields).forEach(key => {
179+
if (fieldsToEncrypt.indexOf(key) === -1) return;
180+
if (!this._fields[key] || (this._fields[key] !== 1 && this._fields[key] !== true)) return;
181+
this._fields[`__enc_${key}`] = 1;
182+
this._fields[`__enc_${key}_d`] = 1;
183+
})
184+
}
185+
next()
186+
}
187+
175188
function updateHook(_next) {
176189
const next = getCompatitibleNextFunc(_next);
177190
for (const field of fieldsToEncrypt) {
@@ -249,6 +262,7 @@ const fieldEncryption = function (schema, options) {
249262
}
250263
});
251264

265+
schema.pre(['find', 'findOne'], findHook);
252266
schema.pre("findOneAndUpdate", updateHook);
253267

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

0 commit comments

Comments
 (0)