整合列表工具类

This commit is contained in:
aixianling
2022-07-02 15:15:05 +08:00
parent 90ab258629
commit e86b8b2f46
3 changed files with 54 additions and 16 deletions

View File

@@ -25,6 +25,7 @@
"fs-extra": "^10.0.1",
"helmet": "^5.0.2",
"mysql": "^2.18.1",
"sql": "^0.78.0",
"uuid": "^8.3.2"
}
}

16
src/rest/custom/list.js Normal file
View File

@@ -0,0 +1,16 @@
const dbUtils = require("../../utils/dbUitls");
module.exports = {
action: "/node/custom/list",
method: "post",
execute: (request, response) => {
let {size, current, name} = request.query
dbUtils.list({
table: 'node_custom_config', con: name,
search: {size, current}
}).then(data => {
response.send({code: 0, data})
}).catch(err => {
response.send({code: 1, err: err?.sqlMessage || err || ""})
})
}
}

View File

@@ -1,11 +1,6 @@
const mysql = require("mysql");
const dbConfig = require("../config/db");
module.exports = {
pool: null,
init: () => {
this.pool = mysql.createPool(dbConfig)
},
query: sql => new Promise((resolve, reject) => {
const query = sql => new Promise((resolve, reject) => {
this.pool?.getConnection((err, conn) => {
if (err) {
console.log(err)
@@ -21,5 +16,31 @@ module.exports = {
})
}
})
});
module.exports = {
pool: null,
init: () => {
this.pool = mysql.createPool(dbConfig)
},
query,
list: ({table, search, con}) => {
let total = 0, records = []
if (table) {
const {current, size} = search, params = JSON.parse(JSON.stringify(search))
delete params.current
delete params.size
const sqlCon = Object.keys(params).map(e => `and ${e}='${params[e]}'`).join(" ")
return Promise.all([
query(`select 1 from ${table} where name like '%${con}%' ${sqlCon}`).then(res => {
return total = res.length
}),
query(`select * from ${table} where name like '%${con}%' ${sqlCon} limit ${(current-1)*size},${size}`).then(res => {
return records = res
})
]).then(() => {
return {records, total}
})
}
},
format: args => args.map(e => `${e.prop}`).join(" ")
}