Skip to content

Rename ExportAdapter to DatabaseController, start splitting Mongo specific logic. #698

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 4 commits into from
Feb 29, 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
17 changes: 17 additions & 0 deletions spec/DatabaseController.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

let DatabaseController = require('../src/Controllers/DatabaseController');
let MongoStorageAdapter = require('../src/Adapters/Storage/Mongo/MongoStorageAdapter');

describe('DatabaseController', () => {
it('can be constructed', done => {
let adapter = new MongoStorageAdapter('mongodb://localhost:27017/test');
let databaseController = new DatabaseController(adapter, {
collectionPrefix: 'test_'
});
databaseController.connect().then(done, error => {
console.log('error', error.stack);
fail();
});
});
});
15 changes: 0 additions & 15 deletions spec/ExportAdapter.spec.js

This file was deleted.

10 changes: 5 additions & 5 deletions spec/Schema.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -520,11 +520,11 @@ describe('Schema', () => {
return obj2.save();
})
.then(() => {
config.database.db.collection('test__Join:aRelation:HasPointersAndRelations', { strict: true }, (err, coll) => {
config.database.adapter.database.collection('test__Join:aRelation:HasPointersAndRelations', { strict: true }, (err, coll) => {
expect(err).toEqual(null);
config.database.loadSchema()
.then(schema => schema.deleteField('aRelation', 'HasPointersAndRelations', config.database.db, 'test_'))
.then(() => config.database.db.collection('test__Join:aRelation:HasPointersAndRelations', { strict: true }, (err, coll) => {
.then(schema => schema.deleteField('aRelation', 'HasPointersAndRelations', config.database.adapter.database, 'test_'))
.then(() => config.database.adapter.database.collection('test__Join:aRelation:HasPointersAndRelations', { strict: true }, (err, coll) => {
expect(err).not.toEqual(null);
done();
}))
Expand All @@ -538,7 +538,7 @@ describe('Schema', () => {
var obj2 = hasAllPODobject();
var p = Parse.Object.saveAll([obj1, obj2])
.then(() => config.database.loadSchema())
.then(schema => schema.deleteField('aString', 'HasAllPOD', config.database.db, 'test_'))
.then(schema => schema.deleteField('aString', 'HasAllPOD', config.database.adapter.database, 'test_'))
.then(() => new Parse.Query('HasAllPOD').get(obj1.id))
.then(obj1Reloaded => {
expect(obj1Reloaded.get('aString')).toEqual(undefined);
Expand Down Expand Up @@ -568,7 +568,7 @@ describe('Schema', () => {
expect(obj1.get('aPointer').id).toEqual(obj1.id);
})
.then(() => config.database.loadSchema())
.then(schema => schema.deleteField('aPointer', 'NewClass', config.database.db, 'test_'))
.then(schema => schema.deleteField('aPointer', 'NewClass', config.database.adapter.database, 'test_'))
.then(() => new Parse.Query('NewClass').get(obj1.id))
.then(obj1 => {
expect(obj1.get('aPointer')).toEqual(undefined);
Expand Down
4 changes: 2 additions & 2 deletions spec/schemas.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -710,10 +710,10 @@ describe('schemas', () => {
}, (error, response, body) => {
expect(response.statusCode).toEqual(200);
expect(response.body).toEqual({});
config.database.db.collection('test__Join:aRelation:MyOtherClass', { strict: true }, (err, coll) => {
config.database.adapter.database.collection('test__Join:aRelation:MyOtherClass', { strict: true }, (err, coll) => {
//Expect Join table to be gone
expect(err).not.toEqual(null);
config.database.db.collection('test_MyOtherClass', { strict: true }, (err, coll) => {
config.database.adapter.database.collection('test_MyOtherClass', { strict: true }, (err, coll) => {
// Expect data table to be gone
expect(err).not.toEqual(null);
request.get({
Expand Down
2 changes: 1 addition & 1 deletion src/Adapters/Files/FilesAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// * getFileLocation(config, request, filename)
//
// Default is GridStoreAdapter, which requires mongo
// and for the API server to be using the ExportAdapter
// and for the API server to be using the DatabaseController with Mongo
// database adapter.

export class FilesAdapter {
Expand Down
8 changes: 4 additions & 4 deletions src/Adapters/Files/GridStoreAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class GridStoreAdapter extends FilesAdapter {
// Returns a promise
createFile(config, filename, data) {
return config.database.connect().then(() => {
let gridStore = new GridStore(config.database.db, filename, 'w');
let gridStore = new GridStore(config.database.adapter.database, filename, 'w');
return gridStore.open();
}).then((gridStore) => {
return gridStore.write(data);
Expand All @@ -22,7 +22,7 @@ export class GridStoreAdapter extends FilesAdapter {

deleteFile(config, filename) {
return config.database.connect().then(() => {
let gridStore = new GridStore(config.database.db, filename, 'w');
let gridStore = new GridStore(config.database.adapter.database, filename, 'w');
return gridStore.open();
}).then((gridStore) => {
return gridStore.unlink();
Expand All @@ -33,9 +33,9 @@ export class GridStoreAdapter extends FilesAdapter {

getFileData(config, filename) {
return config.database.connect().then(() => {
return GridStore.exist(config.database.db, filename);
return GridStore.exist(config.database.adapter.database, filename);
}).then(() => {
let gridStore = new GridStore(config.database.db, filename, 'r');
let gridStore = new GridStore(config.database.adapter.database, filename, 'r');
return gridStore.open();
}).then((gridStore) => {
return gridStore.read();
Expand Down
49 changes: 49 additions & 0 deletions src/Adapters/Storage/Mongo/MongoStorageAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

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;
}

collection(name: string) {
return this.connect().then(() => {
return this.database.collection(name);
});
}

// Used for testing only right now.
collectionsContaining(match: string) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment that this is for clearing out collections during tests? I was kinda confused about why this function exists.

return this.connect().then(() => {
return this.database.collections();
}).then(collections => {
return collections.filter(collection => {
if (collection.namespace.match(/\.system\./)) {
return false;
}
return (collection.collectionName.indexOf(match) == 0);
});
});
}
}

export default MongoStorageAdapter;
module.exports = MongoStorageAdapter; // Required for tests
Loading