Skip to content

Make GridStoreAdapter persist it's own connection and don't talk to config.database. #733

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
Mar 1, 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
60 changes: 40 additions & 20 deletions src/Adapters/Files/GridStoreAdapter.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,47 @@
// GridStoreAdapter
//
// Stores files in Mongo using GridStore
// Requires the database adapter to be based on mongoclient
/**
GridStoreAdapter
Stores files in Mongo using GridStore
Requires the database adapter to be based on mongoclient

import { GridStore } from 'mongodb';
@flow weak
*/

import { MongoClient, GridStore, Db} from 'mongodb';
import { FilesAdapter } from './FilesAdapter';

export class GridStoreAdapter extends FilesAdapter {
_databaseURI: string;
_connectionPromise: Promise<Db>;

constructor(mongoDatabaseURI: string) {
super();
this._databaseURI = mongoDatabaseURI;
this._connect();
}

_connect() {
if (!this._connectionPromise) {
this._connectionPromise = MongoClient.connect(this._databaseURI);
}
return this._connectionPromise;
}

// For a given config object, filename, and data, store a file
// Returns a promise
createFile(config, filename, data) {
return config.database.connect().then(() => {
let gridStore = new GridStore(config.database.adapter.database, filename, 'w');
createFile(config, filename: string, data) {
return this._connect().then(database => {
let gridStore = new GridStore(database, filename, 'w');
return gridStore.open();
}).then((gridStore) => {
}).then(gridStore => {
return gridStore.write(data);
}).then((gridStore) => {
}).then(gridStore => {
return gridStore.close();
});
}

deleteFile(config, filename) {
return config.database.connect().then(() => {
let gridStore = new GridStore(config.database.adapter.database, filename, 'w');
deleteFile(config, filename: string) {
return this._connect().then(database => {
let gridStore = new GridStore(database, filename, 'w');
return gridStore.open();
}).then((gridStore) => {
return gridStore.unlink();
Expand All @@ -31,13 +50,14 @@ export class GridStoreAdapter extends FilesAdapter {
});
}

getFileData(config, filename) {
return config.database.connect().then(() => {
return GridStore.exist(config.database.adapter.database, filename);
}).then(() => {
let gridStore = new GridStore(config.database.adapter.database, filename, 'r');
return gridStore.open();
}).then((gridStore) => {
getFileData(config, filename: string) {
return this._connect().then(database => {
return GridStore.exist(database, filename)
.then(() => {
let gridStore = new GridStore(database, filename, 'r');
return gridStore.open();
});
}).then(gridStore => {
return gridStore.read();
});
}
Expand Down
11 changes: 7 additions & 4 deletions src/DatabaseAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
import DatabaseController from './Controllers/DatabaseController';
import MongoStorageAdapter from './Adapters/Storage/Mongo/MongoStorageAdapter';

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

let adapter = MongoStorageAdapter;
var dbConnections = {};
var databaseURI = 'mongodb://localhost:27017/parse';
var appDatabaseURIs = {};
let dbConnections = {};
let databaseURI = DefaultDatabaseURI;
let appDatabaseURIs = {};

function setAdapter(databaseAdapter) {
adapter = databaseAdapter;
Expand Down Expand Up @@ -61,5 +63,6 @@ module.exports = {
setAdapter: setAdapter,
setDatabaseURI: setDatabaseURI,
setAppDatabaseURI: setAppDatabaseURI,
clearDatabaseURIs: clearDatabaseURIs
clearDatabaseURIs: clearDatabaseURIs,
defaultDatabaseURI: databaseURI
};
6 changes: 4 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function ParseServer({
filesAdapter,
push,
loggerAdapter,
databaseURI,
databaseURI = DatabaseAdapter.defaultDatabaseURI,
cloud,
collectionPrefix = '',
clientKey,
Expand Down Expand Up @@ -129,7 +129,9 @@ function ParseServer({
}
}

const filesControllerAdapter = loadAdapter(filesAdapter, GridStoreAdapter);
const filesControllerAdapter = loadAdapter(filesAdapter, () => {
return new GridStoreAdapter(databaseURI);
});
const pushControllerAdapter = loadAdapter(push, ParsePushAdapter);
const loggerControllerAdapter = loadAdapter(loggerAdapter, FileLoggerAdapter);
const emailControllerAdapter = loadAdapter(emailAdapter);
Expand Down