new RelationDefinition(definition)
Relation definition class. Use to define relationships between models.
Parameters:
Name |
Type |
Description |
definition |
Object
|
|
- Source:
Methods
applyProperties(modelInstance, target)
Apply the configured properties to the target object.
Parameters:
Name |
Type |
Description |
modelInstance |
Object
|
|
target |
Object
|
|
- Source:
applyScope(modelInstance, filter)
Apply the configured scope to the filter/query object.
Parameters:
Name |
Type |
Description |
modelInstance |
Object
|
|
filter |
Object
|
(where, order, limit, fields, ...) |
- Source:
defineMethod(name, function)
Define a relation scope method
Parameters:
Name |
Type |
Description |
name |
String
|
of the method |
function |
function
|
to define |
- Source:
(static) belongsTo(modelToRef)
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.
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'});
```
This optional parameter default value is false, so the related object will
be loaded from cache if available.
Parameters:
Name |
Type |
Description |
modelToRef |
Object
|
String
|
Reference to Model object to which you are
creating the relation: model instance, model name, or name of relation to model. |
Properties:
Name |
Type |
Description |
as |
String
|
Name of the property in the referring model that
corresponds to the foreign key field in the related model. |
foreignKey |
String
|
Name of foreign key property. |
- Source:
(static) hasAndBelongsToMany(modelToRef)
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'});
```
Parameters:
Name |
Type |
Description |
modelToRef |
Object
|
String
|
Reference to Model object to which you are
creating the relation: model instance, model name, or name of relation to model. |
Properties:
Name |
Type |
Description |
as |
String
|
Name of the property in the referring model that
corresponds to the foreign key field in the related model. |
foreignKey |
String
|
Property name of foreign key field. |
model |
Object
|
Model object |
- Source:
(static) hasMany(modelFrom, modelToRef)
Define a "one to many" relationship by specifying the model name
Examples:
```
User.hasMany(Post, {as: 'posts', foreignKey: 'authorId'});
```
```
Book.hasMany(Chapter);
```
Or, equivalently:
```
Book.hasMany('chapters', {model: Chapter});
```
Parameters:
Name |
Type |
Description |
modelFrom |
Model
|
Source model class |
modelToRef |
Object
|
String
|
Reference to Model object to which you are
creating the relation: model instance, model name, or name of relation to model. |
Properties:
Name |
Type |
Description |
as |
String
|
Name of the property in the referring model that corresponds to the foreign key field in the related model. |
foreignKey |
String
|
Property name of foreign key field. |
model |
Object
|
Model object |
- Source:
(static) hasOne(modelFrom, modelToRef)
A HasOne relation creates a one-to-one connection from modelFrom to modelTo.
This relation indicates that each instance of a model contains or possesses
one instance of another model. For example, each supplier in your application
has only one account.
Parameters:
Name |
Type |
Description |
modelFrom |
function
|
The declaring model class |
modelToRef |
Object
|
String
|
Reference to Model object to which you are
creating the relation: model instance, model name, or name of relation to model. |
Properties:
Name |
Type |
Description |
as |
String
|
Name of the property in the referring model that
corresponds to the foreign key field in the related model. |
foreignKey |
String
|
Property name of foreign key field. |
model |
Object
|
Model object |
- Source: