-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
24315e3
commit 2c6d60d
Showing
20 changed files
with
534 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
.vscode-upload.json | ||
package-lock.json | ||
**/apidoc/dist/* | ||
|
||
**/__bak__/* | ||
# compiled output | ||
/dist | ||
/tmp | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
set PORT=8080 | ||
:: 数据库密码 | ||
set MARIADBPWD=xxxx | ||
npm run dev |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
set PORT=8080 | ||
:: 数据库密码 | ||
set MARIADBPWD=xxxx | ||
npm run test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { MARIADBPWD } from './variate'; | ||
|
||
// 服务器开发版 | ||
export default { | ||
mariadb: { | ||
host: 'xxx.xxx.xxx.xxx', | ||
user: 'root', | ||
password: MARIADBPWD, | ||
database: 'koa-ts-full-template-db' | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { MARIADBPWD } from './variate'; | ||
|
||
// 本地开发版 | ||
export default { | ||
mariadb: { | ||
host: 'localhost', | ||
user: 'root', | ||
password: MARIADBPWD, | ||
database: 'koa-ts-full-template-db' | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { MARIADBPWD } from './variate'; | ||
// 正式版 | ||
export default { | ||
mariadb: { | ||
host: 'xxx.xxx.xxx.xxx', | ||
user: 'root', | ||
password: MARIADBPWD, | ||
database: 'koa-ts-full-template-db' | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import devConfig from './dev.config'; | ||
import devServer from './dev-server.config'; | ||
import prodConfig from './prod.config'; | ||
import Debug from 'debug'; | ||
const debug = Debug('dev-server'); | ||
|
||
class GlobalConfig { | ||
config = { | ||
mariadb: devConfig.mariadb, | ||
isDev: /(^dev.*|^test.*)/.test(process.env.npm_lifecycle_event) | ||
}; | ||
|
||
constructor() { | ||
this.getConfig(); | ||
} | ||
|
||
getConfig() { | ||
switch (process.env.npm_lifecycle_event) { | ||
case 'dev': this.config = Object.assign(this.config, devConfig); | ||
break; | ||
case 'devServer': this.config = Object.assign(this.config, devServer); | ||
break; | ||
case 'prod': this.config = Object.assign(this.config, prodConfig); | ||
break; | ||
default: this.config = Object.assign(this.config, devConfig); | ||
} | ||
return this.config; | ||
} | ||
} | ||
|
||
const globalConfig = new GlobalConfig(); | ||
debug(globalConfig.config); | ||
|
||
const GLOBAL_CONFIG = globalConfig.config; | ||
export default GLOBAL_CONFIG; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/** | ||
* 整个项目的入口变量 | ||
*/ | ||
export const MARIADBPWD: string = process.env.MARIADBPWD; // 数据库密码 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
/** | ||
* [ | ||
* { | ||
* sql: 'sql1', | ||
* params: ['params1'] | ||
* } | ||
* { | ||
* sql: 'sql2', | ||
* params: ['params2','params3'] | ||
* } | ||
* ] | ||
*/ | ||
export function joinAnd(sqlWhereArr) { | ||
if (!Array.isArray(sqlWhereArr)) { | ||
return { | ||
sql: '', | ||
params: [] | ||
}; | ||
} | ||
sqlWhereArr = sqlWhereArr.filter(item => item && item !== '' && item.sql && item.sql !== ''); | ||
if (sqlWhereArr.length <= 0) { | ||
return { | ||
sql: '', | ||
params: [] | ||
}; | ||
} | ||
const firstSqlWhere = sqlWhereArr.shift(); | ||
let sqlWhere = firstSqlWhere.sql; | ||
const params = firstSqlWhere.params && Array.isArray(firstSqlWhere.params) ? firstSqlWhere.params : []; | ||
sqlWhereArr.forEach(item => { | ||
if (item.sql && item.sql !== '') { | ||
sqlWhere += ` and ${item.sql} `; | ||
item.params && Array.isArray(item.params) ? params.push(...item.params) : []; | ||
} | ||
}); | ||
return { | ||
sql: sqlWhere, | ||
params | ||
}; | ||
} | ||
|
||
export function andByModel(obj, tbName?: string) { | ||
tbName = tbName ? `${tbName}.` : ''; | ||
if (Object.prototype.toString.call(obj) !== '[object Object]') { | ||
return { | ||
sql: '', | ||
params: [] | ||
}; | ||
} | ||
let sqlWhere = ''; | ||
const params = []; | ||
Object.keys(obj).forEach(item => { | ||
if (obj[item] || obj[item] === '' || obj[item] === 0) { | ||
if (sqlWhere) { | ||
sqlWhere += ` and ${tbName}${item}=? `; | ||
} else { | ||
sqlWhere = ` ${tbName}${item}=? `; | ||
} | ||
params.push(obj[item]); | ||
} | ||
}); | ||
return { | ||
sql: sqlWhere, | ||
params | ||
}; | ||
} | ||
|
||
export function insertByModel(obj) { | ||
if (Object.prototype.toString.call(obj) !== '[object Object]' && Object.prototype.toString.call(obj) !== '[object Array]') { | ||
return { | ||
sql: '', | ||
params: [] | ||
}; | ||
} | ||
const insertKey = []; | ||
const placeholders = []; | ||
const params = []; | ||
// obj为对象时 生成插入一条数据的sql | ||
if (Object.prototype.toString.call(obj) === '[object Object]') { | ||
Object.keys(obj).forEach(item => { | ||
if (obj[item] === '') { | ||
insertKey.push(item); | ||
placeholders.push('?'); | ||
params.push(null); | ||
} else if (obj[item] || obj[item] === 0) { | ||
insertKey.push(item); | ||
placeholders.push('?'); | ||
params.push(obj[item]); | ||
} | ||
}); | ||
return { | ||
sql: ` (${insertKey.join(',')}) values (${placeholders.join(',')}) `, | ||
params | ||
}; | ||
} | ||
// 当obj为数组对象时 生成插入多条数据的sql | ||
if (Object.prototype.toString.call(obj) === '[object Array]') { | ||
let temp = '('; | ||
for (let i = 0; i < obj.length; i++) { | ||
const objItem = obj[i]; | ||
if (i === 0) { | ||
Object.keys(objItem).forEach((item, index) => { | ||
insertKey.push(item); | ||
if (index === Object.keys(objItem).length - 1) { | ||
temp += '?)'; | ||
} else { | ||
temp += '?,'; | ||
} | ||
}); | ||
} | ||
Object.keys(objItem).forEach(item => { | ||
if ( | ||
objItem[item] === '' | ||
|| objItem[item] === undefined | ||
|| objItem[item] === null | ||
) { | ||
params.push(null); | ||
} else { | ||
params.push(objItem[item]); | ||
} | ||
}); | ||
placeholders.push(temp); | ||
} | ||
return { | ||
sql: ` (${insertKey.join(',')}) values ${placeholders.join(',')} `, | ||
params | ||
}; | ||
} | ||
} | ||
|
||
export function updateByModel(obj) { | ||
if (Object.prototype.toString.call(obj) !== '[object Object]') { | ||
return { | ||
sql: '', | ||
params: [] | ||
}; | ||
} | ||
const updateSql = []; | ||
const params = []; | ||
Object.keys(obj).forEach(item => { | ||
if (obj[item] === '') { | ||
updateSql.push(` ${item}=?`); | ||
params.push(null); | ||
} else if (obj[item] || obj[item] === 0) { | ||
updateSql.push(` ${item}=?`); | ||
params.push(obj[item]); | ||
} | ||
}); | ||
if (updateSql.length === 0) { | ||
throw new Error('update error'); | ||
} | ||
return { | ||
sql: updateSql.join(','), | ||
params | ||
}; | ||
} | ||
|
||
export function orderByFields(sort) { | ||
if (Object.prototype.toString.call(sort) !== '[object Array]') { | ||
return { | ||
sql: '', | ||
params: [] | ||
}; | ||
} | ||
const orderBySql = []; | ||
const params = []; | ||
sort.forEach(item => { | ||
if (item.field) { | ||
const order = item.order ? item.order : 'ASC'; | ||
orderBySql.push(` ${item.field} ${order}`); | ||
} | ||
}); | ||
const sql = orderBySql.length > 0 ? `order by ${orderBySql.join(',')}` : ''; | ||
return { | ||
sql, | ||
params | ||
}; | ||
} |
Oops, something went wrong.