Skip to content

Commit 34792ce

Browse files
iyuqatian25
authored andcommitted
feat: add sequelize example (#32)
1 parent 0e6c0ff commit 34792ce

File tree

25 files changed

+1586
-0
lines changed

25 files changed

+1586
-0
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ node_js:
44
- '6'
55
- '7'
66
- '8'
7+
services:
8+
- postgresql
79
install:
810
- npm i npminstall && npminstall
911
script:

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ $ npm start
2424
- [unittest](https://github.com/eggjs/examples/tree/master/unittest)
2525
- [view-nunjucks](https://github.com/eggjs/examples/tree/master/view-nunjucks)
2626
- [custom-env](https://github.com/eggjs/examples/tree/master/custom-env)
27+
- [sequelize-example](https://github.com/eggjs/examples/tree/master/sequelize-example)
2728

2829
[egg]: https://github.com/eggjs/egg

sequelize-example/.travis.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
sudo: false
2+
language: node_js
3+
node_js:
4+
- '6'
5+
- '7'
6+
- '8'
7+
services:
8+
- postgresql
9+
install:
10+
- npm i npminstall && npminstall
11+
script:
12+
- npm run ci
13+
after_script:
14+
- npminstall codecov && codecov

sequelize-example/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# sequelize-example
2+
3+
The [egg] example project that uses [egg-sequelize] plugin.
4+
5+
## QuickStart
6+
7+
<!-- add docs here for user -->
8+
9+
see [egg docs][egg] for more detail.
10+
11+
### Development
12+
```bash
13+
$ npm install
14+
$ createdb example-dev --owner postgres
15+
$ npm run migrate:up
16+
$ npm run dev
17+
$ open http://localhost:7001/users
18+
```
19+
20+
### Deploy
21+
22+
Use `EGG_SERVER_ENV=prod` to enable prod mode
23+
24+
```bash
25+
$ createdb example-prod --owner postgres
26+
$ NODE_ENV=production npm run migrate:up
27+
$ EGG_SERVER_ENV=prod npm start
28+
```
29+
30+
### npm scripts
31+
32+
- Use `npm run lint` to check code style.
33+
- Use `npm test` to run unit test.
34+
- Use `npm run autod` to auto detect dependencies upgrade, see [autod](https://www.npmjs.com/package/autod) for more detail.
35+
36+
37+
[egg]: https://eggjs.org
38+
[egg-sequelize]: https://github.com/eggjs/egg-sequelize

sequelize-example/README.zh-CN.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# sequelize-example
2+
3+
使用 [egg-sequelize] 插件的 [egg] 示例项目。
4+
5+
## 快速入门
6+
7+
<!-- 在此次添加使用文档 -->
8+
9+
如需进一步了解,参见 [egg 文档][egg]
10+
11+
### 本地开发
12+
```bash
13+
$ npm install
14+
$ createdb example-dev --owner postgres
15+
$ npm run migrate:up
16+
$ npm run dev
17+
$ open http://localhost:7001/users
18+
```
19+
20+
### 部署
21+
22+
线上正式环境用 `EGG_SERVER_ENV=prod` 来启动。
23+
24+
```bash
25+
$ createdb example-prod --owner postgres
26+
$ NODE_ENV=production npm run migrate:up
27+
$ EGG_SERVER_ENV=prod npm start
28+
```
29+
30+
### 单元测试
31+
- [egg-bin] 内置了 [mocha], [thunk-mocha], [power-assert], [istanbul] 等框架,让你可以专注于写单元测试,无需理会配套工具。
32+
- 断言库非常推荐使用 [power-assert]
33+
- 具体参见 [egg 文档 -单元测试](https://eggjs.org/zh-cn/core/unittest)
34+
35+
### 内置指令
36+
37+
- 使用 `npm run lint` 来做代码风格检查。
38+
- 使用 `npm test` 来执行单元测试。
39+
- 使用 `npm run autod` 来自动检测依赖更新,详细参见 [autod](https://www.npmjs.com/package/autod)
40+
41+
42+
[egg]: https://eggjs.org
43+
[egg-sequelize]: https://github.com/eggjs/egg-sequelize
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
3+
module.exports = app => {
4+
return class UserController extends app.Controller {
5+
* posts() {
6+
const ctx = this.ctx;
7+
ctx.body = yield ctx.service.post.list(ctx.query);
8+
}
9+
10+
* post() {
11+
const ctx = this.ctx;
12+
ctx.body = yield ctx.service.post.find(ctx.params.id);
13+
}
14+
15+
* create() {
16+
const ctx = this.ctx;
17+
const body = ctx.request.body;
18+
body.user_id = +ctx.params.user_id;
19+
const created = yield ctx.service.post.create(ctx.request.body);
20+
ctx.status = 201;
21+
ctx.body = created;
22+
}
23+
24+
* update() {
25+
const ctx = this.ctx;
26+
const id = ctx.params.id;
27+
const user_id = +ctx.params.user_id;
28+
const body = ctx.request.body;
29+
ctx.body = yield ctx.service.post.update({ id, user_id, updates: body });
30+
}
31+
32+
* del() {
33+
const ctx = this.ctx;
34+
const id = ctx.params.id;
35+
yield ctx.service.post.del(id);
36+
ctx.status = 200;
37+
}
38+
};
39+
};
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
module.exports = app => {
4+
return class UserController extends app.Controller {
5+
* users() {
6+
const ctx = this.ctx;
7+
ctx.body = yield ctx.service.user.list(ctx.query);
8+
}
9+
10+
* user() {
11+
const ctx = this.ctx;
12+
ctx.body = yield ctx.service.user.find(ctx.params.id);
13+
}
14+
15+
* create() {
16+
const ctx = this.ctx;
17+
const created = yield ctx.service.user.create(ctx.request.body);
18+
ctx.status = 201;
19+
ctx.body = created;
20+
}
21+
22+
* update() {
23+
const ctx = this.ctx;
24+
const id = ctx.params.id;
25+
const body = ctx.request.body;
26+
ctx.body = yield ctx.service.user.update({ id, updates: body });
27+
}
28+
29+
* del() {
30+
const ctx = this.ctx;
31+
const id = ctx.params.id;
32+
yield ctx.service.user.del(id);
33+
ctx.status = 200;
34+
}
35+
};
36+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
module.exports = app => {
4+
const { STRING, INTEGER, DATE } = app.Sequelize;
5+
6+
return app.model.define('post', {
7+
id: {
8+
type: INTEGER,
9+
primaryKey: true,
10+
autoIncrement: true,
11+
},
12+
title: STRING(30),
13+
content: STRING(255),
14+
user_id: INTEGER,
15+
created_at: DATE,
16+
updated_at: DATE,
17+
}, {
18+
classMethods: {
19+
associate() {
20+
app.model.Post.belongsTo(app.model.User, { as: 'user', foreignKey: 'user_id' });
21+
},
22+
},
23+
});
24+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
3+
module.exports = app => {
4+
const { STRING, INTEGER, DATE } = app.Sequelize;
5+
6+
return app.model.define('user', {
7+
id: {
8+
type: INTEGER,
9+
primaryKey: true,
10+
autoIncrement: true,
11+
},
12+
name: STRING(30),
13+
age: INTEGER,
14+
created_at: DATE,
15+
updated_at: DATE,
16+
}, {
17+
classMethods: {
18+
associate() {
19+
app.model.User.hasMany(app.model.Post, { as: 'posts', foreignKey: 'user_id' });
20+
},
21+
},
22+
});
23+
};

sequelize-example/app/router.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
module.exports = app => {
4+
app.get('/users', 'user.users');
5+
app.get('/users/:id', 'user.user');
6+
app.post('/users', 'user.create');
7+
app.put('/users/:id', 'user.update');
8+
app.del('/users/:id', 'user.del');
9+
10+
app.get('/posts', 'post.posts');
11+
app.get('/posts/:id', 'post.post');
12+
app.post('/users/:user_id/posts', 'post.create');
13+
app.put('/users/:user_id/posts/:id', 'post.update');
14+
app.del('/users/:user_id/posts/:id', 'post.del');
15+
};

0 commit comments

Comments
 (0)