added relations key
This commit is contained in:
parent
b3d92131b3
commit
24ccb0ffc2
|
@ -666,6 +666,13 @@ AbstractClass.prototype.reset = function () {
|
||||||
AbstractClass.hasMany = function hasMany(anotherClass, params) {
|
AbstractClass.hasMany = function hasMany(anotherClass, params) {
|
||||||
var methodName = params.as; // or pluralize(anotherClass.modelName)
|
var methodName = params.as; // or pluralize(anotherClass.modelName)
|
||||||
var fk = params.foreignKey;
|
var fk = params.foreignKey;
|
||||||
|
|
||||||
|
this.relations[params['as']] = {
|
||||||
|
type: 'hasMany',
|
||||||
|
keyFrom: 'id',
|
||||||
|
keyTo: params['foreignKey'],
|
||||||
|
multiple: true
|
||||||
|
};
|
||||||
// each instance of this class should have method named
|
// each instance of this class should have method named
|
||||||
// pluralize(anotherClass.modelName)
|
// pluralize(anotherClass.modelName)
|
||||||
// which is actually just anotherClass.all({where: {thisModelNameId: this.id}}, cb);
|
// which is actually just anotherClass.all({where: {thisModelNameId: this.id}}, cb);
|
||||||
|
@ -733,6 +740,13 @@ AbstractClass.belongsTo = function (anotherClass, params) {
|
||||||
var methodName = params.as;
|
var methodName = params.as;
|
||||||
var fk = params.foreignKey;
|
var fk = params.foreignKey;
|
||||||
|
|
||||||
|
this.relations[params['as']] = {
|
||||||
|
type: 'belongsTo',
|
||||||
|
keyFrom: params['foreignKey'],
|
||||||
|
keyTo: 'id',
|
||||||
|
multiple: false
|
||||||
|
};
|
||||||
|
|
||||||
this.schema.defineForeignKey(this.modelName, fk);
|
this.schema.defineForeignKey(this.modelName, fk);
|
||||||
this.prototype['__finders__'] = this.prototype['__finders__'] || {};
|
this.prototype['__finders__'] = this.prototype['__finders__'] || {};
|
||||||
|
|
||||||
|
|
|
@ -157,6 +157,7 @@ Schema.prototype.define = function defineClass(className, properties, settings)
|
||||||
hiddenProperty(NewClass, 'modelName', className);
|
hiddenProperty(NewClass, 'modelName', className);
|
||||||
hiddenProperty(NewClass, 'cache', {});
|
hiddenProperty(NewClass, 'cache', {});
|
||||||
hiddenProperty(NewClass, 'mru', []);
|
hiddenProperty(NewClass, 'mru', []);
|
||||||
|
hiddenProperty(NewClass, 'relations', {});
|
||||||
|
|
||||||
// inherit AbstractClass methods
|
// inherit AbstractClass methods
|
||||||
for (var i in AbstractClass) {
|
for (var i in AbstractClass) {
|
||||||
|
|
|
@ -432,6 +432,29 @@ function testOrm(schema) {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (
|
||||||
|
!schema.name.match(/redis/) &&
|
||||||
|
schema.name !== 'memory' &&
|
||||||
|
schema.name !== 'neo4j' &&
|
||||||
|
schema.name !== 'cradle'
|
||||||
|
)
|
||||||
|
it('relations key is working', function (test) {
|
||||||
|
test.ok(User.relations, 'Relations key should be defined');
|
||||||
|
test.ok(User.relations.posts, 'posts relation should exist on User');
|
||||||
|
test.equal(User.relations.posts.type, 'hasMany', 'Type of hasMany relation is hasMany');
|
||||||
|
test.equal(User.relations.posts.multiple, true, 'hasMany relations are multiple');
|
||||||
|
test.equal(User.relations.posts.keyFrom, 'id', 'keyFrom is primary key of model table');
|
||||||
|
test.equal(User.relations.posts.keyTo, 'userId', 'keyTo is foreign key of related model table');
|
||||||
|
|
||||||
|
test.ok(Post.relations, 'Relations key should be defined');
|
||||||
|
test.ok(Post.relations.author, 'author relation should exist on Post');
|
||||||
|
test.equal(Post.relations.author.type, 'belongsTo', 'Type of belongsTo relation is belongsTo');
|
||||||
|
test.equal(Post.relations.author.multiple, false, 'belongsTo relations are not multiple');
|
||||||
|
test.equal(Post.relations.author.keyFrom, 'userId', 'keyFrom is foreign key of model table');
|
||||||
|
test.equal(Post.relations.author.keyTo, 'id', 'keyTo is primary key of related model table');
|
||||||
|
test.done();
|
||||||
|
});
|
||||||
|
|
||||||
it('should handle hasMany relationship', function (test) {
|
it('should handle hasMany relationship', function (test) {
|
||||||
User.create(function (err, u) {
|
User.create(function (err, u) {
|
||||||
if (err) return console.log(err);
|
if (err) return console.log(err);
|
||||||
|
@ -449,7 +472,6 @@ function testOrm(schema) {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
if (
|
if (
|
||||||
!schema.name.match(/redis/) &&
|
!schema.name.match(/redis/) &&
|
||||||
schema.name !== 'memory' &&
|
schema.name !== 'memory' &&
|
||||||
|
|
Loading…
Reference in New Issue