Merge branch 'release/2.11.0' into production
This commit is contained in:
commit
59ca63cbe0
|
@ -3,6 +3,7 @@
|
|||
*/
|
||||
var assert = require('assert');
|
||||
var util = require('util');
|
||||
var async = require('async');
|
||||
var utils = require('./utils');
|
||||
var i8n = require('inflection');
|
||||
var defineScope = require('./scope.js').defineScope;
|
||||
|
@ -910,25 +911,25 @@ HasManyThrough.prototype.create = function create(data, done) {
|
|||
done = data;
|
||||
data = {};
|
||||
}
|
||||
done = done || function(){};
|
||||
|
||||
var modelInstance = this.modelInstance;
|
||||
|
||||
// First create the target model
|
||||
modelTo.create(data, function (err, to) {
|
||||
if (err) {
|
||||
return done && done(err, to);
|
||||
return done(err, to);
|
||||
}
|
||||
// The primary key for the target model
|
||||
var pk2 = definition.modelTo.definition.idName();
|
||||
|
||||
var keys = throughKeys(definition);
|
||||
var fk1 = keys[0];
|
||||
var fk2 = keys[1];
|
||||
|
||||
function createRelation(to, next) {
|
||||
var d = {};
|
||||
d[fk1] = modelInstance[definition.keyFrom];
|
||||
d[fk2] = to[pk2];
|
||||
|
||||
definition.applyProperties(modelInstance, d);
|
||||
|
||||
// Then create the through model
|
||||
|
@ -936,13 +937,20 @@ HasManyThrough.prototype.create = function create(data, done) {
|
|||
if (e) {
|
||||
// Undo creation of the target model
|
||||
to.destroy(function () {
|
||||
done && done(e);
|
||||
next(e);
|
||||
});
|
||||
} else {
|
||||
self.addToCache(to);
|
||||
done && done(err, to);
|
||||
next(err, to);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// process array or single item
|
||||
if (!Array.isArray(to))
|
||||
createRelation(to, done);
|
||||
else
|
||||
async.map(to, createRelation, done);
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -1562,14 +1570,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);
|
||||
//Refresh the cache
|
||||
apply(targetModel, self.addToCache.bind(self));
|
||||
cb && cb(err, targetModel);
|
||||
} else {
|
||||
cb && cb(err);
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "loopback-datasource-juggler",
|
||||
"version": "2.10.3",
|
||||
"version": "2.11.0",
|
||||
"description": "LoopBack DataSoure Juggler",
|
||||
"keywords": [
|
||||
"StrongLoop",
|
||||
|
|
|
@ -61,6 +61,13 @@ describe('relations', function () {
|
|||
db.autoupdate(done);
|
||||
});
|
||||
|
||||
describe('with scope', function() {
|
||||
|
||||
before(function (done) {
|
||||
Book.hasMany(Chapter);
|
||||
done();
|
||||
});
|
||||
|
||||
it('should build record on scope', function (done) {
|
||||
Book.create(function (err, book) {
|
||||
var c = book.chapters.build();
|
||||
|
@ -80,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 () {
|
||||
|
@ -244,6 +270,7 @@ describe('relations', function () {
|
|||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
@ -294,6 +321,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) {
|
||||
Physician.create(function (err, physician) {
|
||||
physician.patients.create({name: 'a'}, function () {
|
||||
|
|
Loading…
Reference in New Issue