Merge pull request #352 from TorchlightSoftware/fix-create-rel

handle relationship create with [array]
This commit is contained in:
Raymond Feng 2014-11-13 09:29:27 -08:00
commit 13d08d83cc
2 changed files with 52 additions and 21 deletions

View File

@ -3,6 +3,7 @@
*/ */
var assert = require('assert'); var assert = require('assert');
var util = require('util'); var util = require('util');
var async = require('async');
var utils = require('./utils'); var utils = require('./utils');
var i8n = require('inflection'); var i8n = require('inflection');
var defineScope = require('./scope.js').defineScope; var defineScope = require('./scope.js').defineScope;
@ -910,39 +911,46 @@ HasManyThrough.prototype.create = function create(data, done) {
done = data; done = data;
data = {}; data = {};
} }
done = done || function(){};
var modelInstance = this.modelInstance; var modelInstance = this.modelInstance;
// First create the target model // First create the target model
modelTo.create(data, function (err, to) { modelTo.create(data, function (err, to) {
if (err) { if (err) {
return done && done(err, to); return done(err, to);
} }
// The primary key for the target model // The primary key for the target model
var pk2 = definition.modelTo.definition.idName(); var pk2 = definition.modelTo.definition.idName();
var keys = throughKeys(definition); var keys = throughKeys(definition);
var fk1 = keys[0]; var fk1 = keys[0];
var fk2 = keys[1]; var fk2 = keys[1];
var d = {}; function createRelation(to, next) {
d[fk1] = modelInstance[definition.keyFrom]; var d = {};
d[fk2] = to[pk2]; d[fk1] = modelInstance[definition.keyFrom];
d[fk2] = to[pk2];
definition.applyProperties(modelInstance, d);
definition.applyProperties(modelInstance, d); // Then create the through model
modelThrough.create(d, function (e, through) {
if (e) {
// Undo creation of the target model
to.destroy(function () {
next(e);
});
} else {
self.addToCache(to);
next(err, to);
}
});
}
// Then create the through model // process array or single item
modelThrough.create(d, function (e, through) { if (!Array.isArray(to))
if (e) { createRelation(to, done);
// Undo creation of the target model else
to.destroy(function () { async.map(to, createRelation, done);
done && done(e);
});
} else {
self.addToCache(to);
done && done(err, to);
}
});
}); });
}; };

View File

@ -294,6 +294,29 @@ describe('relations', function () {
}); });
}); });
it('should create multiple records on scope', function (done) {
var async = require('async');
Physician.create(function (err, physician) {
physician.patients.create([{}, {}], function (err, patients) {
should.not.exist(err);
should.exist(patients);
patients.should.have.lengthOf(2);
function verifyPatient(patient, next) {
Appointment.find({where: {
physicianId: physician.id,
patientId: patient.id
}},
function(err, apps) {
should.not.exist(err);
apps.should.have.lengthOf(1);
next();
});
}
async.forEach(patients, verifyPatient, done);
});
});
});
it('should fetch all scoped instances', function (done) { it('should fetch all scoped instances', function (done) {
Physician.create(function (err, physician) { Physician.create(function (err, physician) {
physician.patients.create({name: 'a'}, function () { physician.patients.create({name: 'a'}, function () {