-
Notifications
You must be signed in to change notification settings - Fork 0
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
Lienren
committed
Jan 8, 2018
1 parent
60ae2c0
commit 8a95dc4
Showing
7 changed files
with
243 additions
and
13 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 |
---|---|---|
@@ -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 |
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,9 @@ | ||
{ | ||
"host": "localhost", | ||
"port": 3306, | ||
"user": "root", | ||
"password": "123456", | ||
"database": "mydb", | ||
"charset": "UTF8_GENERAL_CI", | ||
"dateStrings": true | ||
} |
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 |
---|---|---|
@@ -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" | ||
} |
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,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); | ||
} | ||
}; |
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 |
---|---|---|
|
@@ -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" | ||
|
@@ -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" | ||
|
@@ -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" | ||
|
||
|
@@ -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" | ||
|
@@ -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" | ||
|
@@ -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" | ||
|
@@ -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" | ||
|
@@ -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" | ||
|
@@ -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" |