Skip to content

Commit e9699b6

Browse files
committed
Merge pull request #698 from ParsePlatform/nlutsenko.database.controller
Rename ExportAdapter to DatabaseController, start splitting Mongo specific logic.
2 parents 10efd5d + eb89283 commit e9699b6

12 files changed

+123
-91
lines changed

spec/DatabaseController.spec.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
let DatabaseController = require('../src/Controllers/DatabaseController');
4+
let MongoStorageAdapter = require('../src/Adapters/Storage/Mongo/MongoStorageAdapter');
5+
6+
describe('DatabaseController', () => {
7+
it('can be constructed', done => {
8+
let adapter = new MongoStorageAdapter('mongodb://localhost:27017/test');
9+
let databaseController = new DatabaseController(adapter, {
10+
collectionPrefix: 'test_'
11+
});
12+
databaseController.connect().then(done, error => {
13+
console.log('error', error.stack);
14+
fail();
15+
});
16+
});
17+
});

spec/ExportAdapter.spec.js

-15
This file was deleted.

spec/Schema.spec.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -520,11 +520,11 @@ describe('Schema', () => {
520520
return obj2.save();
521521
})
522522
.then(() => {
523-
config.database.db.collection('test__Join:aRelation:HasPointersAndRelations', { strict: true }, (err, coll) => {
523+
config.database.adapter.database.collection('test__Join:aRelation:HasPointersAndRelations', { strict: true }, (err, coll) => {
524524
expect(err).toEqual(null);
525525
config.database.loadSchema()
526-
.then(schema => schema.deleteField('aRelation', 'HasPointersAndRelations', config.database.db, 'test_'))
527-
.then(() => config.database.db.collection('test__Join:aRelation:HasPointersAndRelations', { strict: true }, (err, coll) => {
526+
.then(schema => schema.deleteField('aRelation', 'HasPointersAndRelations', config.database.adapter.database, 'test_'))
527+
.then(() => config.database.adapter.database.collection('test__Join:aRelation:HasPointersAndRelations', { strict: true }, (err, coll) => {
528528
expect(err).not.toEqual(null);
529529
done();
530530
}))
@@ -538,7 +538,7 @@ describe('Schema', () => {
538538
var obj2 = hasAllPODobject();
539539
var p = Parse.Object.saveAll([obj1, obj2])
540540
.then(() => config.database.loadSchema())
541-
.then(schema => schema.deleteField('aString', 'HasAllPOD', config.database.db, 'test_'))
541+
.then(schema => schema.deleteField('aString', 'HasAllPOD', config.database.adapter.database, 'test_'))
542542
.then(() => new Parse.Query('HasAllPOD').get(obj1.id))
543543
.then(obj1Reloaded => {
544544
expect(obj1Reloaded.get('aString')).toEqual(undefined);
@@ -568,7 +568,7 @@ describe('Schema', () => {
568568
expect(obj1.get('aPointer').id).toEqual(obj1.id);
569569
})
570570
.then(() => config.database.loadSchema())
571-
.then(schema => schema.deleteField('aPointer', 'NewClass', config.database.db, 'test_'))
571+
.then(schema => schema.deleteField('aPointer', 'NewClass', config.database.adapter.database, 'test_'))
572572
.then(() => new Parse.Query('NewClass').get(obj1.id))
573573
.then(obj1 => {
574574
expect(obj1.get('aPointer')).toEqual(undefined);

spec/schemas.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -710,10 +710,10 @@ describe('schemas', () => {
710710
}, (error, response, body) => {
711711
expect(response.statusCode).toEqual(200);
712712
expect(response.body).toEqual({});
713-
config.database.db.collection('test__Join:aRelation:MyOtherClass', { strict: true }, (err, coll) => {
713+
config.database.adapter.database.collection('test__Join:aRelation:MyOtherClass', { strict: true }, (err, coll) => {
714714
//Expect Join table to be gone
715715
expect(err).not.toEqual(null);
716-
config.database.db.collection('test_MyOtherClass', { strict: true }, (err, coll) => {
716+
config.database.adapter.database.collection('test_MyOtherClass', { strict: true }, (err, coll) => {
717717
// Expect data table to be gone
718718
expect(err).not.toEqual(null);
719719
request.get({

src/Adapters/Files/FilesAdapter.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// * getFileLocation(config, request, filename)
99
//
1010
// Default is GridStoreAdapter, which requires mongo
11-
// and for the API server to be using the ExportAdapter
11+
// and for the API server to be using the DatabaseController with Mongo
1212
// database adapter.
1313

1414
export class FilesAdapter {

src/Adapters/Files/GridStoreAdapter.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export class GridStoreAdapter extends FilesAdapter {
1111
// Returns a promise
1212
createFile(config, filename, data) {
1313
return config.database.connect().then(() => {
14-
let gridStore = new GridStore(config.database.db, filename, 'w');
14+
let gridStore = new GridStore(config.database.adapter.database, filename, 'w');
1515
return gridStore.open();
1616
}).then((gridStore) => {
1717
return gridStore.write(data);
@@ -22,7 +22,7 @@ export class GridStoreAdapter extends FilesAdapter {
2222

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

3434
getFileData(config, filename) {
3535
return config.database.connect().then(() => {
36-
return GridStore.exist(config.database.db, filename);
36+
return GridStore.exist(config.database.adapter.database, filename);
3737
}).then(() => {
38-
let gridStore = new GridStore(config.database.db, filename, 'r');
38+
let gridStore = new GridStore(config.database.adapter.database, filename, 'r');
3939
return gridStore.open();
4040
}).then((gridStore) => {
4141
return gridStore.read();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
let mongodb = require('mongodb');
3+
let MongoClient = mongodb.MongoClient;
4+
5+
export class MongoStorageAdapter {
6+
// Private
7+
_uri: string;
8+
// Public
9+
connectionPromise;
10+
database;
11+
12+
constructor(uri: string) {
13+
this._uri = uri;
14+
}
15+
16+
connect() {
17+
if (this.connectionPromise) {
18+
return this.connectionPromise;
19+
}
20+
21+
this.connectionPromise = MongoClient.connect(this._uri).then(database => {
22+
this.database = database;
23+
});
24+
return this.connectionPromise;
25+
}
26+
27+
collection(name: string) {
28+
return this.connect().then(() => {
29+
return this.database.collection(name);
30+
});
31+
}
32+
33+
// Used for testing only right now.
34+
collectionsContaining(match: string) {
35+
return this.connect().then(() => {
36+
return this.database.collections();
37+
}).then(collections => {
38+
return collections.filter(collection => {
39+
if (collection.namespace.match(/\.system\./)) {
40+
return false;
41+
}
42+
return (collection.collectionName.indexOf(match) == 0);
43+
});
44+
});
45+
}
46+
}
47+
48+
export default MongoStorageAdapter;
49+
module.exports = MongoStorageAdapter; // Required for tests

0 commit comments

Comments
 (0)