Skip to content
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

Merged
merged 10 commits into from
Feb 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .autod.conf.js
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',
],
};
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
coverage
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"
}
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. -->
9 changes: 9 additions & 0 deletions .gitignore
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

13 changes: 13 additions & 0 deletions .travis.yml
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
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: 80 additions & 1 deletion README.md
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({});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

加个用例测下文件名大写

}
```

## 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)
5 changes: 5 additions & 0 deletions agent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = agent => {
require('./lib/mongoose')(agent);
};
5 changes: 5 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = app => {
require('./lib/mongoose')(app);
};
7 changes: 7 additions & 0 deletions app/extend/context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

module.exports = {
get model() {
return this.app.model;
},
};
13 changes: 13 additions & 0 deletions config/config.default.js
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: {},
};
51 changes: 51 additions & 0 deletions lib/mongoose.js
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* () {
Copy link
Member

Choose a reason for hiding this comment

The 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);
});

Copy link
Member

Choose a reason for hiding this comment

The 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);
Copy link
Member

Choose a reason for hiding this comment

The 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,
});
}
57 changes: 57 additions & 0 deletions package.json
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": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

加一个 "type": "travis",就不会生成 appveyor 的 ci 文件了

Copy link
Member

Choose a reason for hiding this comment

The 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"
}
}
21 changes: 21 additions & 0 deletions test/fixtures/apps/mongoose-test/app/controller/book.js
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;

};
21 changes: 21 additions & 0 deletions test/fixtures/apps/mongoose-test/app/controller/user.js
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({});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

没有用到 Book 这个 model?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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;

};
9 changes: 9 additions & 0 deletions test/fixtures/apps/mongoose-test/app/model/Book.js
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);
};
9 changes: 9 additions & 0 deletions test/fixtures/apps/mongoose-test/app/model/user.js
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);
};
6 changes: 6 additions & 0 deletions test/fixtures/apps/mongoose-test/app/router.js
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');
};
Loading