cube.js 几个比较核心的是schema 以及driver,driver 是cube.js 开发好的,schema是定义了数据操作的处理
默认cube.js 是基于配置进行数据管理的(dotenv)对于多数据源我们可以基于cube.js 进行支持,同时在schema
的定义中指定datasource ,以下是一个参考使用
项目结构
代码说明
- package.json
主要是依赖
{
"name": "multi-source",
"version": "0.0.1",
"private": true,
"scripts": {
"dev": "./node_modules/.bin/cubejs-server"
},
"devDependencies": {
"@cubejs-backend/postgres-driver": "^0.25.6",
"@cubejs-backend/server": "^0.25.6",
"@cubejs-backend/mysql-driver": "^0.25.8"
}
}
- docker-compose
进行pg 以及mysql 还有redis 环境的准备
version: '2.2'
services:
pg:
image: postgres:10.15-alpine
ports:
- "5432:5432"
environment:
- "POSTGRES_PASSWORD=dalong"
redis:
image: redis
ports:
- "6379:6379"
mysql:
image: mysql:5.7.16
ports:
- 3306:3306
command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
environment:
- "MYSQL_ROOT_PASSWORD=dalong"
- "MYSQL_DATABASE=cubejs"
- "MYSQL_USER=cubejs"
- "MYSQL_PASSWORD=dalong"
- "TZ=Asia/Shanghai"
# cube:
# image: cubejs/cube:v0.25.6
# ports:
# - 4000:4000
# - 3000:3000
# env_file: .env
# volumes:
# - .:/cube/conf
# # We ignore Cube.js deps, because they are built-in inside the official Docker image
# - .empty:/cube/conf/node_modules/@cubejs-backend/
- cube.js
对于不同的数据源使用不对的数据库支持,web 的是mysql,其他的是pg
同时配置devServer:true,这样就会启动的是dev 环境,不然就没有play ground
// Cube.js configuration options: https://cube.dev/docs/config
const PostgresDriver = require("@cubejs-backend/postgres-driver");
const MysqlDriver = require('@cubejs-backend/mysql-driver');
module.exports = {
dbType: ({ dataSource } = {}) => {
if (dataSource === 'web') {
return 'mysql';
} else {
return 'postgres';
}
},
devServer:true,
apiSecret:"4b18a4874cf3fdb858e1150396022375a7d564743a928de54cfb7d78b06924d76691f8bd4d2ad2d436f721b659b57c59e4fad0965c839d3fc52cd67f7576e9bc",
driverFactory: ({ dataSource } = {}) => {
if (dataSource === 'web') {
return new MysqlDriver({
host:"127.0.0.1",
port:"3306",
database:"cubejs",
user:"cubejs",
password:"dalong"
});
} else {
return new PostgresDriver({
user: "postgres",
database: "postgres",
password: "dalong",
port: 5432,
host: "127.0.0.1"
});
}
}
};
- schema
处理目前不太好的地方是不能自动处理,而且目前官方对于多数据源配置没有比较清晰说明,所以我基于
独立配置,生成的schema(当然schema 是可以自己写的,自动生成只是可以简化任务处理),注意datasource
的配置,我们就是通过这个进行不同数据源区分的
Myuser.js
cube(`Myusers`, {
sql: `SELECT * FROM cubejs.myusers`,
dataSource: `web`,
measures: {
count: {
type: `count`,
drillMembers: [id, username]
}
},
dimensions: {
id: {
sql: `id`,
type: `number`,
primaryKey: true
},
username: {
sql: `username`,
type: `string`
}
}
});
Orders.js
cube(`Orders`, {
sql: `
select 1 as id, 100 as amount, 'new' status
UNION ALL
select 2 as id, 200 as amount, 'new' status
UNION ALL
select 3 as id, 300 as amount, 'processed' status
UNION ALL
select 4 as id, 500 as amount, 'processed' status
UNION ALL
select 5 as id, 600 as amount, 'shipped' status
`,
dataSource: `web`,
measures: {
count: {
type: `count`
},
totalAmount: {
sql: `amount`,
type: `sum`
}
},
dimensions: {
status: {
sql: `status`,
type: `string`
}
}
});
PgAggregate.js
cube(`PgAggregate`, {
sql: `SELECT * FROM pg_catalog.pg_aggregate`,
dataSource: `pg`,
measures: {
count: {
type: `count`,
drillMembers: [aggfnoid]
}
},
dimensions: {
aggfnoid: {
sql: `aggfnoid`,
type: `string`
},
aggtransfn: {
sql: `aggtransfn`,
type: `string`
},
aggmtransfn: {
sql: `aggmtransfn`,
type: `string`
},
aggsortop: {
sql: `aggsortop`,
type: `string`
},
aggminitval: {
sql: `aggminitval`,
type: `string`
},
aggkind: {
sql: `aggkind`,
type: `string`
},
aggfinalextra: {
sql: `aggfinalextra`,
type: `string`
},
aggmfinalfn: {
sql: `aggmfinalfn`,
type: `string`
},
aggfinalfn: {
sql: `aggfinalfn`,
type: `string`
},
aggmfinalextra: {
sql: `aggmfinalextra`,
type: `string`
},
agginitval: {
sql: `agginitval`,
type: `string`
},
aggdeserialfn: {
sql: `aggdeserialfn`,
type: `string`
},
aggcombinefn: {
sql: `aggcombinefn`,
type: `string`
},
aggserialfn: {
sql: `aggserialfn`,
type: `string`
},
aggminvtransfn: {
sql: `aggminvtransfn`,
type: `string`
},
aggmtranstype: {
sql: `aggmtranstype`,
type: `string`
},
aggtranstype: {
sql: `aggtranstype`,
type: `string`
}
}
});
运行效果
注意需要准备下mysql 的数据库以及表
- 启动依赖
docker-compose up -d
- 启动服务
yarn dev
- 效果
pg 的
mysql
参考资料
https://cube.dev/docs/config
https://cube.dev/docs/multitenancy-setup
https://cube.dev/docs/cube#parameters-data-source