Merge branch 'release/2.1.0' into production

This commit is contained in:
Raymond Feng 2014-07-27 00:01:15 -07:00
commit 45ece18e69
5 changed files with 94 additions and 10 deletions

View File

@ -647,7 +647,8 @@ DataSource.prototype.getModelDefinition = function (name) {
* ['rest'], or ['db', 'rdbms', 'mysql']
*/
DataSource.prototype.getTypes = function () {
var types = this.connector && this.connector.getTypes() || [];
var getTypes = this.connector && this.connector.getTypes;
var types = getTypes && getTypes() || [];
if (typeof types === 'string') {
types = types.split(/[\s,\/]+/);
}

View File

@ -819,6 +819,8 @@ RelationDefinition.belongsTo = function (modelFrom, modelTo, params) {
keyFrom: fk,
keyTo: idName,
modelTo: modelTo,
properties: params.properties,
scope: params.scope,
options: params.options
});
@ -851,8 +853,8 @@ RelationDefinition.belongsTo = function (modelFrom, modelTo, params) {
BelongsTo.prototype.create = function(targetModelData, cb) {
var self = this;
var modelTo = this.definition.modelTo;
var fk = this.definition.keyTo;
var pk = this.definition.keyFrom;
var fk = this.definition.keyFrom;
var pk = this.definition.keyTo;
var modelInstance = this.modelInstance;
if (typeof targetModelData === 'function' && !cb) {
@ -860,6 +862,8 @@ BelongsTo.prototype.create = function(targetModelData, cb) {
targetModelData = {};
}
this.definition.applyProperties(modelInstance, targetModelData || {});
modelTo.create(targetModelData, function(err, targetModel) {
if(!err) {
modelInstance[fk] = targetModel[pk];
@ -873,6 +877,7 @@ BelongsTo.prototype.create = function(targetModelData, cb) {
BelongsTo.prototype.build = function(targetModelData) {
var modelTo = this.definition.modelTo;
this.definition.applyProperties(this.modelInstance, targetModelData || {});
return new modelTo(targetModelData);
};
@ -911,7 +916,12 @@ BelongsTo.prototype.related = function (refresh, params) {
} else if (typeof params === 'function') { // acts as async getter
var cb = params;
if (cachedValue === undefined) {
modelTo.findById(modelInstance[fk], function (err, inst) {
var query = {where: {}};
query.where[pk] = modelInstance[fk];
this.definition.applyScope(modelInstance, query);
modelTo.findOne(query, function (err, inst) {
if (err) {
return cb(err);
}
@ -919,7 +929,8 @@ BelongsTo.prototype.related = function (refresh, params) {
return cb(null, null);
}
// Check if the foreign key matches the primary key
if (inst[pk] === modelInstance[fk]) {
if (inst[pk] && modelInstance[fk]
&& inst[pk].toString() === modelInstance[fk].toString()) {
self.resetCache(inst);
cb(null, inst);
} else {
@ -1044,6 +1055,7 @@ RelationDefinition.hasOne = function (modelFrom, modelTo, params) {
var relationMethod = relation.related.bind(relation)
relationMethod.create = relation.create.bind(relation);
relationMethod.build = relation.build.bind(relation);
relationMethod._targetClass = relationDef.modelTo.modelName;
return relationMethod;
}
});
@ -1193,7 +1205,8 @@ HasOne.prototype.related = function (refresh, params) {
return cb(null, null);
}
// Check if the foreign key matches the primary key
if (inst[fk] === modelInstance[pk]) {
if (inst[fk] && modelInstance[pk]
&& inst[fk].toString() === modelInstance[pk].toString()) {
self.resetCache(inst);
cb(null, inst);
} else {

View File

@ -221,7 +221,8 @@ function defineScope(cls, targetClass, name, params, methods) {
- If fetching the Elements on which destroyAll is called results in an error
*/
function destroyAll(cb) {
targetClass.destroyAll(this._scope, cb);
var where = (this._scope && this._scope.where) || {};
targetClass.destroyAll(where, cb);
}
}

View File

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

View File

@ -404,7 +404,7 @@ describe('relations', function () {
});
});
describe('hasMany with scope', function () {
describe('hasMany with scope and properties', function () {
it('can be declared with properties', function (done) {
db = getSchema();
Category = db.define('Category', {name: String, productType: String});
@ -437,7 +437,7 @@ describe('relations', function () {
});
});
it('should find record on scope', function (done) {
it('should find records on scope', function (done) {
Category.findOne(function (err, c) {
c.products(function(err, products) {
products.should.have.length(2);
@ -476,6 +476,15 @@ describe('relations', function () {
});
});
it('should find records on scope', function (done) {
Category.findOne(function (err, c) {
c.products(function(err, products) {
products.should.have.length(3);
done();
});
});
});
it('should find record on scope - scoped', function (done) {
Category.findOne(function (err, c) {
c.productType = 'book'; // temporary, for scoping
@ -497,6 +506,24 @@ describe('relations', function () {
});
});
});
it('should delete records on scope - scoped', function (done) {
Category.findOne(function (err, c) {
c.productType = 'tool'; // temporary, for scoping
c.products.destroyAll(function(err, result) {
done();
});
});
});
it('should find record on scope - verify', function (done) {
Category.findOne(function (err, c) {
c.products(function(err, products) {
products.should.have.length(2);
done();
});
});
});
});
@ -556,6 +583,45 @@ describe('relations', function () {
});
});
describe('belongsTo with scope', function () {
var Person, Passport;
it('can be declared with scope and properties', function (done) {
Person = db.define('Person', {name: String, age: Number});
Passport = db.define('Passport', {name: String, notes: String});
Passport.belongsTo(Person, {
properties: { notes: 'passportNotes' },
scope: { fields: { id: true, name: true } }
});
db.automigrate(done);
});
it('should create record on scope', function (done) {
var p = new Passport({ name: 'Passport', notes: 'Some notes...' });
p.person.create({ id: 3, name: 'Fred', age: 36 }, function(err, person) {
p.personId.should.equal(person.id);
p.save(function (err, p) {
person.name.should.equal('Fred');
person.passportNotes.should.equal('Some notes...');
done();
});
});
});
it('should find record on scope', function (done) {
Passport.findOne(function (err, p) {
p.personId.should.equal(3);
p.person(function(err, person) {
person.name.should.equal('Fred');
person.should.not.have.property('age');
person.should.not.have.property('passportNotes');
done();
});
});
});
});
describe('hasOne', function () {
var Supplier, Account;
@ -592,6 +658,9 @@ describe('relations', function () {
});
});
it('should set targetClass on scope property', function() {
should.equal(Supplier.prototype.account._targetClass, 'Account');
});
});
describe('hasAndBelongsToMany', function () {