-
Notifications
You must be signed in to change notification settings - Fork 376
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add rows to hasMany join table in order provided #862
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
var _ = require("lodash"); | ||
var async = require("async"); | ||
var util = require("../Utilities"); | ||
var ORMError = require("../Error"); | ||
var Accessors = { "get": "get", "set": "set", "has": "has", "del": "remove" }; | ||
|
@@ -214,52 +215,39 @@ function extendInstance(Model, Instance, Driver, association) { | |
writable: true | ||
}); | ||
Object.defineProperty(Instance, association.setAccessor, { | ||
value: function (OtherInstance, cb) { | ||
value: function (OtherInstance, next) { | ||
if (association.reversed) { | ||
Instance.save(function (err) { | ||
if (err) { | ||
return cb(err); | ||
return next(err); | ||
} | ||
|
||
if (!Array.isArray(OtherInstance)) { | ||
util.populateConditions(Model, Object.keys(association.field), Instance, OtherInstance, true); | ||
|
||
return OtherInstance.save({}, { saveAssociations: false }, cb); | ||
return OtherInstance.save({}, { saveAssociations: false }, next); | ||
} | ||
|
||
var associations = _.clone(OtherInstance); | ||
|
||
var saveNext = function () { | ||
if (!associations.length) { | ||
return cb(); | ||
} | ||
|
||
var other = associations.pop(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also changed the order in which |
||
var saveAssociation = function (otherInstance, cb) { | ||
util.populateConditions(Model, Object.keys(association.field), Instance, otherInstance, true); | ||
|
||
util.populateConditions(Model, Object.keys(association.field), Instance, other, true); | ||
|
||
other.save({}, { saveAssociations: false }, function (err) { | ||
if (err) { | ||
return cb(err); | ||
} | ||
|
||
saveNext(); | ||
}); | ||
otherInstance.save({}, { saveAssociations: false }, cb); | ||
}; | ||
|
||
return saveNext(); | ||
var associations = _.clone(OtherInstance); | ||
async.eachSeries(associations, saveAssociation, next); | ||
}); | ||
} else { | ||
OtherInstance.save({}, { saveAssociations: false }, function (err) { | ||
if (err) { | ||
return cb(err); | ||
return next(err); | ||
} | ||
|
||
Instance[association.name] = OtherInstance; | ||
|
||
util.populateConditions(association.model, Object.keys(association.field), OtherInstance, Instance); | ||
|
||
return Instance.save({}, { saveAssociations: false }, cb); | ||
return Instance.save({}, { saveAssociations: false }, next); | ||
}); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,30 +8,30 @@ exports.trigger = function () { | |
} | ||
}; | ||
|
||
exports.wait = function () { | ||
var args = Array.prototype.slice.apply(arguments); | ||
var self = args.shift(); | ||
var hook = args.shift(); | ||
var next = args.shift(); | ||
exports.wait = function (self, hook, next, opts) { | ||
if (typeof hook !== "function") { | ||
return next(); | ||
} | ||
|
||
args.push(next); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was a bit complicated and it wasn't clear what it's actually doing (there's an optional |
||
if (typeof hook === "function") { | ||
var hookValue = hook.apply(self, args); | ||
var hookDoesntExpectCallback = hook.length < 1; | ||
var hookValue; | ||
if (opts) { | ||
hookValue = hook.call(self, opts, next); | ||
hookDoesntExpectCallback = hook.length < 2; | ||
} else { | ||
hookValue = hook.call(self, next); | ||
} | ||
|
||
var hookDoesntExpectCallback = hook.length < args.length; | ||
var isPromise = hookValue && typeof(hookValue.then) === "function"; | ||
var isPromise = hookValue && typeof(hookValue.then) === "function"; | ||
|
||
if (hookDoesntExpectCallback) { | ||
if (isPromise) { | ||
return hookValue | ||
.then(function () { | ||
next(); | ||
}) | ||
.catch(next); | ||
} | ||
return next(); | ||
if (hookDoesntExpectCallback) { | ||
if (isPromise) { | ||
return hookValue | ||
.then(function () { | ||
next(); | ||
}) | ||
.catch(next); | ||
} | ||
} else { | ||
return next(); | ||
} | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was the offending line. Rather than switch to
.shift
I decided to simplify by leveragingasync.eachSeries
.