Skip to content

Commit

Permalink
feat: support replica set uri
Browse files Browse the repository at this point in the history
  • Loading branch information
brickyang committed Dec 11, 2017
1 parent f8dccd0 commit cf7bc33
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 153 deletions.
58 changes: 49 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![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]
Expand All @@ -20,9 +19,12 @@

[**中文版**](https://github.com/brickyang/egg-mongo/blob/master/README.zh_CN.md)

This plugin base on [node-mongodb-native](https://github.com/mongodb/node-mongodb-native), provides the official MongoDB native driver and APIs.
This plugin base on
[node-mongodb-native](https://github.com/mongodb/node-mongodb-native), provides
the official MongoDB native driver and APIs.

It wraps some frequently-used API to make it easy to use but keep all properties as it is. For example, to find a document you need this with official API
It wraps some frequently-used API to make it easy to use but keep all properties
as it is. For example, to find a document you need this with official API

```js
db
Expand Down Expand Up @@ -59,13 +61,45 @@ exports.mongo = {

## Configuration

### Single Instance

```js
// {app_root}/config/config.default.js
exports.mongo = {
client: {
host: 'localhost',
port: 27017,
host: 'host',
port: 'port',
name: 'test',
user: 'user',
password: 'password',
},
};
```

### Replica Set (v2.1.0 or higher)

```js
// mongodb://host1:port1,host2:port2/name?replicaSet=test
exports.mongo = {
client: {
host: 'host1, host2',
port: 'port1, port2',
name: 'name',
option: {
replicaSet: 'test',
},
},
};

// mongodb://host:port1,host:port2/name?replicaSet=test
exports.mongo = {
client: {
host: 'host', // or ['host']
port: 'port1, port2', // or ['port1', 'port2']
name: 'name',
option: {
replicaSet: 'test',
},
},
};
```
Expand All @@ -74,7 +108,9 @@ see [config/config.default.js](config/config.default.js) for more detail.

## Example

The APIs provided by plugin usually need two arguments. The first is commonly the collection name, and the second is an object keeps the arguments of official API. For example, to insert one document with official API
The APIs provided by plugin usually need two arguments. The first is commonly
the collection name, and the second is an object keeps the arguments of official
API. For example, to insert one document with official API

```js
db.collection('name').insertOne(doc, options);
Expand Down Expand Up @@ -108,7 +144,9 @@ listCollection();
createCollection();
```

You can always use `app.mongo.db` to call all official APIs. You can check the APIs here: [Node.js MongoDB Driver API](http://mongodb.github.io/node-mongodb-native/2.2/api/).
You can always use `app.mongo.db` to call all official APIs. You can check the
APIs here:
[Node.js MongoDB Driver API](http://mongodb.github.io/node-mongodb-native/2.2/api/).

## Promise

Expand Down Expand Up @@ -140,7 +178,9 @@ async function create(doc) {
}
```

If you use `app.mongo.db` you could use callback(usually the last argument), but this plugin doesn't supports callback because Promise and async/await are better choice.
If you use `app.mongo.db` you could use callback(usually the last argument), but
this plugin doesn't supports callback because Promise and async/await are better
choice.

## License

Expand Down
61 changes: 50 additions & 11 deletions README.zh_CN.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![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]
Expand All @@ -20,9 +19,12 @@

[**English**](https://github.com/brickyang/egg-mongo/blob/master/README.md)

本插件基于 [node-mongodb-native](https://github.com/mongodb/node-mongodb-native),提供了 MongoDB 官方 driver 及 API。
本插件基于
[node-mongodb-native](https://github.com/mongodb/node-mongodb-native),提供了
MongoDB 官方 driver 及 API。

插件对一些常用 API 进行了简单封装以简化使用,同时保留了所有原版属性。例如,使用原版 API 进行一次查找需要写
插件对一些常用 API 进行了简单封装以简化使用,同时保留了所有原版属性。例如,使用
原版 API 进行一次查找需要写

```js
db
Expand Down Expand Up @@ -61,13 +63,45 @@ exports.mongo = {

## 配置

```javascript
### 单实例

```js
// {app_root}/config/config.default.js
exports.mongo = {
client: {
host: 'localhost',
port: 27017,
host: 'host',
port: 'port',
name: 'test',
user: 'user',
password: 'password',
},
};
```

### 集群 (v2.1.0 以上 )

```js
// mongodb://host1:port1,host2:port2/name?replicaSet=test
exports.mongo = {
client: {
host: 'host1, host2',
port: 'port1, port2',
name: 'name',
option: {
replicaSet: 'test',
},
},
};

// mongodb://host:port1,host:port2/name?replicaSet=test
exports.mongo = {
client: {
host: 'host', // or ['host']
port: 'port1, port2', // or ['port1', 'port2']
name: 'name',
option: {
replicaSet: 'test',
},
},
};
```
Expand All @@ -76,7 +110,9 @@ exports.mongo = {

## 使用示例

本插件提供的 API 只是对原版 API 进行了必要的简化,所有属性名称与原版 API 一致。所有针对文档操作的 API,通常接受 2 个参数,第一个参数是 collection 名称,第二个参数是一个对象,属性名即为原版 API 的所有参数。例如,使用原版 API 进行一次插入
本插件提供的 API 只是对原版 API 进行了必要的简化,所有属性名称与原版 API 一致。
所有针对文档操作的 API,通常接受 2 个参数,第一个参数是 collection 名称,第二个
参数是一个对象,属性名即为原版 API 的所有参数。例如,使用原版 API 进行一次插入

```js
db.collection('name').insertOne(doc, options);
Expand Down Expand Up @@ -110,11 +146,13 @@ listCollection();
createCollection();
```

当然,在任何时候你也都可以使用 `app.mongo.db` 调用所有 API。你可以在这里查看所有 API:[Node.js MongoDB Driver API](http://mongodb.github.io/node-mongodb-native/2.2/api/)
当然,在任何时候你也都可以使用 `app.mongo.db` 调用所有 API。你可以在这里查看所有
API:[Node.js MongoDB Driver API](http://mongodb.github.io/node-mongodb-native/2.2/api/)

## 同步与异步

`node-mongodb-native` 所有 API 都支持 Promise,因此你可以自由地以异步或同步方式使用本插件。
`node-mongodb-native` 所有 API 都支持 Promise,因此你可以自由地以异步或同步方式
使用本插件。

### 异步

Expand Down Expand Up @@ -142,7 +180,8 @@ async function create(doc) {
}
```

如果你使用 `app.mongo.db` 调用原版 API,则也可以使用回调函数。插件封装的 API 不支持回调函数,因为 Promise 和 async/await 更加优雅。
如果你使用 `app.mongo.db` 调用原版 API,则也可以使用回调函数。插件封装的 API 不
支持回调函数,因为 Promise 和 async/await 更加优雅。

Node.js 7.6 开始已经原生支持 async/await,不再需要 Babel。

Expand Down
14 changes: 2 additions & 12 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,11 @@ function createMongo(config, app) {
const client = new MongoDB(config);

client.on('connect', () => {
app.coreLogger.info(
'[egg-mongo] Connect success on mongo://%s:%s/%s.',
config.host,
config.port,
config.name
);
app.coreLogger.info(`[egg-mongo] Connect success on ${client.url}.`);
});
/* istanbul ignore next */
client.on('error', error => {
app.coreLogger.warn(
'[egg-mongo] Connect fail on mongo://%s:%s/%s.',
config.host,
config.port,
config.name
);
app.coreLogger.warn(`[egg-mongo] Connect fail on ${client.url}.`);
app.coreLogger.error(error);
});

Expand Down
Loading

0 comments on commit cf7bc33

Please sign in to comment.