封装增删改查
This commit is contained in:
16
src/rest/custom/add.js
Normal file
16
src/rest/custom/add.js
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
const dbUtils = require("../../utils/dbUitls");
|
||||||
|
module.exports = {
|
||||||
|
action: "/node/custom/addOrUpdate",
|
||||||
|
method: "post",
|
||||||
|
execute: (request, response) => {
|
||||||
|
let form = request.body
|
||||||
|
dbUtils.addOrUpdate({
|
||||||
|
table: 'node_custom_config',
|
||||||
|
form
|
||||||
|
}).then(data => {
|
||||||
|
response.send({code: 0, data})
|
||||||
|
}).catch(err => {
|
||||||
|
response.send({code: 1, err: err?.sqlMessage || err || ""})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
13
src/rest/custom/delete.js
Normal file
13
src/rest/custom/delete.js
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
const dbUtils = require("../../utils/dbUitls");
|
||||||
|
module.exports = {
|
||||||
|
action: "/node/custom/delete",
|
||||||
|
method: "post",
|
||||||
|
execute: (request, response) => {
|
||||||
|
const {ids} = request.query
|
||||||
|
dbUtils.delete({table: 'node_custom_config', ids}).then(data => {
|
||||||
|
response.send({code: 0, data})
|
||||||
|
}).catch(err => {
|
||||||
|
response.send({code: 1, err: err?.sqlMessage || err || ""})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
const mysql = require("mysql");
|
const mysql = require("mysql");
|
||||||
const dbConfig = require("../config/db");
|
const dbConfig = require("../config/db");
|
||||||
|
const {v4: uuid} = require("uuid");
|
||||||
const query = sql => new Promise((resolve, reject) => {
|
const query = sql => new Promise((resolve, reject) => {
|
||||||
this.pool?.getConnection((err, conn) => {
|
this.pool?.getConnection((err, conn) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
@@ -24,6 +25,7 @@ module.exports = {
|
|||||||
},
|
},
|
||||||
query,
|
query,
|
||||||
list: ({table, search, con}) => {
|
list: ({table, search, con}) => {
|
||||||
|
//列表查询
|
||||||
let total = 0, records = []
|
let total = 0, records = []
|
||||||
if (table) {
|
if (table) {
|
||||||
const {current, size} = search, params = JSON.parse(JSON.stringify(search))
|
const {current, size} = search, params = JSON.parse(JSON.stringify(search))
|
||||||
@@ -42,5 +44,31 @@ module.exports = {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
addOrUpdate: ({table, form}) => {
|
||||||
|
//新增和更新
|
||||||
|
let sql
|
||||||
|
if (form.id) {//编辑
|
||||||
|
let arr = Object.keys(form).filter(e => form[e]).map(e => {
|
||||||
|
if (typeof form[e] == "object") form[e] = JSON.stringify(form[e])
|
||||||
|
return `${e}='${form[e]}'`
|
||||||
|
})
|
||||||
|
sql = `update ${table} set ${arr.join(",")} where id='${form.id}'`
|
||||||
|
} else {//新增
|
||||||
|
let cols = [], arr = []
|
||||||
|
Object.keys(form).map(e => {
|
||||||
|
if (form[e]) {
|
||||||
|
cols.push(e)
|
||||||
|
if (typeof form[e] == "object") form[e] = JSON.stringify(form[e])
|
||||||
|
arr.push(`'${form[e]}'`)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
sql = `insert into ${table} (id,${cols.join(",")}) values('${uuid()}',${arr.join(",")})`
|
||||||
|
}
|
||||||
|
return query(sql)
|
||||||
|
},
|
||||||
|
delete: ({table, ids}) => {
|
||||||
|
ids = ids?.split(",")?.map(e => `'${e}'`)?.toString()
|
||||||
|
return query(`delete from ${table} where id in (${ids})`)
|
||||||
|
},
|
||||||
format: args => args.map(e => `${e.prop}`).join(" ")
|
format: args => args.map(e => `${e.prop}`).join(" ")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user