-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: :support connection uri with mysql. (#74)
- Loading branch information
1 parent
89cbce4
commit 2632827
Showing
17 changed files
with
201 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' } }); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
13
test/fixtures/apps/connection-uri/config/config.default.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
15
test/fixtures/apps/connection-uri/config/config.unittest.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"name": "connection-uri" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"compilerOptions": { | ||
"strict": true, | ||
"target": "es2017", | ||
"module": "commonjs", | ||
"moduleResolution": "node", | ||
"baseUrl": "./", | ||
"paths": { | ||
"egg-sequelize": ["../../../../"] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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>; | ||
} | ||
} |