Skip to content

Commit

Permalink
✨ skyjt init 模板中增加 mock 例子
Browse files Browse the repository at this point in the history
  • Loading branch information
kongnet committed Aug 27, 2019
1 parent 7da1c72 commit c417d8e
Show file tree
Hide file tree
Showing 9 changed files with 405 additions and 9 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## [0.1.46](https://github.com/kongnet/skybase/compare/v0.1.45...v0.1.46) (2019-08-27)


### :memo:

* 修改说明文档,增加Changelog.md ([fb4708b](https://github.com/kongnet/skybase/commit/fb4708b))



## [0.1.45](https://github.com/kongnet/skybase/compare/v0.1.44...v0.1.45) (2019-08-27)


Expand Down
40 changes: 40 additions & 0 deletions output-template/.gitlab-ci.yml.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
image: node:latest

services:
# - mysql:latest
# - redis:latest
# - postgres:latest

# http://docs.gitlab.com/ce/ci/yaml/README.html#cache

stages:
- init_restart
#- deploy_v5
#- deploy_production

# 1.需要自行配置 gitlab runner
# 2.ssh免登录
# 3.gitlab中设置好 SSH_PRIVATE_KEY

cache:
paths:
- node_modules/
before_script:

job_init_restart:
stage: init_restart
only:
- develop
script:
- 'ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime'
- 'echo "当前时间:`date`"'
- 'which ssh-agent || ( yum update -y && yum install openssh-client -y )'
- eval $(ssh-agent -s)
- ssh-add <(echo "$SSH_PRIVATE_KEY")
- mkdir -p ~/.ssh
- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\tPort 1876\n\n" > ~/.ssh/config'
- 'ssh www@172.16.0.46 -p 1876 "cd /home/www/data_board && git pull && exit"'
# 安装npm模块,万一有新的呢
- 'ssh www@172.16.0.46 -p 1876 "cd /home/www/data_board && yarn install && exit"'
# 重启项目
- 'ssh www@172.16.0.46 -p 1876 "cd /home/www/data_board && npm run test"'
2 changes: 2 additions & 0 deletions output-template/index.js.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ config.beforeMount = async () => {

sky.start(config, async () => {
console.log('{{d.proName}} 项目成功启动')
console.log('http://127.0.0.1:13000/skyapi/mock/first','查看mock例子')
console.log('http://127.0.0.1:13000/skyapi/probe/mysql','查看探针例子')
})
38 changes: 38 additions & 0 deletions output-template/model/api/skyapi/probe.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,43 @@ module.exports = {
'err_code': {},
'test': {},
'front': true
},
'mysqlTree': {
name: 'mysql探针数据',
desc: 'mysql探针数据',
method: 'get',
controller: 'mysqlProbe.getDbTable',
param: {
outputType: {
name: '输出类型',
desc: '接口输出html或者json',
def: 'html',
type: 'string'
}
},
'token': false,
'needSign': false,
'err_code': {},
'test': {},
'front': true
},
'mysqlGrid': {
name: 'mysql探针数据',
desc: 'mysql探针数据',
method: 'get',
controller: 'mysqlProbe.getDbTableColumn',
param: {
outputType: {
name: '输出类型',
desc: '接口输出html或者json',
def: 'html',
type: 'string'
}
},
'token': false,
'needSign': false,
'err_code': {},
'test': {},
'front': true
}
}
46 changes: 46 additions & 0 deletions output-template/router/mysqlProbe.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,51 @@ module.exports = {
} else {
ctx.throwCode(r.code, r.msg)
}
},
async getDbTable (ctx) {
let { outputType } = ctx.checkedData.data
const r = await mysqlProbe.getDbTable(ctx.checkedData.data)
if (r && r.code === 0) {
// outputType = 'json' // 先写死
if (outputType === 'html') {
let f = fs.readFileSync(path.join(__dirname, '../template/tree-mysql.html'))
let obj = {
tableColumn: [{ name: 'root', children: r.data.tableColumn }],
title: 'Mysql树形展示',
len: r.data.len
}
ctx.type = 'html'
ctx.body = $.tpl(f.toString()).render(obj)
} else {
ctx.ok(r)
}
} else {
ctx.throwCode(r.code, r.msg)
}
},
async getDbTableColumn (ctx) {
let { outputType } = ctx.checkedData.data
const r = await mysqlProbe.getDbTableColumn(ctx.checkedData.data)
if (r && r.code === 0) {
if (outputType === 'html') {
let f = fs.readFileSync(path.join(__dirname, '../template/grid-mysql.html'))
let packObj = $.tools.jsonPack(r.data)
packObj.shift()
let obj = {
table_id: 'grid_mysql',
table_title: 'Mysql数据库-表-字段',
table_head: ['数据库', '表', '列', '列类型', '列注解'],
table_col: [0, 1, 2, 3, 4],
// table_col: ['dbName', 'tableName', 'columnName', 'columnKey', 'columnComment'],
table_list: packObj
}
ctx.type = 'html'
ctx.body = $.tpl(f.toString()).render(obj)
} else {
ctx.ok(r)
}
} else {
ctx.throwCode(r.code, r.msg)
}
}
}
77 changes: 71 additions & 6 deletions output-template/service/mysqlProbe.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
/* global db */
const Package = require('../package.json')

