Skip to content

Commit b70c9bf

Browse files
committed
Move and cleanup getting collections into MongoStorageAdapter.
1 parent aa0b405 commit b70c9bf

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

src/Adapters/Storage/Mongo/MongoStorageAdapter.js

+19
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,25 @@ export class MongoStorageAdapter {
2323
});
2424
return this.connectionPromise;
2525
}
26+
27+
collection(name: string) {
28+
return this.connect().then(() => {
29+
return this.database.collection(name);
30+
});
31+
}
32+
33+
collectionsContaining(match: string) {
34+
return this.connect().then(() => {
35+
return this.database.collections();
36+
}).then(collections => {
37+
return collections.filter(collection => {
38+
if (collection.namespace.match(/\.system\./)) {
39+
return false;
40+
}
41+
return (collection.collectionName.indexOf(match) == 0);
42+
});
43+
});
44+
}
2645
}
2746

2847
export default MongoStorageAdapter;

src/Controllers/DatabaseController.js

+5-13
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ DatabaseController.prototype.collection = function(className) {
3939
};
4040

4141
DatabaseController.prototype.rawCollection = function(className) {
42-
return this.connect().then(() => {
43-
return this.adapter.database.collection(this.collectionPrefix + className);
44-
});
42+
return this.adapter.collection(this.collectionPrefix + className);
4543
};
4644

4745
function returnsTrue() {
@@ -345,16 +343,10 @@ DatabaseController.prototype.mongoFind = function(className, query, options = {}
345343
DatabaseController.prototype.deleteEverything = function() {
346344
this.schemaPromise = null;
347345

348-
return this.connect().then(() => {
349-
return this.adapter.database.collections();
350-
}).then((colls) => {
351-
var promises = [];
352-
for (var coll of colls) {
353-
if (!coll.namespace.match(/\.system\./) &&
354-
coll.collectionName.indexOf(this.collectionPrefix) === 0) {
355-
promises.push(coll.drop());
356-
}
357-
}
346+
return this.adapter.collectionsContaining(this.collectionPrefix).then(collections => {
347+
let promises = collections.map(collection => {
348+
return collection.drop();
349+
});
358350
return Promise.all(promises);
359351
});
360352
};

src/DatabaseAdapter.js

-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ function getDatabaseConnection(appId: string, collectionPrefix: string) {
5252
dbConnections[appId] = new DatabaseController(storageAdapter, {
5353
collectionPrefix: collectionPrefix
5454
});
55-
56-
dbConnections[appId].connect();
5755
return dbConnections[appId];
5856
}
5957

0 commit comments

Comments
 (0)