Skip to content

Commit 51511af

Browse files
committed
Merge branch '4.x' into 5970
2 parents 7a0b15f + c28ce33 commit 51511af

File tree

3 files changed

+29
-19
lines changed

3 files changed

+29
-19
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ Mongoose is a [MongoDB](https://www.mongodb.org/) object modeling tool designed
55
[![Slack Status](http://slack.mongoosejs.io/badge.svg)](http://slack.mongoosejs.io)
66
[![Build Status](https://api.travis-ci.org/Automattic/mongoose.svg?branch=master)](https://travis-ci.org/Automattic/mongoose)
77
[![NPM version](https://badge.fury.io/js/mongoose.svg)](http://badge.fury.io/js/mongoose)
8-
[![Dependency Status](https://gemnasium.com/Automattic/mongoose.svg)](https://gemnasium.com/Automattic/mongoose)
9-
[![Get help on Codementor](https://cdn.codementor.io/badges/get_help_github.svg)](https://www.codementor.io/vkarpov?utm_source=github&utm_medium=button&utm_term=vkarpov&utm_campaign=github)
108

119
## Documentation
1210

lib/schema.js

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -804,22 +804,17 @@ Schema.prototype.hasMixedParent = function(path) {
804804
*/
805805
Schema.prototype.setupTimestamp = function(timestamps) {
806806
if (timestamps) {
807-
var paths = ['createdAt', 'updatedAt'].map(handleTimestampOption.bind(null, timestamps));
808-
var createdAt = paths[0];
809-
var updatedAt = paths[1];
810-
var schemaAdditions = paths.reduce(function(cur, path) {
811-
if (path != null) {
812-
var parts = path.split('.');
813-
if (this.pathType(path) === 'adhocOrUndefined') {
814-
for (var i = 0; i < parts.length; ++i) {
815-
cur[parts[i]] = (i < parts.length - 1 ?
816-
cur[parts[i]] || {} :
817-
Date);
818-
}
819-
}
820-
}
821-
return cur;
822-
}.bind(this), {});
807+
var createdAt = handleTimestampOption(timestamps, 'createdAt');
808+
var updatedAt = handleTimestampOption(timestamps, 'updatedAt');
809+
var schemaAdditions = {};
810+
811+
if (updatedAt && !this.paths[updatedAt]) {
812+
schemaAdditions[updatedAt] = Date;
813+
}
814+
815+
if (createdAt && !this.paths[createdAt]) {
816+
schemaAdditions[createdAt] = Date;
817+
}
823818

824819
this.add(schemaAdditions);
825820

test/document.test.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3801,7 +3801,7 @@ describe('document', function() {
38013801
});
38023802

38033803
it('timestamps with nested paths (gh-5051)', function(done) {
3804-
var schema = new Schema({ props: Object }, {
3804+
var schema = new Schema({ props: {} }, {
38053805
timestamps: {
38063806
createdAt: 'props.createdAt',
38073807
updatedAt: 'props.updatedAt'
@@ -3821,6 +3821,23 @@ describe('document', function() {
38213821
});
38223822
});
38233823

3824+
it('Declaring defaults in your schema with timestamps defined (gh-6024)', function(done) {
3825+
var schemaDefinition = {
3826+
name: String,
3827+
misc: {
3828+
hometown: String,
3829+
isAlive: { type: Boolean, default: true }
3830+
}
3831+
};
3832+
3833+
var schemaWithTimestamps = new Schema(schemaDefinition, {timestamps: {createdAt: 'misc.createdAt'}});
3834+
var PersonWithTimestamps = db.model('Person_timestamps', schemaWithTimestamps);
3835+
var dude = new PersonWithTimestamps({ name: 'Keanu', misc: {hometown: 'Beirut'} });
3836+
assert.equal(dude.misc.isAlive, true);
3837+
3838+
done();
3839+
});
3840+
38243841
it('supports $where in pre save hook (gh-4004)', function(done) {
38253842
var Promise = global.Promise;
38263843

0 commit comments

Comments
 (0)