Skip to content
This repository was archived by the owner on Jul 14, 2019. It is now read-only.

Commit 3a0ef61

Browse files
Alex FordAlex Ford
authored andcommitted
Merge pull request #19 from Santinell/master
Added support of pre\post hooks
2 parents 14852ae + 1ffcf8c commit 3a0ef61

File tree

4 files changed

+57
-4
lines changed

4 files changed

+57
-4
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@ services:
33
- mongodb
44
node_js:
55
- "0.10"
6+
- "0.12"
7+
- "4.2"
68
notifications:
79
email: false

index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,17 @@ module.exports = exports = {
130130
schema.plugin(record.plugin, record.options);
131131
}
132132

133+
//Add pre hooks
134+
for (var key in modelData.pre) {
135+
var record = modelData.pre[key];
136+
schema.pre(key, record);
137+
}
138+
139+
//Add post hooks
140+
for (var key in modelData.post) {
141+
var record = modelData.post[key];
142+
schema.post(key, record);
143+
}
133144

134145
// If autoIncrementIds:true then utilize mongoose-auto-increment plugin for this model.
135146
if (settings.autoIncrementNumberIds) {

test/dbmodels/Author.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,25 @@ exports.schema = {
88
},
99
birthday: Date,
1010
gender: String,
11-
bio: String
11+
bio: String,
12+
updateCounter: {type: Number, default: 0}
1213
};
1314

15+
16+
exports.pre = {
17+
save: function (next) {
18+
this.updateCounter++;
19+
next();
20+
}
21+
};
22+
23+
exports.post = {
24+
save: function (record) {
25+
console.log("Post-hook is works!");
26+
}
27+
};
28+
29+
1430
exports.methods = {
1531
formatBirthday: function (formatString) {
1632
return moment(this.birthday).format(formatString);
@@ -46,4 +62,4 @@ exports.virtuals = {
4662
this.name.first = fullName;
4763
}
4864
}
49-
}
65+
}

test/test.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ describe("simpledb", function () {
4848
// We aren't specifying a models directory and the default models directory doesn't exist so this
4949
// should fail with a "readdir" error.
5050
should.exist(err);
51-
['ENOENT, readdir', 'ENOENT, scandir'].should.include(err.message.substr(0, 15));
51+
['ENOENT, readdir', 'ENOENT, scandir', 'ENOENT: no such'].should.include(err.message.substr(0, 15));
5252
done();
5353
});
5454
});
@@ -58,7 +58,7 @@ describe("simpledb", function () {
5858
// We aren't specifying a models directory and the default models directory doesn't exist so this
5959
// should fail with a "readdir" error.
6060
should.exist(err);
61-
['ENOENT, readdir', 'ENOENT, scandir'].should.include(err.message.substr(0, 15));
61+
['ENOENT, readdir', 'ENOENT, scandir', 'ENOENT: no such'].should.include(err.message.substr(0, 15));
6262
done();
6363
});
6464
});
@@ -194,6 +194,30 @@ describe("simpledb", function () {
194194

195195
});
196196

197+
it("should correct work with pre and post hooks", function (done) {
198+
199+
simpledb.init(options, function (err, db) {
200+
201+
var author = new db.Author({
202+
name: {
203+
first: 'Alex',
204+
last: 'Ford'
205+
},
206+
birthday: new Date('3/2/1987')
207+
});
208+
author.updateCounter.should.equal(0);
209+
author.save();
210+
211+
db.Author.findOne(function (err, record) {
212+
record.updateCounter.should.equal(1);
213+
done();
214+
});
215+
216+
});
217+
218+
219+
});
220+
197221
it("should correct set settings of mongoose-auto-increment plugin", function (done) {
198222

199223
var localOptions = extend({}, options, { modelsDir: path.join(__dirname, 'pluginsmodels'), autoIncrementSettings: { startAt: 5, field: 'id' } });

0 commit comments

Comments
 (0)