-
Notifications
You must be signed in to change notification settings - Fork 104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: first implement #2
Changes from all commits
4ee37cc
3562427
ddbc5f5
bfa6bba
8366ea7
f206fa3
8c36df8
1842f90
54fac28
fb1b0f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
write: true, | ||
prefix: '^', | ||
test: [ | ||
'test', | ||
'benchmark', | ||
], | ||
devdep: [ | ||
'egg', | ||
'egg-bin', | ||
'autod', | ||
'eslint', | ||
'eslint-config-egg', | ||
'supertest', | ||
], | ||
exclude: [ | ||
'./test/fixtures', | ||
], | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
coverage |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "eslint-config-egg" | ||
} |
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. --> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
logs/ | ||
npm-debug.log | ||
node_modules/ | ||
coverage/ | ||
.idea/ | ||
run/ | ||
.DS_Store | ||
*.swp | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
sudo: false | ||
language: node_js | ||
node_js: | ||
- '6' | ||
- '7' | ||
install: | ||
- npm i npminstall && npminstall | ||
script: | ||
- npm run ci | ||
after_script: | ||
- npminstall codecov && codecov | ||
services: | ||
- mongodb |
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. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,80 @@ | ||
# egg-mongoose | ||
# egg-mongoose | ||
[![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-mongoose.svg?style=flat-square | ||
[npm-url]: https://npmjs.org/package/egg-mongoose | ||
[travis-image]: https://img.shields.io/travis/eggjs/egg-mongoose.svg?style=flat-square | ||
[travis-url]: https://travis-ci.org/eggjs/egg-mongoose | ||
[codecov-image]: https://img.shields.io/codecov/c/github/eggjs/egg-mongoose.svg?style=flat-square | ||
[codecov-url]: https://codecov.io/github/eggjs/egg-mongoose?branch=master | ||
[david-image]: https://img.shields.io/david/eggjs/egg-mongoose.svg?style=flat-square | ||
[david-url]: https://david-dm.org/eggjs/egg-mongoose | ||
[snyk-image]: https://snyk.io/test/npm/egg-mongoose/badge.svg?style=flat-square | ||
[snyk-url]: https://snyk.io/test/npm/egg-mongoose | ||
[download-image]: https://img.shields.io/npm/dm/egg-mongoose.svg?style=flat-square | ||
[download-url]: https://npmjs.org/package/egg-mongoose | ||
|
||
Egg's mongoose plugin. | ||
|
||
## Install | ||
|
||
```bash | ||
$ npm i egg-mongoose --save | ||
``` | ||
|
||
## Usage | ||
|
||
```js | ||
// {app_root}/config/plugin.js | ||
exports.mongoose = { | ||
enable: true, | ||
package: 'egg-mongoose', | ||
}; | ||
``` | ||
|
||
## Configuration | ||
|
||
```js | ||
// {app_root}/config/config.default.js | ||
exports.mongoose = { | ||
url: 'mongodb://127.0.0.1/example', | ||
options: {} | ||
}; | ||
``` | ||
|
||
see [config/config.default.js](config/config.default.js) for more detail. | ||
|
||
## Example | ||
```js | ||
// app/model/user.js | ||
module.exports = mongoose => { | ||
const UserSchema = new mongoose.Schema({ | ||
userName: { type: String }, | ||
password: { type: String } | ||
}); | ||
|
||
return mongoose.model('User', UserSchema); | ||
} | ||
|
||
// app/controller/user.js | ||
exports.index = function* () { | ||
this.body = yield this.model.user.find({}); | ||
} | ||
``` | ||
|
||
## Questions & Suggestions | ||
|
||
Please open an issue [here](https://github.com/eggjs/egg-mongoose/issues). | ||
|
||
## Contribution | ||
|
||
If you are a contributor, follow [CONTRIBUTING](https://eggjs.org/zh-cn/contributing.html). | ||
|
||
## License | ||
|
||
[MIT](LICENSE) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use strict'; | ||
|
||
module.exports = agent => { | ||
require('./lib/mongoose')(agent); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use strict'; | ||
|
||
module.exports = app => { | ||
require('./lib/mongoose')(app); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
get model() { | ||
return this.app.model; | ||
}, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use strict'; | ||
|
||
/** | ||
* mongoose default config | ||
* http://mongoosejs.com/docs/api.html#index_Mongoose-createConnection | ||
* @member Config#mongoose | ||
* @property {String} url - connect url | ||
* @property {Object} options - options to pass to the driver and mongoose-specific | ||
*/ | ||
exports.mongoose = { | ||
url: '', | ||
options: {}, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const path = require('path'); | ||
const mongoose = require('mongoose'); | ||
|
||
module.exports = app => { | ||
const config = app.config.mongoose; | ||
assert(config.url, '[egg-mongoose] url is required on config'); | ||
app.coreLogger.info('[egg-mongoose] connecting %s', config.url); | ||
|
||
const db = mongoose.createConnection(); | ||
db.Schema = mongoose.Schema; | ||
app.mongoose = db; | ||
|
||
db.on('error', err => { | ||
err.message = `[egg-mongoose]${err.message}`; | ||
app.coreLogger.error(err); | ||
}); | ||
|
||
db.on('disconnected', () => { | ||
app.coreLogger.error(`[egg-mongoose] ${config.url} disconnected`); | ||
}); | ||
|
||
db.on('connected', () => { | ||
app.coreLogger.info(`[egg-mongoose] ${config.url} connected successfully`); | ||
}); | ||
|
||
db.on('reconnected', () => { | ||
app.coreLogger.info(`[egg-mongoose] ${config.url} reconnected successfully`); | ||
}); | ||
|
||
loadModel(app); | ||
|
||
app.beforeStart(function* () { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 直接 function 返回 promise 就行了吧 app.beforeStart(function() {
app.coreLogger.info('[egg-mongoose] starting...');
return db.open(config.url, config.options);
}); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 还是不要了,连接成功后应该打个日志 |
||
app.coreLogger.info('[egg-mongoose] starting...'); | ||
yield db.open(config.url, config.options); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 后面打个日志 |
||
|
||
const serverStatus = yield db.db.command({ serverStatus: 1 }); | ||
const status = serverStatus.ok === 1 ? 'ok' : 'not ok'; | ||
|
||
app.coreLogger.info('[egg-mongoose] start successfully and server status is %s', status); | ||
}); | ||
}; | ||
|
||
function loadModel(app) { | ||
const dir = path.join(app.config.baseDir, 'app/model'); | ||
app.loader.loadToApp(dir, 'model', { | ||
inject: app.mongoose, | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
{ | ||
"name": "egg-mongoose", | ||
"version": "0.1.0", | ||
"description": "egg mongoose plugin", | ||
"eggPlugin": { | ||
"name": "mongoose" | ||
}, | ||
"keywords": [ | ||
"egg", | ||
"egg-model", | ||
"mongoose", | ||
"eggPlugin", | ||
"egg-plugin" | ||
], | ||
"dependencies": { | ||
"mongoose": "^4.8.2" | ||
}, | ||
"devDependencies": { | ||
"autod": "^2.7.1", | ||
"egg": "^0.12.0", | ||
"egg-bin": "^2.1.0", | ||
"egg-mock": "^3.0.0", | ||
"eslint": "^3.15.0", | ||
"eslint-config-egg": "^3.2.0", | ||
"supertest": "^3.0.0", | ||
"webstorm-disable-index": "^1.1.2" | ||
}, | ||
"engines": { | ||
"node": ">=6.0.0" | ||
}, | ||
"scripts": { | ||
"test": "npm run lint -- --fix && npm run test-local", | ||
"test-local": "egg-bin test", | ||
"cov": "egg-bin cov", | ||
"lint": "eslint .", | ||
"ci": "npm run lint && npm run cov", | ||
"autod": "autod" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"app.js", | ||
"agent.js", | ||
"config", | ||
"app", | ||
"lib" | ||
], | ||
"ci": { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 加一个 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 用到 service 了,不能自动生成 travis。要干掉 egg-ci,并干掉这个配置 |
||
"version": "6, 7" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/eggjs/egg-mongoose.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/eggjs/egg/issues" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use strict'; | ||
|
||
module.exports = app => { | ||
class BookController extends app.Controller { | ||
* index() { | ||
const books = yield this.ctx.model.Book.find({}); | ||
this.ctx.body = books; | ||
} | ||
|
||
* create() { | ||
const book = new this.ctx.model.Book({ | ||
name: this.ctx.request.body.name, | ||
}); | ||
yield book.save(); | ||
this.ctx.body = book; | ||
} | ||
} | ||
|
||
return BookController; | ||
|
||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use strict'; | ||
|
||
module.exports = app => { | ||
class UserController extends app.Controller { | ||
* index() { | ||
const users = yield this.ctx.model.user.find({}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 没有用到 Book 这个 model? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Book 这个 model 这里有 book controller 里面用到 |
||
this.ctx.body = users; | ||
} | ||
|
||
* create() { | ||
const user = new this.ctx.model.user({ | ||
name: this.ctx.request.body.name, | ||
}); | ||
yield user.save(); | ||
this.ctx.body = user; | ||
} | ||
} | ||
|
||
return UserController; | ||
|
||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
'use strict'; | ||
|
||
module.exports = mongoose => { | ||
const BoookSchema = new mongoose.Schema({ | ||
name: { type: String }, | ||
}); | ||
|
||
return mongoose.model('Book', BoookSchema); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
'use strict'; | ||
|
||
module.exports = mongoose => { | ||
const UserSchema = new mongoose.Schema({ | ||
name: { type: String }, | ||
}); | ||
|
||
return mongoose.model('User', UserSchema); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
'use strict'; | ||
|
||
module.exports = function(app) { | ||
app.resources('users', '/users', 'user'); | ||
app.resources('books', '/books', 'book'); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
加个用例测下文件名大写