Schema
- It represents a dictionary of properties.
Dataset
,ModelRepository
orModel
can produce aSchema
.
Examples:
context.dataset().schema()
context.repos.Model1.schema()
require('./models/Model1').schema()
1
2
3
4
5
6
2
3
4
5
6
Datasource
- It is produced from a
Schema
by giving a name (acts as table alias). - It refers to a database table or a temporary table.
- It is used by the Query Builders'
from
,innerJoin
,leftJoin
andrightJoin
functions.
Example: Below it makes a query with selecting fields from table Model1
with alias table_alias
.
const schema = context.repos.Model1.schema()
let queryBuilder = context.dataset()
.from(schema.datasource('table_alias'))
.select(
'table_alias.id',
'table_alias.field1',
'table_alias.field2'
)
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
There are many ways to create a datasource instance.
Model1.schema().datasource(string)
//OR a shortcut:
Model1.datasource(string)
modelRepository1.schema().datasource(string)
//OR a shortcut:
modelRepository1.datasource(string)
dataset1.schema().datasource(string)
//OR a shortcut:
dataset1.datasource(string)
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
$
)
ValueSelector (A datasource instance provides an object $
having values of Scalar
or CompiledComputeFunction
which represents the properties of the Schema.
TIP
Scalar
is a single SQL value that can be a field name or SQL functions.
Please see Scalar for more details.
Examples:
export default class MyModel extends Model {
// Nullable Integer
prop1 = this.field(NumberType)
prop2 = this.compute( (parent) => parent.minus(3) )
}
const scalar1 = Model1.datasource('table1').$.prop1
const scalar2 = Model1.datasource('table1').$.prop2()
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
If the key of $
refers to a FieldProperty
's name, the value will be a Scalar
.
If it refers to a ComputeProperty
's name, the value is a Function (CompiledComputeFunction
) that returns a Scalar
.
TIP
Please see Property for more explanation.