cube 是cube.js data schema 的核心,里面定义了生成sql 的说明

一个比较全的schema 例子

cube(`Users`, {
sql: `select * from users`, joins: {
Organizations: {
relationship: `belongsTo`,
sql: `${Users}.organization_id = ${Organizations}.id`
}
}, measures: {
count: {
type: `count`,
sql: `id`
}
}, dimensions: {
createdAt: {
type: `time`,
sql: `created_at`
}, country: {
type: `string`,
sql: `country`
}
}
});

参数说明

  • sql
    sql 主要是为了定义cube 为了查询的table,一般使用的方式是select * from <tablename> 注意我们一般是不需要groupby 等聚合操作的
cube(`Orders`, {
sql: `SELECT * FROM orders`
});

引用其他cube

cube(`Companies`, {
sql: `SELECT users.company_name, users.company_id FROM ${Users.sql()} AS users`
});
  • title
    我们可以使用title 来方便提供便于人们阅读的信息
    如下:
cube(`Orders`, {
sql: `SELECT * FROM orders`, title: `Product Orders`,
});
  • description
    提供对于数据的描述,让使用者更好的理解数据
cube(`Orders`, {
sql: `SELECT * FROM orders`, title: `Product Orders`,
description: `All orders related information`,
});
  • extends
    可以方便实现cube 的复用,类似面向对象的继承
cube(`OrderFacts`, {
sql: `SELECT * FROM orders` measures: {
count: {
type: `count`,
sql: `id`
}
}
}); cube(`ExtendedOrderFacts`, {
extends: OrderFacts, measure: {
doubleCount: {
type: `number`,
sql: `${count} * 2`
}
}
});
  • refreshKey
    cube.js 会帮助我们进行数据的cache,通过refreshKey 我们可以控制数据cache 的策略。默认策略如下
    根据dimensions 的max 值,根据当前cube 的count,如下基于时间戳的
cube(`OrderFacts`, {
sql: `SELECT * FROM orders` refreshKey: {
sql: `SELECT MAX(created_at) FROM orders`
}
});

我们可以通过refreshKey 强制制定cache 策略

cube(`OrderFacts`, {
sql: `SELECT * FROM orders` refreshKey: {
sql: `SELECT date_trunc('hour', NOW())`
}
});

上下文变量

  • filter 参数
    一般的格式FILTER_PARAMS.<CUBE_NAME>.<FILTER_NAME>.filter(expression) 实际上如果使用过了cube,这个主要是为了参数
    传递的
    一个参考cube schema
cube(`OrderFacts`, {
sql: `SELECT * FROM orders WHERE ${FILTER_PARAMS.OrderFacts.date.filter('date')}`, dimensions: {
date: {
sql: `date`,
type: `time`
}
}
});

对于 OrderFacts.date 在['2018-01-01', '2018-12-31'] 范围的dimensions 生成sql 如下:

SELECT * FROM orders WHERE date >= '2018-01-01 00:00:00' and date <= '2018-12-31 23:59:59'

同时我们也可以传递函数,如下

cube(`Events`, {
sql: `
SELECT * FROM schema.\`events*\`
WHERE ${FILTER_PARAMS.Events.date.filter((from, to) =>
`_TABLE_SUFFIX >= FORMAT_TIMESTAMP('%Y%m%d', TIMESTAMP(${from})) AND _TABLE_SUFFIX <= FORMAT_TIMESTAMP('%Y%m%d', TIMESTAMP(${to}))`
)}
`, dimensions: {
date: {
sql: `date`,
type: `time`
}
}
});
  • user 上下文
    这个主要是给cube client 提供的,可以做为一个基于row 级别的安全处理,方便我们进行一些安全上的控制,如下
cube(`Oreders`, {
sql: `SELECT * FROM orders WHERE ${USER_CONTEXT.email.filter('email')}`, dimensions: {
date: {
sql: `date`,
type: `time`
}
}
});

参考资料

https://cube.dev/docs/cube

 
 
 
 

最新文章

  1. C#定义类型转化 及 格式化字符串
  2. 安装 SQL Server 2008 时提示需要删除 SQL Server 2005 Express 工具
  3. 使用percona xtradb cluster的IST方式添加新节点
  4. jpa+spring配置多数据源
  5. svn 清空
  6. ndroid调用平台功能具体技巧分享
  7. selenium+python对页面元素进行高亮显示
  8. Python字符串和列表的内置方法
  9. ABAP 中JSON格式的转换与解析
  10. scrapy 日志处理
  11. 雷林鹏分享:jQuery EasyUI 树形菜单 - 使用标记创建树形菜单
  12. MySQL通过Navicat实现远程连接
  13. 微信小程序开发——点击按钮获取用户授权没反应或反应很慢的解决方法
  14. 基于canvas+ajax+zui.js的蓄电池监控系统
  15. 【git3】git+Gogs+webStorm的使用
  16. 新建一个Java Web程序
  17. C#操作Redis Set 无序集合
  18. Tomcat启动报错java.net.AbstractPlainSocketImpl(java/net/AbstractPlainSocketImpl.java:178:-1)Struts在网络复杂情况下启动报错解决办法
  19. [转] Moran&#39;s I
  20. linux Ubuntu14.04 python(c++版本) tesorflow(python版本)c++

热门文章

  1. 为了防止页面重新自动加载,可以给a标签设置href=&quot;javascript:void(0);&quot;
  2. [LOJ2541] [PKUWC2018] 猎人杀
  3. 二进制方式安装Kubernetes 1.14.2高可用详细步骤
  4. java之struts2之ServletAPI
  5. Selenium与PhantomJS踩过的坑
  6. dicker第三章--网络管理
  7. 【转载】Javascript使用Math.random()随机数函数生成1至1000的随机数
  8. Java8新特性概览
  9. oracle in和exists区别
  10. WinServer-文件共享端口