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

Commit 615b74a4 by Mick Hansen

feat(model): added findCreateFind

1 parent 6202a409
# Next # Next
- [ADDED] Model.findCreateFind: A more performant findOrCreate that will not work under a transaction (atleast not in postgres)
- [FIXED] Show indexes query on Postgres fails to return functional indexes [#3911](https://github.com/sequelize/sequelize/issues/3911) - [FIXED] Show indexes query on Postgres fails to return functional indexes [#3911](https://github.com/sequelize/sequelize/issues/3911)
- [FIXED] Custom field names in json queries - [FIXED] Custom field names in json queries
- [FIXED] JSON cast key using the equality operator. [#3824](https://github.com/sequelize/sequelize/issues/3824) - [FIXED] JSON cast key using the equality operator. [#3824](https://github.com/sequelize/sequelize/issues/3824)
......
...@@ -1875,6 +1875,42 @@ Model.prototype.findOrCreate = function(options) { ...@@ -1875,6 +1875,42 @@ Model.prototype.findOrCreate = function(options) {
}; };
/** /**
* A more performant findOrCreate that will not work under a transaction (atleast not in postgres)
* Will execute a find call, if empty then attempt to create, if unique constraint then attempt to find again
*
* @param {Object} options
* @param {Object} options.where where A hash of search attributes.
* @param {Object} [options.defaults] Default values to use if creating a new instance
* @see {Model#findAll} for a full specification of find and options
* @return {Promise<Instance,created>}
*/
Model.prototype.findCreateFind = function(options) {
if (!options || !options.where) {
throw new Error(
'Missing where attribute in the options parameter passed to findOrCreate.'
);
}
var values = Utils._.clone(options.defaults) || {};
if (Utils._.isPlainObject(options.where)) {
values = _.defaults(values, options.where);
}
return this.findOne(options).bind(this).then(function (result) {
if (result) return [result, false];
return this.create(values, options).bind(this).then(function (result) {
return [result, true];
}).catch(this.sequelize.UniqueConstraintError, function (err) {
return this.findOne(options).then(function (result) {
return [result, false];
});
});
});
};
/**
* Insert or update a single row. An update will be executed if a row which matches the supplied values on either the primary key or a unique key is found. Note that the unique index must be defined in your sequelize model and not just in the table. Otherwise you may experience a unique constraint violation, because sequelize fails to identify the row that should be updated. * Insert or update a single row. An update will be executed if a row which matches the supplied values on either the primary key or a unique key is found. Note that the unique index must be defined in your sequelize model and not just in the table. Otherwise you may experience a unique constraint violation, because sequelize fails to identify the row that should be updated.
* *
* **Implementation details:** * **Implementation details:**
......
...@@ -458,6 +458,35 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -458,6 +458,35 @@ describe(Support.getTestDialectTeaser('Model'), function() {
}); });
}); });
describe('findCreateFind', function () {
(dialect !== 'sqlite' ? it : it.skip)('should work with multiple concurrent calls', function () {
return Promise.join(
this.User.findOrCreate({ where: { uniqueName: 'winner' }}),
this.User.findOrCreate({ where: { uniqueName: 'winner' }}),
this.User.findOrCreate({ where: { uniqueName: 'winner' }}),
function(first, second, third) {
var firstInstance = first[0]
, firstCreated = first[1]
, secondInstance = second[0]
, secondCreated = second[1]
, thirdInstance = third[0]
, thirdCreated = third[1];
expect([firstCreated, secondCreated, thirdCreated].filter(function (value) {
return value;
}).length).to.equal(1);
expect(firstInstance).to.be.ok;
expect(secondInstance).to.be.ok;
expect(thirdInstance).to.be.ok;
expect(firstInstance.id).to.equal(secondInstance.id);
expect(secondInstance.id).to.equal(thirdInstance.id);
}
);
});
});
describe('create', function() { describe('create', function() {
it('works with non-integer primary keys with a default value', function() { it('works with non-integer primary keys with a default value', function() {
var User = this.sequelize.define('User', { var User = this.sequelize.define('User', {
......
'use strict';
/* jshint -W030 */
var chai = require('chai')
, expect = chai.expect
, Support = require(__dirname + '/../support')
, UniqueConstraintError = require(__dirname + '/../../../lib/errors').UniqueConstraintError
, current = Support.sequelize
, cls = require('continuation-local-storage')
, sinon = require('sinon')
, Promise = require('bluebird');
describe(Support.getTestDialectTeaser('Model'), function() {
describe('findCreateFind', function () {
var Model = current.define('Model', {});
beforeEach(function () {
this.sinon = sinon.sandbox.create();
});
afterEach(function () {
this.sinon.restore();
});
it('should return the result of the first find call if not empty', function () {
var result = {}
, where = {prop: Math.random().toString()}
, findSpy = this.sinon.stub(Model, 'findOne').returns(Promise.resolve(result));
return expect(Model.findCreateFind({
where: where
})).to.eventually.eql([result, false]).then(function () {
expect(findSpy).to.have.been.calledOnce;
expect(findSpy.getCall(0).args[0].where).to.equal(where);
});
});
it('should create if first find call is empty', function () {
var result = {}
, where = {prop: Math.random().toString()}
, findSpy = this.sinon.stub(Model, 'findOne').returns(Promise.resolve(null))
, createSpy = this.sinon.stub(Model, 'create').returns(Promise.resolve(result));
return expect(Model.findCreateFind({
where: where
})).to.eventually.eql([result, true]).then(function () {
expect(createSpy).to.have.been.calledWith(where);
});
});
it('should do a second find if create failed do to unique constraint', function () {
var result = {}
, where = {prop: Math.random().toString()}
, findSpy = this.sinon.stub(Model, 'findOne')
, createSpy = this.sinon.stub(Model, 'create').returns(Promise.reject(new UniqueConstraintError()));
findSpy.onFirstCall().returns(Promise.resolve(null));
findSpy.onSecondCall().returns(Promise.resolve(result));
return expect(Model.findCreateFind({
where: where
})).to.eventually.eql([result, false]).then(function () {
expect(findSpy).to.have.been.calledTwice;
expect(findSpy.getCall(1).args[0].where).to.equal(where);
});
});
});
});
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!