Skip to content

Commit

Permalink
feat: :support connection uri with mysql. (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff-Tian authored and dead-horse committed Jun 14, 2019
1 parent 89cbce4 commit 2632827
Show file tree
Hide file tree
Showing 17 changed files with 201 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -8,3 +8,5 @@ run
*.swp
test/fixtures/apps/model-app/run/
test/fixtures/apps/ts/**/*.js
test/fixtures/apps/connection-uri/**/*.js
package-lock.json
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -16,3 +16,4 @@ before_install:
- mysql -e 'CREATE DATABASE IF NOT EXISTS test1;'
- mysql -e 'CREATE DATABASE IF NOT EXISTS test2;'
- mysql -e 'CREATE DATABASE IF NOT EXISTS test3;'
- mysql -e 'CREATE DATABASE IF NOT EXISTS test4;'
13 changes: 13 additions & 0 deletions README.md
Expand Up @@ -65,6 +65,19 @@ exports.sequelize = {
};
```

You can also use the `connection uri` to configure the connection:

```js
exports.sequelize = {
dialect: 'mysql', // support: mysql, mariadb, postgres, mssql
connectionUri: 'mysql://root:@127.0.0.1:3306/test',
// delegate: 'myModel', // load all models to `app[delegate]` and `ctx[delegate]`, default to `model`
// baseDir: 'my_model', // load all files in `app/${baseDir}` as models, default to `model`
// exclude: 'index.js', // ignore `app/${baseDir}/index.js` when load models, support glob and array
// more sequelize options
};
```

egg-sequelize has a default sequelize options below

```js
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Expand Up @@ -4,6 +4,7 @@ interface EggSequelizeOptions extends sequelize.Options {
delegate?: string;
baseDir?: string;
exclude?: string;
connectionUri?: string;
}

interface DataSources {
Expand Down
4 changes: 3 additions & 1 deletion lib/loader.js
Expand Up @@ -55,7 +55,9 @@ module.exports = app => {
delete config.ignore;
}

const sequelize = new app.Sequelize(config.database, config.username, config.password, config);
const sequelize = config.connectionUri ?
new app.Sequelize(config.connectionUri, config) :
new app.Sequelize(config.database, config.username, config.password, config);

const delegate = config.delegate.split('.');
const len = delegate.length;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -20,7 +20,7 @@
"devDependencies": {
"autod": "^3.0.1",
"egg": "^2.10.0",
"egg-bin": "^4.8.1",
"egg-bin": "^4.13.1",
"egg-mock": "^3.19.3",
"eslint": "^5.3.0",
"eslint-config-egg": "^7.0.0",
Expand Down
40 changes: 40 additions & 0 deletions test/connection-uri.test.js
@@ -0,0 +1,40 @@
'use strict';

const coffee = require('coffee');
const path = require('path');
const mm = require('egg-mock');
const assert = require('assert');

describe('connection-uri', () => {
let app;

before(() => {
console.log('compiling...');
return coffee
.fork(require.resolve('typescript/bin/tsc'), [
'-p',
path.resolve(__dirname, './fixtures/apps/connection-uri/tsconfig.json'),
])
.debug()
.expect('code', 0)
.end();
});


before(() => {
app = mm.app({
baseDir: 'apps/connection-uri',
});
return app.ready();
});
before(() => app.model.sync({ force: true }));

after(mm.restore);

describe('Base', () => {
it('sequelize init success', () => {
assert.ok(app.model);
assert.ok(app.model.User);
});
});
});
10 changes: 10 additions & 0 deletions test/fixtures/apps/connection-uri/app/controller/home.ts
@@ -0,0 +1,10 @@
import { Controller } from 'egg';

export default class HomeController extends Controller {
async index() {
const { ctx } = this;
await ctx.model.Monkey.findUser();
await ctx.model.User.associate();
await ctx.model.User.test();
}
}
24 changes: 24 additions & 0 deletions test/fixtures/apps/connection-uri/app/model/monkey.ts
@@ -0,0 +1,24 @@
'use strict';

import { Application } from 'egg';

export default function(app: Application) {
const { STRING, INTEGER, DATE } = app.Sequelize;
const Monkey = app.model.define('monkey', {
name: {
type: STRING,
allowNull: false,
},
user_id: INTEGER,
created_at: DATE,
updated_at: DATE,
}, {
tableName: 'the_monkeys',
});

return class extends Monkey {
static async findUser() {
return app.model.User.findOne({ where: { id: '123' } });
}
}
}
25 changes: 25 additions & 0 deletions test/fixtures/apps/connection-uri/app/model/user.ts
@@ -0,0 +1,25 @@
'use strict';

import { Application } from 'egg';
import assert = require('assert');

export default function(app: Application) {
const { STRING, INTEGER } = app.Sequelize;
const User = app.model.define('user', {
name: STRING(30),
age: INTEGER,
});

return class extends User {
static async associate() {
assert.ok(app.model.User);
}

static async test() {
assert(app.config);
assert(app.model.User === this);
const monkey = await app.model.Monkey.create({ name: 'The Monkey' });
assert(monkey.isNewRecord === false);
}
}
}
7 changes: 7 additions & 0 deletions test/fixtures/apps/connection-uri/app/router.ts
@@ -0,0 +1,7 @@
import { Application } from 'egg';

export default (app: Application) => {
const { controller } = app;

app.get('/', controller.home.index);
}
13 changes: 13 additions & 0 deletions test/fixtures/apps/connection-uri/config/config.default.ts
@@ -0,0 +1,13 @@
import * as path from 'path';
import { EggAppInfo, EggAppConfig, PowerPartial } from 'egg';

export default (appInfo: EggAppInfo) => {
const config = {} as PowerPartial<EggAppConfig>;

config.keys = '123123';

config.sequelize = {
}

return config;
}
15 changes: 15 additions & 0 deletions test/fixtures/apps/connection-uri/config/config.local.ts
@@ -0,0 +1,15 @@
import * as path from 'path';
import { EggAppInfo, EggAppConfig, PowerPartial } from 'egg';

export default (appInfo: EggAppInfo) => {
const config = {} as PowerPartial<EggAppConfig>;

config.keys = '123123';

config.sequelize = {
connectionUri: 'mysql://root:@127.0.0.1:3306/test4',
dialect: 'mysql'
}

return config;
}
15 changes: 15 additions & 0 deletions test/fixtures/apps/connection-uri/config/config.unittest.ts
@@ -0,0 +1,15 @@
import * as path from 'path';
import { EggAppInfo, EggAppConfig, PowerPartial } from 'egg';

export default (appInfo: EggAppInfo) => {
const config = {} as PowerPartial<EggAppConfig>;

config.keys = '123123';

config.sequelize = {
connectionUri: 'mysql://root:@127.0.0.1:3306/test4',
dialect: 'mysql'
}

return config;
}
3 changes: 3 additions & 0 deletions test/fixtures/apps/connection-uri/package.json
@@ -0,0 +1,3 @@
{
"name": "connection-uri"
}
12 changes: 12 additions & 0 deletions test/fixtures/apps/connection-uri/tsconfig.json
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"strict": true,
"target": "es2017",
"module": "commonjs",
"moduleResolution": "node",
"baseUrl": "./",
"paths": {
"egg-sequelize": ["../../../../"]
}
}
}
16 changes: 16 additions & 0 deletions test/fixtures/apps/connection-uri/typings/index.d.ts
@@ -0,0 +1,16 @@
import 'egg';
import 'egg-sequelize';
import HomeController from '../app/controller/home';
import MonkeyModel from '../app/model/monkey';
import UserModel from '../app/model/user';

declare module 'egg' {
interface IController {
home: HomeController;
}

interface IModel {
Monkey: ReturnType<typeof MonkeyModel>;
User: ReturnType<typeof UserModel>;
}
}

0 comments on commit 2632827

Please sign in to comment.