Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/mongoose-field-encryption.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,20 @@ const fieldEncryption = function (schema, options) {
}
});

schema.pre("insertMany", function (_next, docs) {
const next = getCompatitibleNextFunc(_next);

try {
for (let doc of docs) {
encryptFields(doc, fieldsToEncrypt, secret());
}

next();
} catch (err) {
next(err);
}
});

schema.pre("findOneAndUpdate", updateHook);

schema.pre("update", updateHook);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mongoose-field-encryption",
"version": "6.0.0",
"version": "6.0.1",
"description": "A simple symmetric encryption plugin for individual fields. Dependency free, only mongoose peer dependency.",
"main": "lib/mongoose-field-encryption.js",
"types": "lib/mongoose-field-encryption.d.ts",
Expand Down
43 changes: 34 additions & 9 deletions test/test-basic-usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ const mongooseFieldEncryption = require("../lib/mongoose-field-encryption").fiel

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

const postSchema = new Schema({
title: String,
message: String,
});

postSchema.plugin(mongooseFieldEncryption, { fields: ["message"], secret: "some secret key" });

const Post = mongoose.model("Post", postSchema);

describe("basic usage", function () {
this.timeout(5000);

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

setupMongoose(mongoose);

});

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

it("should save a document", function (done) {
// given
const postSchema = new Schema({
title: String,
message: String,
});

postSchema.plugin(mongooseFieldEncryption, { fields: ["message"], secret: "some secret key" });

const Post = mongoose.model("Post", postSchema);

const post = new Post({ title: "some text", message: "hello all" });

// when
Expand All @@ -64,6 +66,29 @@ describe("basic usage", function () {
});
});

it("should save many documents", function (done) {

const post = [new Post({ title: "some text", message: "hello all" }),
new Post({title: "some other text", message: "hello many" }),
new Post({title: "some other other text", message: "aloha!" })];

// when
Post.insertMany(post, function (err) {
// then
if (err) {
return done(err);
}

expect(post[0].title).to.equal("some text");
expect(post[0].message).to.not.be.undefined;
const split = post[0].message.split(":");
expect(split.length).to.equal(2);
expect(post[0].__enc_message).to.be.true;

done();
});
});

it("should search for a document on an encrypted field", function (done) {
// given
const messageSchema = new Schema({
Expand Down