Skip to content

Commit

Permalink
Merge pull request #7203 from lineus/fix-7202
Browse files Browse the repository at this point in the history
Fixes #7202 don't set parent timestamps because a child has timestamps set to false
  • Loading branch information
vkarpov15 committed Nov 2, 2018
2 parents 8f16b67 + 74d56cd commit e9d538e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/schema.js
Expand Up @@ -853,8 +853,12 @@ Schema.prototype.hasMixedParent = function(path) {
* @api private
*/
Schema.prototype.setupTimestamp = function(timestamps) {
const childHasTimestamp = this.childSchemas.
find(s => s.schema.options.timestamps != null);
const childHasTimestamp = this.childSchemas.find(withTimestamp);

function withTimestamp(s) {
const ts = s.schema.options.timestamps;
return ts == true || ts == null;
}

if (!timestamps && !childHasTimestamp) {
return;
Expand Down
18 changes: 18 additions & 0 deletions test/timestamps.test.js
Expand Up @@ -4,6 +4,7 @@ const assert = require('assert');
const start = require('./common');

const mongoose = start.mongoose;
const Schema = mongoose.Schema;

describe('timestamps', function() {
let db;
Expand Down Expand Up @@ -162,4 +163,21 @@ describe('timestamps', function() {
});
});
});

it('no timestamps added when parent/child timestamps explicitly false (gh-7202)', function(done) {
const subSchema = new Schema({}, { timestamps: false });
const schema = new Schema({ sub: subSchema }, { timestamps: false });

const Test = db.model('gh7202', schema);
const test = new Test({ sub: {} });

test.save((err, saved) => {
assert.ifError(err);
assert.strictEqual(saved.createdAt, undefined);
assert.strictEqual(saved.updatedAt, undefined);
assert.strictEqual(saved.sub.createdAt, undefined);
assert.strictEqual(saved.sub.updatedAt, undefined);
done();
});
});
});

0 comments on commit e9d538e

Please sign in to comment.