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

Commit 441d013b by Mick Hansen

Support .or()/.and()/[] in findOrCreate

1 parent 01da602b
...@@ -725,29 +725,31 @@ module.exports = (function() { ...@@ -725,29 +725,31 @@ module.exports = (function() {
DAOFactory.prototype.findOrCreate = function (where, defaults, options) { DAOFactory.prototype.findOrCreate = function (where, defaults, options) {
var self = this var self = this
, params = {} , values = {}
options = Utils._.extend({ options = Utils._.extend({
transaction: null transaction: null
}, options || {}) }, options || {})
for (var attrname in where) { if (!(where instanceof Utils.or) && !(where instanceof Utils.and) && !Array.isArray(where)) {
params[attrname] = where[attrname] for (var attrname in where) {
values[attrname] = where[attrname]
}
} }
return new Utils.CustomEventEmitter(function (emitter) { return new Utils.CustomEventEmitter(function (emitter) {
self.find({ self.find({
where: params where: where
}, { }, {
transaction: options.transaction transaction: options.transaction
}).success(function (instance) { }).success(function (instance) {
if (instance === null) { if (instance === null) {
for (var attrname in defaults) { for (var attrname in defaults) {
params[attrname] = defaults[attrname] values[attrname] = defaults[attrname]
} }
self self
.create(params, options) .create(values, options)
.success(function (instance) { .success(function (instance) {
emitter.emit('success', instance, true) emitter.emit('success', instance, true)
}) })
......
...@@ -112,6 +112,20 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -112,6 +112,20 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
done() done()
}) })
}) })
it("supports .or() (only using default values)", function (done) {
this.User.findOrCreate(
Sequelize.or({username: 'Fooobzz'}, {secretValue: 'Yolo'}),
{username: 'Fooobzz', secretValue: 'Yolo'}
).done(function (err, user, created) {
expect(err).not.to.be.ok
expect(user.username).to.equal('Fooobzz')
expect(user.secretValue).to.equal('Yolo')
expect(created).to.be.true
done()
})
})
}) })
describe('create', function() { describe('create', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!