Relations

  • Models can define ComputeProperty which related to other Models.
  • These Relation ComputeProperty's CompiledComputeFunction accept findOptions as arguments, it allows filtering or ordering the records of the related Models by your findOptions.
  • For Model, there are several function to create Relation ComputeProperty
  1. .belongsTo()
  2. .hasMany()
  3. .hasManyThrough()

TIP

For findOptions, please see findOptions.

belongsTo

Argumentstypedefault value
Related ModelModel
Name of the FieldProperty of current Modelstring
Name of the FieldProperty of related Modelstring"id"
export default class Product extends Model {
    id = this.field(PrimaryKeyType)
    shopId = this.field(NumberType)
    //define a computeProperty as "belongsTo" relation
    shop = Product.belongsTo(Shop, 'shopId', 'id')
}

export default class Shop extends Model {
    id= this.field(PrimaryKeyType)
    //define a computeProperty as "hasMany" relation
    products = Shop.hasMany(Product, 'shopId', 'id')
}




 







1
2
3
4
5
6
7
8
9
10
11
12

hasMany

Argumentstypedefault value
Related ModelModel
Name of the FieldProperty of related Modelstring
Name of the FieldProperty of current Modelstring"id"
export default class Product extends Model {
    id = this.field(PrimaryKeyType)
    shopId = this.field(NumberType)
    //define a computeProperty as "belongsTo" relation
    shop = Product.belongsTo(Shop, 'shopId', 'id')
}

export default class Shop extends Model {
    id= this.field(PrimaryKeyType)
    //define a computeProperty as "hasMany" relation
    products = Shop.hasMany(Product, 'shopId', 'id')
}










 

1
2
3
4
5
6
7
8
9
10
11
12

hasManyThrough