diff --git a/src/Adapters/Storage/Mongo/MongoStorageAdapter.js b/src/Adapters/Storage/Mongo/MongoStorageAdapter.js index e4995cba..914d9bb0 100644 --- a/src/Adapters/Storage/Mongo/MongoStorageAdapter.js +++ b/src/Adapters/Storage/Mongo/MongoStorageAdapter.js @@ -23,6 +23,26 @@ export class MongoStorageAdapter { }); return this.connectionPromise; } + + collection(name: string) { + return this.connect().then(() => { + return this.database.collection(name); + }); + } + + // Used for testing only right now. + collectionsContaining(match: string) { + return this.connect().then(() => { + return this.database.collections(); + }).then(collections => { + return collections.filter(collection => { + if (collection.namespace.match(/\.system\./)) { + return false; + } + return (collection.collectionName.indexOf(match) == 0); + }); + }); + } } export default MongoStorageAdapter; diff --git a/src/Controllers/DatabaseController.js b/src/Controllers/DatabaseController.js index 626c7644..954b1a6f 100644 --- a/src/Controllers/DatabaseController.js +++ b/src/Controllers/DatabaseController.js @@ -39,9 +39,7 @@ DatabaseController.prototype.collection = function(className) { }; DatabaseController.prototype.rawCollection = function(className) { - return this.connect().then(() => { - return this.adapter.database.collection(this.collectionPrefix + className); - }); + return this.adapter.collection(this.collectionPrefix + className); }; function returnsTrue() { @@ -345,16 +343,10 @@ DatabaseController.prototype.mongoFind = function(className, query, options = {} DatabaseController.prototype.deleteEverything = function() { this.schemaPromise = null; - return this.connect().then(() => { - return this.adapter.database.collections(); - }).then((colls) => { - var promises = []; - for (var coll of colls) { - if (!coll.namespace.match(/\.system\./) && - coll.collectionName.indexOf(this.collectionPrefix) === 0) { - promises.push(coll.drop()); - } - } + return this.adapter.collectionsContaining(this.collectionPrefix).then(collections => { + let promises = collections.map(collection => { + return collection.drop(); + }); return Promise.all(promises); }); }; diff --git a/src/DatabaseAdapter.js b/src/DatabaseAdapter.js index feb9311e..47b4dbca 100644 --- a/src/DatabaseAdapter.js +++ b/src/DatabaseAdapter.js @@ -52,8 +52,6 @@ function getDatabaseConnection(appId: string, collectionPrefix: string) { dbConnections[appId] = new DatabaseController(storageAdapter, { collectionPrefix: collectionPrefix }); - - dbConnections[appId].connect(); return dbConnections[appId]; }