Skip to content

Commit

Permalink
Merge branch 'master' into gh7079
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Jan 13, 2019
2 parents 3c917e7 + 804e057 commit 563da27
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 4 deletions.
4 changes: 4 additions & 0 deletions docs/api.jade
Expand Up @@ -58,6 +58,10 @@ block content
h5 Returns:
ul
li <span class="method-type">&laquo;#{prop.return.types}&raquo;</span> !{prop.return.description}
if prop.type != null
h5 Type:
ul
li <span class="method-type">&laquo;#{prop.type}&raquo;</span>
div
| !{prop.description}

Expand Down
2 changes: 2 additions & 0 deletions docs/middleware.jade
Expand Up @@ -35,6 +35,8 @@ block content
In query middleware functions, `this` refers to the query.

* [count](./api.html#query_Query-count)
* [deleteMany](./api.html#query_Query-deleteMany)
* [deleteOne](./api.html#query_Query-deleteOne)
* [find](./api.html#query_Query-find)
* [findOne](./api.html#query_Query-findOne)
* [findOneAndDelete](./api.html#query_Query-findOneAndDelete)
Expand Down
8 changes: 7 additions & 1 deletion docs/source/api.js
Expand Up @@ -70,7 +70,13 @@ function parse() {
break;
case 'property':
ctx.type = 'property';
ctx.name = tag.string;
let str = tag.string;
const match = str.match(/^{\w+}/);
if (match != null) {
ctx.type = match[0].substring(1, match[0].length - 1);
str = str.replace(/^{\w+}\s*/, '');
}
ctx.name = str;
ctx.string = `${ctx.constructor}.prototype.${ctx.name}`;
break;
case 'static':
Expand Down
30 changes: 27 additions & 3 deletions lib/index.js
Expand Up @@ -586,7 +586,7 @@ Mongoose.prototype.plugin = function(fn, opts) {
};

/**
* The default connection of the mongoose module.
* The Mongoose module's default connection. Equivalent to `mongoose.connections][0]`, see [`connections`](#mongoose_Mongoose-connections).
*
* ####Example:
*
Expand All @@ -596,10 +596,11 @@ Mongoose.prototype.plugin = function(fn, opts) {
*
* This is the connection used by default for every model created using [mongoose.model](#index_Mongoose-model).
*
* To create a new connection, use [`createConnection()`](#mongoose_Mongoose-createConnection).
*
* @memberOf Mongoose
* @instance
* @property connection
* @return {Connection}
* @property {Connection} connection
* @api public
*/

Expand All @@ -614,6 +615,29 @@ Mongoose.prototype.__defineSetter__('connection', function(v) {
}
});

/**
* An array containing all [connections](connections.html) associated with this
* Mongoose instance. By default, there is 1 connection. Calling
* [`createConnection()`](#mongoose_Mongoose-createConnection) adds a connection
* to this array.
*
* ####Example:
*
* const mongoose = require('mongoose');
* mongoose.connections.length; // 1, just the default connection
* mongoose.connections[0] === mongoose.connection; // true
*
* mongoose.createConnection('mongodb://localhost:27017/test');
* mongoose.connections.length; // 2
*
* @memberOf Mongoose
* @instance
* @property {Array} connections
* @api public
*/

Mongoose.prototype.connections;

/*!
* Driver dependent APIs
*/
Expand Down
4 changes: 4 additions & 0 deletions lib/model.js
Expand Up @@ -427,6 +427,7 @@ function generateVersionError(doc, modifiedPaths) {
* @param {Boolean} [options.j] set to true for MongoDB to wait until this `save()` has been [journaled before resolving the returned promise](https://docs.mongodb.com/manual/reference/write-concern/#j-option). Overrides the [schema-level `writeConcern` option](/docs/guide.html#writeConcern)
* @param {Number} [options.wtimeout] sets a [timeout for the write concern](https://docs.mongodb.com/manual/reference/write-concern/#wtimeout). Overrides the [schema-level `writeConcern` option](/docs/guide.html#writeConcern).
* @param {Boolean} [options.checkKeys=true] the MongoDB driver prevents you from saving keys that start with '$' or contain '.' by default. Set this option to `false` to skip that check. See [restrictions on field names](https://docs.mongodb.com/manual/reference/limits/#Restrictions-on-Field-Names)
* @param {Boolean} [options.timestamps=true] if `false` and [timestamps](./guide.html#timestamps) are enabled, skip timestamps for this `save()`.
* @param {Function} [fn] optional callback
* @return {Promise|undefined} Returns undefined if used with callback or a Promise otherwise.
* @api public
Expand Down Expand Up @@ -465,8 +466,11 @@ Model.prototype.save = function(options, fn) {
return cb(parallelSave);
}

this.$__.saveOptions = options;

this.$__save(options, error => {
this.$__.saving = undefined;
delete this.$__.saveOptions;

if (error) {
this.$__handleReject(error);
Expand Down
4 changes: 4 additions & 0 deletions lib/schema.js
Expand Up @@ -918,6 +918,10 @@ Schema.prototype.setupTimestamp = function(timestamps) {
this.add(schemaAdditions);

this.pre('save', function(next) {
if (get(this, '$__.saveOptions.timestamps') === false) {
return next();
}

const defaultTimestamp = (this.ownerDocument ? this.ownerDocument() : this).
constructor.base.now();
const auto_id = this._id && this._id.auto;
Expand Down
16 changes: 16 additions & 0 deletions test/schema.timestamps.test.js
Expand Up @@ -222,6 +222,22 @@ describe('schema options.timestamps', function() {
});
});

it('can skip with timestamps: false (gh-7357)', function() {
return co(function*() {
const cat = yield Cat.findOne();

const old = cat.updatedAt;

yield cb => setTimeout(() => cb(), 10);

cat.hobby = 'fishing';

yield cat.save({ timestamps: false });

assert.strictEqual(cat.updatedAt, old);
});
});

it('should change updatedAt when findOneAndUpdate', function(done) {
Cat.create({name: 'test123'}, function(err) {
assert.ifError(err);
Expand Down

0 comments on commit 563da27

Please sign in to comment.