Break schemaController dependency. (#1901)

* Break dependency on MongoCollection for updateMany

* Move transformWhere usage into MongoTransform

* Pass parse schema into transformUpdate

* break dependency on schemaController

* remove schema parameter

* move key name validation up one level

* Move validation out of mongo adapter

* Move validation into Parse Server and transformUpdate in Mongo Adapter

* Update mongo adapter

* Use adapter API

* use and fix mongo adapter api

* Remove/rename stuff

* Kill transform in DBController

* better imports for transform

* Tidy ConfigRouter

* Remove schemaController in more places

* Remove comment
This commit is contained in:
Drew
2016-05-24 17:21:20 -07:00
committed by Peter J. Shin
parent c4499d25e0
commit c9a138d9ca
5 changed files with 89 additions and 116 deletions

View File

@@ -1,8 +1,17 @@
import MongoCollection from './MongoCollection';
import MongoSchemaCollection from './MongoSchemaCollection';
import {parse as parseUrl, format as formatUrl} from '../../../vendor/mongodbUrl';
import * as transform from './MongoTransform';
import _ from 'lodash';
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';
let mongodb = require('mongodb');
let MongoClient = mongodb.MongoClient;
@@ -159,12 +168,11 @@ export class MongoStorageAdapter {
.then(schemasCollection => schemasCollection._fechOneSchemaFrom_SCHEMA(className));
}
// TODO: As yet not particularly well specified. Creates an object. Shouldn't need the
// schemaController, but MongoTransform still needs it :( maybe shouldn't even need the schema,
// TODO: As yet not particularly well specified. Creates an object. Maybe shouldn't even need the schema,
// 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.
createObject(className, object, schemaController, parseFormatSchema) {
const mongoObject = transform.parseObjectToMongoObjectForCreate(schemaController, className, object, parseFormatSchema);
createObject(className, object, schema) {
const mongoObject = parseObjectToMongoObjectForCreate(className, object, schema);
return this.adaptiveCollection(className)
.then(collection => collection.insertOne(mongoObject))
.catch(error => {
@@ -176,15 +184,13 @@ export class MongoStorageAdapter {
});
}
// Remove all objects that match the given parse query. Parse Query should be in Parse Format.
// Remove all objects that match the given Parse Query.
// 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.
// Currently accepts the schema, that may not actually be necessary.
deleteObjectsByQuery(className, query, schema) {
return this.adaptiveCollection(className)
.then(collection => {
let mongoWhere = transform.transformWhere(className, query, schema);
let mongoWhere = transformWhere(className, query, schema);
return collection.deleteMany(mongoWhere)
})
.then(({ result }) => {
@@ -197,23 +203,43 @@ export class MongoStorageAdapter {
});
}
// 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));
}
// Executes a find. Accepts: className, query in Parse format, and { skip, limit, sort }.
find(className, query, schema, { skip, limit, sort }) {
let mongoWhere = this.transform.transformWhere(className, query, schema);
let mongoSort = _.mapKeys(sort, (value, fieldName) => transform.transformKey(className, fieldName, schema));
let mongoWhere = transformWhere(className, query, schema);
let mongoSort = _.mapKeys(sort, (value, fieldName) => transformKey(className, fieldName, schema));
return this.adaptiveCollection(className)
.then(collection => collection.find(mongoWhere, { skip, limit, sort: mongoSort }))
.then(objects => objects.map(object => transform.mongoObjectToParseObject(className, object, schema)));
.then(objects => objects.map(object => mongoObjectToParseObject(className, object, schema)));
}
// Executs a count.
count(className, query, schema) {
return this.adaptiveCollection(className)
.then(collection => collection.count(transform.transformWhere(className, query, schema)));
}
get transform() {
return transform;
.then(collection => collection.count(transformWhere(className, query, schema)));
}
}