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

Commit 290b88c9 by Mick Hansen

Merge pull request #2267 from sequelize/refactor-find

refactor(model/find): find now relies on findAll for the heavy lifting
2 parents 58f2eae1 e620b7d7
...@@ -729,94 +729,31 @@ module.exports = (function() { ...@@ -729,94 +729,31 @@ module.exports = (function() {
* *
* @see {Model#findAll} for an explanation of options and queryOptions * @see {Model#findAll} for an explanation of options and queryOptions
* @return {Promise<Instance>} * @return {Promise<Instance>}
* @alias find
*/ */
Model.prototype.find = function(options, queryOptions) { Model.prototype.findOne = function(param, queryOptions) {
var hasJoin = false; // For sanity findOne will never return if no options are passed
if ([null, undefined].indexOf(param) !== -1) {
// no options defined?
// return an emitter which emits null
if ([null, undefined].indexOf(options) !== -1) {
return Promise.resolve(null); return Promise.resolve(null);
} }
var where var options = {};
, primaryKeys = this.primaryKeys if (typeof param === 'number' || typeof param === 'string' || Buffer.isBuffer(param)) {
, keys = Object.keys(primaryKeys) options.where = {};
, keysLength = keys.length options.where[this.primaryKeyAttribute] = param;
, tableNames = { }; } else {
options = optClone(param);
tableNames[this.getTableName()] = true;
// options is not a hash but an id
if (typeof options === 'number') {
var oldOption = options;
options = { where: {} };
if (keysLength === 1) {
options.where[keys[0]] = oldOption;
} else {
options.where.id = oldOption;
}
} else if (Utils._.size(primaryKeys) && Utils.argsArePrimaryKeys(arguments, primaryKeys)) {
where = {};
Utils._.each(arguments, function(arg, i) {
var key = keys[i];
where[key] = arg;
});
options = { where: where };
} else if (typeof options === 'string' && parseInt(options, 10).toString() === options) {
var parsedId = parseInt(options, 10);
if (!Utils._.isFinite(parsedId)) {
throw new Error('Invalid argument to find(). Must be an id or an options object.');
}
options = { where: parsedId };
} else if (typeof options === 'object') {
options = Utils._.clone(options, function(thing) {
if (Buffer.isBuffer(thing)) { return thing; }
return undefined;
});
if (options.hasOwnProperty('include') && options.include) {
hasJoin = true;
validateIncludedElements.call(this, options, tableNames);
}
// whereCollection is used for non-primary key updates
this.options.whereCollection = options.where || null;
} else if (typeof options === 'string') {
where = {};
if (this.primaryKeyCount === 1) {
where[primaryKeys[keys[0]]] = options;
options = where;
} else if (this.primaryKeyCount < 1) {
// Revert to default behavior which is {where: [int]}
options = {where: parseInt(Number(options) || 0, 0)};
}
}
if (options.attributes === undefined) {
options.attributes = Object.keys(this.tableAttributes);
} }
if (options.limit === undefined && !(options.where && options.where[this.primaryKeyAttribute])) { if (options.limit === undefined && !(options.where && options.where[this.primaryKeyAttribute])) {
options.limit = 1; options.limit = 1;
} }
mapFieldNames.call(this, options, this); return this.findAll(options, Utils._.defaults({
options = paranoidClause.call(this, options); plain: true
}, queryOptions || {}));
return this.QueryInterface.select(this, this.getTableName(), options, Utils._.defaults({
plain: true,
type: QueryTypes.SELECT,
hasJoin: hasJoin,
tableNames: Object.keys(tableNames)
}, queryOptions, { transaction: (options || {}).transaction }));
}; };
Model.prototype.find = Model.prototype.findOne;
/** /**
* Run an aggregation method on the specified field * Run an aggregation method on the specified field
...@@ -1967,7 +1904,8 @@ module.exports = (function() { ...@@ -1967,7 +1904,8 @@ module.exports = (function() {
( (
elem._isSequelizeMethod || elem._isSequelizeMethod ||
elem instanceof Model || elem instanceof Model ||
elem instanceof Transaction elem instanceof Transaction ||
elem instanceof Association
) )
) { ) {
return elem; return elem;
......
...@@ -40,8 +40,12 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -40,8 +40,12 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
User.sync({ force: true }).success(function() { User.sync({ force: true }).success(function() {
sequelize.transaction().then(function(t) { sequelize.transaction().then(function(t) {
User.create({ username: 'foo' }, { transaction: t }).success(function() { User.create({ username: 'foo' }, { transaction: t }).success(function() {
User.find({ username: 'foo' }).success(function(user1) { User.find({
User.find({ username: 'foo' }, { transaction: t }).success(function(user2) { where: { username: 'foo' }
}).success(function(user1) {
User.find({
where: { username: 'foo' },
}, { transaction: t }).success(function(user2) {
expect(user1).to.be.null expect(user1).to.be.null
expect(user2).to.not.be.null expect(user2).to.not.be.null
......
...@@ -58,27 +58,6 @@ describe(Support.getTestDialectTeaser("Paranoid"), function () { ...@@ -58,27 +58,6 @@ describe(Support.getTestDialectTeaser("Paranoid"), function () {
}) })
it( 'test if default required behavior is marked as false', function ( done ) {
var A = this.A,
B = this.B,
options = {
include: [
{
model: B,
}
],
}
A.find( options ).done(function ( err ) {
expect( err ).not.to.be.ok
expect( options.include[0].required ).to.be.equal( false )
done()
})
})
it( 'test if non required is marked as false', function ( done ) { it( 'test if non required is marked as false', function ( done ) {
var A = this.A, var A = this.A,
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!