2016-04-12 19:06:58 -07:00
|
|
|
import MongoCollection from './MongoCollection';
|
|
|
|
|
import MongoSchemaCollection from './MongoSchemaCollection';
|
2016-03-16 14:54:52 -07:00
|
|
|
import {parse as parseUrl, format as formatUrl} from '../../../vendor/mongodbUrl';
|
2016-04-12 19:06:58 -07:00
|
|
|
import _ from 'lodash';
|
2016-03-01 20:04:15 -08:00
|
|
|
|
2016-02-27 02:23:57 -08:00
|
|
|
let mongodb = require('mongodb');
|
|
|
|
|
let MongoClient = mongodb.MongoClient;
|
|
|
|
|
|
2016-03-09 15:20:59 -08:00
|
|
|
const MongoSchemaCollectionName = '_SCHEMA';
|
|
|
|
|
|
2016-02-27 02:23:57 -08:00
|
|
|
export class MongoStorageAdapter {
|
|
|
|
|
// Private
|
|
|
|
|
_uri: string;
|
2016-04-13 05:21:53 -07:00
|
|
|
_collectionPrefix: string;
|
|
|
|
|
_mongoOptions: Object;
|
2016-02-27 02:23:57 -08:00
|
|
|
// Public
|
|
|
|
|
connectionPromise;
|
|
|
|
|
database;
|
|
|
|
|
|
2016-04-13 05:21:53 -07:00
|
|
|
constructor({
|
|
|
|
|
uri,
|
|
|
|
|
collectionPrefix = '',
|
|
|
|
|
mongoOptions = {},
|
|
|
|
|
}) {
|
2016-02-27 02:23:57 -08:00
|
|
|
this._uri = uri;
|
2016-04-13 05:21:53 -07:00
|
|
|
this._collectionPrefix = collectionPrefix;
|
|
|
|
|
this._mongoOptions = mongoOptions;
|
2016-02-27 02:23:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
connect() {
|
|
|
|
|
if (this.connectionPromise) {
|
|
|
|
|
return this.connectionPromise;
|
|
|
|
|
}
|
|
|
|
|
|
Add URI encoding to mongo auth parameters
The mongodb driver requires auth values be URI encoded:
https://github.com/mongodb/node-mongodb-native/commit/044063097dc4dd5e6cf3a3574c555fec7559d38b
This uses node's built-in url module to encode the auth portion, by
parsing and re-formatting it, which causes special characters to get URI
encoded properly:
https://nodejs.org/api/url.html#url_escaped_characters
This is all a bit silly since mongodb just takes our passed uri, and
runs it through the same url parser again, but not before explicitly
erroring on '@' characters in the uri.
This is similiar to #148 (reverted by #297), but with much less code,
and hopefully less breakage. Also, note that `uri_decode_auth` is no
longer needed. That was removed in the above referenced
node-mongodb-native commit.
I've tested this on usernames and passwords with @, !, +, and a space.
Presumably this would also work with usernames and passwords that are
already URI encoded (since parseUrl will simply unescape it, and
formatUrl will escape it again).
2016-03-11 16:10:44 -08:00
|
|
|
// parsing and re-formatting causes the auth value (if there) to get URI
|
|
|
|
|
// encoded
|
|
|
|
|
const encodedUri = formatUrl(parseUrl(this._uri));
|
|
|
|
|
|
2016-04-13 05:21:53 -07:00
|
|
|
this.connectionPromise = MongoClient.connect(encodedUri, this._mongoOptions).then(database => {
|
2016-02-27 02:23:57 -08:00
|
|
|
this.database = database;
|
|
|
|
|
});
|
|
|
|
|
return this.connectionPromise;
|
|
|
|
|
}
|
2016-02-27 03:02:38 -08:00
|
|
|
|
|
|
|
|
collection(name: string) {
|
|
|
|
|
return this.connect().then(() => {
|
|
|
|
|
return this.database.collection(name);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-01 20:04:15 -08:00
|
|
|
adaptiveCollection(name: string) {
|
|
|
|
|
return this.connect()
|
|
|
|
|
.then(() => this.database.collection(name))
|
|
|
|
|
.then(rawCollection => new MongoCollection(rawCollection));
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-09 15:20:59 -08:00
|
|
|
schemaCollection(collectionPrefix: string) {
|
|
|
|
|
return this.connect()
|
|
|
|
|
.then(() => this.adaptiveCollection(collectionPrefix + MongoSchemaCollectionName))
|
|
|
|
|
.then(collection => new MongoSchemaCollection(collection));
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-29 19:41:05 -08:00
|
|
|
collectionExists(name: string) {
|
|
|
|
|
return this.connect().then(() => {
|
|
|
|
|
return this.database.listCollections({ name: name }).toArray();
|
|
|
|
|
}).then(collections => {
|
|
|
|
|
return collections.length > 0;
|
|
|
|
|
});
|
2016-02-29 17:04:38 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dropCollection(name: string) {
|
|
|
|
|
return this.collection(name).then(collection => collection.drop());
|
|
|
|
|
}
|
2016-02-27 03:02:38 -08:00
|
|
|
// Used for testing only right now.
|
|
|
|
|
collectionsContaining(match: string) {
|
|
|
|
|
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);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
2016-04-12 19:06:58 -07:00
|
|
|
|
|
|
|
|
// Remove the column and all the data. For Relations, the _Join collection is handled
|
|
|
|
|
// specially, this function does not delete _Join columns. It should, however, indicate
|
|
|
|
|
// that the relation fields does not exist anymore. In mongo, this means removing it from
|
|
|
|
|
// the _SCHEMA collection. There should be no actual data in the collection under the same name
|
|
|
|
|
// as the relation column, so it's fine to attempt to delete it. If the fields listed to be
|
|
|
|
|
// deleted do not exist, this function should return successfully anyways. Checking for
|
|
|
|
|
// attempts to delete non-existent fields is the responsibility of Parse Server.
|
|
|
|
|
|
|
|
|
|
// Pointer field names are passed for legacy reasons: the original mongo
|
|
|
|
|
// format stored pointer field names differently in the database, and therefore
|
|
|
|
|
// needed to know the type of the field before it could delete it. Future database
|
|
|
|
|
// adatpers should ignore the pointerFieldNames argument. All the field names are in
|
|
|
|
|
// fieldNames, they show up additionally in the pointerFieldNames database for use
|
|
|
|
|
// by the mongo adapter, which deals with the legacy mongo format.
|
|
|
|
|
|
|
|
|
|
// This function is not obligated to delete fields atomically. It is given the field
|
|
|
|
|
// names in a list so that databases that are capable of deleting fields atomically
|
|
|
|
|
// may do so.
|
|
|
|
|
|
|
|
|
|
// Returns a Promise.
|
|
|
|
|
|
|
|
|
|
// 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 mongoFormatNames = nonPointerFieldNames.concat(pointerFieldNames.map(name => `_p_${name}`));
|
|
|
|
|
const collectionUpdate = { '$unset' : {} };
|
|
|
|
|
mongoFormatNames.forEach(name => {
|
|
|
|
|
collectionUpdate['$unset'][name] = null;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const schemaUpdate = { '$unset' : {} };
|
|
|
|
|
fieldNames.forEach(name => {
|
|
|
|
|
schemaUpdate['$unset'][name] = null;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return adaptiveCollection.updateMany({}, collectionUpdate)
|
|
|
|
|
.then(updateResult => {
|
|
|
|
|
return this.schemaCollection(collectionPrefix)
|
|
|
|
|
})
|
|
|
|
|
.then(schemaCollection => schemaCollection.updateSchema(className, schemaUpdate));
|
|
|
|
|
}
|
2016-02-27 02:23:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default MongoStorageAdapter;
|
|
|
|
|
module.exports = MongoStorageAdapter; // Required for tests
|