Update mongodb to the latest version 🚀 (#4449)

* fix(package): update mongodb to version 3.0.0

* Compatibility with MongoDB client 3.0

* Updates Gridstore as well

* Set Read preference to Primary when not specified, to match original implementation

* Update MongoStorageAdapter.js

* Bumps to 3.0.1
This commit is contained in:
greenkeeper[bot]
2017-12-30 00:23:43 -05:00
committed by Florent Vilmart
parent 6143988a82
commit f0f1870aa3
3 changed files with 35 additions and 26 deletions

View File

@@ -31,7 +31,7 @@
"lodash": "4.17.4", "lodash": "4.17.4",
"lru-cache": "4.1.1", "lru-cache": "4.1.1",
"mime": "2.1.0", "mime": "2.1.0",
"mongodb": "2.2.33", "mongodb": "3.0.1",
"multer": "1.3.0", "multer": "1.3.0",
"parse": "1.11.0", "parse": "1.11.0",
"pg-promise": "7.3.2", "pg-promise": "7.3.2",

View File

@@ -21,7 +21,8 @@ export class GridStoreAdapter extends FilesAdapter {
_connect() { _connect() {
if (!this._connectionPromise) { if (!this._connectionPromise) {
this._connectionPromise = MongoClient.connect(this._databaseURI); this._connectionPromise = MongoClient.connect(this._databaseURI)
.then((client) => client.db(client.s.options.dbName));
} }
return this._connectionPromise; return this._connectionPromise;
} }
@@ -29,7 +30,7 @@ export class GridStoreAdapter extends FilesAdapter {
// For a given config object, filename, and data, store a file // For a given config object, filename, and data, store a file
// Returns a promise // Returns a promise
createFile(filename: string, data) { createFile(filename: string, data) {
return this._connect().then(database => { return this._connect().then((database) => {
const gridStore = new GridStore(database, filename, 'w'); const gridStore = new GridStore(database, filename, 'w');
return gridStore.open(); return gridStore.open();
}).then(gridStore => { }).then(gridStore => {

View File

@@ -117,7 +117,12 @@ export class MongoStorageAdapter {
// encoded // encoded
const encodedUri = formatUrl(parseUrl(this._uri)); const encodedUri = formatUrl(parseUrl(this._uri));
this.connectionPromise = MongoClient.connect(encodedUri, this._mongoOptions).then(database => { this.connectionPromise = MongoClient.connect(encodedUri, this._mongoOptions).then(client => {
// Starting mongoDB 3.0, the MongoClient.connect don't return a DB anymore but a client
// Fortunately, we can get back the options and use them to select the proper DB.
// https://github.com/mongodb/node-mongodb-native/blob/2c35d76f08574225b8db02d7bef687123e6bb018/lib/mongo_client.js#L885
const options = client.s.options;
const database = client.db(options.dbName);
if (!database) { if (!database) {
delete this.connectionPromise; delete this.connectionPromise;
return; return;
@@ -128,6 +133,7 @@ export class MongoStorageAdapter {
database.on('close', () => { database.on('close', () => {
delete this.connectionPromise; delete this.connectionPromise;
}); });
this.client = client;
this.database = database; this.database = database;
}).catch((err) => { }).catch((err) => {
delete this.connectionPromise; delete this.connectionPromise;
@@ -138,10 +144,10 @@ export class MongoStorageAdapter {
} }
handleShutdown() { handleShutdown() {
if (!this.database) { if (!this.client) {
return; return;
} }
this.database.close(false); this.client.close(false);
} }
_adaptiveCollection(name: string) { _adaptiveCollection(name: string) {
@@ -516,7 +522,6 @@ export class MongoStorageAdapter {
} }
_parseReadPreference(readPreference) { _parseReadPreference(readPreference) {
if (readPreference) {
switch (readPreference) { switch (readPreference) {
case 'PRIMARY': case 'PRIMARY':
readPreference = ReadPreference.PRIMARY; readPreference = ReadPreference.PRIMARY;
@@ -533,10 +538,13 @@ export class MongoStorageAdapter {
case 'NEAREST': case 'NEAREST':
readPreference = ReadPreference.NEAREST; readPreference = ReadPreference.NEAREST;
break; break;
case undefined:
// this is to match existing tests, which were failing as mongodb@3.0 don't report readPreference anymore
readPreference = ReadPreference.PRIMARY;
break;
default: default:
throw new Parse.Error(Parse.Error.INVALID_QUERY, 'Not supported read preference.'); throw new Parse.Error(Parse.Error.INVALID_QUERY, 'Not supported read preference.');
} }
}
return readPreference; return readPreference;
} }