2022-03-29 15:04:07 +08:00
const mysql = require ( "mysql" ) ;
const dbConfig = require ( "../config/db" ) ;
2022-07-04 09:47:51 +08:00
const { v4 : uuid } = require ( "uuid" ) ;
2022-07-04 14:17:59 +08:00
const dayjs = require ( "dayjs" ) ;
2023-01-18 12:08:51 +08:00
const chalk = require ( "chalk" ) ;
2023-03-01 17:39:17 +08:00
const { checkJson } = require ( "../tools" ) ;
2022-07-02 15:15:05 +08:00
const query = sql => new Promise ( ( resolve , reject ) => {
this . pool ? . getConnection ( ( err , conn ) => {
if ( err ) {
2023-01-18 12:08:51 +08:00
reject ( err )
2022-07-02 15:15:05 +08:00
} else {
conn . query ( sql , ( err , result ) => {
if ( err ) {
reject ( err )
} else {
conn . release ( )
resolve ( result )
}
} )
}
} )
} ) ;
2023-02-16 12:17:18 +08:00
const insert = ( { table , form } ) => {
let sql
if ( form . id ) { //编辑
let arr = Object . keys ( form ) . filter ( e => form [ e ] ) . map ( e => {
2023-03-01 17:39:17 +08:00
if ( typeof form [ e ] == "object" ) {
2023-03-01 17:54:57 +08:00
if ( checkJson ( form [ e ] ) ) form [ e ] = JSON . stringify ( form [ e ] ) . replace ( /"'/g , "\'" )
2023-03-01 17:39:17 +08:00
else form [ e ] = JSON . stringify ( form [ e ] )
}
2023-02-16 12:17:18 +08:00
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 )
2023-03-01 17:39:17 +08:00
if ( typeof form [ e ] == "object" ) {
2023-03-01 17:54:57 +08:00
if ( checkJson ( form [ e ] ) ) form [ e ] = JSON . stringify ( form [ e ] ) . replace ( /"'/g , "\'" )
2023-03-01 17:39:17 +08:00
else form [ e ] = JSON . stringify ( form [ e ] )
}
2023-02-16 12:17:18 +08:00
arr . push ( ` ' ${ form [ e ] } ' ` )
}
} )
sql = ` insert into ${ table } (id,createTime, ${ cols . join ( "," ) } ) values(' ${ uuid ( ) } ',' ${ dayjs ( ) . format ( "YYYY-MM-DD HH:mm:ss" ) } ', ${ arr . join ( "," ) } ) `
}
return sql
}
2022-03-29 15:04:07 +08:00
module . exports = {
pool : null ,
init : ( ) => {
this . pool = mysql . createPool ( dbConfig )
2023-01-18 12:08:51 +08:00
console . log ( ` ${ chalk . bgBlue . black ( " DATABASE " ) } 数据库已连接 ` )
2022-03-29 15:04:07 +08:00
} ,
2022-07-02 15:15:05 +08:00
query ,
2023-02-09 09:30:20 +08:00
list : ( { table , search , con = 'name' , sort } ) => {
2022-07-04 09:47:51 +08:00
//列表查询
2022-07-02 15:15:05 +08:00
let total = 0 , records = [ ]
if ( table ) {
2023-02-02 15:03:37 +08:00
const { current , size = 10 } = search , params = JSON . parse ( JSON . stringify ( search ) )
2023-02-10 16:07:37 +08:00
const conValue = params [ con ] || ""
2022-07-02 15:15:05 +08:00
delete params . current
delete params . size
2023-02-09 09:30:20 +08:00
delete params [ con ]
2022-07-02 15:15:05 +08:00
const sqlCon = Object . keys ( params ) . map ( e => ` and ${ e } =' ${ params [ e ] } ' ` ) . join ( " " )
return Promise . all ( [
2023-02-09 09:30:20 +08:00
query ( ` select 1 from ${ table } where ${ con } like '% ${ conValue } %' ${ sqlCon } ` ) . then ( res => {
2022-07-02 15:15:05 +08:00
return total = res . length
} ) ,
2023-02-09 09:30:20 +08:00
query ( ` select * from ${ table } where ${ con } like '% ${ conValue } %' ${ sqlCon } order by ${ sort || 'createTime' } desc limit ${ ( ( current - 1 ) || 0 ) * size } , ${ size } ` ) . then ( res => {
2022-07-02 15:15:05 +08:00
return records = res
2022-03-29 15:04:07 +08:00
} )
2022-07-02 15:15:05 +08:00
] ) . then ( ( ) => {
return { records , total }
} )
}
} ,
2023-02-10 19:33:57 +08:00
batchInsert ( { table , list } ) {
2023-02-16 12:17:18 +08:00
return query ( list . map ( form => insert ( { table , form } ) ) . join ( ";" ) )
2023-02-10 19:33:57 +08:00
} ,
addOrUpdate : ( { table , form } ) => {
//新增和更新
2023-02-16 12:17:18 +08:00
const sql = insert ( { table , form } )
2022-07-04 09:47:51 +08:00
return query ( sql )
} ,
delete : ( { table , ids } ) => {
ids = ids ? . split ( "," ) ? . map ( e => ` ' ${ e } ' ` ) ? . toString ( )
return query ( ` delete from ${ table } where id in ( ${ ids } ) ` )
} ,
2022-07-04 10:01:33 +08:00
detail : ( { table , id } ) => {
2022-07-04 14:17:59 +08:00
return query ( ` select * from ${ table } where id=' ${ id } ' limit 0,1 ` ) . then ( res => res ? . [ 0 ] )
2022-07-04 10:01:33 +08:00
} ,
2022-07-02 15:15:05 +08:00
format : args => args . map ( e => ` ${ e . prop } ` ) . join ( " " )
2022-03-29 15:04:07 +08:00
}