Skip to content

Commit fc1cdd4

Browse files
drew-grossflovilmart
authored andcommitted
Remove collection prefix and default mongo URI (#1479)
* Remove collection prefix from DB Controller * Remove collection prefix from cache * Revert "Remove collection prefix from cache" This reverts commit 529d67d. * Remove knowledge of default mongo URI from Parse Server * Remove adaptive collection paramater from deleteFields * Tidy up DBAdapter.js
1 parent 3fb3ce1 commit fc1cdd4

File tree

8 files changed

+43
-76
lines changed

8 files changed

+43
-76
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/Files/GridStoreAdapter.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
*/
88

99
import { MongoClient, GridStore, Db} from 'mongodb';
10-
import { FilesAdapter } from './FilesAdapter';
10+
import { FilesAdapter } from './FilesAdapter';
11+
12+
const DefaultMongoURI = 'mongodb://localhost:27017/parse';
1113

1214
export class GridStoreAdapter extends FilesAdapter {
1315
_databaseURI: string;
1416
_connectionPromise: Promise<Db>;
1517

16-
constructor(mongoDatabaseURI: string) {
18+
constructor(mongoDatabaseURI = DefaultMongoURI) {
1719
super();
1820
this._databaseURI = mongoDatabaseURI;
1921
this._connect();

src/Adapters/Storage/Mongo/MongoStorageAdapter.js

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ let mongodb = require('mongodb');
77
let MongoClient = mongodb.MongoClient;
88

99
const MongoSchemaCollectionName = '_SCHEMA';
10+
const DefaultMongoURI = 'mongodb://localhost:27017/parse';
1011

1112
export class MongoStorageAdapter {
1213
// Private
@@ -18,7 +19,7 @@ export class MongoStorageAdapter {
1819
database;
1920

2021
constructor({
21-
uri,
22+
uri = DefaultMongoURI,
2223
collectionPrefix = '',
2324
mongoOptions = {},
2425
}) {
@@ -50,37 +51,38 @@ export class MongoStorageAdapter {
5051

5152
adaptiveCollection(name: string) {
5253
return this.connect()
53-
.then(() => this.database.collection(name))
54+
.then(() => this.database.collection(this._collectionPrefix + name))
5455
.then(rawCollection => new MongoCollection(rawCollection));
5556
}
5657

57-
schemaCollection(collectionPrefix: string) {
58+
schemaCollection() {
5859
return this.connect()
59-
.then(() => this.adaptiveCollection(collectionPrefix + MongoSchemaCollectionName))
60+
.then(() => this.adaptiveCollection(this._collectionPrefix + MongoSchemaCollectionName))
6061
.then(collection => new MongoSchemaCollection(collection));
6162
}
6263

6364
collectionExists(name: string) {
6465
return this.connect().then(() => {
65-
return this.database.listCollections({ name: name }).toArray();
66+
return this.database.listCollections({ name: this._collectionPrefix + name }).toArray();
6667
}).then(collections => {
6768
return collections.length > 0;
6869
});
6970
}
7071

7172
dropCollection(name: string) {
72-
return this.collection(name).then(collection => collection.drop());
73+
return this.collection(this._collectionPrefix + name).then(collection => collection.drop());
7374
}
75+
7476
// Used for testing only right now.
75-
collectionsContaining(match: string) {
77+
allCollections() {
7678
return this.connect().then(() => {
7779
return this.database.collections();
7880
}).then(collections => {
7981
return collections.filter(collection => {
8082
if (collection.namespace.match(/\.system\./)) {
8183
return false;
8284
}
83-
return (collection.collectionName.indexOf(match) == 0);
85+
return (collection.collectionName.indexOf(this._collectionPrefix) == 0);
8486
});
8587
});
8688
}
@@ -105,13 +107,7 @@ export class MongoStorageAdapter {
105107
// may do so.
106108

107109
// Returns a Promise.
108-
109-
// This function currently accepts the collectionPrefix and adaptive collection as a paramater because it isn't
110-
// actually capable of determining the location of it's own _SCHEMA collection without having
111-
// the collectionPrefix. Also, Schemas.js, the caller of this function, only stores the collection
112-
// itself, and not the prefix. Eventually Parse Server won't care what a SchemaCollection is and
113-
// will just tell the DB adapter to do things and it will do them.
114-
deleteFields(className: string, fieldNames, pointerFieldNames, collectionPrefix, adaptiveCollection) {
110+
deleteFields(className: string, fieldNames, pointerFieldNames) {
115111
const nonPointerFieldNames = _.difference(fieldNames, pointerFieldNames);
116112
const mongoFormatNames = nonPointerFieldNames.concat(pointerFieldNames.map(name => `_p_${name}`));
117113
const collectionUpdate = { '$unset' : {} };
@@ -124,10 +120,9 @@ export class MongoStorageAdapter {
124120
schemaUpdate['$unset'][name] = null;
125121
});
126122

127-
return adaptiveCollection.updateMany({}, collectionUpdate)
128-
.then(updateResult => {
129-
return this.schemaCollection(collectionPrefix)
130-
})
123+
return this.adaptiveCollection(className)
124+
.then(collection => collection.updateMany({}, collectionUpdate))
125+
.then(updateResult => this.schemaCollection())
131126
.then(schemaCollection => schemaCollection.updateSchema(className, schemaUpdate));
132127
}
133128
}

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: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,10 @@
1818
import DatabaseController from './Controllers/DatabaseController';
1919
import MongoStorageAdapter from './Adapters/Storage/Mongo/MongoStorageAdapter';
2020

21-
const DefaultDatabaseURI = 'mongodb://localhost:27017/parse';
22-
2321
let dbConnections = {};
24-
let databaseURI = DefaultDatabaseURI;
2522
let appDatabaseURIs = {};
2623
let appDatabaseOptions = {};
2724

28-
function setDatabaseURI(uri) {
29-
databaseURI = uri;
30-
}
31-
3225
function setAppDatabaseURI(appId, uri) {
3326
appDatabaseURIs[appId] = uri;
3427
}
@@ -61,26 +54,21 @@ function getDatabaseConnection(appId: string, collectionPrefix: string) {
6154
return dbConnections[appId];
6255
}
6356

64-
var dbURI = (appDatabaseURIs[appId] ? appDatabaseURIs[appId] : databaseURI);
65-
66-
let storageAdapter = new MongoStorageAdapter({
67-
uri: dbURI,
57+
let mongoAdapterOptions = {
6858
collectionPrefix: collectionPrefix,
69-
mongoOptions: appDatabaseOptions[appId]
70-
});
59+
mongoOptions: appDatabaseOptions[appId],
60+
uri: appDatabaseURIs[appId], //may be undefined if the user didn't supply a URI, in which case the default will be used
61+
}
62+
63+
dbConnections[appId] = new DatabaseController(new MongoStorageAdapter(mongoAdapterOptions));
7164

72-
dbConnections[appId] = new DatabaseController(storageAdapter, {
73-
collectionPrefix: collectionPrefix
74-
});
7565
return dbConnections[appId];
7666
}
7767

7868
module.exports = {
7969
getDatabaseConnection: getDatabaseConnection,
80-
setDatabaseURI: setDatabaseURI,
8170
setAppDatabaseOptions: setAppDatabaseOptions,
8271
setAppDatabaseURI: setAppDatabaseURI,
8372
clearDatabaseSettings: clearDatabaseSettings,
8473
destroyAllDataPermanently: destroyAllDataPermanently,
85-
defaultDatabaseURI: databaseURI
8674
};

src/ParseServer.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ addParseCloud();
5353

5454
// ParseServer works like a constructor of an express app.
5555
// The args that we understand are:
56-
// "databaseAdapter": a class like DatabaseController providing create, find,
57-
// update, and delete
5856
// "filesAdapter": a class like GridStoreAdapter providing create, get,
5957
// and delete
6058
// "loggerAdapter": a class like FileLoggerAdapter providing info, error,
@@ -88,7 +86,7 @@ class ParseServer {
8886
push,
8987
loggerAdapter,
9088
logsFolder,
91-
databaseURI = DatabaseAdapter.defaultDatabaseURI,
89+
databaseURI,
9290
databaseOptions,
9391
cloud,
9492
collectionPrefix = '',
@@ -130,9 +128,7 @@ class ParseServer {
130128
DatabaseAdapter.setAppDatabaseOptions(appId, databaseOptions);
131129
}
132130

133-
if (databaseURI) {
134-
DatabaseAdapter.setAppDatabaseURI(appId, databaseURI);
135-
}
131+
DatabaseAdapter.setAppDatabaseURI(appId, databaseURI);
136132

137133
if (cloud) {
138134
addParseCloud();

src/Schema.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -525,9 +525,7 @@ class Schema {
525525

526526
if (this.data[className][fieldName].type == 'Relation') {
527527
//For relations, drop the _Join table
528-
return database.adaptiveCollection(className).then(collection => {
529-
return database.adapter.deleteFields(className, [fieldName], [], database.collectionPrefix, collection);
530-
})
528+
return database.adapter.deleteFields(className, [fieldName], [])
531529
.then(() => database.dropCollection(`_Join:${fieldName}:${className}`))
532530
.catch(error => {
533531
// 'ns not found' means collection was already gone. Ignore deletion attempt.
@@ -541,8 +539,7 @@ class Schema {
541539

542540
const fieldNames = [fieldName];
543541
const pointerFieldNames = this.data[className][fieldName].type === 'Pointer' ? [fieldName] : [];
544-
return database.adaptiveCollection(className)
545-
.then(collection => database.adapter.deleteFields(className, fieldNames, pointerFieldNames, database.collectionPrefix, collection));
542+
return database.adapter.deleteFields(className, fieldNames, pointerFieldNames);
546543
});
547544
}
548545

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)