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

refactor: support Egg 2 #6

Merged
merged 2 commits into from
Dec 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
sudo: false
language: node_js
node_js:
- '7'
- '8'
- '9'
install:
- npm i npminstall && npminstall
services:
Expand Down
66 changes: 29 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
[npm-image]: https://img.shields.io/npm/v/egg-mongo-native.svg?style=flat-square
[npm-url]: https://npmjs.org/package/egg-mongo-native
[travis-image]: https://img.shields.io/travis/brickyang/egg-mongo.svg?style=flat-square
[travis-url]: https://travis-ci.org/brickyang/egg-mongo
[travis-url]: https://travis-ci.org/brickyang/egg-mongo-native
[codecov-image]: https://img.shields.io/codecov/c/github/brickyang/egg-mongo.svg?style=flat-square
[codecov-url]: https://codecov.io/github/brickyang/egg-mongo?branch=master
[david-image]: https://img.shields.io/david/brickyang/egg-mongo.svg?style=flat-square
Expand All @@ -27,13 +27,14 @@ This plugin base on [node-mongodb-native](https://github.com/mongodb/node-mongod
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.collection('name')
.find(query)
.skip(skip)
.limit(limit)
.project(project)
.sort(sort)
.toArray();
db
.collection('name')
.find(query)
.skip(skip)
.limit(limit)
.project(project)
.sort(sort)
.toArray();
```

and with this plugin
Expand Down Expand Up @@ -93,20 +94,20 @@ The `args` is the object provides the arguments to official API.
Until now, this plugin provides thes APIs

```js
connect() // you don't need to call
insertOne()
findOneAndUpdate()
findOneAndReplace()
findOneAndDelete()
insertMany()
updateMany()
deleteMany()
find()
count()
distinct()
createIndex()
listCollection()
createCollection()
connect(); // you don't need to call
insertOne();
findOneAndUpdate();
findOneAndReplace();
findOneAndDelete();
insertMany();
updateMany();
deleteMany();
find();
count();
distinct();
createIndex();
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/).
Expand All @@ -120,15 +121,16 @@ You can always use `app.mongo.db` to call all official APIs. You can check the A
```js
// Promise
function create(doc) {
app.mongo.insertOne('name', { doc })
.then(result => console.log(result))
.catch(error => console.error(error));
app.mongo
.insertOne('name', { doc })
.then(result => console.log(result))
.catch(error => console.error(error));
}
```

### Sync

````js
```js
// async/await
async function create(doc) {
try {
Expand All @@ -138,17 +140,7 @@ async function create(doc) {
console.error(error);
}
}

// Generator
function* create(doc) {
try {
const result = yield app.mongo.insertOne('name', { doc });
console.log(result);
} catch (errpr) {
console.error(error);
}
}
````
```

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.

Expand Down
62 changes: 27 additions & 35 deletions README.zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
[npm-image]: https://img.shields.io/npm/v/egg-mongo-native.svg?style=flat-square
[npm-url]: https://npmjs.org/package/egg-mongo-native
[travis-image]: https://img.shields.io/travis/brickyang/egg-mongo.svg?style=flat-square
[travis-url]: https://travis-ci.org/brickyang/egg-mongo
[travis-url]: https://travis-ci.org/brickyang/egg-mongo-native
[codecov-image]: https://img.shields.io/codecov/c/github/brickyang/egg-mongo.svg?style=flat-square
[codecov-url]: https://codecov.io/github/brickyang/egg-mongo?branch=master
[david-image]: https://img.shields.io/david/brickyang/egg-mongo.svg?style=flat-square
Expand All @@ -27,13 +27,14 @@
插件对一些常用 API 进行了简单封装以简化使用,同时保留了所有原版属性。例如,使用原版 API 进行一次查找需要写

```js
db.collection('name')
.find(query)
.skip(skip)
.limit(limit)
.project(project)
.sort(sort)
.toArray();
db
.collection('name')
.find(query)
.skip(skip)
.limit(limit)
.project(project)
.sort(sort)
.toArray();
```

封装后
Expand Down Expand Up @@ -95,20 +96,20 @@ app.mongo.insertOne('name', args);
目前插件提供的 API 包括:

```js
connect() // 不需要用户调用
insertOne()
findOneAndUpdate()
findOneAndReplace()
findOneAndDelete()
insertMany()
updateMany()
deleteMany()
find()
count()
distinct()
createIndex()
listCollection()
createCollection()
connect(); // 不需要用户调用
insertOne();
findOneAndUpdate();
findOneAndReplace();
findOneAndDelete();
insertMany();
updateMany();
deleteMany();
find();
count();
distinct();
createIndex();
listCollection();
createCollection();
```

当然,在任何时候你也都可以使用 `app.mongo.db` 调用所有 API。你可以在这里查看所有 API:[Node.js MongoDB Driver API](http://mongodb.github.io/node-mongodb-native/2.2/api/)。
Expand All @@ -122,9 +123,10 @@ createCollection()
```js
// Promise
function create(doc) {
app.mongo.insertOne('name', { doc })
.then(result => console.log(result))
.catch(error => console.error(error));
app.mongo
.insertOne('name', { doc })
.then(result => console.log(result))
.catch(error => console.error(error));
}
```

Expand All @@ -140,16 +142,6 @@ async function create(doc) {
console.error(error);
}
}

// 使用 Generator
function* create(doc) {
try {
const result = yield app.mongo.insertOne('name', { doc });
console.log(result);
} catch (errpr) {
console.error(error);
}
}
```

如果你使用 `app.mongo.db` 调用原版 API,则也可以使用回调函数。插件封装的 API 不支持回调函数,因为 Promise 和 async/await 更加优雅。
Expand Down
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "egg-mongo-native",
"version": "1.0.7",
"version": "2.0.0",
"description": "MongoDB egg.js plugin using native driver.",
"eggPlugin": {
"name": "mongo"
Expand All @@ -14,22 +14,22 @@
"mongo-driver"
],
"dependencies": {
"mongodb": "^2.2.29"
"mongodb": "^2.2.0"
},
"devDependencies": {
"autod": "^2.8.0",
"autod": "^3.0.1",
"autod-egg": "^1.0.0",
"egg": "^1.5.0",
"egg-bin": "^4.0.4",
"egg": "^2.0.0",
"egg-bin": "^4.3.6",
"egg-ci": "^1.8.0",
"egg-mock": "^3.8.0",
"eslint": "^4.1.1",
"eslint-config-egg": "^5.0.0",
"egg-mock": "^3.13.1",
"eslint": "^4.13.0",
"eslint-config-egg": "^5.1.1",
"supertest": "^3.0.0",
"webstorm-disable-index": "^1.2.0"
},
"engines": {
"node": ">=6.0.0"
"node": ">=8.9.0"
},
"scripts": {
"test": "npm run lint -- --fix && npm run pkgfiles && npm run test-local",
Expand All @@ -46,7 +46,7 @@
"lib"
],
"ci": {
"version": "6, 8"
"version": "8, 9"
},
"repository": {
"type": "git",
Expand Down
Loading