Skip to content

Commit

Permalink
Store collection prefix in mongo adapter, and clean up adapter interface
Browse files Browse the repository at this point in the history
  • Loading branch information
drew-gross committed Apr 13, 2016
1 parent 512251e commit df98783
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 31 deletions.
10 changes: 5 additions & 5 deletions spec/DatabaseAdapter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ describe('DatabaseAdapter', () => {
DatabaseAdapter.setAppDatabaseOptions('optionsTest', {foo: "bar"});
let optionsTestDatabaseConnection = DatabaseAdapter.getDatabaseConnection('optionsTest');

expect(optionsTestDatabaseConnection instanceof Object).toBe(true);
expect(optionsTestDatabaseConnection.adapter._options instanceof Object).toBe(true);
expect(optionsTestDatabaseConnection.adapter._options.foo).toBe("bar");
expect(optionsTestDatabaseConnection).toEqual(jasmine.any(Object));
expect(optionsTestDatabaseConnection.adapter._mongoOptions).toEqual(jasmine.any(Object));
expect(optionsTestDatabaseConnection.adapter._mongoOptions.foo).toBe("bar");

DatabaseAdapter.setAppDatabaseURI('noOptionsTest', 'mongodb://localhost:27017/noOptionsTest');
let noOptionsTestDatabaseConnection = DatabaseAdapter.getDatabaseConnection('noOptionsTest');

expect(noOptionsTestDatabaseConnection instanceof Object).toBe(true);
expect(noOptionsTestDatabaseConnection.adapter._options instanceof Object).toBe(false);
expect(noOptionsTestDatabaseConnection).toEqual(jasmine.any(Object));
expect(noOptionsTestDatabaseConnection.adapter._mongoOptions).toEqual(jasmine.any(Object));

done();
});
Expand Down
6 changes: 4 additions & 2 deletions spec/DatabaseController.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ let MongoStorageAdapter = require('../src/Adapters/Storage/Mongo/MongoStorageAda

describe('DatabaseController', () => {
it('can be constructed', done => {
let adapter = new MongoStorageAdapter('mongodb://localhost:27017/test');
let adapter = new MongoStorageAdapter({
uri: 'mongodb://localhost:27017/test'
});
let databaseController = new DatabaseController(adapter, {
collectionPrefix: 'test_'
collectionPrefix: 'test_'
});
databaseController.connect().then(done, error => {
console.log('error', error.stack);
Expand Down
15 changes: 9 additions & 6 deletions spec/MongoStorageAdapter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ const MongoClient = require('mongodb').MongoClient;
describe('MongoStorageAdapter', () => {
it('auto-escapes symbols in auth information', () => {
spyOn(MongoClient, 'connect').and.returnValue(Promise.resolve(null));
new MongoStorageAdapter('mongodb://user!with@+ symbols:password!with@+ symbols@localhost:1234/parse', {})
.connect();
new MongoStorageAdapter({
uri: 'mongodb://user!with@+ symbols:password!with@+ symbols@localhost:1234/parse'
}).connect();
expect(MongoClient.connect).toHaveBeenCalledWith(
'mongodb://user!with%40%2B%20symbols:password!with%40%2B%20symbols@localhost:1234/parse',
jasmine.any(Object)
Expand All @@ -16,8 +17,9 @@ describe('MongoStorageAdapter', () => {

it("doesn't double escape already URI-encoded information", () => {
spyOn(MongoClient, 'connect').and.returnValue(Promise.resolve(null));
new MongoStorageAdapter('mongodb://user!with%40%2B%20symbols:password!with%40%2B%20symbols@localhost:1234/parse', {})
.connect();
new MongoStorageAdapter({
uri: 'mongodb://user!with%40%2B%20symbols:password!with%40%2B%20symbols@localhost:1234/parse'
}).connect();
expect(MongoClient.connect).toHaveBeenCalledWith(
'mongodb://user!with%40%2B%20symbols:password!with%40%2B%20symbols@localhost:1234/parse',
jasmine.any(Object)
Expand All @@ -27,8 +29,9 @@ describe('MongoStorageAdapter', () => {
// https://github.com/ParsePlatform/parse-server/pull/148#issuecomment-180407057
it('preserves replica sets', () => {
spyOn(MongoClient, 'connect').and.returnValue(Promise.resolve(null));
new MongoStorageAdapter('mongodb://test:testpass@ds056315-a0.mongolab.com:59325,ds059315-a1.mongolab.com:59315/testDBname?replicaSet=rs-ds059415', {})
.connect();
new MongoStorageAdapter({
uri: 'mongodb://test:testpass@ds056315-a0.mongolab.com:59325,ds059315-a1.mongolab.com:59315/testDBname?replicaSet=rs-ds059415'
}).connect();
expect(MongoClient.connect).toHaveBeenCalledWith(
'mongodb://test:testpass@ds056315-a0.mongolab.com:59325,ds059315-a1.mongolab.com:59315/testDBname?replicaSet=rs-ds059415',
jasmine.any(Object)
Expand Down
14 changes: 10 additions & 4 deletions src/Adapters/Storage/Mongo/MongoStorageAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,20 @@ const MongoSchemaCollectionName = '_SCHEMA';
export class MongoStorageAdapter {
// Private
_uri: string;
_options: Object;
_collectionPrefix: string;
_mongoOptions: Object;
// Public
connectionPromise;
database;

constructor(uri: string, options: Object) {
constructor({
uri,
collectionPrefix = '',
mongoOptions = {},
}) {
this._uri = uri;
this._options = options;
this._collectionPrefix = collectionPrefix;
this._mongoOptions = mongoOptions;
}

connect() {
Expand All @@ -30,7 +36,7 @@ export class MongoStorageAdapter {
// encoded
const encodedUri = formatUrl(parseUrl(this._uri));

this.connectionPromise = MongoClient.connect(encodedUri, this._options).then(database => {
this.connectionPromise = MongoClient.connect(encodedUri, this._mongoOptions).then(database => {
this.database = database;
});
return this.connectionPromise;
Expand Down
16 changes: 7 additions & 9 deletions src/DatabaseAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,16 @@
//
// Default is MongoStorageAdapter.

import DatabaseController from './Controllers/DatabaseController';
import DatabaseController from './Controllers/DatabaseController';
import MongoStorageAdapter from './Adapters/Storage/Mongo/MongoStorageAdapter';

const DefaultDatabaseURI = 'mongodb://localhost:27017/parse';

let adapter = MongoStorageAdapter;
let dbConnections = {};
let databaseURI = DefaultDatabaseURI;
let appDatabaseURIs = {};
let appDatabaseOptions = {};

function setAdapter(databaseAdapter) {
adapter = databaseAdapter;
}

function setDatabaseURI(uri) {
databaseURI = uri;
}
Expand Down Expand Up @@ -68,17 +63,20 @@ function getDatabaseConnection(appId: string, collectionPrefix: string) {

var dbURI = (appDatabaseURIs[appId] ? appDatabaseURIs[appId] : databaseURI);

let storageAdapter = new adapter(dbURI, appDatabaseOptions[appId]);
let storageAdapter = new MongoStorageAdapter({
uri: dbURI,
collectionPrefix: collectionPrefix,
mongoOptions: appDatabaseOptions[appId]
});

dbConnections[appId] = new DatabaseController(storageAdapter, {
collectionPrefix: collectionPrefix
});
return dbConnections[appId];
}

module.exports = {
dbConnections: dbConnections,
getDatabaseConnection: getDatabaseConnection,
setAdapter: setAdapter,
setDatabaseURI: setDatabaseURI,
setAppDatabaseOptions: setAppDatabaseOptions,
setAppDatabaseURI: setAppDatabaseURI,
Expand Down
5 changes: 0 additions & 5 deletions src/ParseServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ class ParseServer {
appId = requiredParameter('You must provide an appId!'),
masterKey = requiredParameter('You must provide a masterKey!'),
appName,
databaseAdapter,
filesAdapter,
push,
loggerAdapter,
Expand Down Expand Up @@ -127,10 +126,6 @@ class ParseServer {
})
}

if (databaseAdapter) {
DatabaseAdapter.setAdapter(databaseAdapter);
}

if (databaseOptions) {
DatabaseAdapter.setAppDatabaseOptions(appId, databaseOptions);
}
Expand Down

0 comments on commit df98783

Please sign in to comment.