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:
@@ -5,26 +5,11 @@ let transform = require('../src/Adapters/Storage/Mongo/MongoTransform');
|
|||||||
let dd = require('deep-diff');
|
let dd = require('deep-diff');
|
||||||
let mongodb = require('mongodb');
|
let mongodb = require('mongodb');
|
||||||
|
|
||||||
var dummySchema = {
|
|
||||||
data: {},
|
|
||||||
getExpectedType: function(className, key) {
|
|
||||||
if (key == 'userPointer') {
|
|
||||||
return { type: 'Pointer', targetClass: '_User' };
|
|
||||||
} else if (key == 'picture') {
|
|
||||||
return { type: 'File' };
|
|
||||||
} else if (key == 'location') {
|
|
||||||
return { type: 'GeoPoint' };
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
describe('parseObjectToMongoObjectForCreate', () => {
|
describe('parseObjectToMongoObjectForCreate', () => {
|
||||||
|
|
||||||
it('a basic number', (done) => {
|
it('a basic number', (done) => {
|
||||||
var input = {five: 5};
|
var input = {five: 5};
|
||||||
var output = transform.parseObjectToMongoObjectForCreate(dummySchema, null, input, {
|
var output = transform.parseObjectToMongoObjectForCreate(null, input, {
|
||||||
fields: {five: {type: 'Number'}}
|
fields: {five: {type: 'Number'}}
|
||||||
});
|
});
|
||||||
jequal(input, output);
|
jequal(input, output);
|
||||||
@@ -36,7 +21,7 @@ describe('parseObjectToMongoObjectForCreate', () => {
|
|||||||
createdAt: "2015-10-06T21:24:50.332Z",
|
createdAt: "2015-10-06T21:24:50.332Z",
|
||||||
updatedAt: "2015-10-06T21:24:50.332Z"
|
updatedAt: "2015-10-06T21:24:50.332Z"
|
||||||
};
|
};
|
||||||
var output = transform.parseObjectToMongoObjectForCreate(dummySchema, null, input, { fields: {} });
|
var output = transform.parseObjectToMongoObjectForCreate(null, input, { fields: {} });
|
||||||
expect(output._created_at instanceof Date).toBe(true);
|
expect(output._created_at instanceof Date).toBe(true);
|
||||||
expect(output._updated_at instanceof Date).toBe(true);
|
expect(output._updated_at instanceof Date).toBe(true);
|
||||||
done();
|
done();
|
||||||
@@ -48,7 +33,7 @@ describe('parseObjectToMongoObjectForCreate', () => {
|
|||||||
objectId: 'myId',
|
objectId: 'myId',
|
||||||
className: 'Blah',
|
className: 'Blah',
|
||||||
};
|
};
|
||||||
var out = transform.parseObjectToMongoObjectForCreate(dummySchema, null, {pointers: [pointer]},{
|
var out = transform.parseObjectToMongoObjectForCreate(null, {pointers: [pointer]},{
|
||||||
fields: {pointers: {type: 'Array'}}
|
fields: {pointers: {type: 'Array'}}
|
||||||
});
|
});
|
||||||
jequal([pointer], out.pointers);
|
jequal([pointer], out.pointers);
|
||||||
@@ -59,14 +44,14 @@ describe('parseObjectToMongoObjectForCreate', () => {
|
|||||||
//have __op delete in a new object. Figure out what this should actually be testing.
|
//have __op delete in a new object. Figure out what this should actually be testing.
|
||||||
notWorking('a delete op', (done) => {
|
notWorking('a delete op', (done) => {
|
||||||
var input = {deleteMe: {__op: 'Delete'}};
|
var input = {deleteMe: {__op: 'Delete'}};
|
||||||
var output = transform.parseObjectToMongoObjectForCreate(dummySchema, null, input, { fields: {} });
|
var output = transform.parseObjectToMongoObjectForCreate(null, input, { fields: {} });
|
||||||
jequal(output, {});
|
jequal(output, {});
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('basic ACL', (done) => {
|
it('basic ACL', (done) => {
|
||||||
var input = {ACL: {'0123': {'read': true, 'write': true}}};
|
var input = {ACL: {'0123': {'read': true, 'write': true}}};
|
||||||
var output = transform.parseObjectToMongoObjectForCreate(dummySchema, null, input, { fields: {} });
|
var output = transform.parseObjectToMongoObjectForCreate(null, input, { fields: {} });
|
||||||
// This just checks that it doesn't crash, but it should check format.
|
// This just checks that it doesn't crash, but it should check format.
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
@@ -74,7 +59,7 @@ describe('parseObjectToMongoObjectForCreate', () => {
|
|||||||
describe('GeoPoints', () => {
|
describe('GeoPoints', () => {
|
||||||
it('plain', (done) => {
|
it('plain', (done) => {
|
||||||
var geoPoint = {__type: 'GeoPoint', longitude: 180, latitude: -180};
|
var geoPoint = {__type: 'GeoPoint', longitude: 180, latitude: -180};
|
||||||
var out = transform.parseObjectToMongoObjectForCreate(dummySchema, null, {location: geoPoint},{
|
var out = transform.parseObjectToMongoObjectForCreate(null, {location: geoPoint},{
|
||||||
fields: {location: {type: 'GeoPoint'}}
|
fields: {location: {type: 'GeoPoint'}}
|
||||||
});
|
});
|
||||||
expect(out.location).toEqual([180, -180]);
|
expect(out.location).toEqual([180, -180]);
|
||||||
@@ -83,7 +68,7 @@ describe('parseObjectToMongoObjectForCreate', () => {
|
|||||||
|
|
||||||
it('in array', (done) => {
|
it('in array', (done) => {
|
||||||
var geoPoint = {__type: 'GeoPoint', longitude: 180, latitude: -180};
|
var geoPoint = {__type: 'GeoPoint', longitude: 180, latitude: -180};
|
||||||
var out = transform.parseObjectToMongoObjectForCreate(dummySchema, null, {locations: [geoPoint, geoPoint]},{
|
var out = transform.parseObjectToMongoObjectForCreate(null, {locations: [geoPoint, geoPoint]},{
|
||||||
fields: {locations: {type: 'Array'}}
|
fields: {locations: {type: 'Array'}}
|
||||||
});
|
});
|
||||||
expect(out.locations).toEqual([geoPoint, geoPoint]);
|
expect(out.locations).toEqual([geoPoint, geoPoint]);
|
||||||
@@ -92,7 +77,7 @@ describe('parseObjectToMongoObjectForCreate', () => {
|
|||||||
|
|
||||||
it('in sub-object', (done) => {
|
it('in sub-object', (done) => {
|
||||||
var geoPoint = {__type: 'GeoPoint', longitude: 180, latitude: -180};
|
var geoPoint = {__type: 'GeoPoint', longitude: 180, latitude: -180};
|
||||||
var out = transform.parseObjectToMongoObjectForCreate(dummySchema, null, { locations: { start: geoPoint }},{
|
var out = transform.parseObjectToMongoObjectForCreate(null, { locations: { start: geoPoint }},{
|
||||||
fields: {locations: {type: 'Object'}}
|
fields: {locations: {type: 'Object'}}
|
||||||
});
|
});
|
||||||
expect(out).toEqual({ locations: { start: geoPoint } });
|
expect(out).toEqual({ locations: { start: geoPoint } });
|
||||||
@@ -206,7 +191,7 @@ describe('transform schema key changes', () => {
|
|||||||
var input = {
|
var input = {
|
||||||
somePointer: {__type: 'Pointer', className: 'Micro', objectId: 'oft'}
|
somePointer: {__type: 'Pointer', className: 'Micro', objectId: 'oft'}
|
||||||
};
|
};
|
||||||
var output = transform.parseObjectToMongoObjectForCreate(dummySchema, null, input, {
|
var output = transform.parseObjectToMongoObjectForCreate(null, input, {
|
||||||
fields: {somePointer: {type: 'Pointer'}}
|
fields: {somePointer: {type: 'Pointer'}}
|
||||||
});
|
});
|
||||||
expect(typeof output._p_somePointer).toEqual('string');
|
expect(typeof output._p_somePointer).toEqual('string');
|
||||||
@@ -218,7 +203,7 @@ describe('transform schema key changes', () => {
|
|||||||
var input = {
|
var input = {
|
||||||
userPointer: {__type: 'Pointer', className: '_User', objectId: 'qwerty'}
|
userPointer: {__type: 'Pointer', className: '_User', objectId: 'qwerty'}
|
||||||
};
|
};
|
||||||
var output = transform.parseObjectToMongoObjectForCreate(dummySchema, null, input, {
|
var output = transform.parseObjectToMongoObjectForCreate(null, input, {
|
||||||
fields: {userPointer: {type: 'Pointer'}}
|
fields: {userPointer: {type: 'Pointer'}}
|
||||||
});
|
});
|
||||||
expect(typeof output._p_userPointer).toEqual('string');
|
expect(typeof output._p_userPointer).toEqual('string');
|
||||||
@@ -233,7 +218,7 @@ describe('transform schema key changes', () => {
|
|||||||
"Kevin": { "write": true }
|
"Kevin": { "write": true }
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
var output = transform.parseObjectToMongoObjectForCreate(dummySchema, null, input, { fields: {} });
|
var output = transform.parseObjectToMongoObjectForCreate(null, input, { fields: {} });
|
||||||
expect(typeof output._rperm).toEqual('object');
|
expect(typeof output._rperm).toEqual('object');
|
||||||
expect(typeof output._wperm).toEqual('object');
|
expect(typeof output._wperm).toEqual('object');
|
||||||
expect(output.ACL).toBeUndefined();
|
expect(output.ACL).toBeUndefined();
|
||||||
@@ -250,7 +235,7 @@ describe('transform schema key changes', () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var output = transform.parseObjectToMongoObjectForCreate(dummySchema, null, input, { fields: {} });
|
var output = transform.parseObjectToMongoObjectForCreate(null, input, { fields: {} });
|
||||||
expect(typeof output._acl).toEqual('object');
|
expect(typeof output._acl).toEqual('object');
|
||||||
expect(output._acl["Kevin"].w).toBeTruthy();
|
expect(output._acl["Kevin"].w).toBeTruthy();
|
||||||
expect(output._acl["Kevin"].r).toBeUndefined();
|
expect(output._acl["Kevin"].r).toBeUndefined();
|
||||||
|
|||||||
@@ -1,8 +1,17 @@
|
|||||||
import MongoCollection from './MongoCollection';
|
import MongoCollection from './MongoCollection';
|
||||||
import MongoSchemaCollection from './MongoSchemaCollection';
|
import MongoSchemaCollection from './MongoSchemaCollection';
|
||||||
import {parse as parseUrl, format as formatUrl} from '../../../vendor/mongodbUrl';
|
import {
|
||||||
import * as transform from './MongoTransform';
|
parse as parseUrl,
|
||||||
import _ from 'lodash';
|
format as formatUrl,
|
||||||
|
} from '../../../vendor/mongodbUrl';
|
||||||
|
import {
|
||||||
|
parseObjectToMongoObjectForCreate,
|
||||||
|
mongoObjectToParseObject,
|
||||||
|
transformKey,
|
||||||
|
transformWhere,
|
||||||
|
transformUpdate,
|
||||||
|
} from './MongoTransform';
|
||||||
|
import _ from 'lodash';
|
||||||
|
|
||||||
let mongodb = require('mongodb');
|
let mongodb = require('mongodb');
|
||||||
let MongoClient = mongodb.MongoClient;
|
let MongoClient = mongodb.MongoClient;
|
||||||
@@ -159,12 +168,11 @@ export class MongoStorageAdapter {
|
|||||||
.then(schemasCollection => schemasCollection._fechOneSchemaFrom_SCHEMA(className));
|
.then(schemasCollection => schemasCollection._fechOneSchemaFrom_SCHEMA(className));
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: As yet not particularly well specified. Creates an object. Shouldn't need the
|
// TODO: As yet not particularly well specified. Creates an object. Maybe shouldn't even need the schema,
|
||||||
// schemaController, but MongoTransform still needs it :( maybe shouldn't even need the schema,
|
|
||||||
// and should infer from the type. Or maybe does need the schema for validations. Or maybe needs
|
// 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.
|
// the schem only for the legacy mongo format. We'll figure that out later.
|
||||||
createObject(className, object, schemaController, parseFormatSchema) {
|
createObject(className, object, schema) {
|
||||||
const mongoObject = transform.parseObjectToMongoObjectForCreate(schemaController, className, object, parseFormatSchema);
|
const mongoObject = parseObjectToMongoObjectForCreate(className, object, schema);
|
||||||
return this.adaptiveCollection(className)
|
return this.adaptiveCollection(className)
|
||||||
.then(collection => collection.insertOne(mongoObject))
|
.then(collection => collection.insertOne(mongoObject))
|
||||||
.catch(error => {
|
.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 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.
|
// 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) {
|
deleteObjectsByQuery(className, query, schema) {
|
||||||
return this.adaptiveCollection(className)
|
return this.adaptiveCollection(className)
|
||||||
.then(collection => {
|
.then(collection => {
|
||||||
let mongoWhere = transform.transformWhere(className, query, schema);
|
let mongoWhere = transformWhere(className, query, schema);
|
||||||
return collection.deleteMany(mongoWhere)
|
return collection.deleteMany(mongoWhere)
|
||||||
})
|
})
|
||||||
.then(({ result }) => {
|
.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 }.
|
// Executes a find. Accepts: className, query in Parse format, and { skip, limit, sort }.
|
||||||
find(className, query, schema, { skip, limit, sort }) {
|
find(className, query, schema, { skip, limit, sort }) {
|
||||||
let mongoWhere = this.transform.transformWhere(className, query, schema);
|
let mongoWhere = transformWhere(className, query, schema);
|
||||||
let mongoSort = _.mapKeys(sort, (value, fieldName) => transform.transformKey(className, fieldName, schema));
|
let mongoSort = _.mapKeys(sort, (value, fieldName) => transformKey(className, fieldName, schema));
|
||||||
return this.adaptiveCollection(className)
|
return this.adaptiveCollection(className)
|
||||||
.then(collection => collection.find(mongoWhere, { skip, limit, sort: mongoSort }))
|
.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.
|
// Executs a count.
|
||||||
count(className, query, schema) {
|
count(className, query, schema) {
|
||||||
return this.adaptiveCollection(className)
|
return this.adaptiveCollection(className)
|
||||||
.then(collection => collection.count(transform.transformWhere(className, query, schema)));
|
.then(collection => collection.count(transformWhere(className, query, schema)));
|
||||||
}
|
|
||||||
|
|
||||||
get transform() {
|
|
||||||
return transform;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ const transformKey = (className, fieldName, schema) => {
|
|||||||
return fieldName;
|
return fieldName;
|
||||||
}
|
}
|
||||||
|
|
||||||
const transformKeyValueForUpdate = (schema, className, restKey, restValue) => {
|
const transformKeyValueForUpdate = (className, restKey, restValue, parseFormatSchema) => {
|
||||||
// Check if the schema is known since it's a built-in field.
|
// Check if the schema is known since it's a built-in field.
|
||||||
var key = restKey;
|
var key = restKey;
|
||||||
var timeField = false;
|
var timeField = false;
|
||||||
@@ -38,12 +38,6 @@ const transformKeyValueForUpdate = (schema, className, restKey, restValue) => {
|
|||||||
key = '_updated_at';
|
key = '_updated_at';
|
||||||
timeField = true;
|
timeField = true;
|
||||||
break;
|
break;
|
||||||
case '_email_verify_token':
|
|
||||||
key = "_email_verify_token";
|
|
||||||
break;
|
|
||||||
case '_perishable_token':
|
|
||||||
key = "_perishable_token";
|
|
||||||
break;
|
|
||||||
case 'sessionToken':
|
case 'sessionToken':
|
||||||
case '_session_token':
|
case '_session_token':
|
||||||
key = '_session_token';
|
key = '_session_token';
|
||||||
@@ -57,26 +51,9 @@ const transformKeyValueForUpdate = (schema, className, restKey, restValue) => {
|
|||||||
case '_wperm':
|
case '_wperm':
|
||||||
return {key: key, value: restValue};
|
return {key: key, value: restValue};
|
||||||
break;
|
break;
|
||||||
case '$or':
|
|
||||||
throw new Parse.Error(Parse.Error.INVALID_KEY_NAME, 'you can only use $or in queries');
|
|
||||||
case '$and':
|
|
||||||
throw new Parse.Error(Parse.Error.INVALID_KEY_NAME, 'you can only use $and in queries');
|
|
||||||
default:
|
|
||||||
// Other auth data
|
|
||||||
var authDataMatch = key.match(/^authData\.([a-zA-Z0-9_]+)\.id$/);
|
|
||||||
if (authDataMatch) {
|
|
||||||
throw new Parse.Error(Parse.Error.INVALID_KEY_NAME, 'can only query on ' + key);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle special schema key changes
|
if ((parseFormatSchema.fields[key] && parseFormatSchema.fields[key].type === 'Pointer') || (!parseFormatSchema.fields[key] && restValue && restValue.__type == 'Pointer')) {
|
||||||
// TODO: it seems like this is likely to have edge cases where
|
|
||||||
// pointer types are missed
|
|
||||||
var expected = undefined;
|
|
||||||
if (schema && schema.getExpectedType) {
|
|
||||||
expected = schema.getExpectedType(className, key);
|
|
||||||
}
|
|
||||||
if ((expected && expected.type == 'Pointer') || (!expected && restValue && restValue.__type == 'Pointer')) {
|
|
||||||
key = '_p_' + key;
|
key = '_p_' + key;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,9 +78,6 @@ const transformKeyValueForUpdate = (schema, className, restKey, restValue) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Handle normal objects by recursing
|
// Handle normal objects by recursing
|
||||||
if (Object.keys(restValue).some(key => key.includes('$') || key.includes('.'))) {
|
|
||||||
throw new Parse.Error(Parse.Error.INVALID_NESTED_KEY, "Nested keys should not contain the '$' or '.' characters");
|
|
||||||
}
|
|
||||||
value = _.mapValues(restValue, transformInteriorValue);
|
value = _.mapValues(restValue, transformInteriorValue);
|
||||||
return {key, value};
|
return {key, value};
|
||||||
}
|
}
|
||||||
@@ -223,13 +197,7 @@ function transformWhere(className, restWhere, schema) {
|
|||||||
return mongoWhere;
|
return mongoWhere;
|
||||||
}
|
}
|
||||||
|
|
||||||
const parseObjectKeyValueToMongoObjectKeyValue = (
|
const parseObjectKeyValueToMongoObjectKeyValue = (className, restKey, restValue, schema) => {
|
||||||
schema,
|
|
||||||
className,
|
|
||||||
restKey,
|
|
||||||
restValue,
|
|
||||||
parseFormatSchema
|
|
||||||
) => {
|
|
||||||
// Check if the schema is known since it's a built-in field.
|
// Check if the schema is known since it's a built-in field.
|
||||||
let transformedValue;
|
let transformedValue;
|
||||||
let coercedToDate;
|
let coercedToDate;
|
||||||
@@ -267,7 +235,7 @@ const parseObjectKeyValueToMongoObjectKeyValue = (
|
|||||||
if (restValue && restValue.__type !== 'Bytes') {
|
if (restValue && restValue.__type !== 'Bytes') {
|
||||||
//Note: We may not know the type of a field here, as the user could be saving (null) to a field
|
//Note: We may not know the type of a field here, as the user could be saving (null) to a field
|
||||||
//That never existed before, meaning we can't infer the type.
|
//That never existed before, meaning we can't infer the type.
|
||||||
if (parseFormatSchema.fields[restKey] && parseFormatSchema.fields[restKey].type == 'Pointer' || restValue.__type == 'Pointer') {
|
if (schema.fields[restKey] && schema.fields[restKey].type == 'Pointer' || restValue.__type == 'Pointer') {
|
||||||
restKey = '_p_' + restKey;
|
restKey = '_p_' + restKey;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -305,18 +273,17 @@ const parseObjectKeyValueToMongoObjectKeyValue = (
|
|||||||
|
|
||||||
// Main exposed method to create new objects.
|
// Main exposed method to create new objects.
|
||||||
// restCreate is the "create" clause in REST API form.
|
// restCreate is the "create" clause in REST API form.
|
||||||
function parseObjectToMongoObjectForCreate(schema, className, restCreate, parseFormatSchema) {
|
function parseObjectToMongoObjectForCreate(className, restCreate, schema) {
|
||||||
if (className == '_User') {
|
if (className == '_User') {
|
||||||
restCreate = transformAuthData(restCreate);
|
restCreate = transformAuthData(restCreate);
|
||||||
}
|
}
|
||||||
var mongoCreate = transformACL(restCreate);
|
var mongoCreate = transformACL(restCreate);
|
||||||
for (let restKey in restCreate) {
|
for (let restKey in restCreate) {
|
||||||
let { key, value } = parseObjectKeyValueToMongoObjectKeyValue(
|
let { key, value } = parseObjectKeyValueToMongoObjectKeyValue(
|
||||||
schema,
|
|
||||||
className,
|
className,
|
||||||
restKey,
|
restKey,
|
||||||
restCreate[restKey],
|
restCreate[restKey],
|
||||||
parseFormatSchema
|
schema
|
||||||
);
|
);
|
||||||
if (value !== undefined) {
|
if (value !== undefined) {
|
||||||
mongoCreate[key] = value;
|
mongoCreate[key] = value;
|
||||||
@@ -326,10 +293,7 @@ function parseObjectToMongoObjectForCreate(schema, className, restCreate, parseF
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Main exposed method to help update old objects.
|
// Main exposed method to help update old objects.
|
||||||
function transformUpdate(schema, className, restUpdate) {
|
const transformUpdate = (className, restUpdate, parseFormatSchema) => {
|
||||||
if (!restUpdate) {
|
|
||||||
throw 'got empty restUpdate';
|
|
||||||
}
|
|
||||||
if (className == '_User') {
|
if (className == '_User') {
|
||||||
restUpdate = transformAuthData(restUpdate);
|
restUpdate = transformAuthData(restUpdate);
|
||||||
}
|
}
|
||||||
@@ -348,9 +312,8 @@ function transformUpdate(schema, className, restUpdate) {
|
|||||||
mongoUpdate['$set']['_acl'] = acl._acl;
|
mongoUpdate['$set']['_acl'] = acl._acl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (var restKey in restUpdate) {
|
for (var restKey in restUpdate) {
|
||||||
var out = transformKeyValueForUpdate(schema, className, restKey, restUpdate[restKey]);
|
var out = transformKeyValueForUpdate(className, restKey, restUpdate[restKey], parseFormatSchema);
|
||||||
|
|
||||||
// If the output value is an object with any $ keys, it's an
|
// If the output value is an object with any $ keys, it's an
|
||||||
// operator that needs to be lifted onto the top level update
|
// operator that needs to be lifted onto the top level update
|
||||||
|
|||||||
@@ -62,12 +62,6 @@ function DatabaseController(adapter, { skipValidation } = {}) {
|
|||||||
this.schemaPromise = null;
|
this.schemaPromise = null;
|
||||||
this.skipValidation = !!skipValidation;
|
this.skipValidation = !!skipValidation;
|
||||||
this.connect();
|
this.connect();
|
||||||
|
|
||||||
Object.defineProperty(this, 'transform', {
|
|
||||||
get: function() {
|
|
||||||
return adapter.transform;
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
DatabaseController.prototype.WithoutValidation = function() {
|
DatabaseController.prototype.WithoutValidation = function() {
|
||||||
@@ -171,6 +165,7 @@ const filterSensitiveData = (isMaster, aclGroup, className, object) => {
|
|||||||
// acl: a list of strings. If the object to be updated has an ACL,
|
// acl: a list of strings. If the object to be updated has an ACL,
|
||||||
// one of the provided strings must provide the caller with
|
// one of the provided strings must provide the caller with
|
||||||
// write permissions.
|
// write permissions.
|
||||||
|
const specialKeysForUpdate = ['_hashed_password', '_perishable_token', '_email_verify_token'];
|
||||||
DatabaseController.prototype.update = function(className, query, update, {
|
DatabaseController.prototype.update = function(className, query, update, {
|
||||||
acl,
|
acl,
|
||||||
many,
|
many,
|
||||||
@@ -188,8 +183,7 @@ DatabaseController.prototype.update = function(className, query, update, {
|
|||||||
.then(schemaController => {
|
.then(schemaController => {
|
||||||
return (isMaster ? Promise.resolve() : schemaController.validatePermission(className, aclGroup, 'update'))
|
return (isMaster ? Promise.resolve() : schemaController.validatePermission(className, aclGroup, 'update'))
|
||||||
.then(() => this.handleRelationUpdates(className, query.objectId, update))
|
.then(() => this.handleRelationUpdates(className, query.objectId, update))
|
||||||
.then(() => this.adapter.adaptiveCollection(className))
|
.then(() => {
|
||||||
.then(collection => {
|
|
||||||
if (!isMaster) {
|
if (!isMaster) {
|
||||||
query = this.addPointerPermissions(schemaController, className, 'update', query, aclGroup);
|
query = this.addPointerPermissions(schemaController, className, 'update', query, aclGroup);
|
||||||
}
|
}
|
||||||
@@ -209,20 +203,27 @@ DatabaseController.prototype.update = function(className, query, update, {
|
|||||||
}
|
}
|
||||||
throw error;
|
throw error;
|
||||||
})
|
})
|
||||||
.then(parseFormatSchema => {
|
.then(schema => {
|
||||||
var mongoWhere = this.transform.transformWhere(className, query, parseFormatSchema);
|
Object.keys(update).forEach(fieldName => {
|
||||||
mongoUpdate = this.transform.transformUpdate(
|
if (fieldName.match(/^authData\.([a-zA-Z0-9_]+)\.id$/)) {
|
||||||
schemaController,
|
throw new Parse.Error(Parse.Error.INVALID_KEY_NAME, `Invalid field name for update: ${fieldName}`);
|
||||||
className,
|
}
|
||||||
update,
|
fieldName = fieldName.split('.')[0];
|
||||||
{validate: !this.skipValidation}
|
if (!SchemaController.fieldNameIsValid(fieldName) && !specialKeysForUpdate.includes(fieldName)) {
|
||||||
);
|
throw new Parse.Error(Parse.Error.INVALID_KEY_NAME, `Invalid field name for update: ${fieldName}`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
for (let updateOperation in update) {
|
||||||
|
if (Object.keys(updateOperation).some(innerKey => innerKey.includes('$') || innerKey.includes('.'))) {
|
||||||
|
throw new Parse.Error(Parse.Error.INVALID_NESTED_KEY, "Nested keys should not contain the '$' or '.' characters");
|
||||||
|
}
|
||||||
|
}
|
||||||
if (many) {
|
if (many) {
|
||||||
return collection.updateMany(mongoWhere, mongoUpdate);
|
return this.adapter.updateObjectsByQuery(className, query, schema, update);
|
||||||
} else if (upsert) {
|
} else if (upsert) {
|
||||||
return collection.upsertOne(mongoWhere, mongoUpdate);
|
return this.adapter.upsertOneObject(className, query, schema, update);
|
||||||
} else {
|
} else {
|
||||||
return collection.findOneAndUpdate(mongoWhere, mongoUpdate);
|
return this.adapter.findOneAndUpdate(className, query, schema, update);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
@@ -393,7 +394,7 @@ DatabaseController.prototype.create = function(className, object, { acl } = {})
|
|||||||
.then(() => this.handleRelationUpdates(className, null, object))
|
.then(() => this.handleRelationUpdates(className, null, object))
|
||||||
.then(() => schemaController.enforceClassExists(className))
|
.then(() => schemaController.enforceClassExists(className))
|
||||||
.then(() => schemaController.getOneSchema(className, true))
|
.then(() => schemaController.getOneSchema(className, true))
|
||||||
.then(schema => this.adapter.createObject(className, object, schemaController, schema))
|
.then(schema => this.adapter.createObject(className, object, schema))
|
||||||
.then(result => sanitizeDatabaseResult(originalObject, result.ops[0]));
|
.then(result => sanitizeDatabaseResult(originalObject, result.ops[0]));
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -24,9 +24,7 @@ export class GlobalConfigRouter extends PromiseRouter {
|
|||||||
return acc;
|
return acc;
|
||||||
}, {});
|
}, {});
|
||||||
let database = req.config.database.WithoutValidation();
|
let database = req.config.database.WithoutValidation();
|
||||||
return database.update('_GlobalConfig', {objectId: 1}, update, {upsert: true}).then(() => {
|
return database.update('_GlobalConfig', {objectId: 1}, update, {upsert: true}).then(() => ({ response: { result: true } }));
|
||||||
return Promise.resolve({ response: { result: true } });
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mountRoutes() {
|
mountRoutes() {
|
||||||
|
|||||||
Reference in New Issue
Block a user