UNPKG

5.12 kBMarkdownView Raw
1# egg-sequelize
2
3Sequelize plugin in egg
4
5[![NPM version][npm-image]][npm-url]
6[![build status][travis-image]][travis-url]
7[![Test coverage][codecov-image]][codecov-url]
8[![David deps][david-image]][david-url]
9[![Known Vulnerabilities][snyk-image]][snyk-url]
10[![npm download][download-image]][download-url]
11
12[npm-image]: https://img.shields.io/npm/v/egg-sequelize.svg?style=flat-square
13[npm-url]: https://npmjs.org/package/egg-sequelize
14[travis-image]: https://img.shields.io/travis/eggjs/egg-sequelize.svg?style=flat-square
15[travis-url]: https://travis-ci.org/eggjs/egg-sequelize
16[codecov-image]: https://codecov.io/gh/eggjs/egg-sequelize/branch/master/graph/badge.svg
17[codecov-url]: https://codecov.io/gh/eggjs/egg-sequelize
18[david-image]: https://img.shields.io/david/eggjs/egg-sequelize.svg?style=flat-square
19[david-url]: https://david-dm.org/eggjs/egg-sequelize
20[snyk-image]: https://snyk.io/test/npm/egg-sequelize/badge.svg?style=flat-square
21[snyk-url]: https://snyk.io/test/npm/egg-sequelize
22[download-image]: https://img.shields.io/npm/dm/egg-sequelize.svg?style=flat-square
23[download-url]: https://npmjs.org/package/egg-sequelize
24
25Egg's sequelize plugin.
26
27## Install
28
29```bash
30$ npm i --save egg-sequelize
31
32# And one of the following:
33$ npm install --save pg pg-hstore
34$ npm install --save mysql # For both mysql and mariadb dialects
35$ npm install --save tedious # MSSQL
36```
37
38
39## Usage & configuration
40
41- `config.default.js`
42```js
43exports.sequelize = {
44 dialect: 'mysql', // support: mysql, mariadb, postgres, mssql
45 database: 'test',
46 host: 'localhost',
47 port: '3306',
48 username: 'root',
49 password: '',
50};
51```
52- `config/plugin.js`
53``` js
54exports.sequelize = {
55 enable: true,
56 package: 'egg-sequelize'
57}
58```
59- `package.json`
60```json
61{
62 "scripts": {
63 "migrate:new": "egg-sequelize migration:create",
64 "migrate:up": "egg-sequelize db:migrate",
65 "migrate:down": "egg-sequelize db:migrate:undo"
66 }
67}
68```
69
70
71More documents please refer to [Sequelize.js](http://sequelize.readthedocs.io/en/v3/)
72
73## Model files
74
75Please put models under `app/model` dir.
76
77## Conventions
78
79| model file | class name |
80| --------------- | --------------------- |
81| `user.js` | `app.model.User` |
82| `person.js` | `app.model.Person` |
83| `user_group.js` | `app.model.UserGroup` |
84
85- Tables always has timestamp fields: `created_at datetime`, `updated_at datetime`.
86- Use underscore style column name, for example: `user_id`, `comments_count`.
87
88## Examples
89
90### Standard
91
92Define a model first.
93
94```js
95// app/model/user.js
96
97module.exports = app => {
98 const { STRING, INTEGER, DATE } = app.Sequelize;
99
100 return app.model.define('user', {
101 login: STRING,
102 name: STRING(30),
103 password: STRING(32),
104 age: INTEGER,
105 created_at: DATE,
106 updated_at: DATE,
107 }, {
108 classMethods: {
109 * findByLogin(login) {
110 return yield this.findOne({ login: login });
111 },
112 },
113 });
114};
115
116```
117
118Now you can use it in your controller:
119
120```js
121// app/controller/user.js
122module.exports = app => {
123 return class UserController extends app.Controller {
124 * index() {
125 const users = yield this.ctx.model.User.findAll();
126 this.ctx.body = users;
127 }
128 }
129}
130```
131
132### Full example
133
134
135
136```js
137// app/model/post.js
138
139module.exports = app => {
140 const { STRING, INTEGER, DATE } = app.Sequelize;
141
142 return app.model.define('Post', {
143 name: STRING(30),
144 user_id: INTEGER,
145 created_at: DATE,
146 updated_at: DATE,
147 }, {
148 classMethods: {
149 associate() {
150 app.model.Post.belongsTo(app.model.User, { as: 'user' });
151 }
152 }
153 });
154};
155```
156
157
158```js
159// app/controller/post.js
160module.exports = app => {
161 return class PostController extends app.Controller {
162 * index() {
163 const posts = yield this.ctx.model.Post.findAll({
164 attributes: [ 'id', 'user_id' ],
165 include: { model: this.ctx.model.User, as: 'user' },
166 where: { status: 'publish' },
167 order: 'id desc',
168 });
169
170 this.ctx.body = posts;
171 }
172
173 * show() {
174 const post = yield this.ctx.model.Post.findById(this.params.id);
175 const post.user = yield post.getUser();
176 this.ctx.body = post;
177 }
178
179 * destroy() {
180 const post = yield this.ctx.model.Post.findById(this.params.id);
181 yield post.destroy();
182 this.ctx.body = { success: true };
183 }
184 }
185}
186```
187
188## Migrations
189
190If you have added scripts of egg-sequelize into your `package.json`, now you can:
191
192| Command | Description |
193|-----|------|
194| npm run migrate:new | Generate a new Migration file to ./migrations/ |
195| npm run migrate:up | Run Migration |
196| npm run migrate:down | Rollback once Migration |
197
198For example:
199
200```bash
201$ npm run migrate:up
202```
203
204For `test` environment:
205
206```bash
207$ NODE_ENV=test npm run migrate:up
208```
209
210or for `production` environment:
211
212```bash
213$ NODE_ENV=production npm run migrate:up
214```
215
216And you may need to read [Sequelize - Migrations](http://docs.sequelizejs.com/en/v3/docs/migrations/) to learn about how to write Migrations.
217
218## Questions & Suggestions
219
220Please open an issue [here](https://github.com/eggjs/egg/issues).
221
222## License
223
224[MIT](LICENSE)
225