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:
committed by
Florent Vilmart
parent
6143988a82
commit
f0f1870aa3
@@ -31,7 +31,7 @@
|
||||
"lodash": "4.17.4",
|
||||
"lru-cache": "4.1.1",
|
||||
"mime": "2.1.0",
|
||||
"mongodb": "2.2.33",
|
||||
"mongodb": "3.0.1",
|
||||
"multer": "1.3.0",
|
||||
"parse": "1.11.0",
|
||||
"pg-promise": "7.3.2",
|
||||
|
||||
@@ -21,7 +21,8 @@ export class GridStoreAdapter extends FilesAdapter {
|
||||
|
||||
_connect() {
|
||||
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;
|
||||
}
|
||||
@@ -29,7 +30,7 @@ export class GridStoreAdapter extends FilesAdapter {
|
||||
// For a given config object, filename, and data, store a file
|
||||
// Returns a promise
|
||||
createFile(filename: string, data) {
|
||||
return this._connect().then(database => {
|
||||
return this._connect().then((database) => {
|
||||
const gridStore = new GridStore(database, filename, 'w');
|
||||
return gridStore.open();
|
||||
}).then(gridStore => {
|
||||
|
||||
@@ -117,7 +117,12 @@ export class MongoStorageAdapter {
|
||||
// encoded
|
||||
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) {
|
||||
delete this.connectionPromise;
|
||||
return;
|
||||
@@ -128,6 +133,7 @@ export class MongoStorageAdapter {
|
||||
database.on('close', () => {
|
||||
delete this.connectionPromise;
|
||||
});
|
||||
this.client = client;
|
||||
this.database = database;
|
||||
}).catch((err) => {
|
||||
delete this.connectionPromise;
|
||||
@@ -138,10 +144,10 @@ export class MongoStorageAdapter {
|
||||
}
|
||||
|
||||
handleShutdown() {
|
||||
if (!this.database) {
|
||||
if (!this.client) {
|
||||
return;
|
||||
}
|
||||
this.database.close(false);
|
||||
this.client.close(false);
|
||||
}
|
||||
|
||||
_adaptiveCollection(name: string) {
|
||||
@@ -516,7 +522,6 @@ export class MongoStorageAdapter {
|
||||
}
|
||||
|
||||
_parseReadPreference(readPreference) {
|
||||
if (readPreference) {
|
||||
switch (readPreference) {
|
||||
case 'PRIMARY':
|
||||
readPreference = ReadPreference.PRIMARY;
|
||||
@@ -533,10 +538,13 @@ export class MongoStorageAdapter {
|
||||
case 'NEAREST':
|
||||
readPreference = ReadPreference.NEAREST;
|
||||
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:
|
||||
throw new Parse.Error(Parse.Error.INVALID_QUERY, 'Not supported read preference.');
|
||||
}
|
||||
}
|
||||
return readPreference;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user