Skip to content

Remove dependency from DatabaseAdapter on cache.js. #693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion spec/ParseAPI.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ describe('miscellaneous', function() {
obj.set('foo', 'bar');
return obj.save();
}).then(() => {
var db = DatabaseAdapter.getDatabaseConnection(appId);
var db = DatabaseAdapter.getDatabaseConnection(appId, 'test_');
return db.mongoFind('TestObject', {}, {});
}).then((results) => {
expect(results.length).toEqual(1);
Expand Down
3 changes: 2 additions & 1 deletion spec/ParseGlobalConfig.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict';

var request = require('request');
var Parse = require('parse/node').Parse;
var DatabaseAdapter = require('../src/DatabaseAdapter');

var database = DatabaseAdapter.getDatabaseConnection('test');
let database = DatabaseAdapter.getDatabaseConnection('test', 'test_');

describe('a GlobalConfig', () => {
beforeEach(function(done) {
Expand Down
3 changes: 2 additions & 1 deletion spec/ParseInstallation.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use strict';
// These tests check the Installations functionality of the REST API.
// Ported from installation_collection_test.go

Expand All @@ -9,7 +10,7 @@ var Parse = require('parse/node').Parse;
var rest = require('../src/rest');

var config = new Config('test');
var database = DatabaseAdapter.getDatabaseConnection('test');
let database = DatabaseAdapter.getDatabaseConnection('test', 'test_');

describe('Installations', () => {

Expand Down
2 changes: 1 addition & 1 deletion spec/RestCreate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var rest = require('../src/rest');
var request = require('request');

var config = new Config('test');
var database = DatabaseAdapter.getDatabaseConnection('test');
var database = DatabaseAdapter.getDatabaseConnection('test', 'test_');

describe('rest create', () => {
it('handles _id', (done) => {
Expand Down
12 changes: 5 additions & 7 deletions src/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
import cache from './cache';

export class Config {
constructor(applicationId: string, mount: string) {
let DatabaseAdapter = require('./DatabaseAdapter');

constructor(applicationId, mount) {

var DatabaseAdapter = require('./DatabaseAdapter');

var cacheInfo = cache.apps[applicationId];
let cacheInfo = cache.apps[applicationId];
this.valid = !!cacheInfo;
if (!this.valid) {
return;
Expand All @@ -27,7 +25,7 @@ export class Config {
this.facebookAppIds = cacheInfo.facebookAppIds;
this.enableAnonymousUsers = cacheInfo.enableAnonymousUsers;
this.allowClientClassCreation = cacheInfo.allowClientClassCreation;
this.database = DatabaseAdapter.getDatabaseConnection(applicationId);
this.database = DatabaseAdapter.getDatabaseConnection(applicationId, this.collectionPrefix);
this.hooksController = cacheInfo.hooksController;
this.filesController = cacheInfo.filesController;
this.pushController = cacheInfo.pushController;
Expand All @@ -36,7 +34,7 @@ export class Config {

this.mount = mount;
}
};
}

export default Config;
module.exports = Config;
38 changes: 23 additions & 15 deletions src/Controllers/HooksController.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
var DatabaseAdapter = require('../DatabaseAdapter'),
triggers = require('../triggers'),
request = require('request');
const collection = "_Hooks";
/** @flow weak */

import * as DatabaseAdapter from "../DatabaseAdapter";
import * as triggers from "../triggers";
import * as Parse from "parse/node";
import * as request from "request";

const DefaultHooksCollectionName = "_Hooks";

export class HooksController {

constructor(applicationId) {
this.applicationId = applicationId;
_applicationId: string;
_collectionPrefix: string;
_collection;

constructor(applicationId: string, collectionPrefix: string = '') {
this._applicationId = applicationId;
this._collectionPrefix = collectionPrefix;
}

database() {
return DatabaseAdapter.getDatabaseConnection(this.applicationId);
return DatabaseAdapter.getDatabaseConnection(this._applicationId, this._collectionPrefix);
}

collection() {
if (this._collection) {
return Promise.resolve(this._collection)
}
return this.database().rawCollection(collection).then((collection) => {
return this.database().rawCollection(DefaultHooksCollectionName).then((collection) => {
this._collection = collection;
return collection;
});
Expand All @@ -40,12 +48,12 @@ export class HooksController {
}

deleteFunction(functionName) {
triggers.removeFunction(functionName, this.applicationId);
triggers.removeFunction(functionName, this._applicationId);
return this.delete({functionName: functionName});
}

deleteTrigger(className, triggerName) {
triggers.removeTrigger(triggerName, className, this.applicationId);
triggers.removeTrigger(triggerName, className, this._applicationId);
return this.delete({className: className, triggerName: triggerName});
}

Expand All @@ -60,15 +68,15 @@ export class HooksController {
getOne(query) {
return this.collection()
.then(coll => coll.findOne(query, {_id: 0}))
.then(hook => {
.then(hook => {
return hook;
});
}

get(query) {
return this.collection()
.then(coll => coll.find(query, {_id: 0}).toArray())
.then(hooks => {
.then(hooks => {
return hooks;
});
}
Expand Down Expand Up @@ -102,9 +110,9 @@ export class HooksController {
var wrappedFunction = wrapToHTTPRequest(hook);
wrappedFunction.url = hook.url;
if (hook.className) {
triggers.addTrigger(hook.triggerName, hook.className, wrappedFunction, this.applicationId)
triggers.addTrigger(hook.triggerName, hook.className, wrappedFunction, this._applicationId)
} else {
triggers.addFunction(hook.functionName, wrappedFunction, null, this.applicationId);
triggers.addFunction(hook.functionName, wrappedFunction, null, this._applicationId);
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/DatabaseAdapter.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/** @flow weak */
// Database Adapter
//
// Allows you to change the underlying database.
Expand All @@ -13,7 +14,6 @@
// * This list is incomplete and the database process is not fully modularized.
//
// Default is ExportAdapter, which uses mongo.
import cache from './cache';

var ExportAdapter = require('./ExportAdapter');

Expand All @@ -40,14 +40,14 @@ function clearDatabaseURIs() {
dbConnections = {};
}

function getDatabaseConnection(appId) {
function getDatabaseConnection(appId: string, collectionPrefix: string) {
if (dbConnections[appId]) {
return dbConnections[appId];
}

var dbURI = (appDatabaseURIs[appId] ? appDatabaseURIs[appId] : databaseURI);
dbConnections[appId] = new adapter(dbURI, {
collectionPrefix: cache.apps[appId]['collectionPrefix']
collectionPrefix: collectionPrefix
});
dbConnections[appId].connect();
return dbConnections[appId];
Expand All @@ -59,5 +59,5 @@ module.exports = {
setAdapter: setAdapter,
setDatabaseURI: setDatabaseURI,
setAppDatabaseURI: setAppDatabaseURI,
clearDatabaseURIs: clearDatabaseURIs,
clearDatabaseURIs: clearDatabaseURIs
};
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ function ParseServer({
const filesController = new FilesController(filesControllerAdapter);
const pushController = new PushController(pushControllerAdapter);
const loggerController = new LoggerController(loggerControllerAdapter);
const hooksController = new HooksController(appId);
const hooksController = new HooksController(appId, collectionPrefix);

cache.apps[appId] = {
masterKey: masterKey,
Expand All @@ -141,7 +141,7 @@ function ParseServer({
hooksController: hooksController,
enableAnonymousUsers: enableAnonymousUsers,
allowClientClassCreation: allowClientClassCreation,
oauth: oauth,
oauth: oauth
};

// To maintain compatibility. TODO: Remove in v2.1
Expand Down