-
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 1 commit
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
This file was deleted.
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 | ||
* */ | ||
exports.mongoose = { | ||
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. missing jsdoc |
||
url: '', | ||
options: {}, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
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. open 会返回一个 promise,用 beforeStart 吧
|
||
} | ||
|
||
connect() | ||
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. lint 没跑? 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. 看跑 npm test 的提示有跑的 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. 分号 |
||
|
||
db.on('error', (err) => { | ||
app.coreLogger.info('[egg-mongoose] %s error: ', err.message); | ||
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. 要打印 error 对象,否则堆栈会丢失。 db.on('error', err => {
err.message = `[egg-mongoose]${err.message}`;
app.coreLogger.info(err);
}); 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. error(err) |
||
}); | ||
|
||
db.on('disconnected', () => { | ||
app.coreLogger.info('[egg-mongoose] %s disconnected', config.url); | ||
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. 这里打了warn? |
||
connect(); | ||
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. mongoose 不会自动重连的么?需要手动再发起一次 open? 我记得底层的 node-mongodb-native 是有自动重连的,这里不做任何延迟立刻重连看起来有问题 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. @dead-horse 嗯,node-mongodb-native 是有自动重连的,可以在 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. 恩 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. 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* () { | ||
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. 还是不要了,连接成功后应该打个日志 |
||
|
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" | ||
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. add |
||
], | ||
|
@@ -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", | ||
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. 配置 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": { | ||
|
@@ -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" | ||
} | ||
} |
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.
两个 * ?
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.
给出 options 的文档链接