Skip to content

Commit 44ae465

Browse files
adilevyattentiwheresvic
authored andcommitted
Issue #93: Add insertMany Pre hook
- Added a pre hook for insertMany operation. - Added a test
1 parent 99d9499 commit 44ae465

File tree

3 files changed

+49
-10
lines changed

3 files changed

+49
-10
lines changed

lib/mongoose-field-encryption.js

+14
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,20 @@ const fieldEncryption = function (schema, options) {
249249
}
250250
});
251251

252+
schema.pre("insertMany", function (_next, docs) {
253+
const next = getCompatitibleNextFunc(_next);
254+
255+
try {
256+
for (let doc of docs) {
257+
encryptFields(doc, fieldsToEncrypt, secret());
258+
}
259+
260+
next();
261+
} catch (err) {
262+
next(err);
263+
}
264+
});
265+
252266
schema.pre("findOneAndUpdate", updateHook);
253267

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

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mongoose-field-encryption",
3-
"version": "6.0.0",
3+
"version": "6.0.1",
44
"description": "A simple symmetric encryption plugin for individual fields. Dependency free, only mongoose peer dependency.",
55
"main": "lib/mongoose-field-encryption.js",
66
"types": "lib/mongoose-field-encryption.d.ts",

test/test-basic-usage.js

+34-9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ const mongooseFieldEncryption = require("../lib/mongoose-field-encryption").fiel
1414

1515
const uri = process.env.URI || "mongodb://127.0.0.1:27017/mongoose-field-encryption-test";
1616

17+
const postSchema = new Schema({
18+
title: String,
19+
message: String,
20+
});
21+
22+
postSchema.plugin(mongooseFieldEncryption, { fields: ["message"], secret: "some secret key" });
23+
24+
const Post = mongoose.model("Post", postSchema);
25+
1726
describe("basic usage", function () {
1827
this.timeout(5000);
1928

@@ -25,6 +34,7 @@ describe("basic usage", function () {
2534
});
2635

2736
setupMongoose(mongoose);
37+
2838
});
2939

3040
after(function (done) {
@@ -34,15 +44,7 @@ describe("basic usage", function () {
3444
});
3545

3646
it("should save a document", function (done) {
37-
// given
38-
const postSchema = new Schema({
39-
title: String,
40-
message: String,
41-
});
42-
43-
postSchema.plugin(mongooseFieldEncryption, { fields: ["message"], secret: "some secret key" });
44-
45-
const Post = mongoose.model("Post", postSchema);
47+
4648
const post = new Post({ title: "some text", message: "hello all" });
4749

4850
// when
@@ -64,6 +66,29 @@ describe("basic usage", function () {
6466
});
6567
});
6668

69+
it("should save many documents", function (done) {
70+
71+
const post = [new Post({ title: "some text", message: "hello all" }),
72+
new Post({title: "some other text", message: "hello many" }),
73+
new Post({title: "some other other text", message: "aloha!" })];
74+
75+
// when
76+
Post.insertMany(post, function (err) {
77+
// then
78+
if (err) {
79+
return done(err);
80+
}
81+
82+
expect(post[0].title).to.equal("some text");
83+
expect(post[0].message).to.not.be.undefined;
84+
const split = post[0].message.split(":");
85+
expect(split.length).to.equal(2);
86+
expect(post[0].__enc_message).to.be.true;
87+
88+
done();
89+
});
90+
});
91+
6792
it("should search for a document on an encrypted field", function (done) {
6893
// given
6994
const messageSchema = new Schema({

0 commit comments

Comments
 (0)