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

Commit 484bc574 by Mick Hansen

refactor(model/find): find now relies on findAll for the heavy lifting

1 parent c5ef4740
Showing with 20 additions and 79 deletions
......@@ -729,94 +729,31 @@ module.exports = (function() {
*
* @see {Model#findAll} for an explanation of options and queryOptions
* @return {Promise<Instance>}
* @alias find
*/
Model.prototype.find = function(options, queryOptions) {
var hasJoin = false;
// no options defined?
// return an emitter which emits null
if ([null, undefined].indexOf(options) !== -1) {
Model.prototype.findOne = function(param, queryOptions) {
// For sanity findOne will never return if no options are passed
if ([null, undefined].indexOf(param) !== -1) {
return Promise.resolve(null);
}
var where
, primaryKeys = this.primaryKeys
, keys = Object.keys(primaryKeys)
, keysLength = keys.length
, tableNames = { };
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);
var options = {};
if (typeof param === 'number' || typeof param === 'string') {
options.where = {};
options.where[this.primaryKeyAttribute] = param;
} else {
options = optClone(param);
}
if (options.limit === undefined && !(options.where && options.where[this.primaryKeyAttribute])) {
options.limit = 1;
}
mapFieldNames.call(this, options, this);
options = paranoidClause.call(this, options);
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 }));
return this.findAll(options, Utils._.defaults({
plain: true
}, queryOptions || {}));
};
Model.prototype.find = Model.prototype.findOne;
/**
* Run an aggregation method on the specified field
......
......@@ -40,8 +40,12 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
User.sync({ force: true }).success(function() {
sequelize.transaction().then(function(t) {
User.create({ username: 'foo' }, { transaction: t }).success(function() {
User.find({ username: 'foo' }).success(function(user1) {
User.find({ username: 'foo' }, { transaction: t }).success(function(user2) {
User.find({
where: { username: 'foo' }
}).success(function(user1) {
User.find({
where: { username: 'foo' },
}, { transaction: t }).success(function(user2) {
expect(user1).to.be.null
expect(user2).to.not.be.null
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!