#350: Creating a batch via hasMany relation is failing. Added handling of array argument.
This commit is contained in:
parent
f1638e9e4c
commit
5c2468c4ef
|
@ -1540,14 +1540,26 @@ HasMany.prototype.create = function (targetModelData, cb) {
|
|||
targetModelData = {};
|
||||
}
|
||||
targetModelData = targetModelData || {};
|
||||
targetModelData[fk] = modelInstance[pk];
|
||||
|
||||
this.definition.applyProperties(modelInstance, targetModelData);
|
||||
var fkAndProps = function(item) {
|
||||
item[fk] = modelInstance[pk];
|
||||
self.definition.applyProperties(modelInstance, item);
|
||||
};
|
||||
|
||||
var apply = function(data, fn) {
|
||||
if (Array.isArray(data)) {
|
||||
data.forEach(fn);
|
||||
} else {
|
||||
fn(data);
|
||||
}
|
||||
};
|
||||
|
||||
apply(targetModelData, fkAndProps);
|
||||
|
||||
modelTo.create(targetModelData, function(err, targetModel) {
|
||||
if(!err) {
|
||||
//Refresh the cache
|
||||
self.addToCache(targetModel);
|
||||
apply(targetModel, self.addToCache.bind(self));
|
||||
cb && cb(err, targetModel);
|
||||
} else {
|
||||
cb && cb(err);
|
||||
|
|
|
@ -87,6 +87,25 @@ describe('relations', function () {
|
|||
});
|
||||
});
|
||||
|
||||
it('should create a batch of records on scope', function (done) {
|
||||
var chapters = [
|
||||
{name: 'a'},
|
||||
{name: 'z'},
|
||||
{name: 'c'}
|
||||
];
|
||||
Book.create(function (err, book) {
|
||||
book.chapters.create(chapters, function (err, chs) {
|
||||
should.not.exist(err);
|
||||
should.exist(chs);
|
||||
chs.should.have.lengthOf(chapters.length);
|
||||
chs.forEach(function(c) {
|
||||
c.bookId.should.equal(book.id);
|
||||
});
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should fetch all scoped instances', function (done) {
|
||||
Book.create(function (err, book) {
|
||||
book.chapters.create({name: 'a'}, function () {
|
||||
|
|
Loading…
Reference in New Issue