Skip to content

Commit

Permalink
feat:first implement
Browse files Browse the repository at this point in the history
  • Loading branch information
jtyjty99999 committed Jan 20, 2017
0 parents commit 2178e48
Show file tree
Hide file tree
Showing 20 changed files with 29,271 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .autod.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'ues strict';

module.exports = {
write: true,
prefix: '^',
devprefix: '^',
exclude: [
'test/fixtures',
],
devdep: [
'autod',
],
keep: [
],
semver: [
],
};
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test/fixtures
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "eslint-config-egg"
}
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
logs/
npm-debug.log
node_modules/
coverage/
.idea/
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
sudo: false
language: node_js
node_js:
- '4'
- '6'
install:
- npm i npminstall && npminstall
script:
- npm run ci
after_script:
- npminstall codecov && codecov
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) Alibaba Group Holding Limited and other contributors.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
81 changes: 81 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# egg-sequelize

Sequelize plugin in egg

[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][codecov-image]][codecov-url]
[![David deps][david-image]][david-url]
[![Known Vulnerabilities][snyk-image]][snyk-url]
[![npm download][download-image]][download-url]

[npm-image]: https://img.shields.io/npm/v/egg-sequelize.svg?style=flat-square
[npm-url]: https://npmjs.org/package/egg-sequelize
[travis-image]: https://img.shields.io/travis/eggjs/egg-sequelize.svg?style=flat-square
[travis-url]: https://travis-ci.org/eggjs/egg-sequelize
[codecov-image]: https://codecov.io/gh/eggjs/egg-sequelize/branch/master/graph/badge.svg
[codecov-url]: https://codecov.io/gh/eggjs/egg-sequelize
[david-image]: https://img.shields.io/david/eggjs/egg-sequelize.svg?style=flat-square
[david-url]: https://david-dm.org/eggjs/egg-sequelize
[snyk-image]: https://snyk.io/test/npm/egg-sequelize/badge.svg?style=flat-square
[snyk-url]: https://snyk.io/test/npm/egg-sequelize
[download-image]: https://img.shields.io/npm/dm/egg-sequelize.svg?style=flat-square
[download-url]: https://npmjs.org/package/egg-sequelize

Egg's sequelize plugin.

## Install

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

## Usage & configuration

- `config.default.js`

```js
exports.sequelize = {
port: '3306',
host: 'localhost',
username: 'root',
password: '',
database: 'test'
};
```

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

## models

Please set sequelize models under `app/models`

## Examples

`app/model/test.js`

```js

'use strict'

module.exports = function (sequelize) {
return sequelize.define('test', {
name: {
type: sequelize.Sequelize.STRING(30),
}
})
}
```

`app/controller/user.js`

```js

'use strict'

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

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

module.exports = appOrAgent => {
const done = appOrAgent.readyCallback('agent_sequelize');
loader(appOrAgent, done);
};
8 changes: 8 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

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

module.exports = appOrAgent => {
const done = appOrAgent.readyCallback('worker_sequelize');
loader(appOrAgent, done);
};
15 changes: 15 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
environment:
matrix:
- nodejs_version: '4'
- nodejs_version: '6'

install:
- ps: Install-Product node $env:nodejs_version
- npm i npminstall && node_modules\.bin\npminstall

test_script:
- node --version
- npm --version
- npm run ci

build: off
5 changes: 5 additions & 0 deletions config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

exports.sequelize = {
modelPath: 'model',
};
32 changes: 32 additions & 0 deletions lib/loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

const loading = require('loading');
const path = require('path');
const Sequelize = require('sequelize');

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

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');

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);
};
73 changes: 73 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
"name": "egg-sequelize",
"version": "1.0.0",
"description": "egg Sequelize plugin",
"eggPlugin": {
"name": "sequelize"
},
"repository": {
"type": "git",
"url": "[email protected]:eggjs/egg-sequelize.git"
},
"keywords": [
"egg",
"sequelize",
"egg-plugin",
"mysql"
],
"dependencies": {
"loading": "^1.13.0",
"mysql": "^2.12.0",
"sequelize": "^3.27.0"
},
"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"
},
"engines": {
"node": ">=4.0.0"
},
"scripts": {
"test": "npm run lint && npm run test-local",
"test-local": "egg-bin test",
"cov": "egg-bin cov",
"lint": "eslint --ext js . --fix",
"ci": "npm run lint && npm run cov",
"autod": "autod"
},
"ci": {
"version": "4, 6"
},
"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"
},
"homepage": "https://github.com/eggjs/egg-sequelize#readme",
"author": "jtyjty99999",
"license": "MIT"
}
9 changes: 9 additions & 0 deletions test/fixtures/apps/model-app/app/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

require('should');

module.exports = function(app) {
app.get('/models', function*() {
//
});
};
5 changes: 5 additions & 0 deletions test/fixtures/apps/model-app/config/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

exports.sequelize = {
modelPath: 'model',
};
Empty file.
3 changes: 3 additions & 0 deletions test/fixtures/apps/model-app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "model-app"
}
Loading

0 comments on commit 2178e48

Please sign in to comment.