Skip to content

Commit 9125a4c

Browse files
committed
fix: make mongodb dependency optional. Will throw error if package not installed only if used with ReplicaSet class.
1 parent 7c569b7 commit 9125a4c

File tree

4 files changed

+31
-30
lines changed

4 files changed

+31
-30
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"eslint-plugin-prettier": "^3.0.1",
4646
"flowgen": "^1.7.0",
4747
"jest": "^24.3.1",
48-
"mongodb": "^3.1.13",
48+
"mongodb": ">=3.0.0",
4949
"npm-run-all": "^4.1.5",
5050
"prettier": "^1.16.4",
5151
"rimraf": "^2.6.3",
@@ -67,8 +67,8 @@
6767
"tmp": "^0.0.33",
6868
"uuid": "^3.2.1"
6969
},
70-
"peerDependencies": {
71-
"mongodb": ">= 3.0"
70+
"optionalDependencies": {
71+
"mongodb": ">=3.0.0"
7272
},
7373
"scripts": {
7474
"build": "npm-run-all build:*",

src/MongoMemoryReplSet.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { EventEmitter } from 'events';
2-
import { Admin, MongoClient } from 'mongodb';
2+
import * as mongodb from 'mongodb';
33
import MongoMemoryServer from './MongoMemoryServer';
44
import { MongoMemoryServerOptsT } from './MongoMemoryServer';
55
import { generateDbName, getHost } from './util/db_util';
@@ -59,7 +59,7 @@ export default class MongoMemoryReplSet extends EventEmitter {
5959
opts: MongoMemoryReplSetOptsT;
6060
debug: DebugFn;
6161
_state: 'init' | 'running' | 'stopped';
62-
admin?: Admin;
62+
admin?: mongodb.Admin;
6363

6464
constructor(opts: Partial<MongoMemoryReplSetOptsT> = {}) {
6565
super();
@@ -216,7 +216,17 @@ export default class MongoMemoryReplSet extends EventEmitter {
216216
throw new Error('One or more server is required.');
217217
}
218218
const uris = await Promise.all(this.servers.map((server) => server.getUri()));
219-
const conn: MongoClient = await MongoClient.connect(uris[0], { useNewUrlParser: true });
219+
220+
let MongoClient: typeof mongodb.MongoClient;
221+
try {
222+
MongoClient = require('mongodb').MongoClient;
223+
} catch (e) {
224+
throw new Error(
225+
`You need to install "mongodb" package. It's required for checking ReplicaSet state.`
226+
);
227+
}
228+
229+
const conn: mongodb.MongoClient = await MongoClient.connect(uris[0], { useNewUrlParser: true });
220230
try {
221231
const db = await conn.db(this.opts.replSet.dbName);
222232
this.admin = db.admin();

src/util/db_util.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import uuid from 'uuid/v4';
2-
import { ReplStatusResultT } from '../types';
3-
import { Db } from 'mongodb';
2+
43
/**
54
* Returns a database name string.
65
* @param {string} dbName
@@ -17,13 +16,4 @@ export function getHost(uri: string): string {
1716
return uri.replace('mongodb://', '').replace(/\/.*/, '');
1817
}
1918

20-
/**
21-
* Returns replica set status result.
22-
* @param {any} db db instance
23-
*/
24-
export async function getReplStatus(db: Db): Promise<ReplStatusResultT> {
25-
const status = await db.command({ replSetGetStatus: 1 });
26-
return status;
27-
}
28-
2919
export default generateDbName;

yarn.lock

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,9 +1133,10 @@ bser@^2.0.0:
11331133
dependencies:
11341134
node-int64 "^0.4.0"
11351135

1136-
bson@^1.1.0:
1137-
version "1.1.0"
1138-
resolved "https://registry.yarnpkg.com/bson/-/bson-1.1.0.tgz#bee57d1fb6a87713471af4e32bcae36de814b5b0"
1136+
bson@^1.1.1:
1137+
version "1.1.1"
1138+
resolved "https://registry.yarnpkg.com/bson/-/bson-1.1.1.tgz#4330f5e99104c4e751e7351859e2d408279f2f13"
1139+
integrity sha512-jCGVYLoYMHDkOsbwJZBCqwMHyH4c+wzgI9hG7Z6SZJRXWr+x58pdIbm2i9a/jFGCkRJqRUr8eoI7lDWa0hTkxg==
11391140

11401141
btoa-lite@^1.0.0:
11411142
version "1.0.0"
@@ -4288,23 +4289,23 @@ modify-values@^1.0.0:
42884289
version "1.0.1"
42894290
resolved "https://registry.yarnpkg.com/modify-values/-/modify-values-1.0.1.tgz#b3939fa605546474e3e3e3c63d64bd43b4ee6022"
42904291

4291-
mongodb-core@3.1.11:
4292-
version "3.1.11"
4293-
resolved "https://registry.yarnpkg.com/mongodb-core/-/mongodb-core-3.1.11.tgz#b253038dbb4d7329f3d1c2ee5400bb0c9221fde5"
4294-
integrity sha512-rD2US2s5qk/ckbiiGFHeu+yKYDXdJ1G87F6CG3YdaZpzdOm5zpoAZd/EKbPmFO6cQZ+XVXBXBJ660sSI0gc6qg==
4292+
mongodb-core@^3.2.3:
4293+
version "3.2.3"
4294+
resolved "https://registry.yarnpkg.com/mongodb-core/-/mongodb-core-3.2.3.tgz#eb9bcb876f169f5843fd135f7f7686dbac0e9e34"
4295+
integrity sha512-UyI0rmvPPkjOJV8XGWa9VCTq7R4hBVipimhnAXeSXnuAPjuTqbyfA5Ec9RcYJ1Hhu+ISnc8bJ1KfGZd4ZkYARQ==
42954296
dependencies:
4296-
bson "^1.1.0"
4297+
bson "^1.1.1"
42974298
require_optional "^1.0.1"
42984299
safe-buffer "^5.1.2"
42994300
optionalDependencies:
43004301
saslprep "^1.0.0"
43014302

4302-
mongodb@^3.1.13:
4303-
version "3.1.13"
4304-
resolved "https://registry.yarnpkg.com/mongodb/-/mongodb-3.1.13.tgz#f8cdcbb36ad7a08b570bd1271c8525753f75f9f4"
4305-
integrity sha512-sz2dhvBZQWf3LRNDhbd30KHVzdjZx9IKC0L+kSZ/gzYquCF5zPOgGqRz6sSCqYZtKP2ekB4nfLxhGtzGHnIKxA==
4303+
mongodb@>=3.0.0:
4304+
version "3.2.3"
4305+
resolved "https://registry.yarnpkg.com/mongodb/-/mongodb-3.2.3.tgz#4610ee33d300caa74329c2dd03e137210723cd91"
4306+
integrity sha512-jw8UyPsq4QleZ9z+t/pIVy3L++51vKdaJ2Q/XXeYxk/3cnKioAH8H6f5tkkDivrQL4PUgUOHe9uZzkpRFH1XtQ==
43064307
dependencies:
4307-
mongodb-core "3.1.11"
4308+
mongodb-core "^3.2.3"
43084309
safe-buffer "^5.1.2"
43094310

43104311
move-concurrently@^1.0.1:

0 commit comments

Comments
 (0)