不要怂,就是干,撸起袖子干!

Commit d60ab0e3 by tornillo

Fix Instance.reload issues

Fix error message

Fix test

Fix changelog

Fix jshint error
1 parent 47ac7b4b
# Future
- [FIXED] Fixed Instance.reload issues ([#4844](https://github.com/sequelize/sequelize/issues/4844) and [#4452](https://github.com/sequelize/sequelize/issues/4452))
# 3.18.0 # 3.18.0
- [ADDED] Support silent: true in bulk update [#5200](https://github.com/sequelize/sequelize/issues/5200) - [ADDED] Support silent: true in bulk update [#5200](https://github.com/sequelize/sequelize/issues/5200)
- [ADDED] `retry` object now part of global settings and can be overridden per call. The default is 5 retries with a backoff function. `retry` object can be passed to options with max: 0 to turn off this behavior. - [ADDED] `retry` object now part of global settings and can be overridden per call. The default is 5 retries with a backoff function. `retry` object can be passed to options with max: 0 to turn off this behavior.
......
...@@ -287,3 +287,15 @@ error.ConnectionTimedOutError = function (parent) { ...@@ -287,3 +287,15 @@ error.ConnectionTimedOutError = function (parent) {
this.name = 'SequelizeConnectionTimedOutError'; this.name = 'SequelizeConnectionTimedOutError';
}; };
util.inherits(error.ConnectionTimedOutError, error.ConnectionError); util.inherits(error.ConnectionTimedOutError, error.ConnectionError);
/**
* Thrown when a some problem occurred with Instance methods (see message for details)
* @extends BaseError
* @constructor
*/
error.InstanceError = function (message) {
error.BaseError.apply(this, arguments);
this.name = 'SequelizeInstanceError';
this.message = message;
};
util.inherits(error.InstanceError, error.BaseError);
...@@ -5,6 +5,7 @@ var Utils = require('./utils') ...@@ -5,6 +5,7 @@ var Utils = require('./utils')
, BelongsToMany = require('./associations/belongs-to-many') , BelongsToMany = require('./associations/belongs-to-many')
, InstanceValidator = require('./instance-validator') , InstanceValidator = require('./instance-validator')
, QueryTypes = require('./query-types') , QueryTypes = require('./query-types')
, sequelizeErrors = require('./errors')
, Dottie = require('dottie') , Dottie = require('dottie')
, Promise = require('./promise') , Promise = require('./promise')
, _ = require('lodash') , _ = require('lodash')
...@@ -742,7 +743,18 @@ Instance.prototype.reload = function(options) { ...@@ -742,7 +743,18 @@ Instance.prototype.reload = function(options) {
include: this.$options.include || null include: this.$options.include || null
}); });
return this.Model.findOne(options).bind(this).then(function(reload) { return this.Model.findOne(options).bind(this)
.tap(function (reload) {
if (!reload) {
throw new sequelizeErrors.InstanceError(
'Instance could not be reloaded because it does not exist anymore (find call returned null)'
);
}
})
.then(function(reload) {
// update the internal options of the instance
this.$options = reload.$options;
// re-set instance values
this.set(reload.dataValues, { this.set(reload.dataValues, {
raw: true, raw: true,
reset: true && !options.attributes reset: true && !options.attributes
......
...@@ -436,6 +436,14 @@ Sequelize.prototype.InvalidConnectionError = Sequelize.InvalidConnectionError = ...@@ -436,6 +436,14 @@ Sequelize.prototype.InvalidConnectionError = Sequelize.InvalidConnectionError =
Sequelize.prototype.ConnectionTimedOutError = Sequelize.ConnectionTimedOutError = Sequelize.prototype.ConnectionTimedOutError = Sequelize.ConnectionTimedOutError =
sequelizeErrors.ConnectionTimedOutError; sequelizeErrors.ConnectionTimedOutError;
/**
* Thrown when a some problem occurred with Instance methods (see message for details)
* @see {Errors#InstanceError}
*/
Sequelize.prototype.InstanceError = Sequelize.InstanceError =
sequelizeErrors.InstanceError;
Sequelize.prototype.refreshTypes = function () { Sequelize.prototype.refreshTypes = function () {
this.connectionManager.refreshTypeParser(DataTypes); this.connectionManager.refreshTypeParser(DataTypes);
}; };
......
...@@ -513,6 +513,49 @@ describe(Support.getTestDialectTeaser('Instance'), function() { ...@@ -513,6 +513,49 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
}); });
}); });
it('should update internal options of the instance', function() {
var Book = this.sequelize.define('Book', { title: DataTypes.STRING })
, Page = this.sequelize.define('Page', { content: DataTypes.TEXT });
Book.hasMany(Page);
Page.belongsTo(Book);
return Book.sync({force: true}).then(function() {
return Page.sync({force: true}).then(function() {
return Book.create({ title: 'A very old book' }).then(function(book) {
return Page.create().then(function(page) {
return book.setPages([page]).then(function() {
return Book.findOne({
where: { id: book.id }
}).then(function(leBook) {
var oldOptions = leBook.$options;
return leBook.reload({
include: [Page]
}).then(function(leBook) {
expect(oldOptions).not.to.equal(leBook.$options);
expect(leBook.$options.include.length).to.equal(1);
expect(leBook.Pages.length).to.equal(1);
expect(leBook.get({plain: true}).Pages.length).to.equal(1);
});
});
});
});
});
});
});
});
it('should return an error when reload fails', function() {
return this.User.create({ username: 'John Doe' }).then(function(user) {
return user.destroy().then(function () {
return expect(user.reload()).to.be.rejectedWith(
Sequelize.InstanceError,
'Instance could not be reloaded because it does not exist anymore (find call returned null)'
);
});
});
});
it('should set an association to null after deletion, 1-1', function() { it('should set an association to null after deletion, 1-1', function() {
var Shoe = this.sequelize.define('Shoe', { brand: DataTypes.STRING }) var Shoe = this.sequelize.define('Shoe', { brand: DataTypes.STRING })
, Player = this.sequelize.define('Player', { name: DataTypes.STRING }); , Player = this.sequelize.define('Player', { name: DataTypes.STRING });
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!