Skip to content

Commit

Permalink
feat: remove mysql bind (#1)
Browse files Browse the repository at this point in the history
* feat: add mssql pg support
  • Loading branch information
luicfer authored and fengmk2 committed Feb 9, 2017
1 parent 2178e48 commit 2936914
Show file tree
Hide file tree
Showing 18 changed files with 156 additions and 78 deletions.
23 changes: 16 additions & 7 deletions .autod.conf.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
'ues strict';
'use strict';

module.exports = {
write: true,
prefix: '^',
devprefix: '^',
exclude: [
'test/fixtures',
test: [
'test',
'benchmark',
],
devdep: [
'egg',
'egg-ci',
'egg-bin',
'autod',
'eslint',
'eslint-config-egg',
'supertest',
'webstorm-disable-index',
],
keep: [
],
semver: [
exclude: [
'./test/fixtures',
'./docs',
'./coverage',
],
registry: 'https://r.cnpmjs.org',
};
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
test/fixtures
coverage
24 changes: 24 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!--
Thank you for your pull request. Please review below requirements.
Bug fixes and new features should include tests and possibly benchmarks.
Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md
感谢您贡献代码。请确认下列 checklist 的完成情况。
Bug 修复和新功能必须包含测试,必要时请附上性能测试。
Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md
-->

##### Checklist
<!-- Remove items that do not apply. For completed items, change [ ] to [x]. -->

- [ ] `npm test` passes
- [ ] tests and/or benchmarks are included
- [ ] documentation is changed or added
- [ ] commit message follows commit guidelines

##### Affected core subsystem(s)
<!-- Provide affected core subsystem(s). -->


##### Description of change
<!-- Provide a description of the change below this comment. -->
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
logs/
logs
npm-debug.log
node_modules/
coverage/
.idea/
.idea/
run
.DS_Store
*.swp
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
sudo: false
language: node_js
node_js:
- '4'
- '6'
- '7'
install:
- npm i npminstall && npminstall
script:
- npm run ci
after_script:
- npminstall codecov && codecov
services:
- mysql
before_install:
- mysql -e 'CREATE DATABASE IF NOT EXISTS test;'
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,15 @@ Egg's sequelize plugin.
## Install

```bash
$ npm i egg-security
$ npm i --save egg-sequelize

# And one of the following:
$ npm install --save pg pg-hstore
$ npm install --save mysql # For both mysql and mariadb dialects
$ npm install --save tedious # MSSQL
```


## Usage & configuration

- `config.default.js`
Expand All @@ -40,15 +46,16 @@ exports.sequelize = {
host: 'localhost',
username: 'root',
password: '',
database: 'test'
database: 'test',
dialect: 'mysql', // support: mysql, mariadb, postgres, mssql
};
```

More documents please refer to [Sequelize.js](http://sequelize.readthedocs.io/en/v3/)

## models

Please set sequelize models under `app/models`
Please set sequelize models under `app/model`

## Examples

Expand All @@ -74,7 +81,7 @@ module.exports = function (sequelize) {
'use strict'

module.exports = function* () {
this.body = yield this.models.test.find({
this.body = yield this.model.test.find({
'name':'aaa'
})
}
Expand Down
8 changes: 3 additions & 5 deletions agent.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict';

const loader = require('./lib/loader');

module.exports = appOrAgent => {
const done = appOrAgent.readyCallback('agent_sequelize');
loader(appOrAgent, done);
module.exports = agent => {
require('./lib/loader')(agent);
};

8 changes: 3 additions & 5 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict';

const loader = require('./lib/loader');

module.exports = appOrAgent => {
const done = appOrAgent.readyCallback('worker_sequelize');
loader(appOrAgent, done);
module.exports = app => {
require('./lib/loader')(app);
};

6 changes: 6 additions & 0 deletions app/extend/context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';
module.exports = {
get model() {
return this.app.sequelize.models;
},
};
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
environment:
matrix:
- nodejs_version: '4'
- nodejs_version: '6'
- nodejs_version: '7'

install:
- ps: Install-Product node $env:nodejs_version
Expand Down
1 change: 1 addition & 0 deletions config/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

exports.sequelize = {
modelPath: 'model',
dialect: 'mysql',
};
33 changes: 19 additions & 14 deletions lib/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,34 @@ const loading = require('loading');
const path = require('path');
const Sequelize = require('sequelize');

module.exports = (app, done) => {
module.exports = app => {
app.config.sequelize.logging = function(str) {
app.logger.info(`[egg-sequelize] ${str}`);
};
const config = app.config.sequelize;

const sequelize = new Sequelize(
`mysql://${app.config.sequelize.username}:${app.config.sequelize.password}@${app.config.sequelize.host}:${app.config.sequelize.port || 3306}/${app.config.sequelize.database}`,
app.config.sequelize);

sequelize.authenticate().then(() => {
sequelize.Sequelize = Sequelize;

loading(`${path.join(app.baseDir, 'app', app.config.sequelize.modelPath)}`).into(sequelize, 'models');
config.database,
config.username,
config.password,
{
host: config.host,
port: config.port,
dialect: config.dialect,
logging: config.logging,
}
);

app.sequelize = sequelize;
app.beforeStart(function* () {
yield app.sequelize.authenticate();
loading(`${path.join(app.baseDir, 'app', config.modelPath)}`).into(sequelize, 'models');

Object.keys(sequelize.models).forEach(className => {
if ('associate' in sequelize.models[className]) {
sequelize.models[className].associate(sequelize.models);
}
});

app.sequelize = sequelize;
app.context.models = sequelize.models;

done();
}).catch(done);
app.logger.info('[egg-sequelize] start success');
});
};
60 changes: 25 additions & 35 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,65 +5,55 @@
"eggPlugin": {
"name": "sequelize"
},
"repository": {
"type": "git",
"url": "[email protected]:eggjs/egg-sequelize.git"
},
"keywords": [
"egg",
"sequelize",
"egg-plugin",
"mysql"
"eggPlugin",
"orm"
],
"dependencies": {
"loading": "^1.13.0",
"mysql": "^2.12.0",
"sequelize": "^3.27.0"
"sequelize": "^3.30.1"
},
"devDependencies": {
"devDependencies": {
"autod": "^2.7.1",
"beautify-benchmark": "^0.2.4",
"benchmark": "^2.1.1",
"egg": "~0.1.0",
"egg-bin": "1",
"egg-ci": "1",
"egg-mock": "^0.0.4",
"egg-view-nunjucks": "~0.3.0",
"eslint": "3",
"eslint-config-egg": "3",
"pedding": "^1.0.0",
"rimraf": "^2.5.2",
"should": "^11.1.0",
"should-http": "^0.0.4",
"spy": "0",
"supertest": "^2.0.0"
"egg": "^0.10.0",
"egg-bin": "^2.0.2",
"egg-ci": "^1.1.0",
"egg-mock": "^2.3.1",
"eslint": "^3.15.0",
"eslint-config-egg": "^3.2.0",
"mysql": "^2.12.0",
"should": "^11.2.0",
"supertest": "^3.0.0",
"webstorm-disable-index": "^1.1.2"
},
"engines": {
"node": ">=4.0.0"
"node": ">=6.0.0"
},
"scripts": {
"test": "npm run lint && npm run test-local",
"test": "npm run lint -- --fix && npm run test-local",
"test-local": "egg-bin test",
"cov": "egg-bin cov",
"lint": "eslint --ext js . --fix",
"lint": "eslint .",
"ci": "npm run lint && npm run cov",
"autod": "autod"
},
"files": [
"app.js",
"agent.js",
"config",
"app",
"lib"
],
"ci": {
"version": "4, 6"
"version": "6, 7"
},
"repository": {
"type": "git",
"url": "git+https://github.com/eggjs/egg-sequelize.git"
},
"files": [
"agent.js",
"app",
"lib",
"config",
"app.js",
"index.js"
],
"bugs": {
"url": "https://github.com/eggjs/egg/issues"
},
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/apps/model-app/app/model/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

module.exports = sequelize => {
return sequelize.define('test', {
name: {
type: sequelize.Sequelize.STRING(30),
}
})
};
2 changes: 0 additions & 2 deletions test/fixtures/apps/model-app/app/router.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

require('should');

module.exports = function(app) {
app.get('/models', function*() {
//
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/apps/model-app/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@

exports.sequelize = {
modelPath: 'model',
port: '3306',
host: 'localhost',
username: 'root',
password: '',
database: 'test',
dialect: 'mysql',
};
Empty file.
23 changes: 21 additions & 2 deletions test/plugin.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const mm = require('egg-mock');
require('should');

describe('test/plugin.test.js', () => {
let app;
Expand All @@ -15,8 +16,26 @@ describe('test/plugin.test.js', () => {

after(mm.restore);

it('should load models into contxt', () => {
//
it('test config', function() {
const config = app.config.sequelize;
config.should.have.properties([
'modelPath',
'port',
'host',
'username',
'password',
'database',
'dialect',
'logging',
]);
});

it('sequelize init success', function() {
const sequelize = app.sequelize;
sequelize.should.be.ok;
sequelize.should.have.propertyByPath('models', 'test');
});


});

0 comments on commit 2936914

Please sign in to comment.