Split mongodb connection creation from DatabaseController.
This commit is contained in:
@@ -1,15 +1,17 @@
|
|||||||
var DatabaseController = require('../src/Controllers/DatabaseController');
|
'use strict';
|
||||||
|
|
||||||
|
let DatabaseController = require('../src/Controllers/DatabaseController');
|
||||||
|
let MongoStorageAdapter = require('../src/Adapters/Storage/Mongo/MongoStorageAdapter');
|
||||||
|
|
||||||
describe('DatabaseController', () => {
|
describe('DatabaseController', () => {
|
||||||
it('can be constructed', (done) => {
|
it('can be constructed', done => {
|
||||||
var database = new DatabaseController('mongodb://localhost:27017/test',
|
let adapter = new MongoStorageAdapter('mongodb://localhost:27017/test');
|
||||||
{
|
let databaseController = new DatabaseController(adapter, {
|
||||||
collectionPrefix: 'test_'
|
collectionPrefix: 'test_'
|
||||||
});
|
});
|
||||||
database.connect().then(done, (error) => {
|
databaseController.connect().then(done, error => {
|
||||||
console.log('error', error.stack);
|
console.log('error', error.stack);
|
||||||
fail();
|
fail();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
29
src/Adapters/Storage/Mongo/MongoStorageAdapter.js
Normal file
29
src/Adapters/Storage/Mongo/MongoStorageAdapter.js
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
|
||||||
|
let mongodb = require('mongodb');
|
||||||
|
let MongoClient = mongodb.MongoClient;
|
||||||
|
|
||||||
|
export class MongoStorageAdapter {
|
||||||
|
// Private
|
||||||
|
_uri: string;
|
||||||
|
// Public
|
||||||
|
connectionPromise;
|
||||||
|
database;
|
||||||
|
|
||||||
|
constructor(uri: string) {
|
||||||
|
this._uri = uri;
|
||||||
|
}
|
||||||
|
|
||||||
|
connect() {
|
||||||
|
if (this.connectionPromise) {
|
||||||
|
return this.connectionPromise;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.connectionPromise = MongoClient.connect(this._uri).then(database => {
|
||||||
|
this.database = database;
|
||||||
|
});
|
||||||
|
return this.connectionPromise;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default MongoStorageAdapter;
|
||||||
|
module.exports = MongoStorageAdapter; // Required for tests
|
||||||
@@ -2,7 +2,6 @@
|
|||||||
// Parse database.
|
// Parse database.
|
||||||
|
|
||||||
var mongodb = require('mongodb');
|
var mongodb = require('mongodb');
|
||||||
var MongoClient = mongodb.MongoClient;
|
|
||||||
var Parse = require('parse/node').Parse;
|
var Parse = require('parse/node').Parse;
|
||||||
|
|
||||||
var Schema = require('./../Schema');
|
var Schema = require('./../Schema');
|
||||||
@@ -10,10 +9,10 @@ var transform = require('./../transform');
|
|||||||
|
|
||||||
// options can contain:
|
// options can contain:
|
||||||
// collectionPrefix: the string to put in front of every collection name.
|
// collectionPrefix: the string to put in front of every collection name.
|
||||||
function DatabaseController(mongoURI, options = {}) {
|
function DatabaseController(adapter, { collectionPrefix } = {}) {
|
||||||
this.mongoURI = mongoURI;
|
this.adapter = adapter;
|
||||||
|
|
||||||
this.collectionPrefix = options.collectionPrefix;
|
this.collectionPrefix = collectionPrefix;
|
||||||
|
|
||||||
// We don't want a mutable this.schema, because then you could have
|
// We don't want a mutable this.schema, because then you could have
|
||||||
// one request that uses different schemas for different parts of
|
// one request that uses different schemas for different parts of
|
||||||
@@ -28,17 +27,12 @@ function DatabaseController(mongoURI, options = {}) {
|
|||||||
// this.db will be populated with a Mongo "Db" object when the
|
// this.db will be populated with a Mongo "Db" object when the
|
||||||
// promise resolves successfully.
|
// promise resolves successfully.
|
||||||
DatabaseController.prototype.connect = function() {
|
DatabaseController.prototype.connect = function() {
|
||||||
if (this.connectionPromise) {
|
if (this.adapter.connectionPromise) {
|
||||||
// There's already a connection in progress.
|
return this.adapter.connectionPromise;
|
||||||
return this.connectionPromise;
|
|
||||||
}
|
}
|
||||||
|
return this.adapter.connect().then(() => {
|
||||||
this.connectionPromise = Promise.resolve().then(() => {
|
this.db = this.adapter.database;
|
||||||
return MongoClient.connect(this.mongoURI);
|
|
||||||
}).then((db) => {
|
|
||||||
this.db = db;
|
|
||||||
});
|
});
|
||||||
return this.connectionPromise;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Returns a promise for a Mongo collection.
|
// Returns a promise for a Mongo collection.
|
||||||
|
|||||||
@@ -13,9 +13,12 @@
|
|||||||
// * destroy(className, query, options)
|
// * destroy(className, query, options)
|
||||||
// * This list is incomplete and the database process is not fully modularized.
|
// * This list is incomplete and the database process is not fully modularized.
|
||||||
//
|
//
|
||||||
// Default is DatabaseController, which uses mongo at this time.
|
// Default is MongoStorageAdapter.
|
||||||
|
|
||||||
var adapter = require('./Controllers/DatabaseController');
|
import DatabaseController from './Controllers/DatabaseController';
|
||||||
|
import MongoStorageAdapter from './Adapters/Storage/Mongo/MongoStorageAdapter';
|
||||||
|
|
||||||
|
let adapter = MongoStorageAdapter;
|
||||||
var dbConnections = {};
|
var dbConnections = {};
|
||||||
var databaseURI = 'mongodb://localhost:27017/parse';
|
var databaseURI = 'mongodb://localhost:27017/parse';
|
||||||
var appDatabaseURIs = {};
|
var appDatabaseURIs = {};
|
||||||
@@ -44,9 +47,12 @@ function getDatabaseConnection(appId: string, collectionPrefix: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var dbURI = (appDatabaseURIs[appId] ? appDatabaseURIs[appId] : databaseURI);
|
var dbURI = (appDatabaseURIs[appId] ? appDatabaseURIs[appId] : databaseURI);
|
||||||
dbConnections[appId] = new adapter(dbURI, {
|
|
||||||
|
let storageAdapter = new adapter(dbURI);
|
||||||
|
dbConnections[appId] = new DatabaseController(storageAdapter, {
|
||||||
collectionPrefix: collectionPrefix
|
collectionPrefix: collectionPrefix
|
||||||
});
|
});
|
||||||
|
|
||||||
dbConnections[appId].connect();
|
dbConnections[appId].connect();
|
||||||
return dbConnections[appId];
|
return dbConnections[appId];
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user