From 80de78a3986e3548d7369789e0a8d13c8ed6a658 Mon Sep 17 00:00:00 2001 From: "Filipe M. Silva" Date: Fri, 7 Feb 2020 12:16:53 -0300 Subject: [PATCH 1/2] change connection to no get a new one after open a collection --- lib/db.js | 10 ++++++---- package.json | 8 ++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/db.js b/lib/db.js index 997a4ec..4d29077 100644 --- a/lib/db.js +++ b/lib/db.js @@ -34,6 +34,11 @@ class DB { } this.MongoClient = mongoClient || MongoClient; + + this.conn = this.MongoClient.connect(this.url, { + useUnifiedTopology: true, + }); + return this; } @@ -67,10 +72,7 @@ class DB { }; get connection() { - return this.MongoClient.connect(this.url, { - useUnifiedTopology: true, - useNewUrlParser: true, - }).then(result => result.db(this.config.database)); + return this.conn.then(result => result.db(this.config.database)); } get validConfig() { diff --git a/package.json b/package.json index 8b44d76..29dcd58 100644 --- a/package.json +++ b/package.json @@ -7,10 +7,10 @@ "test": "tests" }, "scripts": { - "test": "npm run test:unit-coverage && npm run test:integration", - "test:unit": "mocha ./test/lib/**.test.js", - "test:integration": "mocha ./test/integration.test.js", - "test:unit-coverage": "nyc --reporter=lcov mocha ./test/lib/**.test.js" + "test": "npm run test:coverage && npm run test:integration", + "test:unit": "mocha ./test/lib/**.test.js --exit", + "test:integration": "mocha ./test/integration.test.js --exit", + "test:coverage": "npx nyc@latest --reporter=lcov --reporter=text-summary npm run test:unit" }, "repository": { "type": "git", From 308d926d9e89c074493b6f5c3fd3ac7dcd3eeb93 Mon Sep 17 00:00:00 2001 From: "Filipe M. Silva" Date: Fri, 7 Feb 2020 12:18:43 -0300 Subject: [PATCH 2/2] improve integration test --- test/integration.test.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/test/integration.test.js b/test/integration.test.js index e1f83ec..a2bc80a 100644 --- a/test/integration.test.js +++ b/test/integration.test.js @@ -117,5 +117,33 @@ describe('Test integration with mongo db', () => { done(); }); }); + + it('expect create data in two collections', (done) => { + const col = db.collection('new_collection'); + + col('insert', { name: 'verify' }) + .then((ops) => { + + expect(ops.result).to.be.a('object'); + expect(ops.result).to.have.property('ok'); + expect(ops.result.ok).to.be.equal(1); + + done(); + }); + + const col2 = db.collection('new_collection2'); + + col2('insert', { name: 'verify' }) + .then((ops) => { + + expect(ops.result).to.be.a('object'); + expect(ops.result).to.have.property('ok'); + expect(ops.result.ok).to.be.equal(1); + + done(); + }); + }); + + }); });