Move and cleanup getting collections into MongoStorageAdapter.

This commit is contained in:
Nikita Lutsenko
2016-02-27 03:02:38 -08:00
parent 7215300c1e
commit eb892830e6
3 changed files with 25 additions and 15 deletions

View File

@@ -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;

View File

@@ -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);
});
};

View File

@@ -52,8 +52,6 @@ function getDatabaseConnection(appId: string, collectionPrefix: string) {
dbConnections[appId] = new DatabaseController(storageAdapter, {
collectionPrefix: collectionPrefix
});
dbConnections[appId].connect();
return dbConnections[appId];
}