-
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
Conversation
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.
travis-ci is not running. @jtyjty99999
README.zh_CN.md
Outdated
|
||
## 使用场景 | ||
|
||
- Why and What: 描述为什么会有这个插件,它主要在完成一件什么事情。 |
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.
should modify or delete
README.zh_CN.md
Outdated
0.x | ❌ | ||
|
||
### 依赖的插件 | ||
<!-- |
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.
if not use, delete it
config/config.default.js
Outdated
@@ -0,0 +1,6 @@ | |||
'use strict'; | |||
|
|||
exports.mongoose = { |
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.
missing jsdoc
lib/mongoose.js
Outdated
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); |
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.
will it disconnect ? any retry strategy?
package.json
Outdated
"keywords": [ | ||
"egg", | ||
"eggPlugin", | ||
"egg-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.
add egg-model
, mongoose
package.json
Outdated
@@ -0,0 +1,69 @@ | |||
{ | |||
"name": "egg-mongoose", | |||
"version": "1.0.0", |
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.
0.0.1 or 0.1.0
package.json
Outdated
}, | ||
"devDependencies": { | ||
"autod": "^2.7.1", | ||
"egg": "^0.7.0", |
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.
should run npm run autod
to update deps
package.json
Outdated
"homepage": "https://github.com/eggjs/egg-mongoose#readme", | ||
"author": "Villins", | ||
"license": "MIT", | ||
"boilerplate": { |
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.
remove this
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.
我配下 travis
lib/mongoose.js
Outdated
db.open(config.url, config.options); | ||
} | ||
|
||
connect() |
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.
lint 没跑?
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.
看跑 npm test 的提示有跑的
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.
分号
@atian25 我的测试代码有依赖本地 mongodb service,跑不了 ci... |
@villins 参考下我给你发的文章 |
@jtyjty99999 好的,test 的 mongodb service 有点难 mock |
lib/mongoose.js
Outdated
loadModel(app); | ||
|
||
app.beforeStart(function* () { | ||
app.coreLogger.info('[egg-mongoose] start success'); |
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.
在 beforeStart 里面必须确保链接已经建立
lib/mongoose.js
Outdated
db.open(config.url, config.options); | ||
} | ||
|
||
connect() |
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.
分号
lib/mongoose.js
Outdated
connect() | ||
|
||
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 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);
});
lib/mongoose.js
Outdated
app.mongoose = db; | ||
|
||
function connect() { | ||
db.open(config.url, config.options); |
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.
open 会返回一个 promise,用 beforeStart 吧
app.beforeStart(function* () {
yield db.open(config.url, config.options);
});
travis 能开启 mongodb 么 |
Codecov Report
@@ Coverage Diff @@
## master #2 +/- ##
=========================================
Coverage ? 88.88%
=========================================
Files ? 5
Lines ? 36
Branches ? 0
=========================================
Hits ? 32
Misses ? 4
Partials ? 0
Continue to review full report at Codecov.
|
package.json
Outdated
"autod": "^2.7.1", | ||
"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 comment
The reason will be displayed to describe this comment to others. Learn more.
配置 travis 的 service 的话, 要干掉 egg-ci
有 mongodb service 的测试, 需要配置 appveyor 么? 如果不需要, 要干掉 appveyor.yml 不? |
感觉可以干掉 |
|
||
loadModel(app); | ||
|
||
app.beforeStart(function* () { |
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.
直接 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 comment
The reason will be displayed to describe this comment to others. Learn more.
还是不要了,连接成功后应该打个日志
// app/controller/user.js | ||
exports.index = function* () { | ||
this.body = yield this.model.user.find({}); |
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.
加个用例测下文件名大写
|
||
loadModel(app); | ||
|
||
app.beforeStart(function* () { |
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.
还是不要了,连接成功后应该打个日志
lib/mongoose.js
Outdated
}); | ||
|
||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
这里打了warn?
lib/mongoose.js
Outdated
} | ||
|
||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
error(err)
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.
其他 OK,辛苦了
test/mongoose.test.js
Outdated
.expect(200); | ||
|
||
const res = yield request(app.callback()) | ||
.get('/books'); |
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.
缩进或者不用换行
test/mongoose.test.js
Outdated
.send({ name: 'mongoose' }) | ||
.expect(200); | ||
|
||
const res = yield request(app.callback()) |
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.
如下
appveyor.yml
Outdated
@@ -0,0 +1,15 @@ | |||
environment: |
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.
干掉吧,win 没法测
config/config.default.js
Outdated
* * @member Config#mongoose | ||
* * @property {String} url - connect url | ||
* * @property {Object} options - options to pass to the driver and mongoose-specific | ||
* */ |
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 的文档链接
|
||
app.beforeStart(function* () { | ||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
后面打个日志
|
||
* create() { | ||
let user = new this.ctx.model.user({ | ||
name: this.ctx.request.body.name |
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.
末尾少个逗号,是 fixture lint 关掉了?
好吧,问题不大
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.
object 末尾项也需要加上逗号?
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.
嗯, 我们的 eslint 是这样的. 好处是, 如果加了一个 property, review 的时候就是一行, 而不是两行.
你把上面的 eslintignore 的 fixture 干掉, 跑跑看就知道了.
let user = new this.ctx.model.user({ | ||
name: this.ctx.request.body.name | ||
}) | ||
yield user.save() |
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.
分号
} | ||
|
||
return UserController; | ||
} |
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.
分号,末尾多一空行
.eslintignore
Outdated
@@ -0,0 +1,2 @@ | |||
test/fixtures |
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.
这里之前为何要忽略了?@popomore
"app", | ||
"lib" | ||
], | ||
"ci": { |
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.
加一个 "type": "travis"
,就不会生成 appveyor 的 ci 文件了
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.
用到 service 了,不能自动生成 travis。要干掉 egg-ci,并干掉这个配置
@atian25 codecov 这个错误是需要 fix ? |
cov 先不用管吧 |
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.
+1
@dead-horse 在 beforeStart 做了确保连接建立的检查 |
lib/mongoose.js
Outdated
|
||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
app.coreLogger.error(err);
lib/mongoose.js
Outdated
}); | ||
|
||
db.on('disconnected', () => { | ||
app.coreLogger.warn('[egg-mongoose] %s disconnected', config.url); |
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.
这里也改成打印到 error 里面:
app.coreLogger.error(new Error(`[egg-mongoose] ${config.url} disconnected`);
lib/mongoose.js
Outdated
|
||
db.on('disconnected', () => { | ||
app.coreLogger.warn('[egg-mongoose] %s disconnected', config.url); | ||
connect(); |
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.
mongoose 不会自动重连的么?需要手动再发起一次 open?
我记得底层的 node-mongodb-native 是有自动重连的,这里不做任何延迟立刻重连看起来有问题
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.
@dead-horse 嗯,node-mongodb-native 是有自动重连的,可以在 options 设置重试次数和间隔时间
这样子就不用显式的不间断重连机制,ok?
http://mongodb.github.io/node-mongodb-native/2.1/api/Server.html
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.
ok
lib/mongoose.js
Outdated
yield db.open(config.url, config.options); | ||
|
||
const serverStatus = yield db.db.command({ serverStatus: 1 }); | ||
let status = 'bad'; |
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.
const status = serverStatus.ok === 1 ? 'ok' : 'not ok';
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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Book 这个 model 这里有 book controller 里面用到
其他基本差不多了 |
麻烦再 review 下,可以的话合并发布下 |
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.
辛苦了,棒棒哒
合并发布了 |
@villins 你的 npm 账号是多少 |
可以写一篇文档放到教程里面,和 https://eggjs.org/zh-cn/tutorials/mysql.html 类似 |
[skip ci] ## 1.0.0 (2023-08-12) ### ⚠ BREAKING CHANGES * Drop Node.js < 14 and egg < 3 support ### Features * [BREAKING_CHANGE] add unregular model judgement ([#7](#7)) ([ffde348](ffde348)) * bump mongoose version to 5.0 ([#20](#20)) ([a3405d6](a3405d6)) * first implement ([#2](#2)) ([45419ad](45419ad)) * support mongoose global plugin ([#35](#35)) ([1f450fb](1f450fb)) * support multi client ([#15](#15)) ([22d134b](22d134b)) * support plugins for special clients ([#41](#41)) ([67f8f1f](67f8f1f)) * update mongoose's version to be compatable with typescript schemas (Nodejs >=14.x), with Egg>=3.x ([#54](#54)) ([c87f19d](c87f19d)) ### Bug Fixes * doc typo. ([#40](#40)) ([8ef2ebc](8ef2ebc)) * fix __mongoose refer ([#16](#16)) ([b281b15](b281b15)) * hide password of mongo url ([#32](#32)) ([441b6fc](441b6fc)) * remove heartbeat ([#4](#4)) ([343cc78](343cc78)) * replace auth with string replace ([#34](#34)) ([5b9f8ba](5b9f8ba)) * shall re-throw errors on first connect ([#18](#18)) ([dde9037](dde9037)) * **typescript:** types of mongoose should be dependencies ([#24](#24)) ([de7e54e](de7e54e))
close eggjs/egg#366