Model
- It represents a database table.
- It used as a parent class for your data models. Your models should
extendsModeland be defined withProperties. - All
Modelshave to be registered before using them. Please see Register Models
Usage
Example:
class MyModel extends Model {
//define Property instance here
}
1
2
3
2
3
Define FieldProperty
The method .field() of Model instance creates a FieldProperty (represents a table field).
Example:
class MyModel extends Model {
//define a non-nullable varchar type data
prop1 = this.field(StringNotNullType)
}
1
2
3
4
2
3
4
TIP
Please read the FieldProperty for more details
Define ComputeProperty
The method .compute() of Model instance creates a ComputeProperty that represent a value calculated by using other FieldProperty or SQL functions.
Example:
class MyModel extends Model {
prop1 = this.field(NumberType)
prop2 = this.compute( (parent)=> {
// add 5 to the number value of prop1
return parent.prop1.add(5)
})
}
1
2
3
4
5
6
7
2
3
4
5
6
7
TIP
Please read the FieldProperty for more details
Define Relations
Model class provides various methods to define relations.
Example:
const Shop = require('./shop')
class Product extends Model {
//define a belongsto Relation
shop = Product.belongsTo(Shop)
}
1
2
3
4
5
6
2
3
4
5
6
TIP
Please read the Relations for more details