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
76 changes: 0 additions & 76 deletions README.zh_CN.md

This file was deleted.

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

/**
* * mongoose default config
* * @member Config#mongoose
* * @property {String} url - connect url
* * @property {Object} options - options to pass to the driver and mongoose-specific
* */
Copy link
Member

Choose a reason for hiding this comment

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

两个 * ?

Copy link
Member

Choose a reason for hiding this comment

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

给出 options 的文档链接

exports.mongoose = {
Copy link
Member

Choose a reason for hiding this comment

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

missing jsdoc

url: '',
options: {},
Expand Down
23 changes: 21 additions & 2 deletions lib/mongoose.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,33 @@ module.exports = app => {
assert(config.url, '[egg-mongoose] url is required on config');
app.coreLogger.info('[egg-mongoose] connecting %s', config.url);

const db = mongoose.createConnection(config.url);
const db = mongoose.createConnection();
db.Schema = mongoose.Schema;
app.mongoose = db;

db.once('open', () => {
function connect() {
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.

open 会返回一个 promise,用 beforeStart 吧

app.beforeStart(function* () {
  yield db.open(config.url, config.options);
});

}

connect()
Copy link
Member

Choose a reason for hiding this comment

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

lint 没跑?

Copy link
Member

Choose a reason for hiding this comment

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

配好了

Copy link
Contributor Author

Choose a reason for hiding this comment

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

看跑 npm test 的提示有跑的

Copy link
Member

Choose a reason for hiding this comment

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

分号


db.on('error', (err) => {
app.coreLogger.info('[egg-mongoose] %s error: ', err.message);
Copy link
Member

Choose a reason for hiding this comment

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

要打印 error 对象,否则堆栈会丢失。

db.on('error', err => {
  err.message = `[egg-mongoose]${err.message}`;
  app.coreLogger.info(err);
});

Copy link
Member

Choose a reason for hiding this comment

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

error(err)

});

db.on('disconnected', () => {
app.coreLogger.info('[egg-mongoose] %s disconnected', config.url);
Copy link
Member

Choose a reason for hiding this comment

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

这里打了warn?

connect();
Copy link
Member

Choose a reason for hiding this comment

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

mongoose 不会自动重连的么?需要手动再发起一次 open?

我记得底层的 node-mongodb-native 是有自动重连的,这里不做任何延迟立刻重连看起来有问题

Copy link
Contributor Author

Choose a reason for hiding this comment

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

这里我测试下

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dead-horse 嗯,node-mongodb-native 是有自动重连的,可以在 options 设置重试次数和间隔时间
这样子就不用显式的不间断重连机制,ok?
http://mongodb.github.io/node-mongodb-native/2.1/api/Server.html

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

});

db.on('connected', () => {
app.coreLogger.info('[egg-mongoose] connected %s successfully', config.url);
});

db.on('reconnected', () => {
app.coreLogger.info('[egg-mongoose] reconnected %s successfully', config.url);
});

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.

还是不要了,连接成功后应该打个日志

Expand Down
27 changes: 8 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
{
"name": "egg-mongoose",
"version": "1.0.0",
"version": "0.1.0",
"description": "egg mongoose plugin",
"eggPlugin": {
"name": "mongoose"
},
"keywords": [
"egg",
"egg-model",
"mongoose",
"eggPlugin",
"egg-plugin"
Copy link
Member

Choose a reason for hiding this comment

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

add egg-model, mongoose

],
Expand All @@ -15,13 +17,13 @@
},
"devDependencies": {
"autod": "^2.7.1",
"egg": "^0.7.0",
"egg-bin": "^1.10.0",
"egg": "^0.12.0",
"egg-bin": "^2.1.0",
"egg-ci": "^1.1.0",
Copy link
Member

Choose a reason for hiding this comment

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

配置 travis 的 service 的话, 要干掉 egg-ci

"egg-mock": "^2.0.0",
"eslint": "^3.13.1",
"egg-mock": "^3.0.0",
"eslint": "^3.15.0",
"eslint-config-egg": "^3.2.0",
"supertest": "^2.0.1",
"supertest": "^3.0.0",
"webstorm-disable-index": "^1.1.2"
},
"engines": {
Expand Down Expand Up @@ -52,18 +54,5 @@
},
"bugs": {
"url": "https://github.com/eggjs/egg/issues"
},
"homepage": "https://github.com/eggjs/egg-mongoose#readme",
"author": "Villins",
"license": "MIT",
"boilerplate": {
"name": "egg-boilerplate-plugin",
"version": "1.7.0",
"description": "boilerplate for egg plugin",
"repository": {
"type": "git",
"url": "[email protected]:eggjs/egg-boilerplate-plugin.git"
},
"homepage": "https://github.com/eggjs/egg-boilerplate-plugin"
}
}