cube.js 测试Query 的方法
核心还是基于官方的预编译能力,然后构建自己的query 传递参数进行测试
参考代码
- app.js
const {prepareCompiler} = require("@cubejs-backend/schema-compiler")
module.exports = {
preCompiler:function(content,options){
return prepareCompiler({
localPath: () => __dirname,
dataSchemaFiles: () => Promise.resolve([
{ fileName: 'main.js', content }
])
}, { adapter: 'postgres',
}
}
- index.js
const app = require("./app")
const fs = require("fs")
const {PostgresQuery} = require("@cubejs-backend/schema-compiler")
let {compiler,joinGraph,cubeEvaluator} = app.preCompiler(fs.readFileSync("./demo-schmea.js").toString("utf-8"));
(async function(){
// 需要先执行编译
await compiler.compile();
// 构造PostgresQuery 测试query
let query = new PostgresQuery({joinGraph, cubeEvaluator, compiler },{
measures: ['visitors.visitor_count'],
dimensions: [
'visitors.source'
],
contextSymbols:{
securityContext:{
source:"demoapp"
}
}
})
console.log(query.buildSqlAndParams())
})()
- 效果
说明
通过query 我们可以了解cube.js 的运行机制,同时方便开发测试
参考资料
https://cube.dev/docs/getting-started
https://github.com/cube-js/cube.js
https://github.com/rongfengliang/cubejs-prepareCompiler-learning.git