Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Lienren committed Jan 8, 2018
1 parent 8a95dc4 commit 9fc0b2c
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 96 deletions.
6 changes: 5 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: Lienren
* @Date: 2018-01-02 14:28:21
* @Last Modified by: Lienren
* @Last Modified time: 2018-01-08 16:55:12
* @Last Modified time: 2018-01-08 20:20:25
*/
'use strict';
const http = require('http');
Expand All @@ -13,6 +13,10 @@ const cors = require('koa2-cors');
const bodyParser = require('koa-bodyparser');
const app = new koa();

const mysqlconfig = require('./configs/mysql_config');
const sqlhelper = require('mysql-helper-simple').init(mysqlconfig);
app.context.db = sqlhelper

// 静态存放地址
const staticPath = './static';
app.use(koastatic(path.join(__dirname, staticPath)));
Expand Down
5 changes: 1 addition & 4 deletions configs/mysql_config.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
{
"host": "localhost",
"port": 3306,
"user": "root",
"password": "123456",
"database": "mydb",
"charset": "UTF8_GENERAL_CI",
"dateStrings": true
"database": "mydb"
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"koa-router": "^7.3.0",
"koa-static": "^4.0.2",
"koa2-cors": "^2.0.5",
"mysql": "^2.15.0",
"mysql-helper-simple": "^1.0.2",
"squel": "^5.12.0"
},
"devDependencies": {},
Expand Down
38 changes: 31 additions & 7 deletions router.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
* @Author: Lienren
* @Date: 2018-01-08 16:21:55
* @Last Modified by: Lienren
* @Last Modified time: 2018-01-08 17:07:30
* @Last Modified time: 2018-01-08 20:35:35
*/
'use strict';
const Router = require('koa-router');
const squel = require('squel');
const sqlhelper = require('./utils/mysql-helper');
const router = new Router();

router
Expand All @@ -26,11 +25,36 @@ router
.from('sm_user')
.toParam();

await sqlhelper.query(sql.text, sql.values).then(result => {
console.log(result);
ctx.body = {
...result
};
await ctx.db
.query({
sql: sql.text,
values: sql.values
})
.then(result => {
console.log(result);
ctx.body = {
...result
};
});
})
.get('/getbatch', async (ctx, next) => {
let sql = squel
.select()
.from('sm_user')
.toParam();

await ctx.db.batch([ctx.db.query({ sql: sql.text, values: sql.values })], {
completeHanding: results => {
console.log('result:', results);
ctx.body = {
...results[0]
};
},
exceptionHandling: err => {
ctx.body = {
...err
};
}
});
});

Expand Down
83 changes: 0 additions & 83 deletions utils/mysql-helper.js

This file was deleted.

6 changes: 6 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,12 @@ [email protected]:
version "2.0.0"
resolved "http://registry.npm.taobao.org/ms/download/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"

mysql-helper-simple@^1.0.2:
version "1.0.4"
resolved "http://registry.npm.taobao.org/mysql-helper-simple/download/mysql-helper-simple-1.0.4.tgz#0c9121eb03fe86373b9ea7a0cec39f7050434fbb"
dependencies:
mysql "^2.15.0"

mysql@^2.15.0:
version "2.15.0"
resolved "http://registry.npm.taobao.org/mysql/download/mysql-2.15.0.tgz#ea16841156343e8f2e47fc8985ec41cdd9573b5c"
Expand Down

0 comments on commit 9fc0b2c

Please sign in to comment.