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

Commit 8ff99286 by Sushant

Added rejectOnEmpty option in finder methods

1 parent 8640abe3
......@@ -12,6 +12,7 @@ var Utils = require('./utils')
, Promise = require('./promise')
, QueryTypes = require('./query-types')
, Hooks = require('./hooks')
, sequelizeErrors = require('./errors')
, _ = require('lodash')
, associationsMixin = require('./associations/mixin');
......@@ -34,6 +35,7 @@ var Model = function(name, attributes, options) {
underscored: false,
underscoredAll: false,
paranoid: false,
kenophobic: false,
whereCollection: null,
schema: null,
schemaDelimiter: '',
......@@ -1333,6 +1335,7 @@ Model.prototype.all = function(options) {
* @param {Object} [options.having]
* @param {String} [options.searchPath=DEFAULT] An optional parameter to specify the schema search_path (Postgres only)
* @param {Boolean} [options.benchmark=false] Print query execution time in milliseconds when logging SQL.
* @param {Boolean|Error Instance} [options.kenophobic=false] Throws error when no record is found
*
* @see {Sequelize#query}
* @return {Promise<Array<Instance>>}
......@@ -1353,7 +1356,10 @@ Model.prototype.findAll = function(options) {
tableNames[this.getTableName(options)] = true;
options = optClone(options);
_.defaults(options, { hooks: true });
_.defaults(options, { hooks: true, kenophobic: false });
//set kenophobic option from model config
options.kenophobic = options.kenophobic || this.options.kenophobic;
return Promise.bind(this).then(function() {
conformOptions(options, this);
......@@ -1404,6 +1410,18 @@ Model.prototype.findAll = function(options) {
return this.runHooks('afterFind', results, options);
}
}).then(function (results) {
//kenophobic mode
if (_.isEmpty(results) && options.kenophobic) {
if (typeof options.kenophobic === 'function') {
throw new options.kenophobic();
} else if (typeof options.kenophobic === 'object') {
throw options.kenophobic;
} else {
throw new sequelizeErrors.EmptyResultError();
}
}
return Model.$findSeparate(results, originalOptions);
});
};
......@@ -1516,7 +1534,8 @@ Model.prototype.findOne = function(options) {
// Bypass a possible overloaded findAll.
return Model.prototype.findAll.call(this, _.defaults(options, {
plain: true
plain: true,
kenophobic: false
}));
};
Model.prototype.find = Model.prototype.findOne;
......
......@@ -941,8 +941,7 @@ describe(Support.getTestDialectTeaser('Model'), function() {
return expect(this.User.findOne({
where: {
username: 'ath-kantam-pradakshnami'
}
}, {
},
kenophobic: true
})).to.eventually.be.rejectedWith(Sequelize.EmptyResultError);
});
......@@ -957,12 +956,43 @@ describe(Support.getTestDialectTeaser('Model'), function() {
return expect(this.User.find({
where: {
username: 'some-username-that-is-not-used-anywhere'
}
}, {
},
kenophobic: true
})).to.eventually.be.rejectedWith(Sequelize.EmptyResultError);
});
it('works from model options', function() {
var Model = current.define('Test', {
username: Sequelize.STRING(100)
},{
kenophobic: true
});
return Model.sync({ force: true })
.then(function() {
return expect(Model.findOne({
where: {
username: 'some-username-that-is-not-used-anywhere'
}
})).to.eventually.be.rejectedWith(Sequelize.EmptyResultError);
});
});
it('resolve null when disabled', function() {
var Model = current.define('Test', {
username: Sequelize.STRING(100)
});
return Model.sync({ force: true })
.then(function() {
return expect(Model.findOne({
where: {
username: 'some-username-that-is-not-used-anywhere-for-sure-this-time'
}
})).to.eventually.be.equal(null);
});
});
});
});
......
......@@ -1389,4 +1389,61 @@ describe(Support.getTestDialectTeaser('Model'), function() {
expect(spy.called).to.be.ok;
});
});
describe('kenophobic mode', function() {
it('works from model options', function() {
var Model = current.define('Test', {
username: Sequelize.STRING(100)
},{
kenophobic: true
});
return Model.sync({ force: true })
.then(function() {
return expect(Model.findAll({
where: {
username: 'some-username-that-is-not-used-anywhere'
}
})).to.eventually.be.rejectedWith(Sequelize.EmptyResultError);
});
});
it('throws custom error with initialized', function() {
var Model = current.define('Test', {
username: Sequelize.STRING(100)
},{
kenophobic: new Sequelize.ConnectionError('Some Error') //using custom error instance
});
return Model.sync({ force: true })
.then(function() {
return expect(Model.findAll({
where: {
username: 'some-username-that-is-not-used-anywhere-for-sure-this-time'
}
})).to.eventually.be.rejectedWith(Sequelize.ConnectionError);
});
});
it('throws custom error with instance', function() {
var Model = current.define('Test', {
username: Sequelize.STRING(100)
},{
kenophobic: Sequelize.ConnectionError //using custom error instance
});
return Model.sync({ force: true })
.then(function() {
return expect(Model.findAll({
where: {
username: 'some-username-that-is-not-used-anywhere-for-sure-this-time'
}
})).to.eventually.be.rejectedWith(Sequelize.ConnectionError);
});
});
});
});
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!