Skip to content

Commit

Permalink
Merge branch '4.x' into 5970
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Jan 23, 2018
2 parents 7a0b15f + c28ce33 commit 51511af
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ Mongoose is a [MongoDB](https://www.mongodb.org/) object modeling tool designed
[![Slack Status](http://slack.mongoosejs.io/badge.svg)](http://slack.mongoosejs.io)
[![Build Status](https://api.travis-ci.org/Automattic/mongoose.svg?branch=master)](https://travis-ci.org/Automattic/mongoose)
[![NPM version](https://badge.fury.io/js/mongoose.svg)](http://badge.fury.io/js/mongoose)
[![Dependency Status](https://gemnasium.com/Automattic/mongoose.svg)](https://gemnasium.com/Automattic/mongoose)
[![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)

## Documentation

Expand Down
27 changes: 11 additions & 16 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -804,22 +804,17 @@ Schema.prototype.hasMixedParent = function(path) {
*/
Schema.prototype.setupTimestamp = function(timestamps) {
if (timestamps) {
var paths = ['createdAt', 'updatedAt'].map(handleTimestampOption.bind(null, timestamps));
var createdAt = paths[0];
var updatedAt = paths[1];
var schemaAdditions = paths.reduce(function(cur, path) {
if (path != null) {
var parts = path.split('.');
if (this.pathType(path) === 'adhocOrUndefined') {
for (var i = 0; i < parts.length; ++i) {
cur[parts[i]] = (i < parts.length - 1 ?
cur[parts[i]] || {} :
Date);
}
}
}
return cur;
}.bind(this), {});
var createdAt = handleTimestampOption(timestamps, 'createdAt');
var updatedAt = handleTimestampOption(timestamps, 'updatedAt');
var schemaAdditions = {};

if (updatedAt && !this.paths[updatedAt]) {
schemaAdditions[updatedAt] = Date;
}

if (createdAt && !this.paths[createdAt]) {
schemaAdditions[createdAt] = Date;
}

this.add(schemaAdditions);

Expand Down
19 changes: 18 additions & 1 deletion test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3801,7 +3801,7 @@ describe('document', function() {
});

it('timestamps with nested paths (gh-5051)', function(done) {
var schema = new Schema({ props: Object }, {
var schema = new Schema({ props: {} }, {
timestamps: {
createdAt: 'props.createdAt',
updatedAt: 'props.updatedAt'
Expand All @@ -3821,6 +3821,23 @@ describe('document', function() {
});
});

it('Declaring defaults in your schema with timestamps defined (gh-6024)', function(done) {
var schemaDefinition = {
name: String,
misc: {
hometown: String,
isAlive: { type: Boolean, default: true }
}
};

var schemaWithTimestamps = new Schema(schemaDefinition, {timestamps: {createdAt: 'misc.createdAt'}});
var PersonWithTimestamps = db.model('Person_timestamps', schemaWithTimestamps);
var dude = new PersonWithTimestamps({ name: 'Keanu', misc: {hometown: 'Beirut'} });
assert.equal(dude.misc.isAlive, true);

done();
});

it('supports $where in pre save hook (gh-4004)', function(done) {
var Promise = global.Promise;

Expand Down

0 comments on commit 51511af

Please sign in to comment.