优化数据库连接池

main
expressgy 2 years ago
parent 801507c0a1
commit 469bd3cd95
  1. 15
      Database/index.js
  2. BIN
      fileStorages/16766918623846755ab8d61.jpg
  3. BIN
      fileStorages/16766918705615362e992c39.jpg
  4. BIN
      fileStorages/167669187579672ff2c2c574.jpg
  5. 2
      routes/container.js
  6. 79
      routes/menu.js

@ -1,12 +1,23 @@
const mysql = require('mysql2/promise');
async function initSQL() {
const connection = await mysql.createConnection({
const connection = await mysql.createPool({
host: 'localhost',
user: 'root',
database: 'rgvofficial',
password: 'root'
password: 'root',
waitForConnections: true, //连接超额是否等待
connectionLimit: 10, //一次创建的最大连接数
queueLimit: 0, //可以等待的连接的个数
maxIdle: 10, // max idle connections, the default value is the same as `connectionLimit`
idleTimeout: 60000, // idle connections timeout, in milliseconds, the default value 60000
});
connection.getConnection(function(err, conn) {
// Do something with the connection
conn.query(/* ... */);
// Don't forget to release the connection when finished!
pool.releaseConnection(conn);
})
global.SQL = connection
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 351 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 MiB

@ -77,7 +77,7 @@ router.post('/createContainer', async (ctx, next) => {
}
const createTime = new Date().getTime()
const [rows, fields] = await global.SQL.execute('INSERT INTO container (menuid, name, rank, config, body, createTime) VALUES (?, ?, ?, ?, ?, ?)', [arg.menuId, arg.name, arg.rank, arg.config, arg.body, createTime]);
ctx.body = ctx.body = {
ctx.body ={
data: rows,
state: true,
message: '创建成功'

@ -4,19 +4,82 @@ const router = require('koa-router')()
router.prefix('/menu')
router.get('/getMenu', async (ctx, next) => {
const [rows, fields] = await global.SQL.execute('SELECT * FROM `menu` WHERE `isdelete` = 0');
ctx.body = rows
ctx.body = {
data: rows,
state: true,
message: '获取内容成功'
}
})
router.delete('/deleteMenu', async (ctx, next) => {
const [rows, fields] = await global.SQL.execute('SELECT * FROM `menu` WHERE `isdelete` = 0');
ctx.body = rows
const arg = ctx.request.query;
if(!arg.id){
ctx.body = {
data: {},
state: false,
message: '缺少id'
}
return
}
const [rows, fields] = await global.SQL.execute('UPDATE menu SET isdelete = 1 WHERE `id` = ?', [arg.id]);
ctx.body = {
data: rows,
state: true,
message: '删除菜单成功'
}
})
router.put('/updateMenu', async (ctx, next) => {
const [rows, fields] = await global.SQL.execute('SELECT * FROM `menu` WHERE `isdelete` = 0');
ctx.body = rows
const arg = ctx.request.body;
const field = ['name', 'route', 'rank', 'remarks', 'father', 'icon']
const noField = []
field.map(item => {
if (!Object.keys(arg).includes(item)) noField.push(item)
})
router.post('/createMenu', async (ctx, next) => {
const [rows, fields] = await global.SQL.execute('SELECT * FROM `menu` WHERE `isdelete` = 0');
ctx.body = rows
if (noField.length != 0) {
ctx.body = {
data: {},
state: false,
message: '缺少' + noField
}
return
}
const [rows, fields] = await global.SQL.execute('UPDATE menu SET name = ? , rank = ? , remarks = ? , route = ?, icon = ? WHERE id = ?', [arg.name, arg.rank, arg.remarks, arg.route, arg.icon, Number(arg.id)]);
ctx.body = ctx.body = {
data: rows,
state: true,
message: '更新成功'
}
})
router.post('/createMneuItem', async (ctx, next) => {
const arg = ctx.request.body;
const field = ['name', 'route', 'rank', 'remarks', 'father', 'icon']
const noField = []
field.map(item => {
if (!Object.keys(arg).includes(item)) noField.push(item)
})
if (noField.length != 0) {
ctx.body = {
data: {},
state: false,
message: '缺少' + noField
}
return
}
const [checket, checketFields] = await global.SQL.execute('SELECT * FROM `menu` WHERE `isdelete` = 0 AND `route` = ?', [arg.route]);
if(checket.length != 0){
ctx.body = {
data: {},
state: false,
message: '路由标志已存在:' + arg.route
}
return
}
const createTime = new Date().getTime()
const [rows, fields] = await global.SQL.execute('INSERT INTO menu (name, rank, route, father, remarks, createTime) VALUES (?, ?, ?, ?, ?, ?)', [arg.name, arg.rank, arg.route, arg.father, arg.remarks, createTime])
ctx.body = {
data: rows,
state: true,
message: '创建成功'
}
})
module.exports = router

Loading…
Cancel
Save