├── .autod.conf.js ├── .eslintignore ├── .eslintrc ├── .github └── PULL_REQUEST_TEMPLATE.md ├── .gitignore ├── .travis.yml ├── History.md ├── LICENSE ├── README.md ├── agent.js ├── app.js ├── config └── config.default.js ├── index.d.ts ├── lib └── loader.js ├── package.json └── test ├── connection-uri.test.js ├── datasources.test.js ├── datasources_same_dir.test.js ├── fixtures └── apps │ ├── connection-uri │ ├── app │ │ ├── controller │ │ │ └── home.ts │ │ ├── model │ │ │ ├── monkey.ts │ │ │ └── user.ts │ │ └── router.ts │ ├── config │ │ ├── config.default.ts │ │ ├── config.local.ts │ │ └── config.unittest.ts │ ├── package.json │ ├── tsconfig.json │ └── typings │ │ └── index.d.ts │ ├── datasources-same-dir │ ├── app │ │ └── model │ │ │ ├── Person.js │ │ │ ├── monkey.js │ │ │ ├── other.js │ │ │ ├── post.js │ │ │ └── user.js │ ├── config │ │ └── config.js │ └── package.json │ ├── datasources │ ├── app │ │ ├── model │ │ │ ├── Person.js │ │ │ ├── monkey.js │ │ │ ├── other.js │ │ │ ├── post.js │ │ │ └── user.js │ │ ├── sequelize │ │ │ ├── Person.js │ │ │ ├── monkey.js │ │ │ ├── other.js │ │ │ ├── post.js │ │ │ └── user.js │ │ └── subproperty │ │ │ ├── a │ │ │ ├── Person.js │ │ │ ├── monkey.js │ │ │ ├── other.js │ │ │ ├── post.js │ │ │ └── user.js │ │ │ └── b │ │ │ ├── Person.js │ │ │ ├── monkey.js │ │ │ ├── other.js │ │ │ ├── post.js │ │ │ └── user.js │ ├── config │ │ └── config.js │ └── package.json │ ├── model-app │ ├── app │ │ ├── controller │ │ │ └── users.js │ │ ├── model │ │ │ ├── Person.js │ │ │ ├── monkey.js │ │ │ ├── other.js │ │ │ ├── post.js │ │ │ └── user.js │ │ └── router.js │ ├── config │ │ └── config.js │ └── package.json │ ├── sub-model │ ├── app │ │ └── model │ │ │ ├── sub │ │ │ └── post.js │ │ │ └── user.js │ ├── config │ │ └── config.js │ └── package.json │ └── ts │ ├── app │ ├── controller │ │ └── home.ts │ ├── model │ │ ├── monkey.ts │ │ └── user.ts │ └── router.ts │ ├── config │ ├── config.default.ts │ └── config.local.ts │ ├── tsconfig.json │ └── typings │ └── index.d.ts ├── plugin.test.js ├── submodel.test.js └── ts.test.js /.autod.conf.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { 4 | write: true, 5 | prefix: '^', 6 | test: [ 7 | 'test', 8 | 'benchmark', 9 | ], 10 | dep: [ 11 | "@types/sequelize", 12 | ], 13 | devdep: [ 14 | 'egg', 15 | 'egg-bin', 16 | 'autod', 17 | 'eslint', 18 | 'eslint-config-egg', 19 | ], 20 | exclude: [ 21 | './test/fixtures', 22 | './docs', 23 | './coverage', 24 | ], 25 | registry: 'https://r.cnpmjs.org', 26 | }; 27 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | coverage 2 | fixtures -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "eslint-config-egg" 3 | } 4 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 10 | 11 | ##### Checklist 12 | 13 | 14 | - [ ] `npm test` passes 15 | - [ ] tests and/or benchmarks are included 16 | - [ ] documentation is changed or added 17 | - [ ] commit message follows commit guidelines 18 | 19 | ##### Affected core subsystem(s) 20 | 21 | 22 | 23 | ##### Description of change 24 | 25 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | logs 2 | npm-debug.log 3 | node_modules/ 4 | coverage/ 5 | .idea/ 6 | run 7 | .DS_Store 8 | *.swp 9 | test/fixtures/apps/model-app/run/ 10 | test/fixtures/apps/ts/**/*.js 11 | test/fixtures/apps/connection-uri/**/*.js 12 | package-lock.json 13 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - '10' 5 | - '12' 6 | - '14' 7 | install: 8 | - npm i npminstall && npminstall 9 | script: 10 | - npm run ci 11 | after_script: 12 | - npminstall codecov && codecov 13 | services: 14 | - mysql 15 | before_install: 16 | - mysql -e 'CREATE DATABASE IF NOT EXISTS test;' 17 | - mysql -e 'CREATE DATABASE IF NOT EXISTS test1;' 18 | - mysql -e 'CREATE DATABASE IF NOT EXISTS test2;' 19 | - mysql -e 'CREATE DATABASE IF NOT EXISTS test3;' 20 | - mysql -e 'CREATE DATABASE IF NOT EXISTS test4;' 21 | -------------------------------------------------------------------------------- /History.md: -------------------------------------------------------------------------------- 1 | 2 | 6.0.0 / 2020-09-16 3 | ================== 4 | 5 | **features** 6 | * [[`f585da5`](http://github.com/eggjs/egg-sequelize/commit/f585da5eb7fd22062127c71933ae8b01c88a3623)] - feat: update sequelize to version 6 (#87) (wangzhilongh <>) 7 | 8 | **others** 9 | * [[`b14752b`](http://github.com/eggjs/egg-sequelize/commit/b14752b3dfabdf00eaa1e5193e3edcc024042f46)] - build: test on node 10,12,14 (dead-horse <>) 10 | 11 | 5.2.2 / 2020-07-01 12 | ================== 13 | 14 | **fixes** 15 | * [[`665bbd6`](http://github.com/eggjs/egg-sequelize/commit/665bbd6ce3bee55ba3f7024a701cf5f1b87f1f21)] - fix: exception when change your config.delegate to other name, you will get an TypeError (#84) (bianchui <>) 16 | 17 | 5.2.1 / 2019-12-25 18 | ================== 19 | 20 | **fixes** 21 | * [[`c2c9cf6`](http://github.com/eggjs/egg-sequelize/commit/c2c9cf693e96da31d32d478134cb67743608951f)] - fix: retry when db is busy. (#85) (金炳 <<1520006273@qq.com>>) 22 | * [[`47e9550`](http://github.com/eggjs/egg-sequelize/commit/47e955058340df7f984d1a206c2af9e4fd1491fd)] - fix: README.md (#81) (zfx <<502545703@qq.com>>) 23 | 24 | **others** 25 | * [[`683298d`](http://github.com/eggjs/egg-sequelize/commit/683298dde706b34bdbdb6abd0cab6a7164495967)] - docs: add comment in index.d.ts (#78) (supperchong <<2267805901@qq.com>>) 26 | 27 | 5.2.0 / 2019-07-10 28 | ================== 29 | 30 | **features** 31 | * [[`f760569`](http://github.com/eggjs/egg-sequelize/commit/f760569891daa99c72e2ccd905038cc9125d59f2)] - feat: add model.ctx (dead-horse <>) 32 | 33 | 5.1.0 / 2019-06-14 34 | ================== 35 | 36 | **features** 37 | * [[`2632827`](http://github.com/eggjs/egg-sequelize/commit/263282739cafb22501069afe3304a8e2a76dbd7c)] - feat: :support connection uri with mysql. (#74) (Jeff <>) 38 | 39 | 5.0.1 / 2019-06-11 40 | ================== 41 | 42 | **fixes** 43 | * [[`0fe964e`](http://github.com/eggjs/egg-sequelize/commit/0fe964e8f428285469513d5059b79edb39021d3c)] - fix: fix declaration error (#73) (吖猩 <>) 44 | 45 | **others** 46 | * [[`d813747`](http://github.com/eggjs/egg-sequelize/commit/d8137476aa6b593718ad67e1438d7c0086d7e63c)] - chore: fix default sequelize version in readme (#72) (Chao Xu @老干部 <>) 47 | 48 | 5.0.0 / 2019-05-10 49 | ================== 50 | 51 | **fixes** 52 | * [[`8deb42f`](http://github.com/eggjs/egg-sequelize/commit/8deb42f8d01e6982ecfdb9eff2628656f8a3c519)] - fix: fix version (dead-horse <>) 53 | 54 | **others** 55 | * [[`c9f78b0`](http://github.com/eggjs/egg-sequelize/commit/c9f78b08f2d05eaa97dc5c5e807368a9c6188762)] - deps: sequelize upgrade to v5.0.0 (#71) (Akshay Kr Singh <>) 56 | * [[`04c9e72`](http://github.com/eggjs/egg-sequelize/commit/04c9e72ffb1b19dbfcc42275e68fe5113eb5e2fc)] - docs: fix arrow function (#68) (TZ | 天猪 <>) 57 | 58 | 4.3.1 / 2019-01-08 59 | ================== 60 | 61 | **fixes** 62 | * [[`ceaa7de`](http://github.com/eggjs/egg-sequelize/commit/ceaa7de7c66bf20d0d4e5d1fce912731feb7f6f0)] - fix: fix authenticate retry (#67) (Yiyu He <>) 63 | 64 | 4.3.0 / 2019-01-07 65 | ================== 66 | 67 | **features** 68 | * [[`69c5750`](http://github.com/eggjs/egg-sequelize/commit/69c57508ee6c69a765af30f99ca3d73a9c764102)] - feat: support multiple data sources config (#66) (microud <<201458501212@ytu.edu.cn>>) 69 | 70 | **others** 71 | * [[`15f08dd`](http://github.com/eggjs/egg-sequelize/commit/15f08dd830ef5eda8e3988f6c1da336d03861219)] - docs: port should be number (#64) (Haoliang Gao <>) 72 | 73 | 4.2.0 / 2018-11-12 74 | ================== 75 | 76 | **features** 77 | * [[`c4998e1`](http://github.com/eggjs/egg-sequelize/commit/c4998e1c9d60596a84086f8098764fb45624cbc1)] - feat: multi datasource support load same models (#63) (Yiyu He <>) 78 | 79 | **others** 80 | * [[`b41598e`](http://github.com/eggjs/egg-sequelize/commit/b41598e7c33bb03403d2a3cbfaf172911a1187dc)] - chore: logger typo (#62) (TZ | 天猪 <>) 81 | 82 | 4.1.0 / 2018-08-31 83 | ================== 84 | 85 | **features** 86 | * [[`cc30ec2`](http://github.com/eggjs/egg-sequelize/commit/cc30ec2f8883a5e8bd3d1b200f58fe1b2d476fa8)] - feat: support subproperty for delegate (#61) (Army <>) 87 | 88 | 4.0.7 / 2018-08-20 89 | ================== 90 | 91 | **fixes** 92 | * [[`758c5ff`](http://github.com/eggjs/egg-sequelize/commit/758c5ffbc1884709f87d0354c63e280065360b06)] - fix: avoid set sequelize.ignore by mistake (dead-horse <>) 93 | 94 | 4.0.6 / 2018-08-20 95 | ================== 96 | 97 | **fixes** 98 | * [[`a708411`](http://github.com/eggjs/egg-sequelize/commit/a70841113cbf239a547ecb5cfbb58016c41dc877)] - fix: make ctx.model or app.model configurable (dead-horse <>) 99 | * [[`63a2567`](http://github.com/eggjs/egg-sequelize/commit/63a25678ac73751031b39e5651ebd5abf69bc900)] - fix: use sequelize.exclude instead of sequelize.ignore (dead-horse <>) 100 | 101 | 4.0.5 / 2018-08-20 102 | ================== 103 | 104 | **fixes** 105 | * [[`ff2bcbe`](http://github.com/eggjs/egg-sequelize/commit/ff2bcbe1516c57217844ee75282027d1da812e25)] - fix: avoid console log display conflict (dead-horse <>) 106 | * [[`a3ace2b`](http://github.com/eggjs/egg-sequelize/commit/a3ace2b0e90d156dc8fc85a9f8e5896a3bdf7cdd)] - fix: ctx.model extends app.model (dead-horse <>) 107 | 108 | 4.0.4 / 2018-08-17 109 | ================== 110 | 111 | **fixes** 112 | * [[`708d020`](http://github.com/eggjs/egg-sequelize/commit/708d0207125cdfd99f0e968b76e42cd0440d1afd)] - fix: clean config (dead-horse <>) 113 | 114 | 4.0.3 / 2018-08-17 115 | ================== 116 | 117 | **fixes** 118 | * [[`826f512`](http://github.com/eggjs/egg-sequelize/commit/826f512d9fbb12bd9f4f256ff063fbd5ef11c250)] - fix: improve tsd (shangyu <>) 119 | 120 | **others** 121 | * [[`174d397`](http://github.com/eggjs/egg-sequelize/commit/174d3979e1abdb42317dad6e1ff7d9937c665615)] - docs: add tutorials (dead-horse <>) 122 | * [[`f1be646`](http://github.com/eggjs/egg-sequelize/commit/f1be64626959f719af32b4fbc03311bc332abe98)] - docs: fix example code path (dead-horse <>) 123 | * [[`975873f`](http://github.com/eggjs/egg-sequelize/commit/975873fdb67f21655833ad6ea1fff025f0042f51)] - docs: add default sequelize options (dead-horse <>) 124 | * [[`a6325bf`](http://github.com/eggjs/egg-sequelize/commit/a6325bf8a35366a3cf2bc41208043b100d4b68c1)] - chore: add sub model describe (dead-horse <>) 125 | 126 | 4.0.2 / 2018-08-14 127 | ================== 128 | 129 | **fixes** 130 | * [[`a966ee4`](http://github.com/eggjs/egg-sequelize/commit/a966ee48809b91d896532f256fb4906ae9783a01)] - fix: fix default logging (dead-horse <>) 131 | 132 | 4.0.1 / 2018-08-13 133 | ================== 134 | 135 | **fixes** 136 | * [[`a165ac1`](http://github.com/eggjs/egg-sequelize/commit/a165ac18014d8d53c6b9ba0c13634a2820a7c1ba)] - fix: remove bin file (dead-horse <>) 137 | 138 | 4.0.0 / 2018-08-13 139 | ================== 140 | 141 | **features** 142 | * [[`654fdf9`](http://github.com/eggjs/egg-sequelize/commit/654fdf91a82ff68d54906f122e7f6b22007074ca)] - feat: support config.ignore (dead-horse <>) 143 | * [[`4c727a6`](http://github.com/eggjs/egg-sequelize/commit/4c727a66a2e7f8d0e18949bf1e90b87c2f6e217c)] - feat: support config.Sequelize (dead-horse <>) 144 | * [[`86d660d`](http://github.com/eggjs/egg-sequelize/commit/86d660d5557aa33aa113c91a1468c997a7dc2cc3)] - feat: support config.datasources to define more database (dead-horse <>) 145 | 146 | **others** 147 | * [[`976ab6c`](http://github.com/eggjs/egg-sequelize/commit/976ab6cf8061c7926ebbc050d835581ec4434a34)] - deps: remove unused dependencies (dead-horse <>) 148 | * [[`0e59892`](http://github.com/eggjs/egg-sequelize/commit/0e598926780401379dac70a2530b844bf35b250f)] - docs: add migration in readme (dead-horse <>) 149 | * [[`4d1ac62`](http://github.com/eggjs/egg-sequelize/commit/4d1ac6247e337e110be31e46d58fb6bc4c0a25ae)] - docs: add multiple datasources in readme (dead-horse <>) 150 | * [[`bfd0c26`](http://github.com/eggjs/egg-sequelize/commit/bfd0c26dc451fee3a7adfc30268e3036cef8fb92)] - refactor: remove sequelize-cli integration (dead-horse <>) 151 | * [[`a5670ca`](http://github.com/eggjs/egg-sequelize/commit/a5670ca8fdc5c6134bfa56a761b582eadc690f45)] - refactor: use async function and only support egg@2 (dead-horse <>) 152 | * [[`e4b525e`](http://github.com/eggjs/egg-sequelize/commit/e4b525e18b0f39f517a560de7fe9bf819bddeddf)] - refactor: remove log rewrite (dead-horse <>) 153 | 154 | 3.1.5 / 2018-07-03 155 | ================== 156 | 157 | **features** 158 | * [[`987e394`](http://github.com/eggjs/egg-sequelize/commit/987e3940da2bc392e1bb6da77055942c3ecf5b0e)] - feat(sequelize-cli): upgrade to 4.0.0 (#52) (Lpmvb <>) 159 | 160 | **others** 161 | * [[`ff79aba`](http://github.com/eggjs/egg-sequelize/commit/ff79aba467d13efbc017e08247925fcb93e6aaff)] - Retry 3 times on startup when database connect fail in temporary, to avoid Egg start failed. (#57) (Jason Lee <>) 162 | 163 | 3.1.4 / 2018-05-02 164 | ================== 165 | 166 | **fixes** 167 | * [[`1fb8585`](http://github.com/eggjs/egg-sequelize/commit/1fb858533132efb1ff2b2409ffa3656cb7b48b21)] - fix: add index.d.ts to pkg.files (#51) (duncup <>) 168 | 169 | 3.1.3 / 2018-04-13 170 | ================== 171 | 172 | **fixes** 173 | * [[`c8a1c60`](http://github.com/eggjs/egg-sequelize/commit/c8a1c60244606158b1b3a4193433e764a60e0966)] - fix: using `sequelize.Options` for sequelize config. (#48) (ZhengFang <<215566435@qq.com>>) 174 | 175 | **others** 176 | * [[`2d63647`](http://github.com/eggjs/egg-sequelize/commit/2d6364795d62d91b7d498b2c3ec6fa3be8dc9b58)] - chore:TypeScript support. (#47) (duncup <>) 177 | 178 | 3.1.2 / 2018-02-27 179 | ================== 180 | 181 | * fix: auto create cli folder (#41) 182 | * docs: fix demo code (#42) 183 | 184 | 3.1.1 / 2018-02-06 185 | ================== 186 | 187 | * fix: EGG_SERVER_ENV support for seuqlieze cli (#40) 188 | * docs: fix db sync doc (#31) 189 | * docs: add README for `app.model` (#34) 190 | * docs(README): fix a typo (#33) 191 | 192 | 3.1.0 / 2017-08-03 193 | ================== 194 | 195 | * deps: update dependencies (#26) 196 | * refactor: rewrite cli script with plain js instead of Shell to support multi-platform. (#25) 197 | * docs: add a migration example for show up use `co.wrap`. (#24) 198 | * docs: fix migration url (#22) 199 | * docs: update history (#21) 200 | 201 | 3.0.1 / 2017-06-19 202 | ================== 203 | 204 | * fix: init associate should after load of models (#20) 205 | 206 | 3.0.0 / 2017-06-19 207 | ================== 208 | 209 | * feat: Upgrade Sequelize V4. (#18) 210 | * docs: add sync docs (#17) 211 | * docs(readme): fix the full example with association (#16) 212 | 213 | 2.1.4 / 2017-05-11 214 | ================== 215 | 216 | * fix(migration): always use production config (#14) 217 | 218 | 2.1.3 / 2017-05-11 219 | ================== 220 | 221 | * fix: Migration load config.seuqelize for function type config support. 222 | 223 | 2.1.2 / 2017-05-11 224 | ================== 225 | 226 | * fix: egg-sequelize bin to find correct sequelize-cli path in node_modules. 227 | 228 | 2.1.1 / 2017-05-10 229 | ================== 230 | 231 | * feat: add `egg-sequelize` bin for Sequelize migrations support. (#11) 232 | 233 | 2.0.2 / 2017-04-27 234 | ================== 235 | 236 | * fix: ignore non Sequelize files in app/model path for Model loader. (#10) 237 | * docs: add Suggestions and License (#8) 238 | * feat: use underscore style column name as default (#7) 239 | * docs: add info about how to enable sequelize plugin (#6) 240 | 241 | 2.0.1 / 2017-03-14 242 | ================== 243 | 244 | * fix: Allow all of Sequelize options in `config.sequelize` (#5) 245 | 246 | 2.0.0 / 2017-03-13 247 | ================== 248 | 249 | * feat: [BREAKING_CHANGE] Update default Sequelize configs (#4) 250 | 251 | 1.0.0 / 2017-02-19 252 | ================== 253 | 254 | * chore: complete unittest (#2) 255 | * feat: use loader API to load models (#3) 256 | 257 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Alibaba Group Holding Limited and other contributors. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # egg-sequelize 2 | 3 | [Sequelize](http://sequelizejs.com) plugin for Egg.js. 4 | 5 | > NOTE: This plugin just for integrate Sequelize into Egg.js, more documentation please visit http://sequelizejs.com. 6 | 7 | [![NPM version][npm-image]][npm-url] 8 | [![build status][travis-image]][travis-url] 9 | [![Test coverage][codecov-image]][codecov-url] 10 | [![David deps][david-image]][david-url] 11 | [![Known Vulnerabilities][snyk-image]][snyk-url] 12 | [![npm download][download-image]][download-url] 13 | 14 | [npm-image]: https://img.shields.io/npm/v/egg-sequelize.svg?style=flat-square 15 | [npm-url]: https://npmjs.org/package/egg-sequelize 16 | [travis-image]: https://img.shields.io/travis/eggjs/egg-sequelize.svg?style=flat-square 17 | [travis-url]: https://travis-ci.org/eggjs/egg-sequelize 18 | [codecov-image]: https://codecov.io/gh/eggjs/egg-sequelize/branch/master/graph/badge.svg 19 | [codecov-url]: https://codecov.io/gh/eggjs/egg-sequelize 20 | [david-image]: https://img.shields.io/david/eggjs/egg-sequelize.svg?style=flat-square 21 | [david-url]: https://david-dm.org/eggjs/egg-sequelize 22 | [snyk-image]: https://snyk.io/test/npm/egg-sequelize/badge.svg?style=flat-square 23 | [snyk-url]: https://snyk.io/test/npm/egg-sequelize 24 | [download-image]: https://img.shields.io/npm/dm/egg-sequelize.svg?style=flat-square 25 | [download-url]: https://npmjs.org/package/egg-sequelize 26 | 27 | ## Install 28 | 29 | ```bash 30 | $ npm i --save egg-sequelize 31 | $ npm install --save mysql2 # For both mysql and mariadb dialects 32 | 33 | # Or use other database backend. 34 | $ npm install --save pg pg-hstore # PostgreSQL 35 | $ npm install --save tedious # MSSQL 36 | ``` 37 | 38 | ## Usage & configuration 39 | 40 | > Read the [tutorials](https://eggjs.org/en/tutorials/sequelize.html) to see a full example. 41 | 42 | - Enable plugin in `config/plugin.js` 43 | 44 | ``` js 45 | exports.sequelize = { 46 | enable: true, 47 | package: 'egg-sequelize' 48 | } 49 | ``` 50 | 51 | - Edit your own configurations in `conif/config.{env}.js` 52 | 53 | ```js 54 | exports.sequelize = { 55 | dialect: 'mysql', // support: mysql, mariadb, postgres, mssql 56 | database: 'test', 57 | host: 'localhost', 58 | port: 3306, 59 | username: 'root', 60 | password: '', 61 | // delegate: 'myModel', // load all models to `app[delegate]` and `ctx[delegate]`, default to `model` 62 | // baseDir: 'my_model', // load all files in `app/${baseDir}` as models, default to `model` 63 | // exclude: 'index.js', // ignore `app/${baseDir}/index.js` when load models, support glob and array 64 | // more sequelize options 65 | }; 66 | ``` 67 | 68 | You can also use the `connection uri` to configure the connection: 69 | 70 | ```js 71 | exports.sequelize = { 72 | dialect: 'mysql', // support: mysql, mariadb, postgres, mssql 73 | connectionUri: 'mysql://root:@127.0.0.1:3306/test', 74 | // delegate: 'myModel', // load all models to `app[delegate]` and `ctx[delegate]`, default to `model` 75 | // baseDir: 'my_model', // load all files in `app/${baseDir}` as models, default to `model` 76 | // exclude: 'index.js', // ignore `app/${baseDir}/index.js` when load models, support glob and array 77 | // more sequelize options 78 | }; 79 | ``` 80 | 81 | egg-sequelize has a default sequelize options below 82 | 83 | ```js 84 | { 85 | delegate: 'model', 86 | baseDir: 'model', 87 | logging(...args) { 88 | // if benchmark enabled, log used 89 | const used = typeof args[1] === 'number' ? `[${args[1]}ms]` : ''; 90 | app.logger.info('[egg-sequelize]%s %s', used, args[0]); 91 | }, 92 | host: 'localhost', 93 | port: 3306, 94 | username: 'root', 95 | benchmark: true, 96 | define: { 97 | freezeTableName: false, 98 | underscored: true, 99 | }, 100 | }; 101 | ``` 102 | 103 | More documents please refer to [Sequelize.js](http://docs.sequelizejs.com/manual/installation/usage.html) 104 | 105 | ## Model files 106 | 107 | Please put models under `app/model` dir by default. 108 | 109 | ## Conventions 110 | 111 | | model file | class name | 112 | | ---------------- | ----------------------- | 113 | | `user.js` | `app.model.User` | 114 | | `person.js` | `app.model.Person` | 115 | | `user_group.js` | `app.model.UserGroup` | 116 | | `user/profile.js`| `app.model.User.Profile`| 117 | 118 | - Tables always has timestamp fields: `created_at datetime`, `updated_at datetime`. 119 | - Use underscore style column name, for example: `user_id`, `comments_count`. 120 | 121 | ## Examples 122 | 123 | ### Standard 124 | 125 | Define a model first. 126 | 127 | > NOTE: `options.delegate` default to `model`, so `app.model` is an [Instance of Sequelize](http://docs.sequelizejs.com/class/lib/sequelize.js~Sequelize.html#instance-constructor-constructor), so you can use methods like: `app.model.sync, app.model.query ...` 128 | 129 | ```js 130 | // app/model/user.js 131 | 132 | module.exports = app => { 133 | const { STRING, INTEGER, DATE } = app.Sequelize; 134 | 135 | const User = app.model.define('user', { 136 | login: STRING, 137 | name: STRING(30), 138 | password: STRING(32), 139 | age: INTEGER, 140 | last_sign_in_at: DATE, 141 | created_at: DATE, 142 | updated_at: DATE, 143 | }); 144 | 145 | User.findByLogin = async function(login) { 146 | return await this.findOne({ 147 | where: { 148 | login: login 149 | } 150 | }); 151 | } 152 | 153 | // don't use arraw function 154 | User.prototype.logSignin = async function() { 155 | return await this.update({ last_sign_in_at: new Date() }); 156 | } 157 | 158 | return User; 159 | }; 160 | 161 | ``` 162 | 163 | Now you can use it in your controller: 164 | 165 | ```js 166 | // app/controller/user.js 167 | class UserController extends Controller { 168 | async index() { 169 | const users = await this.ctx.model.User.findAll(); 170 | this.ctx.body = users; 171 | } 172 | 173 | async show() { 174 | const user = await this.ctx.model.User.findByLogin(this.ctx.params.login); 175 | await user.logSignin(); 176 | this.ctx.body = user; 177 | } 178 | } 179 | ``` 180 | 181 | ### Associate 182 | 183 | Define all your associations in `Model.associate()` and egg-sequelize will execute it after all models loaded. See example below. 184 | 185 | ### Multiple Datasources 186 | 187 | egg-sequelize support load multiple datasources independently. You can use `config.sequelize.datasources` to configure and load multiple datasources. 188 | 189 | ```js 190 | // config/config.default.js 191 | exports.sequelize = { 192 | datasources: [ 193 | { 194 | delegate: 'model', // load all models to app.model and ctx.model 195 | baseDir: 'model', // load models from `app/model/*.js` 196 | database: 'biz', 197 | // other sequelize configurations 198 | }, 199 | { 200 | delegate: 'admninModel', // load all models to app.adminModel and ctx.adminModel 201 | baseDir: 'admin_model', // load models from `app/admin_model/*.js` 202 | database: 'admin', 203 | // other sequelize configurations 204 | }, 205 | ], 206 | }; 207 | ``` 208 | 209 | Then we can define model like this: 210 | 211 | ```js 212 | // app/model/user.js 213 | module.exports = app => { 214 | const { STRING, INTEGER, DATE } = app.Sequelize; 215 | 216 | const User = app.model.define('user', { 217 | login: STRING, 218 | name: STRING(30), 219 | password: STRING(32), 220 | age: INTEGER, 221 | last_sign_in_at: DATE, 222 | created_at: DATE, 223 | updated_at: DATE, 224 | }); 225 | 226 | return User; 227 | }; 228 | 229 | // app/admin_model/user.js 230 | module.exports = app => { 231 | const { STRING, INTEGER, DATE } = app.Sequelize; 232 | 233 | const User = app.adminModel.define('user', { 234 | login: STRING, 235 | name: STRING(30), 236 | password: STRING(32), 237 | age: INTEGER, 238 | last_sign_in_at: DATE, 239 | created_at: DATE, 240 | updated_at: DATE, 241 | }); 242 | 243 | return User; 244 | }; 245 | ``` 246 | 247 | If you define the same model for different datasource, the same model file will be excute twice for different database, so we can use the secound argument to get the sequelize instance: 248 | 249 | ```js 250 | // app/model/user.js 251 | // if this file will load multiple times for different datasource 252 | // we can use the secound argument to get the sequelize instance 253 | module.exports = (app, model) => { 254 | const { STRING, INTEGER, DATE } = app.Sequelize; 255 | 256 | const User = model.define('user', { 257 | login: STRING, 258 | name: STRING(30), 259 | password: STRING(32), 260 | age: INTEGER, 261 | last_sign_in_at: DATE, 262 | created_at: DATE, 263 | updated_at: DATE, 264 | }); 265 | 266 | return User; 267 | }; 268 | ``` 269 | 270 | ### Customize Sequelize 271 | 272 | By default, egg-sequelize will use sequelize@5, you can cusomize sequelize version by pass sequelize instance with `config.sequelize.Sequelize` like this: 273 | 274 | ```js 275 | // config/config.default.js 276 | exports.sequelize = { 277 | Sequelize: require('sequelize'), 278 | }; 279 | ``` 280 | 281 | ### Full example 282 | 283 | ```js 284 | // app/model/post.js 285 | 286 | module.exports = app => { 287 | const { STRING, INTEGER, DATE } = app.Sequelize; 288 | 289 | const Post = app.model.define('Post', { 290 | name: STRING(30), 291 | user_id: INTEGER, 292 | created_at: DATE, 293 | updated_at: DATE, 294 | }); 295 | 296 | Post.associate = function() { 297 | app.model.Post.belongsTo(app.model.User, { as: 'user' }); 298 | } 299 | 300 | return Post; 301 | }; 302 | ``` 303 | 304 | 305 | ```js 306 | // app/controller/post.js 307 | class PostController extends Controller { 308 | async index() { 309 | const posts = await this.ctx.model.Post.findAll({ 310 | attributes: [ 'id', 'user_id' ], 311 | include: { model: this.ctx.model.User, as: 'user' }, 312 | where: { status: 'publish' }, 313 | order: 'id desc', 314 | }); 315 | 316 | this.ctx.body = posts; 317 | } 318 | 319 | async show() { 320 | const post = await this.ctx.model.Post.findByPk(this.params.id); 321 | const user = await post.getUser(); 322 | post.setDataValue('user', user); 323 | this.ctx.body = post; 324 | } 325 | 326 | async destroy() { 327 | const post = await this.ctx.model.Post.findByPk(this.params.id); 328 | await post.destroy(); 329 | this.ctx.body = { success: true }; 330 | } 331 | } 332 | ``` 333 | 334 | ## Sync model to db 335 | 336 | **We strongly recommend you to use [Sequelize - Migrations](http://docs.sequelizejs.com/manual/tutorial/migrations.html) to create or migrate database.** 337 | 338 | **This code should only be used in development.** 339 | 340 | ```js 341 | // {app_root}/app.js 342 | module.exports = app => { 343 | if (app.config.env === 'local' || app.config.env === 'unittest') { 344 | app.beforeStart(async () => { 345 | await app.model.sync({force: true}); 346 | }); 347 | } 348 | }; 349 | ``` 350 | 351 | ## Migration 352 | 353 | Using [sequelize-cli](https://github.com/sequelize/cli) to help manage your database, data structures and seed data. Please read [Sequelize - Migrations](http://docs.sequelizejs.com/manual/tutorial/migrations.html) to learn more infomations. 354 | 355 | ## Recommended example 356 | 357 | - https://github.com/eggjs/examples/tree/master/sequelize/ 358 | 359 | ## Questions & Suggestions 360 | 361 | Please open an issue [here](https://github.com/eggjs/egg/issues). 362 | 363 | ## License 364 | 365 | [MIT](LICENSE) 366 | 367 | -------------------------------------------------------------------------------- /agent.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = agent => { 4 | require('./lib/loader')(agent); 5 | }; 6 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = app => { 4 | require('./lib/loader')(app); 5 | }; 6 | 7 | -------------------------------------------------------------------------------- /config/config.default.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | exports.sequelize = { 4 | dialect: 'mysql', 5 | database: '', 6 | host: 'localhost', 7 | port: 3306, 8 | username: 'root', 9 | password: '', 10 | 11 | // support customize your own Squelize 12 | // Sequelize: require('sequelize'), // v5 or v3 13 | 14 | // support multi datasources by config.sequelize.datasources 15 | // datasources: [ 16 | // { 17 | // delegate: 'model', // lood to `app[delegate]` 18 | // baseDir: 'model', // models in `app/${model}` 19 | // // other sequelize configurations 20 | // }, 21 | // { 22 | // delegate: 'sequelize', // lood to `app[delegate]` 23 | // baseDir: 'sequelize', // models in `app/${model}` 24 | // // other sequelize configurations 25 | // }, 26 | // ], 27 | }; 28 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | import * as sequelize from 'sequelize'; 2 | 3 | interface EggSequelizeOptions extends sequelize.Options { 4 | /** 5 | * load all models to `app[delegate]` and `ctx[delegate]`, default to `model` 6 | */ 7 | delegate?: string; 8 | 9 | /** 10 | * load models from `app/model/*.js` 11 | */ 12 | baseDir?: string; 13 | 14 | /** 15 | * ignore `app/${baseDir}/index.js` when load models, support glob and array 16 | */ 17 | exclude?: string | Array; 18 | 19 | /** 20 | * A full database URI 21 | * @example 22 | * `connectionUri:"mysql://localhost:3306/database"` 23 | */ 24 | connectionUri?: string; 25 | } 26 | 27 | interface DataSources { 28 | datasources: EggSequelizeOptions[]; 29 | } 30 | 31 | declare module 'egg' { 32 | interface IModel extends sequelize.Sequelize, PlainObject {} 33 | 34 | // extend app 35 | interface Application { 36 | Sequelize: typeof sequelize; 37 | model: IModel; 38 | } 39 | 40 | // extend context 41 | interface Context { 42 | model: IModel; 43 | } 44 | 45 | // extend your config 46 | interface EggAppConfig { 47 | sequelize: EggSequelizeOptions | DataSources; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /lib/loader.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const path = require('path'); 4 | const sleep = require('mz-modules/sleep'); 5 | const AUTH_RETRIES = Symbol('authenticateRetries'); 6 | 7 | module.exports = app => { 8 | const defaultConfig = { 9 | delegate: 'model', 10 | baseDir: 'model', 11 | logging(...args) { 12 | // if benchmark enabled, log used 13 | const used = typeof args[1] === 'number' ? `(${args[1]}ms)` : ''; 14 | app.logger.info('[egg-sequelize]%s %s', used, args[0]); 15 | }, 16 | host: 'localhost', 17 | port: 3306, 18 | username: 'root', 19 | benchmark: true, 20 | define: { 21 | freezeTableName: false, 22 | underscored: true, 23 | }, 24 | }; 25 | 26 | const config = app.config.sequelize; 27 | // support customize sequelize 28 | app.Sequelize = config.Sequelize || require('sequelize'); 29 | 30 | const databases = []; 31 | if (!config.datasources) { 32 | databases.push(loadDatabase(Object.assign({}, defaultConfig, config))); 33 | } else { 34 | config.datasources.forEach(datasource => { 35 | databases.push(loadDatabase(Object.assign({}, defaultConfig, datasource))); 36 | }); 37 | } 38 | 39 | app.beforeStart(async () => { 40 | await Promise.all(databases.map(database => authenticate(database))); 41 | }); 42 | 43 | /** 44 | * load database to app[config.delegate] 45 | * @param {Object} config config for load 46 | * - delegate: load model to app[delegate] 47 | * - baseDir: where model located 48 | * - other sequelize configures(database username, password, etc...) 49 | * @return {Object} sequelize instance 50 | */ 51 | function loadDatabase(config = {}) { 52 | if (typeof config.ignore === 'string' || Array.isArray(config.ignore)) { 53 | app.deprecate(`[egg-sequelize] if you want to exclude ${config.ignore} when load models, please set to config.sequelize.exclude instead of config.sequelize.ignore`); 54 | config.exclude = config.ignore; 55 | delete config.ignore; 56 | } 57 | 58 | const sequelize = config.connectionUri ? 59 | new app.Sequelize(config.connectionUri, config) : 60 | new app.Sequelize(config.database, config.username, config.password, config); 61 | 62 | const delegate = config.delegate.split('.'); 63 | const len = delegate.length; 64 | 65 | let model = app; 66 | let context = app.context; 67 | for (let i = 0; i < len - 1; i++) { 68 | model = model[delegate[i]] = model[delegate[i]] || {}; 69 | context = context[delegate[i]] = context[delegate[i]] || {}; 70 | } 71 | 72 | if (model[delegate[len - 1]]) { 73 | throw new Error(`[egg-sequelize] app[${config.delegate}] is already defined`); 74 | } 75 | 76 | Object.defineProperty(model, delegate[len - 1], { 77 | value: sequelize, 78 | writable: false, 79 | configurable: true, 80 | }); 81 | 82 | const DELEGATE = Symbol(`context#sequelize_${config.delegate}`); 83 | Object.defineProperty(context, delegate[len - 1], { 84 | get() { 85 | // context.model is different with app.model 86 | // so we can change the properties of ctx.model.xxx 87 | if (!this[DELEGATE]) { 88 | this[DELEGATE] = Object.create(model[delegate[len - 1]]); 89 | this[DELEGATE].ctx = this; 90 | } 91 | return this[DELEGATE]; 92 | }, 93 | configurable: true, 94 | }); 95 | 96 | const modelDir = path.join(app.baseDir, 'app', config.baseDir); 97 | 98 | const models = []; 99 | const target = Symbol(config.delegate); 100 | 101 | app.loader.loadToApp(modelDir, target, { 102 | caseStyle: 'upper', 103 | ignore: config.exclude, 104 | filter(model) { 105 | if (!model || !model.sequelize) return false; 106 | models.push(model); 107 | return true; 108 | }, 109 | initializer(factory) { 110 | if (typeof factory === 'function') { 111 | return factory(app, sequelize); 112 | } 113 | }, 114 | }); 115 | Object.assign(model[delegate[len - 1]], app[target]); 116 | 117 | models.forEach(model => { 118 | typeof model.associate === 'function' && model.associate(); 119 | }); 120 | 121 | return model[delegate[len - 1]]; 122 | } 123 | 124 | /** 125 | * Authenticate to test Database connection. 126 | * 127 | * This method will retry 3 times when database connect fail in temporary, to avoid Egg start failed. 128 | * @param {Application} database instance of sequelize 129 | */ 130 | async function authenticate(database) { 131 | database[AUTH_RETRIES] = database[AUTH_RETRIES] || 0; 132 | 133 | try { 134 | await database.authenticate(); 135 | } catch (e) { 136 | if (database[AUTH_RETRIES] >= 3) throw e; 137 | 138 | // sleep 1s to retry, max 3 times 139 | database[AUTH_RETRIES] += 1; 140 | app.logger.warn(`Sequelize Error: ${e.message}, sleep 1 seconds to retry...`); 141 | await sleep(1000); 142 | await authenticate(database); 143 | } 144 | } 145 | }; 146 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "egg-sequelize", 3 | "version": "6.0.0", 4 | "description": "egg Sequelize plugin", 5 | "eggPlugin": { 6 | "name": "sequelize" 7 | }, 8 | "keywords": [ 9 | "egg", 10 | "sequelize", 11 | "egg-plugin", 12 | "eggPlugin", 13 | "orm" 14 | ], 15 | "dependencies": { 16 | "@types/sequelize": "^4.27.24", 17 | "mz-modules": "^2.1.0", 18 | "sequelize": "^6.0.0" 19 | }, 20 | "devDependencies": { 21 | "autod": "^3.0.1", 22 | "egg": "^2.10.0", 23 | "egg-bin": "^4.13.1", 24 | "egg-mock": "^3.19.3", 25 | "eslint": "^5.3.0", 26 | "eslint-config-egg": "^7.0.0", 27 | "mysql2": "^1.6.1" 28 | }, 29 | "engines": { 30 | "node": ">=10.0.0" 31 | }, 32 | "scripts": { 33 | "test": "npm run lint -- --fix && npm run test-local", 34 | "test-local": "egg-bin test", 35 | "cov": "egg-bin cov", 36 | "lint": "eslint .", 37 | "ci": "npm run lint && npm run cov", 38 | "autod": "autod" 39 | }, 40 | "files": [ 41 | "app", 42 | "config", 43 | "agent.js", 44 | "lib", 45 | "app.js", 46 | "index.d.ts" 47 | ], 48 | "repository": { 49 | "type": "git", 50 | "url": "git+https://github.com/eggjs/egg-sequelize.git" 51 | }, 52 | "bugs": { 53 | "url": "https://github.com/eggjs/egg/issues" 54 | }, 55 | "homepage": "https://github.com/eggjs/egg-sequelize#readme", 56 | "author": "jtyjty99999", 57 | "license": "MIT" 58 | } 59 | -------------------------------------------------------------------------------- /test/connection-uri.test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const coffee = require('coffee'); 4 | const path = require('path'); 5 | const mm = require('egg-mock'); 6 | const assert = require('assert'); 7 | 8 | describe('connection-uri', () => { 9 | let app; 10 | 11 | before(() => { 12 | console.log('compiling...'); 13 | return coffee 14 | .fork(require.resolve('typescript/bin/tsc'), [ 15 | '-p', 16 | path.resolve(__dirname, './fixtures/apps/connection-uri/tsconfig.json'), 17 | ]) 18 | .debug() 19 | .expect('code', 0) 20 | .end(); 21 | }); 22 | 23 | 24 | before(() => { 25 | app = mm.app({ 26 | baseDir: 'apps/connection-uri', 27 | }); 28 | return app.ready(); 29 | }); 30 | before(() => app.model.sync({ force: true })); 31 | 32 | after(mm.restore); 33 | 34 | describe('Base', () => { 35 | it('sequelize init success', () => { 36 | assert.ok(app.model); 37 | assert.ok(app.model.User); 38 | }); 39 | }); 40 | }); 41 | -------------------------------------------------------------------------------- /test/datasources.test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const mm = require('egg-mock'); 5 | 6 | describe('test/datasources.test.js', () => { 7 | let app; 8 | 9 | before(() => { 10 | app = mm.app({ 11 | baseDir: 'apps/datasources', 12 | }); 13 | return app.ready(); 14 | }); 15 | before(async () => { 16 | await app.model.sync({ force: true }); 17 | await app.sequelize.sync({ force: true }); 18 | await app.subproperty.a.sync({ force: true }); 19 | await app.subproperty.b.sync({ force: true }); 20 | }); 21 | 22 | after(mm.restore); 23 | 24 | describe('Base', () => { 25 | it('sequelize init success', () => { 26 | assert(app.model); 27 | assert(app.sequelize); 28 | assert(app.Sequelize); 29 | assert(app.subproperty.a); 30 | assert(app.subproperty.b); 31 | }); 32 | 33 | it('ctx model property getter', () => { 34 | const ctx = app.mockContext(); 35 | assert(ctx.model); 36 | assert(ctx.model.User); 37 | assert(ctx.model.Monkey); 38 | assert(ctx.model.Person); 39 | assert(ctx.sequelize); 40 | assert(ctx.sequelize.User); 41 | assert(ctx.sequelize.Monkey); 42 | assert(!ctx.sequelize.Person); // ignored 43 | assert(ctx.model.User !== ctx.sequelize.User); 44 | assert(ctx.subproperty.a); 45 | assert(ctx.subproperty.a.User); 46 | assert(ctx.subproperty.a.Monkey); 47 | assert(ctx.subproperty.a.Person); 48 | assert(ctx.subproperty.b); 49 | assert(ctx.subproperty.b.User); 50 | assert(ctx.subproperty.b.Monkey); 51 | assert(ctx.subproperty.b.Person); 52 | assert(ctx.subproperty.a.User !== ctx.subproperty.b.User); 53 | }); 54 | 55 | it('has right tableName', () => { 56 | assert(app.model.Person.tableName === 'people'); 57 | assert(app.model.User.tableName === 'users'); 58 | assert(app.model.Monkey.tableName === 'the_monkeys'); 59 | assert(app.sequelize.User.tableName === 'users'); 60 | assert(app.sequelize.Monkey.tableName === 'the_monkeys'); 61 | assert(app.subproperty.a.Monkey.tableName === 'the_monkeys'); 62 | assert(app.subproperty.b.Monkey.tableName === 'the_monkeys'); 63 | }); 64 | }); 65 | 66 | describe('Test model', () => { 67 | it('User.test method work', async function() { 68 | await app.model.User.test(); 69 | }); 70 | 71 | it('should work timestramp', async function() { 72 | let user = await app.model.User.create({ name: 'huacnlee' }); 73 | assert(user.isNewRecord === false); 74 | assert(user.name === 'huacnlee'); 75 | assert(user.created_at !== null); 76 | assert(user.updated_at !== null); 77 | 78 | user = await app.sequelize.User.create({ name: 'huacnlee' }); 79 | assert(user.isNewRecord === false); 80 | assert(user.name === 'huacnlee'); 81 | assert(user.created_at !== null); 82 | assert(user.updated_at !== null); 83 | }); 84 | }); 85 | 86 | describe('Associate', () => { 87 | it('ctx model associate init success', () => { 88 | const ctx = app.mockContext(); 89 | assert.ok(ctx.model); 90 | assert.ok(ctx.model.User); 91 | assert.ok(ctx.model.User.prototype.hasPosts); 92 | assert.ok(ctx.model.Post); 93 | 94 | assert.ok(ctx.sequelize); 95 | assert.ok(ctx.sequelize.User); 96 | assert.ok(ctx.sequelize.User.prototype.hasPosts); 97 | assert.ok(ctx.sequelize.Post); 98 | 99 | assert.ok(ctx.subproperty.a); 100 | assert.ok(ctx.subproperty.a.User); 101 | assert.ok(ctx.subproperty.a.User.prototype.hasPosts); 102 | assert.ok(ctx.subproperty.a.Post); 103 | 104 | assert.ok(ctx.subproperty.a); 105 | assert.ok(ctx.subproperty.a.User); 106 | assert.ok(ctx.subproperty.a.User.prototype.hasPosts); 107 | assert.ok(ctx.subproperty.a.Post); 108 | }); 109 | }); 110 | }); 111 | -------------------------------------------------------------------------------- /test/datasources_same_dir.test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const mm = require('egg-mock'); 5 | 6 | describe('test/datasources_same_dir.test.js', () => { 7 | let app; 8 | 9 | before(() => { 10 | app = mm.app({ 11 | baseDir: 'apps/datasources-same-dir', 12 | }); 13 | return app.ready(); 14 | }); 15 | before(async () => { 16 | await app.model.sync({ force: true }); 17 | await app.sequelize.sync({ force: true }); 18 | }); 19 | 20 | after(mm.restore); 21 | 22 | describe('Base', () => { 23 | it('sequelize init success', () => { 24 | assert(app.model); 25 | assert(app.sequelize); 26 | assert(app.Sequelize); 27 | }); 28 | 29 | it('ctx model property getter', () => { 30 | const ctx = app.mockContext(); 31 | assert(ctx.model); 32 | assert(ctx.model.User); 33 | assert(ctx.model.Monkey); 34 | assert(ctx.model.Person); 35 | assert(ctx.sequelize); 36 | assert(ctx.sequelize.User); 37 | assert(ctx.sequelize.Monkey); 38 | assert(!ctx.sequelize.Person); // ignored 39 | assert(ctx.model.User !== ctx.sequelize.User); 40 | }); 41 | 42 | it('has right tableName', () => { 43 | assert(app.model.Person.tableName === 'people'); 44 | assert(app.model.User.tableName === 'users'); 45 | assert(app.model.Monkey.tableName === 'the_monkeys'); 46 | assert(app.sequelize.User.tableName === 'users'); 47 | assert(app.sequelize.Monkey.tableName === 'the_monkeys'); 48 | }); 49 | }); 50 | 51 | describe('Test model', () => { 52 | it('User.test method work', async function() { 53 | await app.model.User.test(); 54 | }); 55 | 56 | it('should work timestramp', async function() { 57 | let user = await app.model.User.create({ name: 'huacnlee' }); 58 | assert(user.isNewRecord === false); 59 | assert(user.name === 'huacnlee'); 60 | assert(user.created_at !== null); 61 | assert(user.updated_at !== null); 62 | 63 | user = await app.sequelize.User.create({ name: 'huacnlee' }); 64 | assert(user.isNewRecord === false); 65 | assert(user.name === 'huacnlee'); 66 | assert(user.created_at !== null); 67 | assert(user.updated_at !== null); 68 | }); 69 | }); 70 | 71 | describe('Associate', () => { 72 | it('ctx model associate init success', () => { 73 | const ctx = app.mockContext(); 74 | assert.ok(ctx.model); 75 | assert.ok(ctx.model.User); 76 | assert.ok(ctx.model.User.prototype.hasPosts); 77 | assert.ok(ctx.model.Post); 78 | 79 | assert.ok(ctx.sequelize); 80 | assert.ok(ctx.sequelize.User); 81 | assert.ok(ctx.sequelize.User.prototype.hasPosts); 82 | assert.ok(ctx.sequelize.Post); 83 | }); 84 | }); 85 | }); 86 | -------------------------------------------------------------------------------- /test/fixtures/apps/connection-uri/app/controller/home.ts: -------------------------------------------------------------------------------- 1 | import { Controller } from 'egg'; 2 | 3 | export default class HomeController extends Controller { 4 | async index() { 5 | const { ctx } = this; 6 | await ctx.model.Monkey.findUser(); 7 | await ctx.model.User.associate(); 8 | await ctx.model.User.test(); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /test/fixtures/apps/connection-uri/app/model/monkey.ts: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | import { Application } from 'egg'; 4 | 5 | export default function(app: Application) { 6 | const { STRING, INTEGER, DATE } = app.Sequelize; 7 | const Monkey = app.model.define('monkey', { 8 | name: { 9 | type: STRING, 10 | allowNull: false, 11 | }, 12 | user_id: INTEGER, 13 | created_at: DATE, 14 | updated_at: DATE, 15 | }, { 16 | tableName: 'the_monkeys', 17 | }); 18 | 19 | return class extends Monkey { 20 | static async findUser() { 21 | return app.model.User.findOne({ where: { id: '123' } }); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /test/fixtures/apps/connection-uri/app/model/user.ts: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | import { Application } from 'egg'; 4 | import assert = require('assert'); 5 | 6 | export default function(app: Application) { 7 | const { STRING, INTEGER } = app.Sequelize; 8 | const User = app.model.define('user', { 9 | name: STRING(30), 10 | age: INTEGER, 11 | }); 12 | 13 | return class extends User { 14 | static async associate() { 15 | assert.ok(app.model.User); 16 | } 17 | 18 | static async test() { 19 | assert(app.config); 20 | assert(app.model.User === this); 21 | const monkey = await app.model.Monkey.create({ name: 'The Monkey' }); 22 | assert(monkey.isNewRecord === false); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /test/fixtures/apps/connection-uri/app/router.ts: -------------------------------------------------------------------------------- 1 | import { Application } from 'egg'; 2 | 3 | export default (app: Application) => { 4 | const { controller } = app; 5 | 6 | app.get('/', controller.home.index); 7 | } 8 | -------------------------------------------------------------------------------- /test/fixtures/apps/connection-uri/config/config.default.ts: -------------------------------------------------------------------------------- 1 | import * as path from 'path'; 2 | import { EggAppInfo, EggAppConfig, PowerPartial } from 'egg'; 3 | 4 | export default (appInfo: EggAppInfo) => { 5 | const config = {} as PowerPartial; 6 | 7 | config.keys = '123123'; 8 | 9 | config.sequelize = { 10 | } 11 | 12 | return config; 13 | } 14 | -------------------------------------------------------------------------------- /test/fixtures/apps/connection-uri/config/config.local.ts: -------------------------------------------------------------------------------- 1 | import * as path from 'path'; 2 | import { EggAppInfo, EggAppConfig, PowerPartial } from 'egg'; 3 | 4 | export default (appInfo: EggAppInfo) => { 5 | const config = {} as PowerPartial; 6 | 7 | config.keys = '123123'; 8 | 9 | config.sequelize = { 10 | connectionUri: 'mysql://root:@127.0.0.1:3306/test4', 11 | dialect: 'mysql' 12 | } 13 | 14 | return config; 15 | } 16 | -------------------------------------------------------------------------------- /test/fixtures/apps/connection-uri/config/config.unittest.ts: -------------------------------------------------------------------------------- 1 | import * as path from 'path'; 2 | import { EggAppInfo, EggAppConfig, PowerPartial } from 'egg'; 3 | 4 | export default (appInfo: EggAppInfo) => { 5 | const config = {} as PowerPartial; 6 | 7 | config.keys = '123123'; 8 | 9 | config.sequelize = { 10 | connectionUri: 'mysql://root:@127.0.0.1:3306/test4', 11 | dialect: 'mysql' 12 | } 13 | 14 | return config; 15 | } 16 | -------------------------------------------------------------------------------- /test/fixtures/apps/connection-uri/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "connection-uri" 3 | } 4 | -------------------------------------------------------------------------------- /test/fixtures/apps/connection-uri/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "strict": true, 4 | "target": "es2017", 5 | "module": "commonjs", 6 | "moduleResolution": "node", 7 | "baseUrl": "./", 8 | "paths": { 9 | "egg-sequelize": ["../../../../"] 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /test/fixtures/apps/connection-uri/typings/index.d.ts: -------------------------------------------------------------------------------- 1 | import 'egg'; 2 | import 'egg-sequelize'; 3 | import HomeController from '../app/controller/home'; 4 | import MonkeyModel from '../app/model/monkey'; 5 | import UserModel from '../app/model/user'; 6 | 7 | declare module 'egg' { 8 | interface IController { 9 | home: HomeController; 10 | } 11 | 12 | interface IModel { 13 | Monkey: ReturnType; 14 | User: ReturnType; 15 | } 16 | } -------------------------------------------------------------------------------- /test/fixtures/apps/datasources-same-dir/app/model/Person.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = (app, model) => { 4 | const { STRING } = app.Sequelize; 5 | const Person = model.define('person', { 6 | name: STRING(30), 7 | }); 8 | 9 | return Person; 10 | }; 11 | -------------------------------------------------------------------------------- /test/fixtures/apps/datasources-same-dir/app/model/monkey.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = (app, model) => { 4 | const { STRING, INTEGER, DATE } = app.Sequelize; 5 | const Monkey = model.define('monkey', { 6 | name: { 7 | type: STRING, 8 | allowNull: false, 9 | }, 10 | user_id: INTEGER, 11 | created_at: DATE, 12 | updated_at: DATE, 13 | }, { 14 | tableName: 'the_monkeys', 15 | 16 | classMethods: { 17 | }, 18 | 19 | instanceMethods: { 20 | }, 21 | }); 22 | 23 | Monkey.findUser = async function() { 24 | return model.User.find({ id: 1 }); 25 | }; 26 | 27 | return Monkey; 28 | }; 29 | -------------------------------------------------------------------------------- /test/fixtures/apps/datasources-same-dir/app/model/other.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { 4 | }; 5 | -------------------------------------------------------------------------------- /test/fixtures/apps/datasources-same-dir/app/model/post.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | 5 | module.exports = (app, model) => { 6 | const { INTEGER, STRING } = app.Sequelize; 7 | const Post = model.define('post', { 8 | user_id: INTEGER, 9 | name: STRING(30), 10 | }); 11 | 12 | Post.associate = function() { 13 | assert.ok(model.User); 14 | assert.ok(model.Post); 15 | model.Post.belongsTo(model.User, { as: 'user', foreignKey: 'user_id' }); 16 | }; 17 | 18 | return Post; 19 | }; 20 | -------------------------------------------------------------------------------- /test/fixtures/apps/datasources-same-dir/app/model/user.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | 5 | module.exports = (app, model) => { 6 | const { STRING, INTEGER } = app.Sequelize; 7 | const User = model.define('user', { 8 | name: STRING(30), 9 | age: INTEGER, 10 | }); 11 | 12 | User.associate = function() { 13 | assert.ok(model.User); 14 | assert.ok(model.Post); 15 | model.User.hasMany(model.Post, { as: 'posts', foreignKey: 'user_id' }); 16 | }; 17 | 18 | User.test = async function() { 19 | assert(app.config); 20 | assert(model.User === this); 21 | const monkey = await model.Monkey.create({ name: 'The Monkey' }); 22 | assert(monkey.id); 23 | assert(monkey.isNewRecord === false); 24 | assert(monkey.name === 'The Monkey'); 25 | }; 26 | 27 | return User; 28 | }; 29 | -------------------------------------------------------------------------------- /test/fixtures/apps/datasources-same-dir/config/config.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | exports.sequelize = { 4 | datasources: [ 5 | { 6 | delegate: 'model', 7 | baseDir: 'model', 8 | database: 'test', 9 | dialect: 'mysql', 10 | }, 11 | { 12 | delegate: 'sequelize', 13 | baseDir: 'model', 14 | database: 'test1', 15 | dialect: 'mysql', 16 | exclude: 'Person.js', 17 | }, 18 | ], 19 | }; 20 | 21 | exports.keys = '0jN4Fw7ZBjo4xtrLklDg4g=='; 22 | -------------------------------------------------------------------------------- /test/fixtures/apps/datasources-same-dir/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "datasources-same-dir" 3 | } 4 | -------------------------------------------------------------------------------- /test/fixtures/apps/datasources/app/model/Person.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = app => { 4 | const { STRING } = app.Sequelize; 5 | const Person = app.model.define('person', { 6 | name: STRING(30), 7 | }); 8 | 9 | return Person; 10 | }; 11 | -------------------------------------------------------------------------------- /test/fixtures/apps/datasources/app/model/monkey.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = app => { 4 | const { STRING, INTEGER, DATE } = app.Sequelize; 5 | const Monkey = app.model.define('monkey', { 6 | name: { 7 | type: STRING, 8 | allowNull: false, 9 | }, 10 | user_id: INTEGER, 11 | created_at: DATE, 12 | updated_at: DATE, 13 | }, { 14 | tableName: 'the_monkeys', 15 | 16 | classMethods: { 17 | }, 18 | 19 | instanceMethods: { 20 | }, 21 | }); 22 | 23 | Monkey.findUser = async function() { 24 | return app.model.User.find({ id: 1 }); 25 | }; 26 | 27 | return Monkey; 28 | }; 29 | -------------------------------------------------------------------------------- /test/fixtures/apps/datasources/app/model/other.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { 4 | }; 5 | -------------------------------------------------------------------------------- /test/fixtures/apps/datasources/app/model/post.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | 5 | module.exports = app => { 6 | const { INTEGER, STRING } = app.Sequelize; 7 | const Post = app.model.define('post', { 8 | user_id: INTEGER, 9 | name: STRING(30), 10 | }); 11 | 12 | Post.associate = function() { 13 | assert.ok(app.model.User); 14 | assert.ok(app.model.Post); 15 | app.model.Post.belongsTo(app.model.User, { as: 'user', foreignKey: 'user_id' }); 16 | }; 17 | 18 | return Post; 19 | }; 20 | -------------------------------------------------------------------------------- /test/fixtures/apps/datasources/app/model/user.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | 5 | module.exports = app => { 6 | const { STRING, INTEGER } = app.Sequelize; 7 | const User = app.model.define('user', { 8 | name: STRING(30), 9 | age: INTEGER, 10 | }); 11 | 12 | User.associate = function() { 13 | assert.ok(app.model.User); 14 | assert.ok(app.model.Post); 15 | app.model.User.hasMany(app.model.Post, { as: 'posts', foreignKey: 'user_id' }); 16 | }; 17 | 18 | User.test = async function() { 19 | assert(app.config); 20 | assert(app.model.User === this); 21 | const monkey = await app.model.Monkey.create({ name: 'The Monkey' }); 22 | assert(monkey.id); 23 | assert(monkey.isNewRecord === false); 24 | assert(monkey.name === 'The Monkey'); 25 | }; 26 | 27 | return User; 28 | }; 29 | -------------------------------------------------------------------------------- /test/fixtures/apps/datasources/app/sequelize/Person.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = app => { 4 | const { STRING } = app.Sequelize; 5 | const Person = app.sequelize.define('person', { 6 | name: STRING(30), 7 | }); 8 | 9 | return Person; 10 | }; 11 | -------------------------------------------------------------------------------- /test/fixtures/apps/datasources/app/sequelize/monkey.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = app => { 4 | const { STRING, INTEGER, DATE } = app.Sequelize; 5 | const Monkey = app.sequelize.define('monkey', { 6 | name: { 7 | type: STRING, 8 | allowNull: false, 9 | }, 10 | user_id: INTEGER, 11 | created_at: DATE, 12 | updated_at: DATE, 13 | }, { 14 | tableName: 'the_monkeys', 15 | 16 | classMethods: { 17 | }, 18 | 19 | instanceMethods: { 20 | }, 21 | }); 22 | 23 | Monkey.findUser = async function() { 24 | return app.sequelize.User.find({ id: 1 }); 25 | }; 26 | 27 | return Monkey; 28 | }; 29 | -------------------------------------------------------------------------------- /test/fixtures/apps/datasources/app/sequelize/other.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { 4 | }; 5 | -------------------------------------------------------------------------------- /test/fixtures/apps/datasources/app/sequelize/post.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | 5 | module.exports = app => { 6 | const { INTEGER, STRING } = app.Sequelize; 7 | const Post = app.sequelize.define('post', { 8 | user_id: INTEGER, 9 | name: STRING(30), 10 | }); 11 | 12 | Post.associate = function() { 13 | assert.ok(app.sequelize.User); 14 | assert.ok(app.sequelize.Post); 15 | app.sequelize.Post.belongsTo(app.sequelize.User, { as: 'user', foreignKey: 'user_id' }); 16 | }; 17 | 18 | return Post; 19 | }; 20 | -------------------------------------------------------------------------------- /test/fixtures/apps/datasources/app/sequelize/user.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | 5 | module.exports = app => { 6 | const { STRING, INTEGER } = app.Sequelize; 7 | const User = app.sequelize.define('user', { 8 | name: STRING(30), 9 | age: INTEGER, 10 | }); 11 | 12 | User.associate = function() { 13 | assert.ok(app.sequelize.User); 14 | assert.ok(app.sequelize.Post); 15 | app.sequelize.User.hasMany(app.sequelize.Post, { as: 'posts', foreignKey: 'user_id' }); 16 | }; 17 | 18 | User.test = async function() { 19 | assert(app.config); 20 | assert(app.sequelize.User === this); 21 | const monkey = await app.sequelize.Monkey.create({ name: 'The Monkey' }); 22 | assert(monkey.id); 23 | assert(monkey.isNewRecord === false); 24 | assert(monkey.name === 'The Monkey'); 25 | }; 26 | 27 | return User; 28 | }; 29 | -------------------------------------------------------------------------------- /test/fixtures/apps/datasources/app/subproperty/a/Person.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = app => { 4 | const { STRING } = app.Sequelize; 5 | const Person = app.subproperty.a.define('person', { 6 | name: STRING(30), 7 | }); 8 | 9 | return Person; 10 | }; 11 | -------------------------------------------------------------------------------- /test/fixtures/apps/datasources/app/subproperty/a/monkey.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = app => { 4 | const { STRING, INTEGER, DATE } = app.Sequelize; 5 | const Monkey = app.subproperty.a.define('monkey', { 6 | name: { 7 | type: STRING, 8 | allowNull: false, 9 | }, 10 | user_id: INTEGER, 11 | created_at: DATE, 12 | updated_at: DATE, 13 | }, { 14 | tableName: 'the_monkeys', 15 | 16 | classMethods: { 17 | }, 18 | 19 | instanceMethods: { 20 | }, 21 | }); 22 | 23 | Monkey.findUser = async function() { 24 | return app.subproperty.a.User.find({ id: 1 }); 25 | }; 26 | 27 | return Monkey; 28 | }; 29 | -------------------------------------------------------------------------------- /test/fixtures/apps/datasources/app/subproperty/a/other.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { 4 | }; 5 | -------------------------------------------------------------------------------- /test/fixtures/apps/datasources/app/subproperty/a/post.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | 5 | module.exports = app => { 6 | const { INTEGER, STRING } = app.Sequelize; 7 | const Post = app.subproperty.a.define('post', { 8 | user_id: INTEGER, 9 | name: STRING(30), 10 | }); 11 | 12 | Post.associate = function() { 13 | assert.ok(app.subproperty.a.User); 14 | assert.ok(app.subproperty.a.Post); 15 | app.subproperty.a.Post.belongsTo(app.subproperty.a.User, { as: 'user', foreignKey: 'user_id' }); 16 | }; 17 | 18 | return Post; 19 | }; 20 | -------------------------------------------------------------------------------- /test/fixtures/apps/datasources/app/subproperty/a/user.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | 5 | module.exports = app => { 6 | const { STRING, INTEGER } = app.Sequelize; 7 | const User = app.subproperty.a.define('user', { 8 | name: STRING(30), 9 | age: INTEGER, 10 | }); 11 | 12 | User.associate = function() { 13 | assert.ok(app.subproperty.a.User); 14 | assert.ok(app.subproperty.a.Post); 15 | app.subproperty.a.User.hasMany(app.subproperty.a.Post, { as: 'posts', foreignKey: 'user_id' }); 16 | }; 17 | 18 | User.test = async function() { 19 | assert(app.config); 20 | assert(app.subproperty.a.User === this); 21 | const monkey = await app.subproperty.a.Monkey.create({ name: 'The Monkey' }); 22 | assert(monkey.id); 23 | assert(monkey.isNewRecord === false); 24 | assert(monkey.name === 'The Monkey'); 25 | }; 26 | 27 | return User; 28 | }; 29 | -------------------------------------------------------------------------------- /test/fixtures/apps/datasources/app/subproperty/b/Person.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = app => { 4 | const { STRING } = app.Sequelize; 5 | const Person = app.subproperty.b.define('person', { 6 | name: STRING(30), 7 | }); 8 | 9 | return Person; 10 | }; 11 | -------------------------------------------------------------------------------- /test/fixtures/apps/datasources/app/subproperty/b/monkey.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = app => { 4 | const { STRING, INTEGER, DATE } = app.Sequelize; 5 | const Monkey = app.subproperty.b.define('monkey', { 6 | name: { 7 | type: STRING, 8 | allowNull: false, 9 | }, 10 | user_id: INTEGER, 11 | created_at: DATE, 12 | updated_at: DATE, 13 | }, { 14 | tableName: 'the_monkeys', 15 | 16 | classMethods: { 17 | }, 18 | 19 | instanceMethods: { 20 | }, 21 | }); 22 | 23 | Monkey.findUser = async function() { 24 | return app.subproperty.b.User.find({ id: 1 }); 25 | }; 26 | 27 | return Monkey; 28 | }; 29 | -------------------------------------------------------------------------------- /test/fixtures/apps/datasources/app/subproperty/b/other.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { 4 | }; 5 | -------------------------------------------------------------------------------- /test/fixtures/apps/datasources/app/subproperty/b/post.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | 5 | module.exports = app => { 6 | const { INTEGER, STRING } = app.Sequelize; 7 | const Post = app.subproperty.b.define('post', { 8 | user_id: INTEGER, 9 | name: STRING(30), 10 | }); 11 | 12 | Post.associate = function() { 13 | assert.ok(app.subproperty.b.User); 14 | assert.ok(app.subproperty.b.Post); 15 | app.subproperty.b.Post.belongsTo(app.subproperty.b.User, { as: 'user', foreignKey: 'user_id' }); 16 | }; 17 | 18 | return Post; 19 | }; 20 | -------------------------------------------------------------------------------- /test/fixtures/apps/datasources/app/subproperty/b/user.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | 5 | module.exports = app => { 6 | const { STRING, INTEGER } = app.Sequelize; 7 | const User = app.subproperty.b.define('user', { 8 | name: STRING(30), 9 | age: INTEGER, 10 | }); 11 | 12 | User.associate = function() { 13 | assert.ok(app.subproperty.b.User); 14 | assert.ok(app.subproperty.b.Post); 15 | app.subproperty.b.User.hasMany(app.subproperty.b.Post, { as: 'posts', foreignKey: 'user_id' }); 16 | }; 17 | 18 | User.test = async function() { 19 | assert(app.config); 20 | assert(app.subproperty.b.User === this); 21 | const monkey = await app.subproperty.b.Monkey.create({ name: 'The Monkey' }); 22 | assert(monkey.id); 23 | assert(monkey.isNewRecord === false); 24 | assert(monkey.name === 'The Monkey'); 25 | }; 26 | 27 | return User; 28 | }; 29 | -------------------------------------------------------------------------------- /test/fixtures/apps/datasources/config/config.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | exports.sequelize = { 4 | datasources: [ 5 | { 6 | delegate: 'model', 7 | baseDir: 'model', 8 | database: 'test', 9 | dialect: 'mysql', 10 | }, 11 | { 12 | delegate: 'sequelize', 13 | baseDir: 'sequelize', 14 | database: 'test1', 15 | dialect: 'mysql', 16 | exclude: 'Person.js', 17 | }, 18 | { 19 | delegate: 'subproperty.a', 20 | baseDir: 'subproperty/a', 21 | database: 'test2', 22 | dialect: 'mysql', 23 | }, 24 | { 25 | delegate: 'subproperty.b', 26 | baseDir: 'subproperty/b', 27 | database: 'test3', 28 | dialect: 'mysql', 29 | }, 30 | ], 31 | }; 32 | 33 | exports.keys = '0jN4Fw7ZBjo4xtrLklDg4g=='; 34 | -------------------------------------------------------------------------------- /test/fixtures/apps/datasources/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "datasources" 3 | } 4 | -------------------------------------------------------------------------------- /test/fixtures/apps/model-app/app/controller/users.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = app => { 4 | return class UsersController extends app.Controller { 5 | async show() { 6 | const user = await this.ctx.model.User.findByPk(this.ctx.params.id); 7 | this.ctx.body = user; 8 | } 9 | 10 | async create() { 11 | await app.model.User.create({ 12 | name: this.ctx.request.body.name, 13 | }); 14 | } 15 | }; 16 | }; 17 | -------------------------------------------------------------------------------- /test/fixtures/apps/model-app/app/model/Person.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = app => { 4 | const { STRING } = app.Sequelize; 5 | const Person = app.model.define('person', { 6 | name: STRING(30), 7 | }); 8 | 9 | return Person; 10 | }; 11 | -------------------------------------------------------------------------------- /test/fixtures/apps/model-app/app/model/monkey.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = app => { 4 | const { STRING, INTEGER, DATE } = app.Sequelize; 5 | const Monkey = app.model.define('monkey', { 6 | name: { 7 | type: STRING, 8 | allowNull: false, 9 | }, 10 | user_id: INTEGER, 11 | created_at: DATE, 12 | updated_at: DATE, 13 | }, { 14 | tableName: 'the_monkeys', 15 | 16 | classMethods: { 17 | }, 18 | 19 | instanceMethods: { 20 | }, 21 | }); 22 | 23 | Monkey.findUser = async function() { 24 | return app.model.User.find({ id: 1 }); 25 | }; 26 | 27 | return Monkey; 28 | }; 29 | -------------------------------------------------------------------------------- /test/fixtures/apps/model-app/app/model/other.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { 4 | }; 5 | -------------------------------------------------------------------------------- /test/fixtures/apps/model-app/app/model/post.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | 5 | module.exports = app => { 6 | const { INTEGER, STRING } = app.Sequelize; 7 | const Post = app.model.define('post', { 8 | user_id: INTEGER, 9 | name: STRING(30), 10 | }); 11 | 12 | Post.associate = function() { 13 | assert.ok(app.model.User); 14 | assert.ok(app.model.Post); 15 | app.model.Post.belongsTo(app.model.User, { as: 'user', foreignKey: 'user_id' }); 16 | }; 17 | 18 | return Post; 19 | }; 20 | -------------------------------------------------------------------------------- /test/fixtures/apps/model-app/app/model/user.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | 5 | module.exports = app => { 6 | const { STRING, INTEGER } = app.Sequelize; 7 | const User = app.model.define('user', { 8 | name: STRING(30), 9 | age: INTEGER, 10 | }); 11 | 12 | User.associate = function() { 13 | assert.ok(app.model.User); 14 | assert.ok(app.model.Post); 15 | app.model.User.hasMany(app.model.Post, { as: 'posts', foreignKey: 'user_id' }); 16 | }; 17 | 18 | User.test = async function() { 19 | assert(app.config); 20 | assert(app.model.User === this); 21 | const monkey = await app.model.Monkey.create({ name: 'The Monkey' }); 22 | assert(monkey.id); 23 | assert(monkey.isNewRecord === false); 24 | assert(monkey.name === 'The Monkey'); 25 | }; 26 | 27 | return User; 28 | }; 29 | -------------------------------------------------------------------------------- /test/fixtures/apps/model-app/app/router.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function(app) { 4 | app.resources('users', '/users', 'users'); 5 | }; 6 | -------------------------------------------------------------------------------- /test/fixtures/apps/model-app/config/config.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | exports.sequelize = { 4 | port: '3306', 5 | host: '127.0.0.1', 6 | username: 'root', 7 | password: '', 8 | database: 'test', 9 | dialect: 'mysql', 10 | pool: { 11 | max: 5, 12 | min: 0, 13 | idle: 10000, 14 | }, 15 | storage: 'db/test-foo.sqlite', 16 | timezone: '+08:01', 17 | }; 18 | 19 | exports.keys = '0jN4Fw7ZBjo4xtrLklDg4g=='; 20 | -------------------------------------------------------------------------------- /test/fixtures/apps/model-app/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "model-app" 3 | } -------------------------------------------------------------------------------- /test/fixtures/apps/sub-model/app/model/sub/post.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | 5 | module.exports = app => { 6 | const { INTEGER, STRING } = app.Sequelize; 7 | const Post = app.model.define('post', { 8 | user_id: INTEGER, 9 | name: STRING(30), 10 | }); 11 | 12 | Post.associate = function() { 13 | assert.ok(app.model.User); 14 | assert.ok(app.model.Sub.Post); 15 | app.model.Sub.Post.belongsTo(app.model.User, { as: 'user', foreignKey: 'user_id' }); 16 | }; 17 | 18 | return Post; 19 | }; 20 | -------------------------------------------------------------------------------- /test/fixtures/apps/sub-model/app/model/user.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | 5 | module.exports = app => { 6 | const { STRING, INTEGER } = app.Sequelize; 7 | const User = app.model.define('user', { 8 | name: STRING(30), 9 | age: INTEGER, 10 | }); 11 | 12 | User.associate = function() { 13 | assert.ok(app.model.User); 14 | assert.ok(app.model.Sub.Post); 15 | app.model.User.hasMany(app.model.Sub.Post, { as: 'posts', foreignKey: 'user_id' }); 16 | }; 17 | 18 | return User; 19 | }; 20 | -------------------------------------------------------------------------------- /test/fixtures/apps/sub-model/config/config.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | exports.sequelize = { 4 | port: '3306', 5 | host: '127.0.0.1', 6 | username: 'root', 7 | password: '', 8 | database: 'test', 9 | dialect: 'mysql', 10 | pool: { 11 | max: 5, 12 | min: 0, 13 | idle: 10000, 14 | }, 15 | storage: 'db/test-foo.sqlite', 16 | timezone: '+08:01', 17 | }; 18 | 19 | exports.keys = '0jN4Fw7ZBjo4xtrLklDg4g=='; 20 | -------------------------------------------------------------------------------- /test/fixtures/apps/sub-model/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sub-model" 3 | } 4 | -------------------------------------------------------------------------------- /test/fixtures/apps/ts/app/controller/home.ts: -------------------------------------------------------------------------------- 1 | import { Controller } from 'egg'; 2 | 3 | export default class HomeController extends Controller { 4 | async index() { 5 | const { ctx } = this; 6 | await ctx.model.Monkey.findUser(); 7 | await ctx.model.User.associate(); 8 | await ctx.model.User.test(); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /test/fixtures/apps/ts/app/model/monkey.ts: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | import { Application } from 'egg'; 4 | 5 | export default function(app: Application) { 6 | const { STRING, INTEGER, DATE } = app.Sequelize; 7 | const Monkey = app.model.define('monkey', { 8 | name: { 9 | type: STRING, 10 | allowNull: false, 11 | }, 12 | user_id: INTEGER, 13 | created_at: DATE, 14 | updated_at: DATE, 15 | }, { 16 | tableName: 'the_monkeys', 17 | }); 18 | 19 | return class extends Monkey { 20 | static async findUser() { 21 | return app.model.User.findOne({ where: { id: '123' } }); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /test/fixtures/apps/ts/app/model/user.ts: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | import { Application } from 'egg'; 4 | import assert = require('assert'); 5 | 6 | export default function(app: Application) { 7 | const { STRING, INTEGER } = app.Sequelize; 8 | const User = app.model.define('user', { 9 | name: STRING(30), 10 | age: INTEGER, 11 | }); 12 | 13 | return class extends User { 14 | static async associate() { 15 | assert.ok(app.model.User); 16 | } 17 | 18 | static async test() { 19 | assert(app.config); 20 | assert(app.model.User === this); 21 | const monkey = await app.model.Monkey.create({ name: 'The Monkey' }); 22 | assert(monkey.isNewRecord === false); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /test/fixtures/apps/ts/app/router.ts: -------------------------------------------------------------------------------- 1 | import { Application } from 'egg'; 2 | 3 | export default (app: Application) => { 4 | const { controller } = app; 5 | 6 | app.get('/', controller.home.index); 7 | } 8 | -------------------------------------------------------------------------------- /test/fixtures/apps/ts/config/config.default.ts: -------------------------------------------------------------------------------- 1 | import * as path from 'path'; 2 | import { EggAppInfo, EggAppConfig, PowerPartial } from 'egg'; 3 | 4 | export default (appInfo: EggAppInfo) => { 5 | const config = {} as PowerPartial; 6 | 7 | config.keys = '123123'; 8 | 9 | config.sequelize = { 10 | datasources: [ 11 | { 12 | delegate: 'model', 13 | baseDir: 'model', 14 | database: 'test', 15 | dialect: 'mysql', 16 | }, 17 | { 18 | delegate: 'sequelize', 19 | baseDir: 'sequelize', 20 | database: 'test1', 21 | dialect: 'mysql', 22 | exclude: 'Person.js', 23 | }, 24 | { 25 | delegate: 'subproperty.a', 26 | baseDir: 'subproperty/a', 27 | database: 'test2', 28 | dialect: 'mysql', 29 | }, 30 | { 31 | delegate: 'subproperty.b', 32 | baseDir: 'subproperty/b', 33 | database: 'test3', 34 | dialect: 'mysql', 35 | }, 36 | ] 37 | } 38 | 39 | return config; 40 | } 41 | -------------------------------------------------------------------------------- /test/fixtures/apps/ts/config/config.local.ts: -------------------------------------------------------------------------------- 1 | import * as path from 'path'; 2 | import { EggAppInfo, EggAppConfig, PowerPartial } from 'egg'; 3 | 4 | export default (appInfo: EggAppInfo) => { 5 | const config = {} as PowerPartial; 6 | 7 | config.keys = '123123'; 8 | 9 | config.sequelize = { 10 | port: 3306, 11 | host: '127.0.0.1', 12 | username: 'root', 13 | password: '', 14 | database: 'test', 15 | dialect: 'mysql', 16 | pool: { 17 | max: 5, 18 | min: 0, 19 | idle: 10000, 20 | }, 21 | storage: 'db/test-foo.sqlite', 22 | timezone: '+08:01', 23 | } 24 | 25 | return config; 26 | } 27 | -------------------------------------------------------------------------------- /test/fixtures/apps/ts/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "strict": true, 4 | "target": "es2017", 5 | "module": "commonjs", 6 | "moduleResolution": "node", 7 | "baseUrl": "./", 8 | "paths": { 9 | "egg-sequelize": ["../../../../"] 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /test/fixtures/apps/ts/typings/index.d.ts: -------------------------------------------------------------------------------- 1 | import 'egg'; 2 | import 'egg-sequelize'; 3 | import HomeController from '../app/controller/home'; 4 | import MonkeyModel from '../app/model/monkey'; 5 | import UserModel from '../app/model/user'; 6 | 7 | declare module 'egg' { 8 | interface IController { 9 | home: HomeController; 10 | } 11 | 12 | interface IModel { 13 | Monkey: ReturnType; 14 | User: ReturnType; 15 | } 16 | } -------------------------------------------------------------------------------- /test/plugin.test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const mm = require('egg-mock'); 5 | 6 | describe('test/plugin.test.js', () => { 7 | let app; 8 | 9 | before(() => { 10 | app = mm.app({ 11 | baseDir: 'apps/model-app', 12 | }); 13 | return app.ready(); 14 | }); 15 | before(() => app.model.sync({ force: true })); 16 | 17 | after(mm.restore); 18 | 19 | describe('Base', () => { 20 | it('sequelize init success', () => { 21 | assert(app.model); 22 | }); 23 | 24 | it('ctx model property getter', () => { 25 | const ctx = app.mockContext(); 26 | assert.ok(ctx.model); 27 | assert.ok(ctx.model.ctx === ctx); 28 | assert.ok(ctx.model.User); 29 | assert.ok(ctx.model.Monkey); 30 | assert.ok(ctx.model.Person); 31 | assert.ok(ctx.model !== app.model); 32 | }); 33 | 34 | it('model not load non Sequelize files', async function() { 35 | assert(!('Other' in app.model)); 36 | 37 | const ctx = app.mockContext(); 38 | assert(!('Other' in ctx.model)); 39 | }); 40 | 41 | it('has right tableName', () => { 42 | assert(app.model.Person.tableName === 'people'); 43 | assert(app.model.User.tableName === 'users'); 44 | assert(app.model.Monkey.tableName === 'the_monkeys'); 45 | }); 46 | }); 47 | 48 | describe('Database options', () => { 49 | let config; 50 | 51 | before(() => { 52 | config = app.model.options; 53 | }); 54 | 55 | it('should work with default config', async function() { 56 | assert(config.define.freezeTableName === false); 57 | assert(config.port === '3306'); 58 | assert(config.username === 'root'); 59 | assert(config.password === ''); 60 | assert(config.logging !== false); 61 | assert(config.benchmark === true); 62 | }); 63 | 64 | it('should work with fixture configs', async function() { 65 | assert(config.dialect === 'mysql'); 66 | assert(config.host === '127.0.0.1'); 67 | assert(config.pool.idle === 10000); 68 | assert(config.timezone === '+08:01'); 69 | assert(config.storage === 'db/test-foo.sqlite'); 70 | }); 71 | }); 72 | 73 | describe('Test model', () => { 74 | it('User.test method work', async function() { 75 | await app.model.User.test(); 76 | }); 77 | 78 | it('should work timestramp', async function() { 79 | const user = await app.model.User.create({ name: 'huacnlee' }); 80 | assert(user.isNewRecord === false); 81 | assert(user.name === 'huacnlee'); 82 | assert(user.created_at !== null); 83 | assert(user.updated_at !== null); 84 | }); 85 | }); 86 | 87 | describe('Test controller', () => { 88 | it('should get data from create', async function() { 89 | app.mockCsrf(); 90 | 91 | await app.httpRequest(app.callback()) 92 | .post('/users') 93 | .send({ 94 | name: 'popomore', 95 | }); 96 | const user = await app.model.User.findOne({ 97 | where: { name: 'popomore' }, 98 | }); 99 | assert.ok(user); 100 | assert(user.name === 'popomore'); 101 | assert(user.isNewRecord === false); 102 | const res = await app.httpRequest(app.callback()) 103 | .get(`/users/${user.id}`); 104 | assert(res.status === 200); 105 | assert(res.body.name === 'popomore'); 106 | }); 107 | }); 108 | 109 | describe('Associate', () => { 110 | it('ctx model associate init success', () => { 111 | const ctx = app.mockContext(); 112 | assert.ok(ctx.model); 113 | assert.ok(ctx.model.User); 114 | assert.ok(ctx.model.User.prototype.hasPosts); 115 | assert.ok(ctx.model.Post); 116 | }); 117 | }); 118 | }); 119 | -------------------------------------------------------------------------------- /test/submodel.test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const mm = require('egg-mock'); 5 | 6 | describe('test/plugin.test.js', () => { 7 | let app; 8 | 9 | before(() => { 10 | app = mm.app({ 11 | baseDir: 'apps/sub-model', 12 | }); 13 | return app.ready(); 14 | }); 15 | before(() => app.model.sync({ force: true })); 16 | 17 | after(mm.restore); 18 | 19 | describe('Base', () => { 20 | it('sequelize init success', () => { 21 | assert.ok(app.model); 22 | assert.ok(app.model.User); 23 | assert.ok(app.model.Sub.Post); 24 | }); 25 | 26 | it('ctx model property getter', () => { 27 | const ctx = app.mockContext(); 28 | assert.ok(ctx.model); 29 | assert.ok(ctx.model.User); 30 | assert.ok(ctx.model.Sub.Post); 31 | }); 32 | }); 33 | 34 | describe('Associate', () => { 35 | it('ctx model associate init success', () => { 36 | const ctx = app.mockContext(); 37 | assert.ok(ctx.model); 38 | assert.ok(ctx.model.User); 39 | assert.ok(ctx.model.User.prototype.hasPosts); 40 | assert.ok(ctx.model.Sub.Post); 41 | console.log(ctx.model.Sub.Post); 42 | assert.ok(ctx.model.Sub.Post.prototype.getUser); 43 | }); 44 | }); 45 | }); 46 | -------------------------------------------------------------------------------- /test/ts.test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const coffee = require('coffee'); 4 | const path = require('path'); 5 | 6 | describe('typescript', () => { 7 | it('should compile ts without error', () => { 8 | return coffee.fork( 9 | require.resolve('typescript/bin/tsc'), 10 | [ '-p', path.resolve(__dirname, './fixtures/apps/ts/tsconfig.json') ] 11 | ) 12 | .debug() 13 | .expect('code', 0) 14 | .end(); 15 | }); 16 | }); 17 | --------------------------------------------------------------------------------