Skip to content

Commit ff62923

Browse files
committed
Add support for custom properties in types, closes #32 and closes #36
1 parent 2b7d91f commit ff62923

File tree

5 files changed

+118
-25
lines changed

5 files changed

+118
-25
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ node_js:
1010
- "iojs"
1111
services:
1212
- mongodb
13+
- elasticsearch

index.js

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,39 @@
55
* MIT Licensed
66
*/
77

8+
var defaults = require('defaults');
9+
810
function timestampsPlugin(schema, options) {
911
var updatedAt = 'updatedAt';
1012
var createdAt = 'createdAt';
11-
var updatedAtType = Date;
12-
var createdAtType = Date;
13-
13+
var updatedAtOpts = Date;
14+
var createdAtOpts = Date;
15+
var dataObj = {};
16+
1417
if (typeof options === 'object') {
15-
if (typeof options.updatedAt === 'string') {
16-
updatedAt = options.updatedAt;
17-
} else if (typeof options.updatedAt === 'object') {
18-
updatedAt = options.updatedAt.name || updatedAt;
19-
updatedAtType = options.updatedAt.type || updatedAtType;
20-
}
21-
if (typeof options.createdAt === 'string') {
22-
createdAt = options.createdAt;
23-
} else if (typeof options.createdAt === 'object') {
24-
createdAt = options.createdAt.name || createdAt;
25-
createdAtType = options.createdAt.type || createdAtType;
26-
}
18+
if (typeof options.updatedAt === 'string') {
19+
updatedAt = options.updatedAt;
20+
} else if (typeof options.updatedAt === 'object') {
21+
updatedAtOpts = defaults(options.updatedAt, {
22+
name: updatedAt,
23+
type: Date
24+
});
25+
updatedAt = updatedAtOpts.name;
26+
}
27+
28+
if (typeof options.createdAt === 'string') {
29+
createdAt = options.createdAt;
30+
} else if (typeof options.createdAt === 'object') {
31+
createdAtOpts = defaults(options.createdAt, {
32+
name: createdAt,
33+
type: Date
34+
});
35+
createdAt = createdAtOpts.name;
36+
}
2737
}
2838

29-
var dataObj = {};
30-
dataObj[updatedAt] = updatedAtType;
39+
dataObj[updatedAt] = updatedAtOpts;
40+
3141
if (schema.path(createdAt)) {
3242
schema.add(dataObj);
3343
schema.virtual(createdAt)
@@ -43,9 +53,9 @@ function timestampsPlugin(schema, options) {
4353
}
4454
next();
4555
});
46-
56+
4757
} else {
48-
dataObj[createdAt] = createdAtType;
58+
dataObj[createdAt] = createdAtOpts;
4959
schema.add(dataObj);
5060
schema.pre('save', function (next) {
5161
if (!this[createdAt]) {
@@ -56,7 +66,7 @@ function timestampsPlugin(schema, options) {
5666
next();
5767
});
5868
}
59-
69+
6070
schema.pre('findOneAndUpdate', function (next) {
6171
if (this.op === 'findOneAndUpdate') {
6272
this._update = this._update || {};

package.json

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "mongoose-timestamp",
33
"description": "Mongoose plugin that adds createdAt and updatedAt auto-assigned date properties",
4-
"version": "0.5.0",
4+
"version": "0.6.0",
55
"author": "Nicholas Penree <[email protected]>",
66
"keywords": [
77
"mongodb",
@@ -16,15 +16,20 @@
1616
"url": "git://github.com/drudge/mongoose-timestamp"
1717
},
1818
"devDependencies": {
19-
"mongoose": "~4.0.1",
20-
"should": "~3.2.0",
21-
"mocha": "~1.18.2"
19+
"mocha": "~2.5.3",
20+
"mongoosastic": "^4.0.2",
21+
"mongoose": "~4.5.3",
22+
"request": "^2.72.0",
23+
"should": "~9.0.2"
2224
},
2325
"scripts": {
2426
"test": "mocha -u bdd -R spec -c ./test/*test.js"
2527
},
2628
"license": "MIT",
2729
"engine": {
2830
"node": ">= 0.6"
31+
},
32+
"dependencies": {
33+
"defaults": "^1.0.3"
2934
}
3035
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
2+
/**
3+
* @list dependencies
4+
**/
5+
6+
var mocha = require('mocha');
7+
var should = require('should');
8+
var mongoose = require('mongoose');
9+
var Schema = mongoose.Schema;
10+
var timestamps = require('../');
11+
var request = require('request');
12+
var mongoosastic = require('mongoosastic');
13+
14+
mongoose = mongoose.createConnection('mongodb://localhost/mongoose_timestamps')
15+
mongoose.on('error', function (err) {
16+
console.error('MongoDB error: ' + err.message);
17+
console.error('Make sure a mongoDB server is running and accessible by this application')
18+
});
19+
20+
var opts = {
21+
createdAt: {
22+
name: 'customNameCreatedAt',
23+
type: String,
24+
index: true
25+
},
26+
updatedAt: {
27+
name: 'customNameUpdatedAt',
28+
type: String,
29+
es_indexed: true
30+
}
31+
};
32+
33+
var CustomizedTypeOptionsTimeCopSchema = new Schema({
34+
email: String
35+
});
36+
37+
CustomizedTypeOptionsTimeCopSchema.plugin(timestamps, opts);
38+
CustomizedTypeOptionsTimeCopSchema.plugin(mongoosastic);
39+
40+
var CustomizedTypeOptionsTimeCop = mongoose.model('CustomizedTypeOptionsTimeCop', CustomizedTypeOptionsTimeCopSchema);
41+
42+
describe('timestamps custom names and types with options', function() {
43+
it('should create an index when passed in the createdAt options', function(done) {
44+
CustomizedTypeOptionsTimeCop.collection.getIndexes(function(err, res) {
45+
var idx = res[opts.createdAt.name + '_1'];
46+
idx.should.be.an.Array().with.length(1);
47+
var comp = idx[0];
48+
comp.should.be.an.Array().with.length(2);
49+
comp[0].should.equal(opts.createdAt.name);
50+
done();
51+
});
52+
});
53+
54+
it('should create an elastic search index when passed es_indexed = true', function(done) {
55+
var customCop = new CustomizedTypeOptionsTimeCop({email: '[email protected]'});
56+
customCop.save(function (err) {
57+
request({
58+
url: 'http://127.0.0.1:9200/customizedtypeoptionstimecops',
59+
json: true
60+
}, function(err, res, body) {
61+
if (err) return done(err);
62+
body.should.have.a.property('customizedtypeoptionstimecops');
63+
body.customizedtypeoptionstimecops.should.have.a.property('mappings');
64+
body.customizedtypeoptionstimecops.mappings.should.have.a.property('customizedtypeoptionstimecop');
65+
body.customizedtypeoptionstimecops.mappings.customizedtypeoptionstimecop.should.have.a.property('properties');
66+
body.customizedtypeoptionstimecops.mappings.customizedtypeoptionstimecop.properties.should.have.a.property(opts.updatedAt.name);
67+
done();
68+
});
69+
});
70+
});
71+
72+
after(function() {
73+
mongoose.close();
74+
});
75+
});

test/sub_document_test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ var mongoose = require('mongoose');
99
var Schema = mongoose.Schema;
1010
var timestamps = require('../');
1111

12+
mongoose.Promise = global.Promise || mongoose.Promise;
13+
1214
mongoose = mongoose.createConnection('mongodb://localhost/mongoose_timestamps')
1315
mongoose.on('error', function (err) {
1416
console.error('MongoDB error: ' + err.message);
@@ -43,7 +45,7 @@ describe('sub document timestamps', function() {
4345
done();
4446
});
4547
});
46-
48+
4749
it('should not have updatedAt change if parent was updated but not sub document', function(done) {
4850
TimeCopWithSubDocs.findOne({email: '[email protected]'}, function (err, found) {
4951
found.email = '[email protected]';

0 commit comments

Comments
 (0)