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 529d67dd617b64c69c36a8a63382456e95edcab8. * Remove knowledge of default mongo URI from Parse Server * Remove adaptive collection paramater from deleteFields * Tidy up DBAdapter.js
This commit is contained in:
@@ -6,11 +6,10 @@ let MongoStorageAdapter = require('../src/Adapters/Storage/Mongo/MongoStorageAda
|
|||||||
describe('DatabaseController', () => {
|
describe('DatabaseController', () => {
|
||||||
it('can be constructed', done => {
|
it('can be constructed', done => {
|
||||||
let adapter = new MongoStorageAdapter({
|
let adapter = new MongoStorageAdapter({
|
||||||
uri: 'mongodb://localhost:27017/test'
|
uri: 'mongodb://localhost:27017/test',
|
||||||
});
|
collectionPrefix: 'test_',
|
||||||
let databaseController = new DatabaseController(adapter, {
|
|
||||||
collectionPrefix: 'test_'
|
|
||||||
});
|
});
|
||||||
|
let databaseController = new DatabaseController(adapter);
|
||||||
databaseController.connect().then(done, error => {
|
databaseController.connect().then(done, error => {
|
||||||
console.log('error', error.stack);
|
console.log('error', error.stack);
|
||||||
fail();
|
fail();
|
||||||
|
|||||||
@@ -7,13 +7,15 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { MongoClient, GridStore, Db} from 'mongodb';
|
import { MongoClient, GridStore, Db} from 'mongodb';
|
||||||
import { FilesAdapter } from './FilesAdapter';
|
import { FilesAdapter } from './FilesAdapter';
|
||||||
|
|
||||||
|
const DefaultMongoURI = 'mongodb://localhost:27017/parse';
|
||||||
|
|
||||||
export class GridStoreAdapter extends FilesAdapter {
|
export class GridStoreAdapter extends FilesAdapter {
|
||||||
_databaseURI: string;
|
_databaseURI: string;
|
||||||
_connectionPromise: Promise<Db>;
|
_connectionPromise: Promise<Db>;
|
||||||
|
|
||||||
constructor(mongoDatabaseURI: string) {
|
constructor(mongoDatabaseURI = DefaultMongoURI) {
|
||||||
super();
|
super();
|
||||||
this._databaseURI = mongoDatabaseURI;
|
this._databaseURI = mongoDatabaseURI;
|
||||||
this._connect();
|
this._connect();
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ let mongodb = require('mongodb');
|
|||||||
let MongoClient = mongodb.MongoClient;
|
let MongoClient = mongodb.MongoClient;
|
||||||
|
|
||||||
const MongoSchemaCollectionName = '_SCHEMA';
|
const MongoSchemaCollectionName = '_SCHEMA';
|
||||||
|
const DefaultMongoURI = 'mongodb://localhost:27017/parse';
|
||||||
|
|
||||||
export class MongoStorageAdapter {
|
export class MongoStorageAdapter {
|
||||||
// Private
|
// Private
|
||||||
@@ -18,7 +19,7 @@ export class MongoStorageAdapter {
|
|||||||
database;
|
database;
|
||||||
|
|
||||||
constructor({
|
constructor({
|
||||||
uri,
|
uri = DefaultMongoURI,
|
||||||
collectionPrefix = '',
|
collectionPrefix = '',
|
||||||
mongoOptions = {},
|
mongoOptions = {},
|
||||||
}) {
|
}) {
|
||||||
@@ -50,29 +51,30 @@ export class MongoStorageAdapter {
|
|||||||
|
|
||||||
adaptiveCollection(name: string) {
|
adaptiveCollection(name: string) {
|
||||||
return this.connect()
|
return this.connect()
|
||||||
.then(() => this.database.collection(name))
|
.then(() => this.database.collection(this._collectionPrefix + name))
|
||||||
.then(rawCollection => new MongoCollection(rawCollection));
|
.then(rawCollection => new MongoCollection(rawCollection));
|
||||||
}
|
}
|
||||||
|
|
||||||
schemaCollection(collectionPrefix: string) {
|
schemaCollection() {
|
||||||
return this.connect()
|
return this.connect()
|
||||||
.then(() => this.adaptiveCollection(collectionPrefix + MongoSchemaCollectionName))
|
.then(() => this.adaptiveCollection(this._collectionPrefix + MongoSchemaCollectionName))
|
||||||
.then(collection => new MongoSchemaCollection(collection));
|
.then(collection => new MongoSchemaCollection(collection));
|
||||||
}
|
}
|
||||||
|
|
||||||
collectionExists(name: string) {
|
collectionExists(name: string) {
|
||||||
return this.connect().then(() => {
|
return this.connect().then(() => {
|
||||||
return this.database.listCollections({ name: name }).toArray();
|
return this.database.listCollections({ name: this._collectionPrefix + name }).toArray();
|
||||||
}).then(collections => {
|
}).then(collections => {
|
||||||
return collections.length > 0;
|
return collections.length > 0;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
dropCollection(name: string) {
|
dropCollection(name: string) {
|
||||||
return this.collection(name).then(collection => collection.drop());
|
return this.collection(this._collectionPrefix + name).then(collection => collection.drop());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Used for testing only right now.
|
// Used for testing only right now.
|
||||||
collectionsContaining(match: string) {
|
allCollections() {
|
||||||
return this.connect().then(() => {
|
return this.connect().then(() => {
|
||||||
return this.database.collections();
|
return this.database.collections();
|
||||||
}).then(collections => {
|
}).then(collections => {
|
||||||
@@ -80,7 +82,7 @@ export class MongoStorageAdapter {
|
|||||||
if (collection.namespace.match(/\.system\./)) {
|
if (collection.namespace.match(/\.system\./)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return (collection.collectionName.indexOf(match) == 0);
|
return (collection.collectionName.indexOf(this._collectionPrefix) == 0);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -105,13 +107,7 @@ export class MongoStorageAdapter {
|
|||||||
// may do so.
|
// may do so.
|
||||||
|
|
||||||
// Returns a Promise.
|
// Returns a Promise.
|
||||||
|
deleteFields(className: string, fieldNames, pointerFieldNames) {
|
||||||
// This function currently accepts the collectionPrefix and adaptive collection as a paramater because it isn't
|
|
||||||
// actually capable of determining the location of it's own _SCHEMA collection without having
|
|
||||||
// the collectionPrefix. Also, Schemas.js, the caller of this function, only stores the collection
|
|
||||||
// itself, and not the prefix. Eventually Parse Server won't care what a SchemaCollection is and
|
|
||||||
// will just tell the DB adapter to do things and it will do them.
|
|
||||||
deleteFields(className: string, fieldNames, pointerFieldNames, collectionPrefix, adaptiveCollection) {
|
|
||||||
const nonPointerFieldNames = _.difference(fieldNames, pointerFieldNames);
|
const nonPointerFieldNames = _.difference(fieldNames, pointerFieldNames);
|
||||||
const mongoFormatNames = nonPointerFieldNames.concat(pointerFieldNames.map(name => `_p_${name}`));
|
const mongoFormatNames = nonPointerFieldNames.concat(pointerFieldNames.map(name => `_p_${name}`));
|
||||||
const collectionUpdate = { '$unset' : {} };
|
const collectionUpdate = { '$unset' : {} };
|
||||||
@@ -124,10 +120,9 @@ export class MongoStorageAdapter {
|
|||||||
schemaUpdate['$unset'][name] = null;
|
schemaUpdate['$unset'][name] = null;
|
||||||
});
|
});
|
||||||
|
|
||||||
return adaptiveCollection.updateMany({}, collectionUpdate)
|
return this.adaptiveCollection(className)
|
||||||
.then(updateResult => {
|
.then(collection => collection.updateMany({}, collectionUpdate))
|
||||||
return this.schemaCollection(collectionPrefix)
|
.then(updateResult => this.schemaCollection())
|
||||||
})
|
|
||||||
.then(schemaCollection => schemaCollection.updateSchema(className, schemaUpdate));
|
.then(schemaCollection => schemaCollection.updateSchema(className, schemaUpdate));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,13 +10,9 @@ var Schema = require('./../Schema');
|
|||||||
var transform = require('./../transform');
|
var transform = require('./../transform');
|
||||||
const deepcopy = require('deepcopy');
|
const deepcopy = require('deepcopy');
|
||||||
|
|
||||||
// options can contain:
|
function DatabaseController(adapter) {
|
||||||
// collectionPrefix: the string to put in front of every collection name.
|
|
||||||
function DatabaseController(adapter, { collectionPrefix } = {}) {
|
|
||||||
this.adapter = adapter;
|
this.adapter = adapter;
|
||||||
|
|
||||||
this.collectionPrefix = collectionPrefix;
|
|
||||||
|
|
||||||
// We don't want a mutable this.schema, because then you could have
|
// We don't want a mutable this.schema, because then you could have
|
||||||
// one request that uses different schemas for different parts of
|
// one request that uses different schemas for different parts of
|
||||||
// it. Instead, use loadSchema to get a schema.
|
// it. Instead, use loadSchema to get a schema.
|
||||||
@@ -32,25 +28,21 @@ DatabaseController.prototype.connect = function() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
DatabaseController.prototype.adaptiveCollection = function(className) {
|
DatabaseController.prototype.adaptiveCollection = function(className) {
|
||||||
return this.adapter.adaptiveCollection(this.collectionPrefix + className);
|
return this.adapter.adaptiveCollection(className);
|
||||||
};
|
};
|
||||||
|
|
||||||
DatabaseController.prototype.schemaCollection = function() {
|
DatabaseController.prototype.schemaCollection = function() {
|
||||||
return this.adapter.schemaCollection(this.collectionPrefix);
|
return this.adapter.schemaCollection();
|
||||||
};
|
};
|
||||||
|
|
||||||
DatabaseController.prototype.collectionExists = function(className) {
|
DatabaseController.prototype.collectionExists = function(className) {
|
||||||
return this.adapter.collectionExists(this.collectionPrefix + className);
|
return this.adapter.collectionExists(className);
|
||||||
};
|
};
|
||||||
|
|
||||||
DatabaseController.prototype.dropCollection = function(className) {
|
DatabaseController.prototype.dropCollection = function(className) {
|
||||||
return this.adapter.dropCollection(this.collectionPrefix + className);
|
return this.adapter.dropCollection(className);
|
||||||
};
|
};
|
||||||
|
|
||||||
function returnsTrue() {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
DatabaseController.prototype.validateClassName = function(className) {
|
DatabaseController.prototype.validateClassName = function(className) {
|
||||||
if (!Schema.classNameIsValid(className)) {
|
if (!Schema.classNameIsValid(className)) {
|
||||||
const error = new Parse.Error(Parse.Error.INVALID_CLASS_NAME, 'invalid className: ' + className);
|
const error = new Parse.Error(Parse.Error.INVALID_CLASS_NAME, 'invalid className: ' + className);
|
||||||
@@ -62,7 +54,7 @@ DatabaseController.prototype.validateClassName = function(className) {
|
|||||||
// Returns a promise for a schema object.
|
// Returns a promise for a schema object.
|
||||||
// If we are provided a acceptor, then we run it on the schema.
|
// If we are provided a acceptor, then we run it on the schema.
|
||||||
// If the schema isn't accepted, we reload it at most once.
|
// If the schema isn't accepted, we reload it at most once.
|
||||||
DatabaseController.prototype.loadSchema = function(acceptor = returnsTrue) {
|
DatabaseController.prototype.loadSchema = function(acceptor = () => true) {
|
||||||
|
|
||||||
if (!this.schemaPromise) {
|
if (!this.schemaPromise) {
|
||||||
this.schemaPromise = this.schemaCollection().then(collection => {
|
this.schemaPromise = this.schemaCollection().then(collection => {
|
||||||
@@ -388,10 +380,8 @@ DatabaseController.prototype.mongoFind = function(className, query, options = {}
|
|||||||
DatabaseController.prototype.deleteEverything = function() {
|
DatabaseController.prototype.deleteEverything = function() {
|
||||||
this.schemaPromise = null;
|
this.schemaPromise = null;
|
||||||
|
|
||||||
return this.adapter.collectionsContaining(this.collectionPrefix).then(collections => {
|
return this.adapter.allCollections().then(collections => {
|
||||||
let promises = collections.map(collection => {
|
let promises = collections.map(collection => collection.drop());
|
||||||
return collection.drop();
|
|
||||||
});
|
|
||||||
return Promise.all(promises);
|
return Promise.all(promises);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -18,17 +18,10 @@
|
|||||||
import DatabaseController from './Controllers/DatabaseController';
|
import DatabaseController from './Controllers/DatabaseController';
|
||||||
import MongoStorageAdapter from './Adapters/Storage/Mongo/MongoStorageAdapter';
|
import MongoStorageAdapter from './Adapters/Storage/Mongo/MongoStorageAdapter';
|
||||||
|
|
||||||
const DefaultDatabaseURI = 'mongodb://localhost:27017/parse';
|
|
||||||
|
|
||||||
let dbConnections = {};
|
let dbConnections = {};
|
||||||
let databaseURI = DefaultDatabaseURI;
|
|
||||||
let appDatabaseURIs = {};
|
let appDatabaseURIs = {};
|
||||||
let appDatabaseOptions = {};
|
let appDatabaseOptions = {};
|
||||||
|
|
||||||
function setDatabaseURI(uri) {
|
|
||||||
databaseURI = uri;
|
|
||||||
}
|
|
||||||
|
|
||||||
function setAppDatabaseURI(appId, uri) {
|
function setAppDatabaseURI(appId, uri) {
|
||||||
appDatabaseURIs[appId] = uri;
|
appDatabaseURIs[appId] = uri;
|
||||||
}
|
}
|
||||||
@@ -61,26 +54,21 @@ function getDatabaseConnection(appId: string, collectionPrefix: string) {
|
|||||||
return dbConnections[appId];
|
return dbConnections[appId];
|
||||||
}
|
}
|
||||||
|
|
||||||
var dbURI = (appDatabaseURIs[appId] ? appDatabaseURIs[appId] : databaseURI);
|
let mongoAdapterOptions = {
|
||||||
|
|
||||||
let storageAdapter = new MongoStorageAdapter({
|
|
||||||
uri: dbURI,
|
|
||||||
collectionPrefix: collectionPrefix,
|
collectionPrefix: collectionPrefix,
|
||||||
mongoOptions: appDatabaseOptions[appId]
|
mongoOptions: appDatabaseOptions[appId],
|
||||||
});
|
uri: appDatabaseURIs[appId], //may be undefined if the user didn't supply a URI, in which case the default will be used
|
||||||
|
}
|
||||||
|
|
||||||
|
dbConnections[appId] = new DatabaseController(new MongoStorageAdapter(mongoAdapterOptions));
|
||||||
|
|
||||||
dbConnections[appId] = new DatabaseController(storageAdapter, {
|
|
||||||
collectionPrefix: collectionPrefix
|
|
||||||
});
|
|
||||||
return dbConnections[appId];
|
return dbConnections[appId];
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
getDatabaseConnection: getDatabaseConnection,
|
getDatabaseConnection: getDatabaseConnection,
|
||||||
setDatabaseURI: setDatabaseURI,
|
|
||||||
setAppDatabaseOptions: setAppDatabaseOptions,
|
setAppDatabaseOptions: setAppDatabaseOptions,
|
||||||
setAppDatabaseURI: setAppDatabaseURI,
|
setAppDatabaseURI: setAppDatabaseURI,
|
||||||
clearDatabaseSettings: clearDatabaseSettings,
|
clearDatabaseSettings: clearDatabaseSettings,
|
||||||
destroyAllDataPermanently: destroyAllDataPermanently,
|
destroyAllDataPermanently: destroyAllDataPermanently,
|
||||||
defaultDatabaseURI: databaseURI
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -53,8 +53,6 @@ addParseCloud();
|
|||||||
|
|
||||||
// ParseServer works like a constructor of an express app.
|
// ParseServer works like a constructor of an express app.
|
||||||
// The args that we understand are:
|
// The args that we understand are:
|
||||||
// "databaseAdapter": a class like DatabaseController providing create, find,
|
|
||||||
// update, and delete
|
|
||||||
// "filesAdapter": a class like GridStoreAdapter providing create, get,
|
// "filesAdapter": a class like GridStoreAdapter providing create, get,
|
||||||
// and delete
|
// and delete
|
||||||
// "loggerAdapter": a class like FileLoggerAdapter providing info, error,
|
// "loggerAdapter": a class like FileLoggerAdapter providing info, error,
|
||||||
@@ -88,7 +86,7 @@ class ParseServer {
|
|||||||
push,
|
push,
|
||||||
loggerAdapter,
|
loggerAdapter,
|
||||||
logsFolder,
|
logsFolder,
|
||||||
databaseURI = DatabaseAdapter.defaultDatabaseURI,
|
databaseURI,
|
||||||
databaseOptions,
|
databaseOptions,
|
||||||
cloud,
|
cloud,
|
||||||
collectionPrefix = '',
|
collectionPrefix = '',
|
||||||
@@ -130,9 +128,7 @@ class ParseServer {
|
|||||||
DatabaseAdapter.setAppDatabaseOptions(appId, databaseOptions);
|
DatabaseAdapter.setAppDatabaseOptions(appId, databaseOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (databaseURI) {
|
DatabaseAdapter.setAppDatabaseURI(appId, databaseURI);
|
||||||
DatabaseAdapter.setAppDatabaseURI(appId, databaseURI);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (cloud) {
|
if (cloud) {
|
||||||
addParseCloud();
|
addParseCloud();
|
||||||
|
|||||||
@@ -525,9 +525,7 @@ class Schema {
|
|||||||
|
|
||||||
if (this.data[className][fieldName].type == 'Relation') {
|
if (this.data[className][fieldName].type == 'Relation') {
|
||||||
//For relations, drop the _Join table
|
//For relations, drop the _Join table
|
||||||
return database.adaptiveCollection(className).then(collection => {
|
return database.adapter.deleteFields(className, [fieldName], [])
|
||||||
return database.adapter.deleteFields(className, [fieldName], [], database.collectionPrefix, collection);
|
|
||||||
})
|
|
||||||
.then(() => database.dropCollection(`_Join:${fieldName}:${className}`))
|
.then(() => database.dropCollection(`_Join:${fieldName}:${className}`))
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
// 'ns not found' means collection was already gone. Ignore deletion attempt.
|
// 'ns not found' means collection was already gone. Ignore deletion attempt.
|
||||||
@@ -541,8 +539,7 @@ class Schema {
|
|||||||
|
|
||||||
const fieldNames = [fieldName];
|
const fieldNames = [fieldName];
|
||||||
const pointerFieldNames = this.data[className][fieldName].type === 'Pointer' ? [fieldName] : [];
|
const pointerFieldNames = this.data[className][fieldName].type === 'Pointer' ? [fieldName] : [];
|
||||||
return database.adaptiveCollection(className)
|
return database.adapter.deleteFields(className, fieldNames, pointerFieldNames);
|
||||||
.then(collection => database.adapter.deleteFields(className, fieldNames, pointerFieldNames, database.collectionPrefix, collection));
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
// testing-routes.js
|
// testing-routes.js
|
||||||
import cache from './cache';
|
import cache from './cache';
|
||||||
import * as middlewares from './middlewares';
|
import * as middlewares from './middlewares';
|
||||||
import { ParseServer } from './index';
|
import { ParseServer } from './index';
|
||||||
import { Parse } from 'parse/node';
|
import { Parse } from 'parse/node';
|
||||||
|
|
||||||
var express = require('express'),
|
var express = require('express'),
|
||||||
cryptoUtils = require('./cryptoUtils');
|
cryptoUtils = require('./cryptoUtils');
|
||||||
@@ -31,7 +31,7 @@ function createApp(req, res) {
|
|||||||
res.status(200).send(keys);
|
res.status(200).send(keys);
|
||||||
}
|
}
|
||||||
|
|
||||||
// deletes all collections with the collectionPrefix of the app
|
// deletes all collections that belong to the app
|
||||||
function clearApp(req, res) {
|
function clearApp(req, res) {
|
||||||
if (!req.auth.isMaster) {
|
if (!req.auth.isMaster) {
|
||||||
return res.status(401).send({ "error": "unauthorized" });
|
return res.status(401).send({ "error": "unauthorized" });
|
||||||
|
|||||||
Reference in New Issue
Block a user