1const plugin = function updateOnPlugin(schema: any, options: any) {
2 // Add new property to Schema
3 schema.add({ updatedOn: Date });
4 schema.add({ createdOn: Date });
5
6 // Pre Hook
7 schema.pre('save', function (next: any) {
8 if (!this.isModified()) {
9 return next();
10 }
11 this.updatedOn = new Date;
12 if (!this.createdOn) {
13 this.createdOn = new Date;
14 }
15 next();
16 });
17
18 // Index
19 if (options && options.index) {
20 schema.path('updatedOn').index(options.index);
21 }
22};
23export default plugin;Created on 1/15/2020