2016-05-24 17:21:20 -07:00
|
|
|
import MongoCollection from './MongoCollection';
|
|
|
|
|
import MongoSchemaCollection from './MongoSchemaCollection';
|
|
|
|
|
import {
|
|
|
|
|
parse as parseUrl,
|
|
|
|
|
format as formatUrl,
|
|
|
|
|
} from '../../../vendor/mongodbUrl';
|
|
|
|
|
import {
|
|
|
|
|
parseObjectToMongoObjectForCreate,
|
|
|
|
|
mongoObjectToParseObject,
|
|
|
|
|
transformKey,
|
|
|
|
|
transformWhere,
|
|
|
|
|
transformUpdate,
|
|
|
|
|
} from './MongoTransform';
|
|
|
|
|
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-04-13 16:45:07 -07:00
|
|
|
const DefaultMongoURI = 'mongodb://localhost:27017/parse';
|
2016-03-09 15:20:59 -08:00
|
|
|
|
2016-04-25 11:47:57 -07:00
|
|
|
const storageAdapterAllCollections = mongoAdapter => {
|
|
|
|
|
return mongoAdapter.connect()
|
|
|
|
|
.then(() => mongoAdapter.database.collections())
|
|
|
|
|
.then(collections => {
|
|
|
|
|
return collections.filter(collection => {
|
|
|
|
|
if (collection.namespace.match(/\.system\./)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
// TODO: If you have one app with a collection prefix that happens to be a prefix of another
|
|
|
|
|
// apps prefix, this will go very very badly. We should fix that somehow.
|
|
|
|
|
return (collection.collectionName.indexOf(mongoAdapter._collectionPrefix) == 0);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
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({
|
2016-04-13 16:45:07 -07:00
|
|
|
uri = DefaultMongoURI,
|
2016-04-13 05:21:53 -07:00
|
|
|
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;
|
|
|
|
|
});
|
2016-06-10 20:27:21 -07:00
|
|
|
|
2016-02-27 02:23:57 -08:00
|
|
|
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()
|
2016-04-13 16:45:07 -07:00
|
|
|
.then(() => this.database.collection(this._collectionPrefix + name))
|
2016-03-01 20:04:15 -08:00
|
|
|
.then(rawCollection => new MongoCollection(rawCollection));
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-13 16:45:07 -07:00
|
|
|
schemaCollection() {
|
2016-03-09 15:20:59 -08:00
|
|
|
return this.connect()
|
2016-04-14 19:24:56 -04:00
|
|
|
.then(() => this.adaptiveCollection(MongoSchemaCollectionName))
|
2016-03-09 15:20:59 -08:00
|
|
|
.then(collection => new MongoSchemaCollection(collection));
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-29 19:41:05 -08:00
|
|
|
collectionExists(name: string) {
|
|
|
|
|
return this.connect().then(() => {
|
2016-04-13 16:45:07 -07:00
|
|
|
return this.database.listCollections({ name: this._collectionPrefix + name }).toArray();
|
2016-02-29 19:41:05 -08:00
|
|
|
}).then(collections => {
|
|
|
|
|
return collections.length > 0;
|
|
|
|
|
});
|
2016-02-29 17:04:38 -08:00
|
|
|
}
|
|
|
|
|
|
2016-04-25 11:47:57 -07:00
|
|
|
// Deletes a schema. Resolve if successful. If the schema doesn't
|
|
|
|
|
// exist, resolve with undefined. If schema exists, but can't be deleted for some other reason,
|
|
|
|
|
// reject with INTERNAL_SERVER_ERROR.
|
|
|
|
|
deleteOneSchema(className: string) {
|
2016-04-18 17:06:51 -07:00
|
|
|
return this.collection(this._collectionPrefix + className).then(collection => collection.drop())
|
|
|
|
|
.catch(error => {
|
|
|
|
|
// 'ns not found' means collection was already gone. Ignore deletion attempt.
|
|
|
|
|
if (error.message == 'ns not found') {
|
2016-06-10 20:27:21 -07:00
|
|
|
return;
|
2016-04-18 17:06:51 -07:00
|
|
|
}
|
2016-06-10 20:27:21 -07:00
|
|
|
throw error;
|
2016-04-18 17:06:51 -07:00
|
|
|
});
|
2016-02-29 17:04:38 -08:00
|
|
|
}
|
2016-04-13 16:45:07 -07:00
|
|
|
|
2016-04-25 11:47:57 -07:00
|
|
|
// Delete all data known to this adatper. Used for testing.
|
|
|
|
|
deleteAllSchemas() {
|
|
|
|
|
return storageAdapterAllCollections(this)
|
|
|
|
|
.then(collections => Promise.all(collections.map(collection => collection.drop())));
|
2016-02-27 03:02:38 -08:00
|
|
|
}
|
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.
|
2016-04-13 16:45:07 -07:00
|
|
|
deleteFields(className: string, fieldNames, pointerFieldNames) {
|
2016-04-12 19:06:58 -07:00
|
|
|
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;
|
|
|
|
|
});
|
|
|
|
|
|
2016-04-13 16:45:07 -07:00
|
|
|
return this.adaptiveCollection(className)
|
|
|
|
|
.then(collection => collection.updateMany({}, collectionUpdate))
|
|
|
|
|
.then(updateResult => this.schemaCollection())
|
2016-04-12 19:06:58 -07:00
|
|
|
.then(schemaCollection => schemaCollection.updateSchema(className, schemaUpdate));
|
|
|
|
|
}
|
2016-04-14 19:24:56 -04:00
|
|
|
|
2016-04-18 17:06:00 -07:00
|
|
|
// Return a promise for all schemas known to this adapter, in Parse format. In case the
|
|
|
|
|
// schemas cannot be retrieved, returns a promise that rejects. Requirements for the
|
|
|
|
|
// rejection reason are TBD.
|
|
|
|
|
getAllSchemas() {
|
|
|
|
|
return this.schemaCollection().then(schemasCollection => schemasCollection._fetchAllSchemasFrom_SCHEMA());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Return a promise for the schema with the given name, in Parse format. If
|
|
|
|
|
// this adapter doesn't know about the schema, return a promise that rejects with
|
|
|
|
|
// undefined as the reason.
|
|
|
|
|
getOneSchema(className) {
|
2016-04-20 13:35:48 -07:00
|
|
|
return this.schemaCollection()
|
|
|
|
|
.then(schemasCollection => schemasCollection._fechOneSchemaFrom_SCHEMA(className));
|
2016-04-18 17:06:00 -07:00
|
|
|
}
|
|
|
|
|
|
2016-05-24 17:21:20 -07:00
|
|
|
// TODO: As yet not particularly well specified. Creates an object. Maybe shouldn't even need the schema,
|
2016-04-20 13:35:48 -07:00
|
|
|
// and should infer from the type. Or maybe does need the schema for validations. Or maybe needs
|
|
|
|
|
// the schem only for the legacy mongo format. We'll figure that out later.
|
2016-05-24 17:21:20 -07:00
|
|
|
createObject(className, object, schema) {
|
|
|
|
|
const mongoObject = parseObjectToMongoObjectForCreate(className, object, schema);
|
2016-04-18 09:45:48 -07:00
|
|
|
return this.adaptiveCollection(className)
|
2016-05-12 08:24:15 +08:00
|
|
|
.then(collection => collection.insertOne(mongoObject))
|
|
|
|
|
.catch(error => {
|
|
|
|
|
if (error.code === 11000) { // Duplicate value
|
|
|
|
|
throw new Parse.Error(Parse.Error.DUPLICATE_VALUE,
|
|
|
|
|
'A duplicate value for a field with unique values was provided');
|
|
|
|
|
}
|
2016-06-10 20:27:21 -07:00
|
|
|
throw error;
|
2016-05-12 08:24:15 +08:00
|
|
|
});
|
2016-04-18 09:45:48 -07:00
|
|
|
}
|
|
|
|
|
|
2016-05-24 17:21:20 -07:00
|
|
|
// Remove all objects that match the given Parse Query.
|
2016-04-22 14:05:21 -07:00
|
|
|
// If no objects match, reject with OBJECT_NOT_FOUND. If objects are found and deleted, resolve with undefined.
|
|
|
|
|
// If there is some other error, reject with INTERNAL_SERVER_ERROR.
|
2016-05-18 18:35:02 -07:00
|
|
|
deleteObjectsByQuery(className, query, schema) {
|
2016-04-22 14:05:21 -07:00
|
|
|
return this.adaptiveCollection(className)
|
|
|
|
|
.then(collection => {
|
2016-05-24 17:21:20 -07:00
|
|
|
let mongoWhere = transformWhere(className, query, schema);
|
2016-04-22 14:05:21 -07:00
|
|
|
return collection.deleteMany(mongoWhere)
|
|
|
|
|
})
|
|
|
|
|
.then(({ result }) => {
|
|
|
|
|
if (result.n === 0) {
|
|
|
|
|
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Object not found.');
|
|
|
|
|
}
|
|
|
|
|
return Promise.resolve();
|
|
|
|
|
}, error => {
|
|
|
|
|
throw new Parse.Error(Parse.Error.INTERNAL_SERVER_ERROR, 'Database adapter error');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-24 17:21:20 -07:00
|
|
|
// Apply the update to all objects that match the given Parse Query.
|
|
|
|
|
updateObjectsByQuery(className, query, schema, update) {
|
|
|
|
|
const mongoUpdate = transformUpdate(className, update, schema);
|
|
|
|
|
const mongoWhere = transformWhere(className, query, schema);
|
|
|
|
|
return this.adaptiveCollection(className)
|
|
|
|
|
.then(collection => collection.updateMany(mongoWhere, mongoUpdate));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Hopefully we can get rid of this in favor of updateObjectsByQuery.
|
|
|
|
|
findOneAndUpdate(className, query, schema, update) {
|
|
|
|
|
const mongoUpdate = transformUpdate(className, update, schema);
|
|
|
|
|
const mongoWhere = transformWhere(className, query, schema);
|
|
|
|
|
return this.adaptiveCollection(className)
|
|
|
|
|
.then(collection => collection.findOneAndUpdate(mongoWhere, mongoUpdate));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Hopefully we can get rid of this. It's only used for config and hooks.
|
|
|
|
|
upsertOneObject(className, query, schema, update) {
|
|
|
|
|
const mongoUpdate = transformUpdate(className, update, schema);
|
|
|
|
|
const mongoWhere = transformWhere(className, query, schema);
|
|
|
|
|
return this.adaptiveCollection(className)
|
|
|
|
|
.then(collection => collection.upsertOne(mongoWhere, mongoUpdate));
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-23 16:31:51 -07:00
|
|
|
// Executes a find. Accepts: className, query in Parse format, and { skip, limit, sort }.
|
2016-05-23 20:22:04 -07:00
|
|
|
find(className, query, schema, { skip, limit, sort }) {
|
2016-05-24 17:21:20 -07:00
|
|
|
let mongoWhere = transformWhere(className, query, schema);
|
|
|
|
|
let mongoSort = _.mapKeys(sort, (value, fieldName) => transformKey(className, fieldName, schema));
|
2016-05-23 16:31:51 -07:00
|
|
|
return this.adaptiveCollection(className)
|
2016-05-23 21:16:03 -07:00
|
|
|
.then(collection => collection.find(mongoWhere, { skip, limit, sort: mongoSort }))
|
2016-05-24 17:21:20 -07:00
|
|
|
.then(objects => objects.map(object => mongoObjectToParseObject(className, object, schema)));
|
2016-05-23 16:31:51 -07:00
|
|
|
}
|
|
|
|
|
|
2016-06-10 20:27:21 -07:00
|
|
|
// Create a unique index. Unique indexes on nullable fields are not allowed. Since we don't
|
|
|
|
|
// currently know which fields are nullable and which aren't, we ignore that criteria.
|
|
|
|
|
// As such, we shouldn't expose this function to users of parse until we have an out-of-band
|
|
|
|
|
// Way of determining if a field is nullable. Undefined doesn't count against uniqueness,
|
|
|
|
|
// which is why we use sparse indexes.
|
|
|
|
|
ensureUniqueness(className, fieldNames, schema) {
|
|
|
|
|
let indexCreationRequest = {};
|
|
|
|
|
let mongoFieldNames = fieldNames.map(fieldName => transformKey(className, fieldName, schema));
|
|
|
|
|
mongoFieldNames.forEach(fieldName => {
|
|
|
|
|
indexCreationRequest[fieldName] = 1;
|
|
|
|
|
});
|
|
|
|
|
return this.adaptiveCollection(className)
|
|
|
|
|
.then(collection => collection._ensureSparseUniqueIndexInBackground(indexCreationRequest))
|
|
|
|
|
.catch(error => {
|
|
|
|
|
if (error.code === 11000) {
|
|
|
|
|
throw new Parse.Error(Parse.Error.DUPLICATE_VALUE, 'Tried to ensure field uniqueness for a class that already has duplicates.');
|
|
|
|
|
} else {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-28 09:25:09 -07:00
|
|
|
// Used in tests
|
|
|
|
|
_rawFind(className, query) {
|
|
|
|
|
return this.adaptiveCollection(className).then(collection => collection.find(query));
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-23 19:00:58 -07:00
|
|
|
// Executs a count.
|
2016-05-23 20:19:03 -07:00
|
|
|
count(className, query, schema) {
|
2016-05-23 19:00:58 -07:00
|
|
|
return this.adaptiveCollection(className)
|
2016-05-24 17:21:20 -07:00
|
|
|
.then(collection => collection.count(transformWhere(className, query, schema)));
|
2016-04-14 19:24:56 -04:00
|
|
|
}
|
2016-02-27 02:23:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default MongoStorageAdapter;
|
|
|
|
|
module.exports = MongoStorageAdapter; // Required for tests
|