Skip to content

Commit

Permalink
Revert "Revert "Support passing both URL and collection name to const…
Browse files Browse the repository at this point in the history
…ructor (#25)""

And fix version number change

This reverts commit 8f06df9bf96f9201f0cbbbf5825a5778f46da780.
  • Loading branch information
lukechilds authored and Jytesh committed Apr 17, 2021
1 parent 2e0f3ae commit b863ca3
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
4 changes: 1 addition & 3 deletions packages/keyv-mongo/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## Node
.env

#npm5
package-lock.json
Expand All @@ -23,9 +24,6 @@ coverage
# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

Expand Down
3 changes: 2 additions & 1 deletion packages/keyv-mongo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@
"pify": "3.0.0"
},
"devDependencies": {
"@keyv/test-suite": "*",
"ava": "^0.25.0",
"coveralls": "^3.0.0",
"dotenv": "^6.2.0",
"eslint-config-xo-lukechilds": "^1.0.0",
"keyv": "*",
"@keyv/test-suite": "*",
"nyc": "^11.0.3",
"requirable": "^1.0.1",
"this": "^1.0.2",
Expand Down
14 changes: 7 additions & 7 deletions packages/keyv-mongo/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ const mongojs = require('mongojs');
const pify = require('pify');

class KeyvMongo extends EventEmitter {
constructor(opts) {
constructor(url, opts) {
super();
this.ttlSupport = false;
opts = opts || {};
if (typeof opts === 'string') {
opts = { url: opts };
url = url || {};
if (typeof url === 'string') {
url = { url };
}
if (opts.uri) {
opts = Object.assign({ url: opts.uri }, opts);
if (url.uri) {
url = Object.assign({ url: url.uri }, url);
}
this.opts = Object.assign({
url: 'mongodb://127.0.0.1:27017',
collection: 'keyv'
}, opts);
}, url, opts);
this.db = mongojs(this.opts.url);

const collection = this.db.collection(this.opts.collection);
Expand Down
15 changes: 13 additions & 2 deletions packages/keyv-mongo/test/test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import 'dotenv/config'; // eslint-disable-line import/no-unassigned-import
import test from 'ava';
import keyvTestSuite, { keyvOfficialTests } from '@keyv/test-suite';
import Keyv from 'keyv';
import KeyvMongo from 'this';

keyvOfficialTests(test, Keyv, 'mongodb://127.0.0.1:27017', 'mongodb://127.0.0.1:1234');
const mongoURL = process.env.MONGO_URL || 'mongodb://127.0.0.1:27017';

const store = () => new KeyvMongo();
keyvOfficialTests(test, Keyv, mongoURL, 'mongodb://127.0.0.1:1234');

const store = () => new KeyvMongo(mongoURL);
keyvTestSuite(test, Keyv, store);

test('Collection option merges into default options', t => {
Expand All @@ -16,6 +19,14 @@ test('Collection option merges into default options', t => {
});
});

test('Collection option merges into default options if URL is passed', t => {
const store = new KeyvMongo(mongoURL, { collection: 'foo' });
t.deepEqual(store.opts, {
url: mongoURL,
collection: 'foo'
});
});

test('.delete() with no args doesn\'t empty the collection', async t => {
const store = new KeyvMongo('foo'); // Make sure we don't actually connect
t.false(await store.delete());
Expand Down

0 comments on commit b863ca3

Please sign in to comment.