-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunkit-example.js
91 lines (79 loc) · 2.44 KB
/
runkit-example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
require('mysql2')
const { format } = require('sql-formatter')
const { ORM, Model, PrimaryKeyType, StringType, StringNotNullType, DateType, NumberNotNullType } = require('taichi-orm')
/*
* Runkit doesn't support Class Properties
* So we assign properties in constructor
*/
class ShopModel extends Model{
constructor(...args){
super(...args)
this.id = this.field(PrimaryKeyType)
this.name = this.field(new StringType({length: 100}))
this.products = ShopModel.hasMany(ProductModel, 'shopId')
}
}
class ColorModel extends Model{
constructor(...args){
super(...args)
this.id = this.field(PrimaryKeyType)
this.code = this.field(new StringNotNullType({length: 50}))
}
}
class ProductColorModel extends Model{
constructor(...args){
super(...args)
this.id = this.field(PrimaryKeyType)
this.productId = this.field(NumberNotNullType)
this.colorId = this.field(NumberNotNullType)
this.type = this.field(new StringNotNullType({length: 50}))
}
}
class ProductModel extends Model{
constructor(...args){
super(...args)
this.id = this.field(PrimaryKeyType)
this.name = this.field(new StringType({length: 100}))
this.createdAt = this.field(DateType)
this.shopId = this.field(NumberNotNullType)
this.shop = ProductModel.belongsTo(ShopModel, 'shopId')
this.colors = ProductModel.hasManyThrough(ProductColorModel, ColorModel, 'id', 'colorId', 'productId')
//computed property created based on colors
this.colorsWithType = ProductModel.compute( (parent, type = 'main') => {
return parent.$.colors({
where: ({through}) => through.type.equals(type)
})
})
}
}
(async() =>{
// configure database
const orm = new ORM({
models: {
Shop: ShopModel,
Product: ProductModel,
Color: ColorModel,
ProductColor: ProductColorModel
},
knexConfig: {
client: 'mysql2'
}
})
const { Shop, Product, Color, ProductColor } = orm.getContext().repos
// computed fields are the relations
// you can do complicated query in one go
// Graph-like selecting Models "Shop > Product > Color"
let records = await Shop.find({
select: {
products: {
select: {
colors: {
limit: 2
},
colorsWithType: 'main'
}
}
}
}).getBuilder().toSqlString()
console.log('print the sql', format(records))
})()