Skip to content

Commit

Permalink
Added unit test for setting default values with timestamps.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chas DeVeas authored and vkarpov15 committed Jan 22, 2018
1 parent bff15e3 commit 0ff2f94
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
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
2 changes: 1 addition & 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 Down

0 comments on commit 0ff2f94

Please sign in to comment.