Class moose.plugins.AssociationPlugin
plugin to expose association capability.
The associations exposed include- oneToMany - Foreign key in associated model's table points to this model's primary key. Each current model object can be associated with more than one associated model objects. Each associated model object can be associated with only one current model object.
- manyToOne - Foreign key in current model's table points to associated model's primary key. Each associated model object can be associated with more than one current model objects. Each current model object can be associated with only one associated model object.
- oneToOne - Similar to one_to_many in terms of foreign keys, but only one object is associated to the current object through the association. The methods created are similar to many_to_one, except that the one_to_one setter method saves the passed object./li>
- manyToMany - A join table is used that has a foreign key that points to this model's primary key and a foreign key that points to the associated model's primary key. Each current model object can be associated with many associated model objects, and each associated model object can be associated with many current model objects./li>
Defined in: association.js.
Constructor Attributes | Constructor Name and Description |
---|---|
Field Attributes | Field Name and Description |
---|---|
<static> |
moose.plugins.AssociationPlugin.fetchType
|
Method Attributes | Method Name and Description |
---|---|
<static> |
moose.plugins.AssociationPlugin.manyToMany(name, options, key)
The manyToMany association allows a model to be associated to many other rows in another model.
|
<static> |
moose.plugins.AssociationPlugin.manyToOne(name, options, key)
See AssociationPlugin.oneToMany
|
<static> |
moose.plugins.AssociationPlugin.oneToMany(name, options, key)
One of the most common forms of associtaions.
|
<static> |
moose.plugins.AssociationPlugin.oneToOne(name, options, key)
Simplest form of association. |
Field Detail
<static>
moose.plugins.AssociationPlugin.fetchType
Method Detail
<static>
moose.plugins.AssociationPlugin.manyToMany(name, options, key)
The manyToMany association allows a model to be associated to many other rows in another model.
and the associated model can be associated with many rows in this model. This is done by
using a join table to associate the two models.
For example consider phone numbers. Each person can have multiple phone numbers.
phone person_phone person ------ ---------------------- ----- |id | |person_id | phone_id| |id | ------ ---------------------- ----- | 1 | | 1 | 1 | | 1 | | . | <------ | 1 | 2 | ------> | 2 | | . | | 2 | 2 | | 3 | | n | | 2 | 1 | | 4 | ------ ---------------------- -----
//define the PhoneNumber model var PhoneNumber = moose.addModel("phone"); //define Person model var Person = moose.addModel("person"); //Create manyToMany relationship from person to PhoneNumber Person.manyToMany("phoneNumbers", { model : PhoneNumber.tableName, joinTable : "person_phone", key : {person_id : "phone_id"} }); PhoneNumber.manyToMany("owners", { model : Person.tableName, joinTable : "person_phone", key : {phone_id : "person_id"} }); Person.findById(1).then(function(person){ person.phoneNumbers.then(function(numbers){ numbers.length => 2 }); }); PhoneNumber.findById(1).then(function(number){ number.owners.then(function(owners){ owners.length => 2; }); });
- Parameters:
- {String} name
- the alias of the association. The key you provide here is how the association will be looked up on instances of this model.
- {Object} options
- object that describes the association.
- {String} options.model
- the table name of the model that this Model is associated with.
- {String} options.joinTable
- the name of the joining table.
- {Function} options.filter
- Custom filter to define a custom association.
The filter is called in the scope of model that the association is added to.
Say we have the same models as defined above.
//Define the join table model so we can query it. PersonPhone = moose.addModel(person_phone); PhoneNumber.manyToMany("owners", { model : Person.tableName, joinTable : "person_phone", filter : function(){ //find all the person ids var jd = PhoneNumber.dataset .select('person_id') .find({phone_id : this.id}); //now query person with the ids! return Person.filter({id : {"in" : jd}}); } });
- {AssociationPlugin.fetchType.EAGER|AssociationPlugin.fetchType.EAGER} options.fetchType Optional, Default: AssociationPlugin.fetchType.LAZY
- how fetch the association, if specified to lazy then the association is lazy loaded. Otherwise the association is loaded when the model is loaded.
- {Object} key
- this defines the foreign key relationship
{thisModelsKey : otherModelsKey}
- {String|Object} options.orderBy Optional
- column or columns to order the associated model by.
<static>
moose.plugins.AssociationPlugin.manyToOne(name, options, key)
See AssociationPlugin.oneToMany
- Parameters:
- {String} name
- the alias of the association. The key you provide here is how the association will be looked up on instances of this model.
- {Object} options
- object that describes the association.
- {String} options.model
- the table name of the model that this Model is associated with.
- {Function} options.filter
- Custom filter to define a custom association.
The filter is called in the scope of model that the association is added to.
Say we have a model called Child that is a many to one to a model called BioFather.
Child.manyToOne("biologicalFather", { model : BioFather.tableName, fetchType : BioFather.fetchType.EAGER, filter : function(){ return BioFather.filter({id : this.bidFatherId}); } });
- {AssociationPlugin.fetchType.EAGER|AssociationPlugin.fetchType.EAGER} options.fetchType Optional, Default: AssociationPlugin.fetchType.LAZY
- how fetch the association, if specified to lazy then the association is lazy loaded. Otherwise the association is loaded when the model is loaded.
- {Object} key
- this defines the foreign key relationship
- {String|Array} options.orderBy Optional
- column or columns to order the associated model by.
{thisModelsKey : otherModelsKey}
- {String|Object} options.orderBy Optional
- column or columns to order the associated model by.
<static>
moose.plugins.AssociationPlugin.oneToMany(name, options, key)
One of the most common forms of associtaions. One to Many is the inverse of Many to one. One to Many often describes a parent child reationship,
where the One To many Model is the parent, and the many to one model is the child.
For example consider a BiologicalFather and his children. The father can have many children, but a child can have only one Biological Father.
biological_father child -------------_ ------------------------- |id | name | |id | bioFatherId | name | -------------- ------------------------- | 1 | Fred | | 1 | 1 | Bobby | | 2 | Ben | ------> | 2 | 1 | Alice | | 3 | Bob | | 3 | 1 | Susan | | 4 | Scott | | 4 | 4 | Brad | -------------- -------------------------
//define Social security model var BioFather = moose.addModel("biological_father"); //define Person model var Child = moose.addModel("child"); //Create oneToMany relationship from father to child BioFather.oneToMany("children", { model : Child.tableName, key : {id : "bioFatherId"} }); //Create oneToOne relation ship from ssn to person with a fetchtype of eager. Child.manyToOne("biologicalFather", { model : BioFather.tableName, fetchType : BioFather.fetchType.EAGER, key : {bioFatherId : "id"} }); Child.findById(1).then(function(child){ child.father.name => "fred" }); BioFather.findById(1).then(function(father){ father.children.then(function(children){ children.length => 3 }); });
- Parameters:
- {String} name
- the alias of the association. The key you provide here is how the association will be looked up on instances of this model.
- {Object} options
- object that describes the association.
- {String} options.model
- the table name of the model that this Model is associated with.
- {Function} options.filter
- Custom filter to define a custom association.
The filter is called in the scope of model that the association is added to.
Say we have a model called BioFather that is a one to many to a model called Child.
BioFather.oneToMany("children", { model : Child.tableName, fetchType : BioFather.fetchType.EAGER, filter : function(){ return Child.filter({bioFatherId : this.id}); } });
- {AssociationPlugin.fetchType.EAGER|AssociationPlugin.fetchType.EAGER} options.fetchType Optional, Default: AssociationPlugin.fetchType.LAZY
- how fetch the association, if specified to lazy then the association is lazy loaded. Otherwise the association is loaded when the model is loaded.
- {Object} key
- this defines the foreign key relationship
{thisModelsKey : otherModelsKey}
- {String|Object} options.orderBy Optional
- column or columns to order the associated model by.
<static>
moose.plugins.AssociationPlugin.oneToOne(name, options, key)
Simplest form of association. This describes where there is a one to one relationship between classes.
When createing a reciprocal one to one relationship between models one of the models should be a many to one association. The table that contains the foreign key should contain have the manyToOne relationship.For example consider social security numbers. There is one social security per person, this would be considered a one to one relationship.
Person SSN NUMBER ------------------------- --------------- |id | ssn | |id | ------------------------- --------------- |00000001 | 111111111 | | 111111111 | | ......... | ......... | ------> | ......... | | ......... | ......... | | ......... | | nnnnnnnnn | nnnnnnnn | | nnnnnnnnn | ------------------------- ---------------
//define Social security model var SocialSecurityNumber = moose.addModel("ssn"); //define Person model var Person = moose.addModel("person"); //Create oneToOne relation ship from ssn to person with a fetchtype of eager. SocialSecurityNumber.oneToOne("person", { model : Person.tableName, fetchType : SocialSecurityNumber.fetchType.EAGER, key : {id : "ssn"} }); //Create oneToMany relation ship from person to ssn, //It is many to one because is contains the ssn foreign key. Person.manyToOne("ssn", { model : SocialSecurityNumber.tableName, key : {ssn : "id"} }); Person.findById(1).then(function(person){ person.ssn.then(function(ssn){ ssn.id => 111111111 }); }); SocialSecurityNumber.findById(111111111).then(function(ssn){ ssn.person.id => 1 });
- Parameters:
- {String} name
- the alias of the association. The key you provide here is how the association will be looked up on instances of this model.
- {Object} options
- object that describes the association.
- {String} options.model
- the table name of the model that this Model is associated with.
- {Function} options.filter
- Custom filter to define a custom association.
The filter is called in the scope of model that the association is added to.
Say we have the same models as defined above.
SocialSecurityNumber.oneToOne("person", { model : Person.tableName, filter : function(){ //find the worker that has my id. return Person.filter({ssn : this.id}); } });
- {AssociationPlugin.fetchType.EAGER|AssociationPlugin.fetchType.EAGER} options.fetchType Optional, Default: AssociationPlugin.fetchType.LAZY
- how fetch the association, if specified to lazy then the association is lazy loaded. Otherwise the association is loaded when the model is loaded.
- {Object} key
- this defines the foreign key relationship
{thisModelsKey : otherModelsKey}
- {String|Object} options.orderBy Optional
- column or columns to order the associated model by.