Skip to content

Commit

Permalink
feat: support config.Sequelize
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse authored and fengmk2 committed Aug 13, 2018
1 parent 86d660d commit 4c727a6
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,21 @@ module.exports = app => {
}
```

### Associate

Define all your associations in `Model.associate()` and egg-sequelize will execute it after all models loaded. See example below.

### Customize Sequelize

By default, egg-sequelize will use sequelize@4, you can cusomize sequelize version by pass sequelize instance with `config.sequelize.Sequelize` like this:

```js
// config/config.default.js
exports.sequelize = {
sequelize: require('sequelize');
};
```

### Full example

```js
Expand Down
1 change: 0 additions & 1 deletion agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@
module.exports = agent => {
require('./lib/loader')(agent);
};

17 changes: 17 additions & 0 deletions config/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,21 @@ exports.sequelize = {
port: 3306,
username: 'root',
password: '',

// support customize your own Squelize
Sequelize: require('sequelize'), // v5 or v3

// support multi datasources by config.sequelize.datasources
// datasources: [
// {
// delegate: 'model', // lood to `app[delegate]`
// baseDir: 'model', // models in `app/${model}`
// // other sequelize configurations
// },
// {
// delegate: 'sequelize', // lood to `app[delegate]`
// baseDir: 'sequelize', // models in `app/${model}`
// // other sequelize configurations
// },
// ],
};
6 changes: 3 additions & 3 deletions lib/loader.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
'use strict';

const path = require('path');
const Sequelize = require('sequelize');
const AUTH_RETRIES = Symbol('authenticateRetries');
const sleep = require('mz-modules/sleep');
const AUTH_RETRIES = Symbol('authenticateRetries');

module.exports = app => {
const defaultConfig = {
Expand All @@ -21,7 +20,8 @@ module.exports = app => {
};

const config = app.config.sequelize;
app.Sequelize = Sequelize;
// support customize sequelize
app.Sequelize = config.Sequelize || require('sequelize');

const databases = [];
if (!config.datasources) {
Expand Down

0 comments on commit 4c727a6

Please sign in to comment.