Merge branch 'release/2.11.0' into production

This commit is contained in:
Raymond Feng 2014-11-13 12:58:28 -08:00
commit 59ca63cbe0
3 changed files with 252 additions and 182 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 // Then create the through model
modelThrough.create(d, function (e, through) { modelThrough.create(d, function (e, through) {
if (e) { if (e) {
// Undo creation of the target model // Undo creation of the target model
to.destroy(function () { to.destroy(function () {
done && done(e); next(e);
}); });
} else { } else {
self.addToCache(to); 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);
}); });
}; };
@ -1556,20 +1564,32 @@ HasMany.prototype.create = function (targetModelData, cb) {
var fk = this.definition.keyTo; var fk = this.definition.keyTo;
var pk = this.definition.keyFrom; var pk = this.definition.keyFrom;
var modelInstance = this.modelInstance; var modelInstance = this.modelInstance;
if (typeof targetModelData === 'function' && !cb) { if (typeof targetModelData === 'function' && !cb) {
cb = targetModelData; cb = targetModelData;
targetModelData = {}; targetModelData = {};
} }
targetModelData = targetModelData || {}; targetModelData = targetModelData || {};
targetModelData[fk] = modelInstance[pk];
var fkAndProps = function(item) {
this.definition.applyProperties(modelInstance, targetModelData); 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) { modelTo.create(targetModelData, function(err, targetModel) {
if(!err) { if(!err) {
// Refresh the cache //Refresh the cache
self.addToCache(targetModel); apply(targetModel, self.addToCache.bind(self));
cb && cb(err, targetModel); cb && cb(err, targetModel);
} else { } else {
cb && cb(err); cb && cb(err);

View File

@ -1,6 +1,6 @@
{ {
"name": "loopback-datasource-juggler", "name": "loopback-datasource-juggler",
"version": "2.10.3", "version": "2.11.0",
"description": "LoopBack DataSoure Juggler", "description": "LoopBack DataSoure Juggler",
"keywords": [ "keywords": [
"StrongLoop", "StrongLoop",

View File

@ -61,190 +61,217 @@ describe('relations', function () {
db.autoupdate(done); db.autoupdate(done);
}); });
it('should build record on scope', function (done) { describe('with scope', function() {
Book.create(function (err, book) {
var c = book.chapters.build();
c.bookId.should.equal(book.id);
c.save(done);
});
});
it('should create record on scope', function (done) { before(function (done) {
Book.create(function (err, book) { Book.hasMany(Chapter);
book.chapters.create(function (err, c) { done();
should.not.exist(err); });
should.exist(c);
it('should build record on scope', function (done) {
Book.create(function (err, book) {
var c = book.chapters.build();
c.bookId.should.equal(book.id); c.bookId.should.equal(book.id);
done(); c.save(done);
});
});
});
it('should fetch all scoped instances', function (done) {
Book.create(function (err, book) {
book.chapters.create({name: 'a'}, function () {
book.chapters.create({name: 'z'}, function () {
book.chapters.create({name: 'c'}, function () {
verify(book);
});
});
});
});
function verify(book) {
book.chapters(function (err, ch) {
should.not.exist(err);
should.exist(ch);
ch.should.have.lengthOf(3);
var chapters = book.chapters();
chapters.should.eql(ch);
book.chapters({order: 'name DESC'}, function (e, c) {
should.not.exist(e);
should.exist(c);
c.shift().name.should.equal('z');
c.pop().name.should.equal('a');
done();
});
});
}
});
it('should find scoped record', function (done) {
var id;
Book.create(function (err, book) {
book.chapters.create({name: 'a'}, function (err, ch) {
id = ch.id;
book.chapters.create({name: 'z'}, function () {
book.chapters.create({name: 'c'}, function () {
verify(book);
});
});
}); });
}); });
function verify(book) { it('should create record on scope', function (done) {
book.chapters.findById(id, function (err, ch) { Book.create(function (err, book) {
should.not.exist(err); book.chapters.create(function (err, c) {
should.exist(ch);
ch.id.should.eql(id);
done();
});
}
});
it('should count scoped records - all and filtered', function (done) {
Book.create(function (err, book) {
book.chapters.create({name: 'a'}, function (err, ch) {
book.chapters.create({name: 'b'}, function () {
book.chapters.create({name: 'c'}, function () {
verify(book);
});
});
});
});
function verify(book) {
book.chapters.count(function (err, count) {
should.not.exist(err);
count.should.equal(3);
book.chapters.count({ name: 'b' }, function (err, count) {
should.not.exist(err); should.not.exist(err);
count.should.equal(1); should.exist(c);
c.bookId.should.equal(book.id);
done(); done();
}); });
}); });
} });
});
it('should set targetClass on scope property', function() { it('should create a batch of records on scope', function (done) {
should.equal(Book.prototype.chapters._targetClass, 'Chapter'); var chapters = [
}); {name: 'a'},
{name: 'z'},
it('should update scoped record', function (done) { {name: 'c'}
var id; ];
Book.create(function (err, book) { Book.create(function (err, book) {
book.chapters.create({name: 'a'}, function (err, ch) { book.chapters.create(chapters, function (err, chs) {
id = ch.id; should.not.exist(err);
book.chapters.updateById(id, {name: 'aa'}, function(err, ch) { should.exist(chs);
verify(book); chs.should.have.lengthOf(chapters.length);
chs.forEach(function(c) {
c.bookId.should.equal(book.id);
});
done();
}); });
}); });
}); });
function verify(book) { it('should fetch all scoped instances', function (done) {
book.chapters.findById(id, function (err, ch) { Book.create(function (err, book) {
should.not.exist(err); book.chapters.create({name: 'a'}, function () {
should.exist(ch); book.chapters.create({name: 'z'}, function () {
ch.id.should.eql(id); book.chapters.create({name: 'c'}, function () {
ch.name.should.equal('aa'); verify(book);
done(); });
}); });
}
});
it('should destroy scoped record', function (done) {
var id;
Book.create(function (err, book) {
book.chapters.create({name: 'a'}, function (err, ch) {
id = ch.id;
book.chapters.destroy(id, function(err, ch) {
verify(book);
}); });
}); });
function verify(book) {
book.chapters(function (err, ch) {
should.not.exist(err);
should.exist(ch);
ch.should.have.lengthOf(3);
var chapters = book.chapters();
chapters.should.eql(ch);
book.chapters({order: 'name DESC'}, function (e, c) {
should.not.exist(e);
should.exist(c);
c.shift().name.should.equal('z');
c.pop().name.should.equal('a');
done();
});
});
}
}); });
function verify(book) { it('should find scoped record', function (done) {
book.chapters.findById(id, function (err, ch) { var id;
should.exist(err); Book.create(function (err, book) {
done(); book.chapters.create({name: 'a'}, function (err, ch) {
id = ch.id;
book.chapters.create({name: 'z'}, function () {
book.chapters.create({name: 'c'}, function () {
verify(book);
});
});
});
}); });
}
});
it('should check existence of a scoped record', function (done) { function verify(book) {
var id; book.chapters.findById(id, function (err, ch) {
Book.create(function (err, book) { should.not.exist(err);
book.chapters.create({name: 'a'}, function (err, ch) { should.exist(ch);
id = ch.id; ch.id.should.eql(id);
book.chapters.create({name: 'z'}, function () { done();
book.chapters.create({name: 'c'}, function () { });
}
});
it('should count scoped records - all and filtered', function (done) {
Book.create(function (err, book) {
book.chapters.create({name: 'a'}, function (err, ch) {
book.chapters.create({name: 'b'}, function () {
book.chapters.create({name: 'c'}, function () {
verify(book);
});
});
});
});
function verify(book) {
book.chapters.count(function (err, count) {
should.not.exist(err);
count.should.equal(3);
book.chapters.count({ name: 'b' }, function (err, count) {
should.not.exist(err);
count.should.equal(1);
done();
});
});
}
});
it('should set targetClass on scope property', function() {
should.equal(Book.prototype.chapters._targetClass, 'Chapter');
});
it('should update scoped record', function (done) {
var id;
Book.create(function (err, book) {
book.chapters.create({name: 'a'}, function (err, ch) {
id = ch.id;
book.chapters.updateById(id, {name: 'aa'}, function(err, ch) {
verify(book); verify(book);
}); });
}); });
}); });
function verify(book) {
book.chapters.findById(id, function (err, ch) {
should.not.exist(err);
should.exist(ch);
ch.id.should.eql(id);
ch.name.should.equal('aa');
done();
});
}
}); });
function verify(book) { it('should destroy scoped record', function (done) {
book.chapters.exists(id, function (err, flag) { var id;
Book.create(function (err, book) {
book.chapters.create({name: 'a'}, function (err, ch) {
id = ch.id;
book.chapters.destroy(id, function(err, ch) {
verify(book);
});
});
});
function verify(book) {
book.chapters.findById(id, function (err, ch) {
should.exist(err);
done();
});
}
});
it('should check existence of a scoped record', function (done) {
var id;
Book.create(function (err, book) {
book.chapters.create({name: 'a'}, function (err, ch) {
id = ch.id;
book.chapters.create({name: 'z'}, function () {
book.chapters.create({name: 'c'}, function () {
verify(book);
});
});
});
});
function verify(book) {
book.chapters.exists(id, function (err, flag) {
should.not.exist(err);
flag.should.be.eql(true);
done();
});
}
});
it('should check ignore related data on creation - array', function (done) {
Book.create({ chapters: [] }, function (err, book) {
should.not.exist(err); should.not.exist(err);
flag.should.be.eql(true); book.chapters.should.be.a.function;
var obj = book.toObject();
should.not.exist(obj.chapters);
done();
});
});
it('should check ignore related data on creation - object', function (done) {
Book.create({ chapters: {} }, function (err, book) {
should.not.exist(err);
book.chapters.should.be.a.function;
var obj = book.toObject();
should.not.exist(obj.chapters);
done(); done();
}); });
}
});
it('should check ignore related data on creation - array', function (done) {
Book.create({ chapters: [] }, function (err, book) {
should.not.exist(err);
book.chapters.should.be.a.function;
var obj = book.toObject();
should.not.exist(obj.chapters);
done();
}); });
}); });
it('should check ignore related data on creation - object', function (done) {
Book.create({ chapters: {} }, function (err, book) {
should.not.exist(err);
book.chapters.should.be.a.function;
var obj = book.toObject();
should.not.exist(obj.chapters);
done();
});
});
}); });
describe('hasMany through', function () { describe('hasMany through', function () {
@ -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) { 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 () {
@ -3384,4 +3434,4 @@ describe('relations', function () {
}); });
}); });