module.exports = {
getTableColumnSize
}

async function getTableColumnSize () {
let sql = `select
let sql = `select
table_schema as 'dbName',
table_name as 'tableName',
table_rows as 'rowCount',
Expand All @@ -15,6 +10,27 @@ async function getTableColumnSize () {
table_comment as 'tableComment'
from information_schema.tables
order by data_length desc, index_length desc;`
let tableSql = `SELECT
table_schema as db,
table_name as table_name,
table_comment as table_comment
FROM
information_schema.\`TABLES\`
WHERE
table_schema NOT IN ( 'information_schema', 'performance_schema', 'mysql', 'sys' );`
let columnSql = `SELECT
table_schema AS db_name,
table_name AS table_name,
column_name AS column_name,
column_type AS column_type,
column_key AS column_key,
column_comment AS column_comment
FROM
information_schema.\`COLUMNS\`
WHERE
table_schema NOT IN ( 'information_schema', 'performance_schema', 'mysql', 'sys' )
ORDER BY table_schema;`
async function getTableColumnSize () {
let r = await db.cmd(sql).run()
let arr = []
let arrSize = []
Expand All @@ -38,3 +54,52 @@ async function getTableColumnSize () {
data: { tableRow: arr, tableSize: arrSize }
}
}
async function getDbTable () {
let r = await db.cmd(sql).run()
let arr = []
let obj = {}
r.forEach(item => {
if (obj[item.dbName]) {
arr[obj[item.dbName]].children.push({ name: item.tableComment + ' ' + item.tableName })
} else {
if (!['performance_schema', 'mysql', 'information_schema', 'sys', 'happyminer_test'].includes(item.dbName)) {
arr.push({ name: item.dbName, children: [] })
obj[item.dbName] = arr.length - 1
}
}
})
arr.shift()
return {
code: 0,
data: { tableColumn: arr, len: r.length }
}
}
async function getDbTableColumn () {
let r = await db.cmd(tableSql).run()
let dbTableCommentMap = {}
r.map(it => {
dbTableCommentMap[[it.db, it.table_name].join('$$')] = it.table_comment.trim().replaceAll('\r\n', '')
})
r = await db.cmd(columnSql).run()
r = r.map(it => {
// console.log(dbTableCommentMap[it.db_name + '$$' + it.table_name])
return {
dbName: it.db_name,
tableName: it.table_name + ' ' + dbTableCommentMap[it.db_name + '$$' + it.table_name],
columnName: it.column_name,
columnKey: it.column_key, // PRI->UNI->MUL
columnComment: it.column_comment
}
})
return {
code: 0,
data: r
}
}

getDbTableColumn()
module.exports = {
getTableColumnSize,
getDbTable,
getDbTableColumn
}
59 changes: 59 additions & 0 deletions output-template/template/grid-mysql.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<style type="text/css">
table.gridtable {
font-family: verdana,arial,sans-serif;
font-size:11px;
color:#333333;
border-width: 1px;
border-color: #666666;
border-collapse: collapse;
width:50%;
}
table.gridtable th {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #666666;
background-color: #dedede;
}
table.gridtable td {
text-align: left;
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #666666;
background-color: #ffffff;
}
</style>
<h3>{{ d.table_title }}</h3>
<table id="{{d.table_id}}" class="gridtable">
{{# let headLen}}
{{# if(d.table_head){ headLen=d.table_head.length;}}
<thead>
<tr>
<th>序号</th>
{{# for(let i = 0, len = d.table_head.length; i < len; i++){ }}
<th>{{ d.table_head[i] }}</th>
{{# } }}
</tr>
</thead>
{{# } else { headLen=d.table_col.length; }}
<thead>
<tr>
<th>序号</th>
{{# for(let i = 0, len = d.table_col.length; i < len; i++){ }}
<th>{{ d.table_col[i] }}</th>
{{# } }}
</tr>
</thead>
{{# } }}
<tbody>
{{# for(let i = 0, len = d.table_list.length; i < len; i++){ }}
<tr>
<td>{{i+1}}</td>
{{# for(let k = 0, len1 = headLen; k < len1; k++){ }}
<td>{{ d.table_list[i][d.table_col[k]]||'' }}</td>
{{# } }}
</tr>
{{# } }}
</tbody>
</table>

0 comments on commit c417d8e

Please sign in to comment.