Skip to content

Commit 997da89

Browse files
committed
Split mongodb connection creation from DatabaseController.
1 parent d78c274 commit 997da89

File tree

4 files changed

+54
-23
lines changed

4 files changed

+54
-23
lines changed

spec/DatabaseController.spec.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
var DatabaseController = require('../src/Controllers/DatabaseController');
1+
'use strict';
2+
3+
let DatabaseController = require('../src/Controllers/DatabaseController');
4+
let MongoStorageAdapter = require('../src/Adapters/Storage/Mongo/MongoStorageAdapter');
25

36
describe('DatabaseController', () => {
4-
it('can be constructed', (done) => {
5-
var database = new DatabaseController('mongodb://localhost:27017/test',
6-
{
7+
it('can be constructed', done => {
8+
let adapter = new MongoStorageAdapter('mongodb://localhost:27017/test');
9+
let databaseController = new DatabaseController(adapter, {
710
collectionPrefix: 'test_'
8-
});
9-
database.connect().then(done, (error) => {
11+
});
12+
databaseController.connect().then(done, error => {
1013
console.log('error', error.stack);
1114
fail();
1215
});
1316
});
14-
1517
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
let mongodb = require('mongodb');
3+
let MongoClient = mongodb.MongoClient;
4+
5+
export class MongoStorageAdapter {
6+
// Private
7+
_uri: string;
8+
// Public
9+
connectionPromise;
10+
database;
11+
12+
constructor(uri: string) {
13+
this._uri = uri;
14+
}
15+
16+
connect() {
17+
if (this.connectionPromise) {
18+
return this.connectionPromise;
19+
}
20+
21+
this.connectionPromise = MongoClient.connect(this._uri).then(database => {
22+
this.database = database;
23+
});
24+
return this.connectionPromise;
25+
}
26+
}
27+
28+
export default MongoStorageAdapter;
29+
module.exports = MongoStorageAdapter; // Required for tests

src/Controllers/DatabaseController.js

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@
22
// Parse database.
33

44
var mongodb = require('mongodb');
5-
var MongoClient = mongodb.MongoClient;
65
var Parse = require('parse/node').Parse;
76

87
var Schema = require('./../Schema');
98
var transform = require('./../transform');
109

1110
// options can contain:
1211
// collectionPrefix: the string to put in front of every collection name.
13-
function DatabaseController(mongoURI, options = {}) {
14-
this.mongoURI = mongoURI;
12+
function DatabaseController(adapter, { collectionPrefix } = {}) {
13+
this.adapter = adapter;
1514

16-
this.collectionPrefix = options.collectionPrefix;
15+
this.collectionPrefix = collectionPrefix;
1716

1817
// We don't want a mutable this.schema, because then you could have
1918
// one request that uses different schemas for different parts of
@@ -28,17 +27,12 @@ function DatabaseController(mongoURI, options = {}) {
2827
// this.db will be populated with a Mongo "Db" object when the
2928
// promise resolves successfully.
3029
DatabaseController.prototype.connect = function() {
31-
if (this.connectionPromise) {
32-
// There's already a connection in progress.
33-
return this.connectionPromise;
30+
if (this.adapter.connectionPromise) {
31+
return this.adapter.connectionPromise;
3432
}
35-
36-
this.connectionPromise = Promise.resolve().then(() => {
37-
return MongoClient.connect(this.mongoURI);
38-
}).then((db) => {
39-
this.db = db;
33+
return this.adapter.connect().then(() => {
34+
this.db = this.adapter.database;
4035
});
41-
return this.connectionPromise;
4236
};
4337

4438
// Returns a promise for a Mongo collection.

src/DatabaseAdapter.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313
// * destroy(className, query, options)
1414
// * This list is incomplete and the database process is not fully modularized.
1515
//
16-
// Default is DatabaseController, which uses mongo at this time.
16+
// Default is MongoStorageAdapter.
1717

18-
var adapter = require('./Controllers/DatabaseController');
18+
import DatabaseController from './Controllers/DatabaseController';
19+
import MongoStorageAdapter from './Adapters/Storage/Mongo/MongoStorageAdapter';
20+
21+
let adapter = MongoStorageAdapter;
1922
var dbConnections = {};
2023
var databaseURI = 'mongodb://localhost:27017/parse';
2124
var appDatabaseURIs = {};
@@ -44,9 +47,12 @@ function getDatabaseConnection(appId: string, collectionPrefix: string) {
4447
}
4548

4649
var dbURI = (appDatabaseURIs[appId] ? appDatabaseURIs[appId] : databaseURI);
47-
dbConnections[appId] = new adapter(dbURI, {
50+
51+
let storageAdapter = new adapter(dbURI);
52+
dbConnections[appId] = new DatabaseController(storageAdapter, {
4853
collectionPrefix: collectionPrefix
4954
});
55+
5056
dbConnections[appId].connect();
5157
return dbConnections[appId];
5258
}

0 commit comments

Comments
 (0)