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

feat: first implement #2

merged 10 commits into from
Feb 16, 2017

Conversation

villins
Copy link
Contributor

@villins villins commented Feb 15, 2017

@villins
Copy link
Contributor Author

villins commented Feb 15, 2017

@jtyjty99999

@jtyjty99999
Copy link
Member

@atian25 @popomore 也来看下?

Copy link
Member

@atian25 atian25 left a 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: 描述为什么会有这个插件,它主要在完成一件什么事情。
Copy link
Member

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 | ❌

### 依赖的插件
<!--
Copy link
Member

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

@@ -0,0 +1,6 @@
'use strict';

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

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

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"
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

package.json Outdated
@@ -0,0 +1,69 @@
{
"name": "egg-mongoose",
"version": "1.0.0",
Copy link
Member

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",
Copy link
Member

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": {
Copy link
Member

Choose a reason for hiding this comment

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

remove this

Copy link
Member

@atian25 atian25 left a 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()
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.

分号

@villins
Copy link
Contributor Author

villins commented Feb 15, 2017

@atian25 我的测试代码有依赖本地 mongodb service,跑不了 ci...

@jtyjty99999
Copy link
Member

@villins 参考下我给你发的文章

@villins
Copy link
Contributor Author

villins commented Feb 15, 2017

@jtyjty99999 好的,test 的 mongodb service 有点难 mock

lib/mongoose.js Outdated
loadModel(app);

app.beforeStart(function* () {
app.coreLogger.info('[egg-mongoose] start success');
Copy link
Member

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

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

@atian25
Copy link
Member

atian25 commented Feb 15, 2017 via email

lib/mongoose.js Outdated
app.mongoose = db;

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

@popomore
Copy link
Member

travis 能开启 mongodb 么

@jtyjty99999
Copy link
Member

@codecov
Copy link

codecov bot commented Feb 15, 2017

Codecov Report

❗ No coverage uploaded for pull request base (master@31b4a38). Click here to learn what that means.
The diff coverage is 88.88%.

@@            Coverage Diff            @@
##             master       #2   +/-   ##
=========================================
  Coverage          ?   88.88%           
=========================================
  Files             ?        5           
  Lines             ?       36           
  Branches          ?        0           
=========================================
  Hits              ?       32           
  Misses            ?        4           
  Partials          ?        0
Impacted Files Coverage Δ
config/config.default.js 100% <100%> (ø)
agent.js 100% <100%> (ø)
app/extend/context.js 100% <100%> (ø)
app.js 100% <100%> (ø)
lib/mongoose.js 86.2% <86.2%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 31b4a38...fb1b0f7. Read the comment docs.

package.json Outdated
"autod": "^2.7.1",
"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

@atian25
Copy link
Member

atian25 commented Feb 15, 2017

有 mongodb service 的测试, 需要配置 appveyor 么? 如果不需要, 要干掉 appveyor.yml 不?

cc @jtyjty99999 @fengmk2

@popomore
Copy link
Member

感觉可以干掉


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/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.

加个用例测下文件名大写


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.

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

lib/mongoose.js Outdated
});

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?

lib/mongoose.js Outdated
}

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(err)

Copy link
Member

@atian25 atian25 left a comment

Choose a reason for hiding this comment

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

其他 OK,辛苦了

.expect(200);

const res = yield request(app.callback())
.get('/books');
Copy link
Member

Choose a reason for hiding this comment

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

缩进或者不用换行

.send({ name: 'mongoose' })
.expect(200);

const res = yield request(app.callback())
Copy link
Member

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:
Copy link
Member

Choose a reason for hiding this comment

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

干掉吧,win 没法测

* * @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 的文档链接


app.beforeStart(function* () {
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.

后面打个日志


* create() {
let user = new this.ctx.model.user({
name: this.ctx.request.body.name
Copy link
Member

Choose a reason for hiding this comment

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

末尾少个逗号,是 fixture lint 关掉了?

好吧,问题不大

Copy link
Contributor Author

Choose a reason for hiding this comment

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

object 末尾项也需要加上逗号?

Copy link
Member

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

Choose a reason for hiding this comment

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

分号

}

return UserController;
}
Copy link
Member

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
Copy link
Member

Choose a reason for hiding this comment

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

这里之前为何要忽略了?@popomore

"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,并干掉这个配置

@villins
Copy link
Contributor Author

villins commented Feb 16, 2017

@atian25 codecov 这个错误是需要 fix ?

@atian25
Copy link
Member

atian25 commented Feb 16, 2017

cov 先不用管吧

Copy link
Member

@atian25 atian25 left a comment

Choose a reason for hiding this comment

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

+1

@villins
Copy link
Contributor Author

villins commented Feb 16, 2017

@dead-horse 在 beforeStart 做了确保连接建立的检查

lib/mongoose.js Outdated

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.

app.coreLogger.error(err);

lib/mongoose.js Outdated
});

db.on('disconnected', () => {
app.coreLogger.warn('[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.

这里也改成打印到 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();
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

lib/mongoose.js Outdated
yield db.open(config.url, config.options);

const serverStatus = yield db.db.command({ serverStatus: 1 });
let status = 'bad';
Copy link
Member

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({});
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 里面用到

@dead-horse
Copy link
Member

其他基本差不多了

@villins
Copy link
Contributor Author

villins commented Feb 16, 2017

麻烦再 review 下,可以的话合并发布下

Copy link
Member

@atian25 atian25 left a comment

Choose a reason for hiding this comment

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

辛苦了,棒棒哒

@dead-horse
Copy link
Member

合并发布了

@dead-horse dead-horse merged commit 45419ad into eggjs:master Feb 16, 2017
@dead-horse
Copy link
Member

@villins 你的 npm 账号是多少

@dead-horse
Copy link
Member

可以写一篇文档放到教程里面,和 https://eggjs.org/zh-cn/tutorials/mysql.html 类似

fengmk2 pushed a commit that referenced this pull request Aug 12, 2023
[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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

egg-mongoose
5 participants