Skip to content

Commit

Permalink
Move and cleanup getting collections into MongoDatabaseAdapter.
Browse files Browse the repository at this point in the history
  • Loading branch information
nlutsenko committed Feb 27, 2016
1 parent 002d53c commit d18e8b5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
19 changes: 19 additions & 0 deletions src/Adapters/Database/Mongo/MongoDatabaseAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,25 @@ export class MongoDatabaseAdapter {
});
return this.connectionPromise;
}

collection(name: string) {
return this.connect().then(() => {
return this.database.collection(name);
});
}

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 MongoDatabaseAdapter;
Expand Down
18 changes: 5 additions & 13 deletions src/Controllers/DatabaseController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
});
};
Expand Down
2 changes: 0 additions & 2 deletions src/DatabaseAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ function getDatabaseConnection(appId: string, collectionPrefix: string) {
dbConnections[appId] = new DatabaseController(dbAdapter, {
collectionPrefix: collectionPrefix
});

dbConnections[appId].connect();
return dbConnections[appId];
}

Expand Down

0 comments on commit d18e8b5

Please sign in to comment.