Skip to content

Commit

Permalink
feat: use mysql db
Browse files Browse the repository at this point in the history
  • Loading branch information
SimpleCodeCX committed Nov 11, 2019
1 parent 24315e3 commit 2c6d60d
Show file tree
Hide file tree
Showing 20 changed files with 534 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
.vscode-upload.json
package-lock.json
**/apidoc/dist/*

**/__bak__/*
# compiled output
/dist
/tmp
Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"ts"
],
"dependencies": {
"debug": "^4.1.1",
"koa": "^2.8.1",
"koa-body": "^4.1.1",
"koa-favicon": "^2.0.1",
Expand All @@ -31,16 +32,21 @@
"koa-router": "^7.4.0",
"koa-static-server": "^1.4.0",
"koa-views": "^6.2.1",
"log4js": "^5.3.0",
"mysql": "^2.17.1",
"pug": "^2.0.4"
},
"devDependencies": {
"@types/debug": "^4.1.5",
"@types/jest": "^24.0.19",
"@types/koa": "2.0.50",
"@types/koa-compose": "3.2.4",
"@types/koa-logger": "^3.1.1",
"@types/koa-router": "^7.0.42",
"@types/koa-views": "^2.0.3",
"@types/log4js": "^2.3.5",
"@types/mocha": "^5.2.7",
"@types/mysql": "^2.15.8",
"@types/node": "^12.7.5",
"@types/shelljs": "^0.8.5",
"@types/supertest": "^2.0.8",
Expand Down
4 changes: 4 additions & 0 deletions shell/start-local.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
set PORT=8080
:: 数据库密码
set MARIADBPWD=xxxx
npm run dev
4 changes: 4 additions & 0 deletions shell/test.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
set PORT=8080
:: 数据库密码
set MARIADBPWD=xxxx
npm run test
11 changes: 11 additions & 0 deletions src/app/config/dev-server.config.ts
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'
}
};
11 changes: 11 additions & 0 deletions src/app/config/dev.config.ts
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'
}
};
10 changes: 10 additions & 0 deletions src/app/config/prod.config.ts
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'
}
};
35 changes: 35 additions & 0 deletions src/app/config/system.config.ts
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;
4 changes: 4 additions & 0 deletions src/app/config/variate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* 整个项目的入口变量
*/
export const MARIADBPWD: string = process.env.MARIADBPWD; // 数据库密码
8 changes: 4 additions & 4 deletions src/app/core/bll/v1/user_bll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import { UserResModel, UserDbModel } from '@model/v1';
* bll 层主要对 dal 层进行调用,并且格式化数据,并被 controller 层调用
*/

export async function getUserList(dbModel?: UserDbModel, search?: string, page_no?: number, page_size?: number) {
const userList: Array<UserDbModel> = await userDal.getUserList(dbModel, search, page_no, page_size);
export async function getUserList(dbModel?: UserDbModel, search?: string, pageNo?: number, pageSize?: number) {
const userList: Array<UserDbModel> = await userDal.getUserList(dbModel, search, pageNo, pageSize);
return formatDbList(userList);
}

export async function getByName(userName) {
const user: UserDbModel = await userDal.getByName(userName);
return formatDb(user);
const users: Array<UserDbModel> = await userDal.getByName(userName);
return formatDb(users[0]);
}

export function formatDb(db: UserDbModel): UserResModel {
Expand Down
178 changes: 178 additions & 0 deletions src/app/core/dal/common/dalHelper.ts
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
};
}
Loading

0 comments on commit 2c6d60d

Please sign in to comment.