Skip to content

Commit

Permalink
Create mysql-helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Lienren committed Jan 8, 2018
1 parent 60ae2c0 commit 8a95dc4
Show file tree
Hide file tree
Showing 7 changed files with 243 additions and 13 deletions.
48 changes: 44 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,51 @@
# koademo
# Koademo

koa demo project

# run
``` bash
# Run

```bash
> yarn
> node app.js
```

# browser
# Browser

http://localhost:3000/

# Attention

## Mysql table

```bash
DROP TABLE IF EXISTS `sm_user`;
CREATE TABLE `sm_user` (
`uid` int(11) NOT NULL AUTO_INCREMENT,
`uname` varchar(100) DEFAULT NULL,
`addtime` datetime DEFAULT NULL,
`isdel` int(1) DEFAULT NULL,
PRIMARY KEY (`uid`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;;

SET FOREIGN_KEY_CHECKS = 1;
```

## Configs

./configs/mysql_config.json

```bash
{
"host": "localhost",
"port": 3306,
"user": "root",
"password": "123456",
"database": "mydb",
"charset": "UTF8_GENERAL_CI",
"dateStrings": true
}
```

## Utils

./utils/mysql-helper
8 changes: 4 additions & 4 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
* @Author: Lienren
* @Date: 2018-01-02 14:28:21
* @Last Modified by: Lienren
* @Last Modified time: 2018-01-02 15:18:14
* @Last Modified time: 2018-01-08 16:55:12
*/

'use strict';
const http = require('http');
const path = require('path');
const koa = require('koa');
const static = require('koa-static');
const koastatic = require('koa-static');
const cors = require('koa2-cors');
const bodyParser = require('koa-bodyparser');
const app = new koa();

// 静态存放地址
const staticPath = './static';
app.use(static(path.join(__dirname, staticPath)));
app.use(koastatic(path.join(__dirname, staticPath)));

// 配置跨域访问
app.use(
Expand Down
9 changes: 9 additions & 0 deletions configs/mysql_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"host": "localhost",
"port": 3306,
"user": "root",
"password": "123456",
"database": "mydb",
"charset": "UTF8_GENERAL_CI",
"dateStrings": true
}
27 changes: 25 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
{
"name": "koademo",
"version": "1.0.0",
"description": "koa demo",
"main": "app.js",
"dependencies": {
"koa": "^2.4.1",
"koa-bodyparser": "^4.2.0",
"koa-router": "^7.3.0",
"koa-static": "^4.0.2",
"koa2-cors": "^2.0.5"
}
"koa2-cors": "^2.0.5",
"mysql": "^2.15.0",
"squel": "^5.12.0"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/lienren/koademo.git"
},
"keywords": [
"koa"
],
"author": "lienren",
"license": "ISC",
"bugs": {
"url": "https://github.com/lienren/koademo/issues"
},
"homepage": "https://github.com/lienren/koademo#readme"
}
20 changes: 18 additions & 2 deletions router.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
/*
* @Author: Lienren
* @Date: 2018-01-08 16:21:55
* @Last Modified by: Lienren
* @Last Modified time: 2018-01-08 16:21:55
* @Last Modified by: Lienren
* @Last Modified time: 2018-01-08 17:07:30
*/
'use strict';
const Router = require('koa-router');
const squel = require('squel');
const sqlhelper = require('./utils/mysql-helper');
const router = new Router();

router
Expand All @@ -16,6 +19,19 @@ router
ctx.body = {
...ctx.request.body
};
})
.get('/getusers', async (ctx, next) => {
let sql = squel
.select()
.from('sm_user')
.toParam();

await sqlhelper.query(sql.text, sql.values).then(result => {
console.log(result);
ctx.body = {
...result
};
});
});

module.exports = router.routes();
83 changes: 83 additions & 0 deletions utils/mysql-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* @Author: Lienren
* @Date: 2018-01-08 16:34:24
* @Last Modified by: Lienren
* @Last Modified time: 2018-01-08 16:34:24
*/
'use strict';
const mysql = require('mysql');
const mysql_config = require('../configs/mysql_config.json');

module.exports = {
start() {
this.connection = mysql.createConnection(mysql_config);
this.connection.connect();
return this;
},
end() {
if (this.connection) {
this.connection.end();
}
return this;
},
execute(sql, values, callback) {
if (values) {
sql = mysql.format(sql, values);
}

this.connection.query(sql, (err, rows, fields) => {
callback({ err: err, rows: rows, fields: fields });
});
return this;
},
query(sql, values) {
return new Promise((resolve, reject) => {
this.start()
.execute(sql, values, result => {
if (result.err) {
reject(result.err);
} else {
resolve(result.rows);
}
})
.end();
});
},
create(sql, values) {
let get_auto_id = 'SELECT LAST_INSERT_ID() as id;';
return new Promise((resolve, reject) => {
this.start()
.execute(sql, values, result => {
if (result.err) {
reject(result.err);
}
})
.execute(get_auto_id, result => {
if (result.err) {
reject(result.err);
} else {
resolve(result.rows);
}
})
.end();
});
},
update(sql, values) {
return new Promise((resolve, reject) => {
this.start()
.execute(sql, values, result => {
if (result.err) {
reject(result.err);
} else {
resolve({ rows: result.rows.affectedRows });
}
})
.end();
});
},
batch(executes, { completeHanding = result => {}, exceptionHandling = err => {} }) {
Promise.all(executes)
.then(completeHanding)
.catch(exceptionHandling);
}
};
61 changes: 60 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ any-promise@^1.0.0, any-promise@^1.1.0:
version "1.3.0"
resolved "http://registry.npm.taobao.org/any-promise/download/any-promise-1.3.0.tgz#abc6afeedcea52e809cdc0376aed3ce39635d17f"

[email protected]:
version "4.0.4"
resolved "http://registry.npm.taobao.org/bignumber.js/download/bignumber.js-4.0.4.tgz#7c40f5abcd2d6623ab7b99682ee7db81b11889a4"

[email protected]:
version "3.0.0"
resolved "http://registry.npm.taobao.org/bytes/download/bytes-3.0.0.tgz#d32815404d689699f85a4ea4fa8755dd13a96048"
Expand Down Expand Up @@ -49,6 +53,10 @@ copy-to@^2.0.1:
version "2.0.1"
resolved "http://registry.npm.taobao.org/copy-to/download/copy-to-2.0.1.tgz#2680fbb8068a48d08656b6098092bdafc906f4a5"

core-util-is@~1.0.0:
version "1.0.2"
resolved "http://registry.npm.taobao.org/core-util-is/download/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"

debug@*, debug@^3.1.0:
version "3.1.0"
resolved "http://registry.npm.taobao.org/debug/download/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261"
Expand Down Expand Up @@ -125,7 +133,7 @@ inflation@^2.0.0:
version "2.0.0"
resolved "http://registry.npm.taobao.org/inflation/download/inflation-2.0.0.tgz#8b417e47c28f925a45133d914ca1fd389107f30f"

[email protected]:
[email protected], inherits@~2.0.3:
version "2.0.3"
resolved "http://registry.npm.taobao.org/inherits/download/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de"

Expand All @@ -137,6 +145,10 @@ [email protected]:
version "0.0.1"
resolved "http://registry.npm.taobao.org/isarray/download/isarray-0.0.1.tgz#8a18acfca9a8f4177e09abfc6038939b05d1eedf"

isarray@~1.0.0:
version "1.0.0"
resolved "http://registry.npm.taobao.org/isarray/download/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11"

keygrip@~1.0.2:
version "1.0.2"
resolved "http://registry.npm.taobao.org/keygrip/download/keygrip-1.0.2.tgz#ad3297c557069dea8bcfe7a4fa491b75c5ddeb91"
Expand Down Expand Up @@ -251,6 +263,15 @@ [email protected]:
version "2.0.0"
resolved "http://registry.npm.taobao.org/ms/download/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"

mysql@^2.15.0:
version "2.15.0"
resolved "http://registry.npm.taobao.org/mysql/download/mysql-2.15.0.tgz#ea16841156343e8f2e47fc8985ec41cdd9573b5c"
dependencies:
bignumber.js "4.0.4"
readable-stream "2.3.3"
safe-buffer "5.1.1"
sqlstring "2.3.0"

mz@^2.6.0:
version "2.7.0"
resolved "http://registry.npm.taobao.org/mz/download/mz-2.7.0.tgz#95008057a56cafadc2bc63dde7f9ff6955948e32"
Expand Down Expand Up @@ -291,6 +312,10 @@ path-to-regexp@^1.1.1:
dependencies:
isarray "0.0.1"

process-nextick-args@~1.0.6:
version "1.0.7"
resolved "http://registry.npm.taobao.org/process-nextick-args/download/process-nextick-args-1.0.7.tgz#150e20b756590ad3f91093f25a4f2ad8bff30ba3"

qs@^6.4.0:
version "6.5.1"
resolved "http://registry.npm.taobao.org/qs/download/qs-6.5.1.tgz#349cdf6eef89ec45c12d7d5eb3fc0c870343a6d8"
Expand All @@ -304,13 +329,29 @@ raw-body@^2.2.0:
iconv-lite "0.4.19"
unpipe "1.0.0"

[email protected]:
version "2.3.3"
resolved "http://registry.npm.taobao.org/readable-stream/download/readable-stream-2.3.3.tgz#368f2512d79f9d46fdfc71349ae7878bbc1eb95c"
dependencies:
core-util-is "~1.0.0"
inherits "~2.0.3"
isarray "~1.0.0"
process-nextick-args "~1.0.6"
safe-buffer "~5.1.1"
string_decoder "~1.0.3"
util-deprecate "~1.0.1"

resolve-path@^1.3.3:
version "1.3.3"
resolved "http://registry.npm.taobao.org/resolve-path/download/resolve-path-1.3.3.tgz#4d83aba6468c2b8e632a575e3f52b0fa0dbe1a5c"
dependencies:
http-errors "~1.5.0"
path-is-absolute "1.0.1"

[email protected], safe-buffer@~5.1.0, safe-buffer@~5.1.1:
version "5.1.1"
resolved "http://registry.npm.taobao.org/safe-buffer/download/safe-buffer-5.1.1.tgz#893312af69b2123def71f57889001671eeb2c853"

[email protected]:
version "1.0.2"
resolved "http://registry.npm.taobao.org/setprototypeof/download/setprototypeof-1.0.2.tgz#81a552141ec104b88e89ce383103ad5c66564d08"
Expand All @@ -319,10 +360,24 @@ [email protected]:
version "1.0.3"
resolved "http://registry.npm.taobao.org/setprototypeof/download/setprototypeof-1.0.3.tgz#66567e37043eeb4f04d91bd658c0cbefb55b8e04"

[email protected]:
version "2.3.0"
resolved "http://registry.npm.taobao.org/sqlstring/download/sqlstring-2.3.0.tgz#525b8a4fd26d6f71aa61e822a6caf976d31ad2a8"

squel@^5.12.0:
version "5.12.0"
resolved "http://registry.npm.taobao.org/squel/download/squel-5.12.0.tgz#f8d0310db8d2ca63b5c36c11defc0f7993fcf78e"

"statuses@>= 1.3.1 < 2", statuses@^1.2.0:
version "1.4.0"
resolved "http://registry.npm.taobao.org/statuses/download/statuses-1.4.0.tgz#bb73d446da2796106efcc1b601a253d6c46bd087"

string_decoder@~1.0.3:
version "1.0.3"
resolved "http://registry.npm.taobao.org/string_decoder/download/string_decoder-1.0.3.tgz#0fc67d7c141825de94282dd536bec6b9bce860ab"
dependencies:
safe-buffer "~5.1.0"

thenify-all@^1.0.0:
version "1.6.0"
resolved "http://registry.npm.taobao.org/thenify-all/download/thenify-all-1.6.0.tgz#1a1918d402d8fc3f98fbf234db0bcc8cc10e9726"
Expand Down Expand Up @@ -350,6 +405,10 @@ urijs@^1.19.0:
version "1.19.0"
resolved "http://registry.npm.taobao.org/urijs/download/urijs-1.19.0.tgz#d8aa284d0e7469703a6988ad045c4cbfdf08ada0"

util-deprecate@~1.0.1:
version "1.0.2"
resolved "http://registry.npm.taobao.org/util-deprecate/download/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"

vary@^1.0.0:
version "1.1.2"
resolved "http://registry.npm.taobao.org/vary/download/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc"

0 comments on commit 8a95dc4

Please sign in to comment.