Skip to content

Commit d4d1be3

Browse files
committed
Remove collection prefix from DB Controller
1 parent 3fb3ce1 commit d4d1be3

File tree

6 files changed

+31
-53
lines changed

6 files changed

+31
-53
lines changed

spec/DatabaseController.spec.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ let MongoStorageAdapter = require('../src/Adapters/Storage/Mongo/MongoStorageAda
66
describe('DatabaseController', () => {
77
it('can be constructed', done => {
88
let adapter = new MongoStorageAdapter({
9-
uri: 'mongodb://localhost:27017/test'
10-
});
11-
let databaseController = new DatabaseController(adapter, {
12-
collectionPrefix: 'test_'
9+
uri: 'mongodb://localhost:27017/test',
10+
collectionPrefix: 'test_',
1311
});
12+
let databaseController = new DatabaseController(adapter);
1413
databaseController.connect().then(done, error => {
1514
console.log('error', error.stack);
1615
fail();

src/Adapters/Storage/Mongo/MongoStorageAdapter.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,37 +50,38 @@ export class MongoStorageAdapter {
5050

5151
adaptiveCollection(name: string) {
5252
return this.connect()
53-
.then(() => this.database.collection(name))
53+
.then(() => this.database.collection(this._collectionPrefix + name))
5454
.then(rawCollection => new MongoCollection(rawCollection));
5555
}
5656

57-
schemaCollection(collectionPrefix: string) {
57+
schemaCollection() {
5858
return this.connect()
59-
.then(() => this.adaptiveCollection(collectionPrefix + MongoSchemaCollectionName))
59+
.then(() => this.adaptiveCollection(this._collectionPrefix + MongoSchemaCollectionName))
6060
.then(collection => new MongoSchemaCollection(collection));
6161
}
6262

6363
collectionExists(name: string) {
6464
return this.connect().then(() => {
65-
return this.database.listCollections({ name: name }).toArray();
65+
return this.database.listCollections({ name: this._collectionPrefix + name }).toArray();
6666
}).then(collections => {
6767
return collections.length > 0;
6868
});
6969
}
7070

7171
dropCollection(name: string) {
72-
return this.collection(name).then(collection => collection.drop());
72+
return this.collection(this._collectionPrefix + name).then(collection => collection.drop());
7373
}
74+
7475
// Used for testing only right now.
75-
collectionsContaining(match: string) {
76+
allCollections() {
7677
return this.connect().then(() => {
7778
return this.database.collections();
7879
}).then(collections => {
7980
return collections.filter(collection => {
8081
if (collection.namespace.match(/\.system\./)) {
8182
return false;
8283
}
83-
return (collection.collectionName.indexOf(match) == 0);
84+
return (collection.collectionName.indexOf(this._collectionPrefix) == 0);
8485
});
8586
});
8687
}
@@ -106,12 +107,12 @@ export class MongoStorageAdapter {
106107

107108
// Returns a Promise.
108109

109-
// This function currently accepts the collectionPrefix and adaptive collection as a paramater because it isn't
110+
// This function currently accepts the adaptive collection as a paramater because it isn't
110111
// actually capable of determining the location of it's own _SCHEMA collection without having
111112
// the collectionPrefix. Also, Schemas.js, the caller of this function, only stores the collection
112113
// itself, and not the prefix. Eventually Parse Server won't care what a SchemaCollection is and
113114
// will just tell the DB adapter to do things and it will do them.
114-
deleteFields(className: string, fieldNames, pointerFieldNames, collectionPrefix, adaptiveCollection) {
115+
deleteFields(className: string, fieldNames, pointerFieldNames, adaptiveCollection) {
115116
const nonPointerFieldNames = _.difference(fieldNames, pointerFieldNames);
116117
const mongoFormatNames = nonPointerFieldNames.concat(pointerFieldNames.map(name => `_p_${name}`));
117118
const collectionUpdate = { '$unset' : {} };
@@ -125,9 +126,7 @@ export class MongoStorageAdapter {
125126
});
126127

127128
return adaptiveCollection.updateMany({}, collectionUpdate)
128-
.then(updateResult => {
129-
return this.schemaCollection(collectionPrefix)
130-
})
129+
.then(updateResult => this.schemaCollection())
131130
.then(schemaCollection => schemaCollection.updateSchema(className, schemaUpdate));
132131
}
133132
}

src/Controllers/DatabaseController.js

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,9 @@ var Schema = require('./../Schema');
1010
var transform = require('./../transform');
1111
const deepcopy = require('deepcopy');
1212

13-
// options can contain:
14-
// collectionPrefix: the string to put in front of every collection name.
15-
function DatabaseController(adapter, { collectionPrefix } = {}) {
13+
function DatabaseController(adapter) {
1614
this.adapter = adapter;
1715

18-
this.collectionPrefix = collectionPrefix;
19-
2016
// We don't want a mutable this.schema, because then you could have
2117
// one request that uses different schemas for different parts of
2218
// it. Instead, use loadSchema to get a schema.
@@ -32,25 +28,21 @@ DatabaseController.prototype.connect = function() {
3228
};
3329

3430
DatabaseController.prototype.adaptiveCollection = function(className) {
35-
return this.adapter.adaptiveCollection(this.collectionPrefix + className);
31+
return this.adapter.adaptiveCollection(className);
3632
};
3733

3834
DatabaseController.prototype.schemaCollection = function() {
39-
return this.adapter.schemaCollection(this.collectionPrefix);
35+
return this.adapter.schemaCollection();
4036
};
4137

4238
DatabaseController.prototype.collectionExists = function(className) {
43-
return this.adapter.collectionExists(this.collectionPrefix + className);
39+
return this.adapter.collectionExists(className);
4440
};
4541

4642
DatabaseController.prototype.dropCollection = function(className) {
47-
return this.adapter.dropCollection(this.collectionPrefix + className);
43+
return this.adapter.dropCollection(className);
4844
};
4945

50-
function returnsTrue() {
51-
return true;
52-
}
53-
5446
DatabaseController.prototype.validateClassName = function(className) {
5547
if (!Schema.classNameIsValid(className)) {
5648
const error = new Parse.Error(Parse.Error.INVALID_CLASS_NAME, 'invalid className: ' + className);
@@ -62,7 +54,7 @@ DatabaseController.prototype.validateClassName = function(className) {
6254
// Returns a promise for a schema object.
6355
// If we are provided a acceptor, then we run it on the schema.
6456
// If the schema isn't accepted, we reload it at most once.
65-
DatabaseController.prototype.loadSchema = function(acceptor = returnsTrue) {
57+
DatabaseController.prototype.loadSchema = function(acceptor = () => true) {
6658

6759
if (!this.schemaPromise) {
6860
this.schemaPromise = this.schemaCollection().then(collection => {
@@ -388,10 +380,8 @@ DatabaseController.prototype.mongoFind = function(className, query, options = {}
388380
DatabaseController.prototype.deleteEverything = function() {
389381
this.schemaPromise = null;
390382

391-
return this.adapter.collectionsContaining(this.collectionPrefix).then(collections => {
392-
let promises = collections.map(collection => {
393-
return collection.drop();
394-
});
383+
return this.adapter.allCollections().then(collections => {
384+
let promises = collections.map(collection => collection.drop());
395385
return Promise.all(promises);
396386
});
397387
};

src/DatabaseAdapter.js

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,9 @@ import MongoStorageAdapter from './Adapters/Storage/Mongo/MongoStorageAdapter';
2121
const DefaultDatabaseURI = 'mongodb://localhost:27017/parse';
2222

2323
let dbConnections = {};
24-
let databaseURI = DefaultDatabaseURI;
2524
let appDatabaseURIs = {};
2625
let appDatabaseOptions = {};
2726

28-
function setDatabaseURI(uri) {
29-
databaseURI = uri;
30-
}
31-
3227
function setAppDatabaseURI(appId, uri) {
3328
appDatabaseURIs[appId] = uri;
3429
}
@@ -61,26 +56,21 @@ function getDatabaseConnection(appId: string, collectionPrefix: string) {
6156
return dbConnections[appId];
6257
}
6358

64-
var dbURI = (appDatabaseURIs[appId] ? appDatabaseURIs[appId] : databaseURI);
65-
6659
let storageAdapter = new MongoStorageAdapter({
67-
uri: dbURI,
60+
uri: appDatabaseURIs[appId] ? appDatabaseURIs[appId] : DefaultDatabaseURI,
6861
collectionPrefix: collectionPrefix,
6962
mongoOptions: appDatabaseOptions[appId]
7063
});
7164

72-
dbConnections[appId] = new DatabaseController(storageAdapter, {
73-
collectionPrefix: collectionPrefix
74-
});
65+
dbConnections[appId] = new DatabaseController(storageAdapter);
7566
return dbConnections[appId];
7667
}
7768

7869
module.exports = {
7970
getDatabaseConnection: getDatabaseConnection,
80-
setDatabaseURI: setDatabaseURI,
8171
setAppDatabaseOptions: setAppDatabaseOptions,
8272
setAppDatabaseURI: setAppDatabaseURI,
8373
clearDatabaseSettings: clearDatabaseSettings,
8474
destroyAllDataPermanently: destroyAllDataPermanently,
85-
defaultDatabaseURI: databaseURI
75+
defaultDatabaseURI: DefaultDatabaseURI
8676
};

src/Schema.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ class Schema {
526526
if (this.data[className][fieldName].type == 'Relation') {
527527
//For relations, drop the _Join table
528528
return database.adaptiveCollection(className).then(collection => {
529-
return database.adapter.deleteFields(className, [fieldName], [], database.collectionPrefix, collection);
529+
return database.adapter.deleteFields(className, [fieldName], [], collection);
530530
})
531531
.then(() => database.dropCollection(`_Join:${fieldName}:${className}`))
532532
.catch(error => {
@@ -542,7 +542,7 @@ class Schema {
542542
const fieldNames = [fieldName];
543543
const pointerFieldNames = this.data[className][fieldName].type === 'Pointer' ? [fieldName] : [];
544544
return database.adaptiveCollection(className)
545-
.then(collection => database.adapter.deleteFields(className, fieldNames, pointerFieldNames, database.collectionPrefix, collection));
545+
.then(collection => database.adapter.deleteFields(className, fieldNames, pointerFieldNames, collection));
546546
});
547547
}
548548

src/testing-routes.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// testing-routes.js
2-
import cache from './cache';
2+
import cache from './cache';
33
import * as middlewares from './middlewares';
4-
import { ParseServer } from './index';
5-
import { Parse } from 'parse/node';
4+
import { ParseServer } from './index';
5+
import { Parse } from 'parse/node';
66

77
var express = require('express'),
88
cryptoUtils = require('./cryptoUtils');
@@ -31,7 +31,7 @@ function createApp(req, res) {
3131
res.status(200).send(keys);
3232
}
3333

34-
// deletes all collections with the collectionPrefix of the app
34+
// deletes all collections that belong to the app
3535
function clearApp(req, res) {
3636
if (!req.auth.isMaster) {
3737
return res.status(401).send({ "error": "unauthorized" });

0 commit comments

Comments
 (0)