标签:category 关联 res tar target 设置 注意 def 一对一
joins: {
TargetCubeName: {
relationship: `belongsTo` || `hasMany` || `hasOne`,
sql: `SQL ON clause`
}
}
{
member: "Posts.authorName",
operator: "set"
}
注意定义join 的relationship必须明确,不然可能会产生不一样的结果
cube("Users", {
joins: {
Profile: {
relationship: `hasOne`,
sql: `${Users}.id = ${Profile}.user_id`
}
}
});
hasMany
定义一对多的场景,比如一个作者有多本书
cube("Authors", {
joins: {
Books: {
relationship: `hasMany`,
sql: `${Authors}.id = ${Books}.author_id`
}
}
});
belongsTo
定义多对一的场景,比如一个客户有多个订单,对于order 与客户的关联就是订单属于客户
cube("Orders", {
joins: {
Customers: {
relationship: `belongsTo`,
sql: `${Orders}.customer_id = ${Customers}.id`
}
}
});
sql: `${Orders}.customer_id = ${Customers}.id`
为了join 的正确处理,很多时候设置正确的主键也很必要,注意主键是在维度定义的
dimensions: {
authorId: {
sql: `id`,
type: `number`,
primaryKey: true
}
}
使用${CUBE} 可以引用当前的cube schema 定义,在处理join 的时候很有必要
dimensions: {
name: {
sql: `${CUBE}.name`,
type: `string`
}
}
cube.js 基于Dijkstra‘s algorithm 算法可以解决a-b,b-c 然后a-c 的关联问题
比如:
cube(`A`, {
// ...
joins: {
B: {
sql: `${A}.b_id = ${B}.id`,
relationship: `belongsTo`
}
},
?
measures: {
count: {
type: `count`
}
}
});
?
cube(`B`, {
// ...
joins: {
C: {
sql: `${B}.c_id = ${C}.id`,
relationship: `belongsTo`
}
}
});
?
cube(`C`, {
// ...
?
dimensions: {
category: {
sql: `category`,
type: `string`
}
}
});
我们可以使用
{
measures: [‘A.count‘],
dimensions: [‘C.category‘]
}
https://cube.dev/docs/joins
https://cube.dev/docs/direction-of-joins/
标签:category 关联 res tar target 设置 注意 def 一对一
原文地址:https://www.cnblogs.com/rongfengliang/p/14332375.html