From e4a084aa84c1c6abfbb20d319e8b8cf9d8986b50 Mon Sep 17 00:00:00 2001 From: brickyang Date: Sun, 10 Dec 2017 23:22:39 +0800 Subject: [PATCH 1/2] refactor: support Egg 2 --- .travis.yml | 14 ++--- README.md | 66 +++++++++++------------- README.zh_CN.md | 62 ++++++++++------------ package.json | 20 +++---- test/mongo.test.js | 126 ++++++++++++++++++++++++++------------------- 5 files changed, 144 insertions(+), 144 deletions(-) diff --git a/.travis.yml b/.travis.yml index 37fae7d..960bc57 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,15 +1,11 @@ sudo: false language: node_js node_js: -- '7' -- '8' + - '8' + - '9' install: -- npm i npminstall && npminstall -services: -- mongodb -before_script: -- sleep 15 + - npm i npminstall && npminstall script: -- npm run ci + - npm run ci after_script: -- npminstall codecov && codecov + - npminstall codecov && codecov diff --git a/README.md b/README.md index 0129965..825e082 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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/). @@ -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 { @@ -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. diff --git a/README.zh_CN.md b/README.zh_CN.md index d047bd8..a362242 100644 --- a/README.zh_CN.md +++ b/README.zh_CN.md @@ -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 @@ -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(); ``` 封装后 @@ -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/)。 @@ -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)); } ``` @@ -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 更加优雅。 diff --git a/package.json b/package.json index 6158861..235613d 100644 --- a/package.json +++ b/package.json @@ -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" @@ -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", @@ -46,7 +46,7 @@ "lib" ], "ci": { - "version": "6, 8" + "version": "8, 9" }, "repository": { "type": "git", diff --git a/test/mongo.test.js b/test/mongo.test.js index b906e2d..3c621c2 100644 --- a/test/mongo.test.js +++ b/test/mongo.test.js @@ -26,7 +26,9 @@ describe('test/mongo.test.js', () => { describe('insertOne()', () => { it('should insert success', async () => { - const result = await app.mongo.insertOne(NAME, { doc: { title: 'new doc' } }); + const result = await app.mongo.insertOne(NAME, { + doc: { title: 'new doc' }, + }); assert(result.insertedCount === 1); assert(typeof result.insertedId === 'string'); assert(result.value.hasOwnProperty('_id')); @@ -116,7 +118,12 @@ describe('test/mongo.test.js', () => { describe('findOneAndReplace()', () => { let id; - beforeEach(async () => ({ insertedId: id } = await app.mongo.insertOne(NAME, { doc: { title: 'new doc' } }))); + beforeEach( + async () => + ({ insertedId: id } = await app.mongo.insertOne(NAME, { + doc: { title: 'new doc' }, + })) + ); it('should replace success', async () => { const result = await app.mongo.findOneAndReplace(NAME, { @@ -168,10 +175,9 @@ describe('test/mongo.test.js', () => { describe('findOneAndDelete()', () => { let id; beforeEach(async () => { - const result = await app.mongo.insertMany(NAME, { docs: [ - { title: 'new doc' }, - { title: 'new doc' }, - ] }); + const result = await app.mongo.insertMany(NAME, { + docs: [{ title: 'new doc' }, { title: 'new doc' }], + }); id = result.insertedIds[0].toString(); }); @@ -215,11 +221,7 @@ describe('test/mongo.test.js', () => { describe('insertMany()', () => { it('should insert success', async () => { const result = await app.mongo.insertMany(NAME, { - docs: [ - { title: 'doc1' }, - { title: 'doc2' }, - { title: 'doc3' }, - ], + docs: [{ title: 'doc1' }, { title: 'doc2' }, { title: 'doc3' }], }); assert(result.insertedCount === 3); assert(Array.isArray(result.insertedIds)); @@ -238,14 +240,17 @@ describe('test/mongo.test.js', () => { }); describe('updateMany()', async () => { - beforeEach(async () => await app.mongo.insertMany(NAME, { - docs: [ - { title: 'doc1', type: 'doc' }, - { title: 'doc2', type: 'doc' }, - { title: 'doc3', type: 'text' }, - { title: 'doc4', type: 'text' }, - ], - })); + beforeEach( + async () => + await app.mongo.insertMany(NAME, { + docs: [ + { title: 'doc1', type: 'doc' }, + { title: 'doc2', type: 'doc' }, + { title: 'doc3', type: 'text' }, + { title: 'doc4', type: 'text' }, + ], + }) + ); afterEach(async () => await app.mongo.deleteMany(NAME, {})); @@ -301,14 +306,17 @@ describe('test/mongo.test.js', () => { }); describe('deleteMany()', () => { - beforeEach(async () => await app.mongo.insertMany(NAME, { - docs: [ - { title: 'doc1', type: 'doc' }, - { title: 'doc2', type: 'doc' }, - { title: 'doc3', type: 'text' }, - { title: 'doc4', type: 'text' }, - ], - })); + beforeEach( + async () => + await app.mongo.insertMany(NAME, { + docs: [ + { title: 'doc1', type: 'doc' }, + { title: 'doc2', type: 'doc' }, + { title: 'doc3', type: 'text' }, + { title: 'doc4', type: 'text' }, + ], + }) + ); afterEach(async () => await app.mongo.deleteMany(NAME, {})); @@ -326,13 +334,16 @@ describe('test/mongo.test.js', () => { }); describe('find()', () => { - beforeEach(async () => await app.mongo.insertMany(NAME, { - docs: [ - { index: 1, type: 'doc' }, - { index: 2, type: 'doc' }, - { index: 3, type: 'doc' }, - ], - })); + beforeEach( + async () => + await app.mongo.insertMany(NAME, { + docs: [ + { index: 1, type: 'doc' }, + { index: 2, type: 'doc' }, + { index: 3, type: 'doc' }, + ], + }) + ); it('should find success', async () => { const result = await app.mongo.find(NAME, { @@ -374,14 +385,17 @@ describe('test/mongo.test.js', () => { }); describe('count()', () => { - beforeEach(async () => await app.mongo.insertMany(NAME, { - docs: [ - { type: 'doc' }, - { type: 'doc' }, - { type: 'text' }, - { type: 'text' }, - ], - })); + beforeEach( + async () => + await app.mongo.insertMany(NAME, { + docs: [ + { type: 'doc' }, + { type: 'doc' }, + { type: 'text' }, + { type: 'text' }, + ], + }) + ); it('should count success', async () => { const result = await app.mongo.count(NAME, { @@ -397,14 +411,17 @@ describe('test/mongo.test.js', () => { }); describe('distinct()', () => { - beforeEach(async () => await app.mongo.insertMany(NAME, { - docs: [ - { type: 'doc' }, - { type: 'doc' }, - { type: 'text' }, - { type: 'text' }, - ], - })); + beforeEach( + async () => + await app.mongo.insertMany(NAME, { + docs: [ + { type: 'doc' }, + { type: 'doc' }, + { type: 'text' }, + { type: 'text' }, + ], + }) + ); it('should distinct success', async () => { const result = await app.mongo.distinct(NAME, { @@ -431,12 +448,16 @@ describe('test/mongo.test.js', () => { describe('createIndex()', () => { it('should create index success', async () => { - const result = await app.mongo.createIndex(NAME, { fieldOrSpec: { title: -1 } }); + const result = await app.mongo.createIndex(NAME, { + fieldOrSpec: { title: -1 }, + }); assert(result === 'title_-1'); }); it('should create index success', async () => { - const result = await app.mongo.createIndex(NAME, { fieldOrSpec: 'title' }); + const result = await app.mongo.createIndex(NAME, { + fieldOrSpec: 'title', + }); assert(result === 'title_1'); }); @@ -471,6 +492,5 @@ describe('test/mongo.test.js', () => { assert(error instanceof Error); } }); - }); }); From 6f41872b24097f1bda9af7a9cae45b6b9e65342b Mon Sep 17 00:00:00 2001 From: brickyang Date: Sun, 10 Dec 2017 23:28:00 +0800 Subject: [PATCH 2/2] fix: fix .travis.yml --- .travis.yml | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 960bc57..a307db6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,11 +1,15 @@ sudo: false language: node_js node_js: - - '8' - - '9' +- '8' +- '9' install: - - npm i npminstall && npminstall +- npm i npminstall && npminstall +services: +- mongodb +before_script: +- sleep 15 script: - - npm run ci +- npm run ci after_script: - - npminstall codecov && codecov +- npminstall codecov && codecov