2014-03-12 23:28:46 +00:00
|
|
|
/*!
|
2013-04-11 23:23:34 +00:00
|
|
|
* Dependencies
|
|
|
|
*/
|
2014-06-15 22:53:58 +00:00
|
|
|
var relation = require('./relation-definition');
|
|
|
|
var RelationDefinition = relation.RelationDefinition;
|
2013-04-11 23:23:34 +00:00
|
|
|
|
2014-06-15 22:53:58 +00:00
|
|
|
module.exports = RelationMixin;
|
2013-06-05 21:33:52 +00:00
|
|
|
|
2014-03-12 23:28:46 +00:00
|
|
|
/**
|
2014-06-15 22:53:58 +00:00
|
|
|
* RelationMixin class. Use to define relationships between models.
|
2014-03-12 23:28:46 +00:00
|
|
|
*
|
2014-06-15 22:53:58 +00:00
|
|
|
* @class RelationMixin
|
2014-03-12 23:28:46 +00:00
|
|
|
*/
|
2014-06-15 22:53:58 +00:00
|
|
|
function RelationMixin() {
|
2014-01-29 02:00:12 +00:00
|
|
|
}
|
|
|
|
|
2013-04-11 23:23:34 +00:00
|
|
|
/**
|
2014-05-16 00:59:23 +00:00
|
|
|
* Define a "one to many" relationship by specifying the model name
|
2014-03-12 23:28:46 +00:00
|
|
|
*
|
2014-05-16 00:59:23 +00:00
|
|
|
* Examples:
|
|
|
|
* ```
|
|
|
|
* User.hasMany(Post, {as: 'posts', foreignKey: 'authorId'});
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* Book.hasMany(Chapter);
|
|
|
|
* ```
|
|
|
|
* Or, equivalently:
|
|
|
|
* ```
|
|
|
|
* Book.hasMany('chapters', {model: Chapter});
|
|
|
|
* ```
|
2014-05-22 00:50:44 +00:00
|
|
|
*
|
|
|
|
* Query and create related models:
|
|
|
|
*
|
|
|
|
* ```js
|
|
|
|
* Book.create(function(err, book) {
|
|
|
|
*
|
|
|
|
* // Create a chapter instance ready to be saved in the data source.
|
|
|
|
* var chapter = book.chapters.build({name: 'Chapter 1'});
|
|
|
|
*
|
|
|
|
* // Save the new chapter
|
|
|
|
* chapter.save();
|
|
|
|
*
|
|
|
|
* // you can also call the Chapter.create method with the `chapters` property which will build a chapter
|
|
|
|
* // instance and save the it in the data source.
|
|
|
|
* book.chapters.create({name: 'Chapter 2'}, function(err, savedChapter) {
|
|
|
|
* // this callback is optional
|
|
|
|
* });
|
|
|
|
*
|
|
|
|
* // Query chapters for the book
|
|
|
|
* book.chapters(function(err, chapters) { // all chapters with bookId = book.id
|
|
|
|
* console.log(chapters);
|
|
|
|
* });
|
|
|
|
*
|
|
|
|
* book.chapters({where: {name: 'test'}, function(err, chapters) {
|
|
|
|
* // All chapters with bookId = book.id and name = 'test'
|
|
|
|
* console.log(chapters);
|
|
|
|
* });
|
|
|
|
* });
|
|
|
|
*```
|
2014-06-15 22:53:58 +00:00
|
|
|
* @param {Object|String} modelTo Model object (or String name of model) to which you are creating the relationship.
|
2014-06-21 06:02:37 +00:00
|
|
|
* @options {Object} parameters Configuration parameters; see below.
|
2014-05-22 22:02:57 +00:00
|
|
|
* @property {String} as Name of the property in the referring model that corresponds to the foreign key field in the related model.
|
2014-05-16 00:59:23 +00:00
|
|
|
* @property {String} foreignKey Property name of foreign key field.
|
|
|
|
* @property {Object} model Model object
|
2013-04-11 23:23:34 +00:00
|
|
|
*/
|
2014-06-15 22:53:58 +00:00
|
|
|
RelationMixin.hasMany = function hasMany(modelTo, params) {
|
2014-08-13 09:28:23 +00:00
|
|
|
return RelationDefinition.hasMany(this, modelTo, params);
|
2013-04-11 23:23:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2014-05-16 00:59:23 +00:00
|
|
|
* Declare "belongsTo" relation that sets up a one-to-one connection with another model, such that each
|
|
|
|
* instance of the declaring model "belongs to" one instance of the other model.
|
2013-04-11 23:23:34 +00:00
|
|
|
*
|
2014-05-16 00:59:23 +00:00
|
|
|
* For example, if an application includes users and posts, and each post can be written by exactly one user.
|
|
|
|
* The following code specifies that `Post` has a reference called `author` to the `User` model via the `userId` property of `Post`
|
|
|
|
* as the foreign key.
|
|
|
|
* ```
|
|
|
|
* Post.belongsTo(User, {as: 'author', foreignKey: 'userId'});
|
|
|
|
* ```
|
|
|
|
* You can then access the author in one of the following styles.
|
|
|
|
* Get the User object for the post author asynchronously:
|
|
|
|
* ```
|
|
|
|
* post.author(callback);
|
|
|
|
* ```
|
|
|
|
* Get the User object for the post author synchronously:
|
|
|
|
* ```
|
|
|
|
* post.author();
|
2015-12-28 15:03:37 +00:00
|
|
|
* ```
|
2014-05-16 00:59:23 +00:00
|
|
|
* Set the author to be the given user:
|
|
|
|
* ```
|
|
|
|
* post.author(user)
|
|
|
|
* ```
|
|
|
|
* Examples:
|
2014-03-12 23:28:46 +00:00
|
|
|
*
|
|
|
|
* Suppose the model Post has a *belongsTo* relationship with User (the author of the post). You could declare it this way:
|
|
|
|
* ```js
|
2013-04-11 23:23:34 +00:00
|
|
|
* Post.belongsTo(User, {as: 'author', foreignKey: 'userId'});
|
2014-03-12 23:28:46 +00:00
|
|
|
* ```
|
2013-04-11 23:23:34 +00:00
|
|
|
*
|
|
|
|
* When a post is loaded, you can load the related author with:
|
2014-03-12 23:28:46 +00:00
|
|
|
* ```js
|
2013-04-11 23:23:34 +00:00
|
|
|
* post.author(function(err, user) {
|
|
|
|
* // the user variable is your user object
|
|
|
|
* });
|
2014-03-12 23:28:46 +00:00
|
|
|
* ```
|
2013-04-11 23:23:34 +00:00
|
|
|
*
|
|
|
|
* The related object is cached, so if later you try to get again the author, no additional request will be made.
|
|
|
|
* But there is an optional boolean parameter in first position that set whether or not you want to reload the cache:
|
2014-03-12 23:28:46 +00:00
|
|
|
* ```js
|
2013-04-11 23:23:34 +00:00
|
|
|
* post.author(true, function(err, user) {
|
|
|
|
* // The user is reloaded, even if it was already cached.
|
|
|
|
* });
|
2014-03-12 23:28:46 +00:00
|
|
|
* ```
|
2013-04-11 23:23:34 +00:00
|
|
|
* This optional parameter default value is false, so the related object will be loaded from cache if available.
|
2014-06-21 06:02:37 +00:00
|
|
|
*
|
2014-06-15 22:53:58 +00:00
|
|
|
* @param {Class|String} modelTo Model object (or String name of model) to which you are creating the relationship.
|
2014-05-22 00:50:44 +00:00
|
|
|
* @options {Object} params Configuration parameters; see below.
|
2014-05-22 22:02:57 +00:00
|
|
|
* @property {String} as Name of the property in the referring model that corresponds to the foreign key field in the related model.
|
2014-05-16 00:59:23 +00:00
|
|
|
* @property {String} foreignKey Name of foreign key property.
|
2014-03-12 23:28:46 +00:00
|
|
|
*
|
2013-04-11 23:23:34 +00:00
|
|
|
*/
|
2014-06-15 22:53:58 +00:00
|
|
|
RelationMixin.belongsTo = function (modelTo, params) {
|
2014-08-13 09:28:23 +00:00
|
|
|
return RelationDefinition.belongsTo(this, modelTo, params);
|
2013-04-11 23:23:34 +00:00
|
|
|
};
|
|
|
|
|
2013-04-12 21:35:06 +00:00
|
|
|
/**
|
2014-05-16 00:59:23 +00:00
|
|
|
* A hasAndBelongsToMany relation creates a direct many-to-many connection with another model, with no intervening model.
|
|
|
|
* For example, if your application includes users and groups, with each group having many users and each user appearing
|
|
|
|
* in many groups, you could declare the models this way:
|
|
|
|
* ```
|
|
|
|
* User.hasAndBelongsToMany('groups', {model: Group, foreignKey: 'groupId'});
|
|
|
|
* ```
|
|
|
|
* Then, to get the groups to which the user belongs:
|
|
|
|
* ```
|
|
|
|
* user.groups(callback);
|
|
|
|
* ```
|
|
|
|
* Create a new group and connect it with the user:
|
|
|
|
* ```
|
|
|
|
* user.groups.create(data, callback);
|
|
|
|
* ```
|
|
|
|
* Connect an existing group with the user:
|
|
|
|
* ```
|
|
|
|
* user.groups.add(group, callback);
|
|
|
|
* ```
|
|
|
|
* Remove the user from the group:
|
|
|
|
* ```
|
|
|
|
* user.groups.remove(group, callback);
|
|
|
|
* ```
|
|
|
|
*
|
2014-06-15 22:53:58 +00:00
|
|
|
* @param {String|Object} modelTo Model object (or String name of model) to which you are creating the relationship.
|
2014-03-11 22:38:07 +00:00
|
|
|
* the relation
|
2014-05-22 00:50:44 +00:00
|
|
|
* @options {Object} params Configuration parameters; see below.
|
2014-05-22 22:02:57 +00:00
|
|
|
* @property {String} as Name of the property in the referring model that corresponds to the foreign key field in the related model.
|
2014-05-16 00:59:23 +00:00
|
|
|
* @property {String} foreignKey Property name of foreign key field.
|
2014-05-22 00:50:44 +00:00
|
|
|
* @property {Object} model Model object
|
2013-04-12 21:35:06 +00:00
|
|
|
*/
|
2014-06-15 22:53:58 +00:00
|
|
|
RelationMixin.hasAndBelongsToMany = function hasAndBelongsToMany(modelTo, params) {
|
2014-08-13 09:28:23 +00:00
|
|
|
return RelationDefinition.hasAndBelongsToMany(this, modelTo, params);
|
2013-04-12 21:35:06 +00:00
|
|
|
};
|
2014-01-24 17:09:53 +00:00
|
|
|
|
2014-08-19 20:10:35 +00:00
|
|
|
RelationMixin.hasOne = function hasOne(modelTo, params) {
|
2014-08-13 09:28:23 +00:00
|
|
|
return RelationDefinition.hasOne(this, modelTo, params);
|
2013-04-12 21:35:06 +00:00
|
|
|
};
|
2014-07-27 14:30:45 +00:00
|
|
|
|
2014-08-19 20:10:35 +00:00
|
|
|
RelationMixin.referencesMany = function referencesMany(modelTo, params) {
|
2014-08-13 09:28:23 +00:00
|
|
|
return RelationDefinition.referencesMany(this, modelTo, params);
|
2014-07-29 08:51:33 +00:00
|
|
|
};
|
|
|
|
|
2014-08-19 20:10:35 +00:00
|
|
|
RelationMixin.embedsOne = function embedsOne(modelTo, params) {
|
|
|
|
return RelationDefinition.embedsOne(this, modelTo, params);
|
|
|
|
};
|
|
|
|
|
|
|
|
RelationMixin.embedsMany = function embedsMany(modelTo, params) {
|
2014-08-13 09:28:23 +00:00
|
|
|
return RelationDefinition.embedsMany(this, modelTo, params);
|
2014-07-27 14:30:45 +00:00
|
|
|
};
|