Merge pull request #275 from clarkorz/feature/add-with-through-data

Allow to add connection with through data for HasManyThrough relation
This commit is contained in:
Raymond Feng 2014-09-02 22:44:01 -07:00
commit cf3128467c
2 changed files with 24 additions and 2 deletions

View File

@ -917,14 +917,18 @@ HasManyThrough.prototype.create = function create(data, done) {
/**
* Add the target model instance to the 'hasMany' relation
* @param {Object|ID} acInst The actual instance or id value
* @param {Object} [data] Optional data object for the through model to be created
*/
HasManyThrough.prototype.add = function (acInst, done) {
HasManyThrough.prototype.add = function (acInst, data, done) {
var self = this;
var definition = this.definition;
var modelThrough = definition.modelThrough;
var pk1 = definition.keyFrom;
var data = {};
if (typeof data === 'function') {
done = data;
data = {};
}
var query = {};
// The primary key for the target model

View File

@ -433,6 +433,24 @@ describe('relations', function () {
});
});
it('should allow to add connection with through data', function (done) {
Physician.create({name: 'ph1'}, function (e, physician) {
Patient.create({name: 'pa1'}, function (e, patient) {
var now = Date.now();
physician.patients.add(patient, { date: new Date(now) }, function (e, app) {
should.not.exist(e);
should.exist(app);
app.should.be.an.instanceOf(Appointment);
app.physicianId.should.equal(physician.id);
app.patientId.should.equal(patient.id);
app.patientId.should.equal(patient.id);
app.date.getTime().should.equal(now);
done();
});
});
});
});
it('should allow to remove connection with instance', function (done) {
var id;
Physician.create(function (err, physician) {