This commit is contained in:
Antonio Davi Macedo Coelho de Castro
2020-04-22 09:04:07 -07:00
committed by GitHub
parent 489aeae784
commit 0dec1c80e2

View File

@@ -42,7 +42,7 @@ function mongoFieldToParseSchemaField(type) {
const nonFieldSchemaKeys = ['_id', '_metadata', '_client_permissions']; const nonFieldSchemaKeys = ['_id', '_metadata', '_client_permissions'];
function mongoSchemaFieldsToParseSchemaFields(schema) { function mongoSchemaFieldsToParseSchemaFields(schema) {
var fieldNames = Object.keys(schema).filter( var fieldNames = Object.keys(schema).filter(
key => nonFieldSchemaKeys.indexOf(key) === -1 (key) => nonFieldSchemaKeys.indexOf(key) === -1
); );
var response = fieldNames.reduce((obj, fieldName) => { var response = fieldNames.reduce((obj, fieldName) => {
obj[fieldName] = mongoFieldToParseSchemaField(schema[fieldName]); obj[fieldName] = mongoFieldToParseSchemaField(schema[fieldName]);
@@ -110,7 +110,7 @@ function mongoSchemaToParseSchema(mongoSchema) {
function _mongoSchemaQueryFromNameQuery(name: string, query) { function _mongoSchemaQueryFromNameQuery(name: string, query) {
const object = { _id: name }; const object = { _id: name };
if (query) { if (query) {
Object.keys(query).forEach(key => { Object.keys(query).forEach((key) => {
object[key] = query[key]; object[key] = query[key];
}); });
} }
@@ -158,13 +158,13 @@ class MongoSchemaCollection {
_fetchAllSchemasFrom_SCHEMA() { _fetchAllSchemasFrom_SCHEMA() {
return this._collection return this._collection
._rawFind({}) ._rawFind({})
.then(schemas => schemas.map(mongoSchemaToParseSchema)); .then((schemas) => schemas.map(mongoSchemaToParseSchema));
} }
_fetchOneSchemaFrom_SCHEMA(name: string) { _fetchOneSchemaFrom_SCHEMA(name: string) {
return this._collection return this._collection
._rawFind(_mongoSchemaQueryFromNameQuery(name), { limit: 1 }) ._rawFind(_mongoSchemaQueryFromNameQuery(name), { limit: 1 })
.then(results => { .then((results) => {
if (results.length === 1) { if (results.length === 1) {
return mongoSchemaToParseSchema(results[0]); return mongoSchemaToParseSchema(results[0]);
} else { } else {
@@ -175,17 +175,16 @@ class MongoSchemaCollection {
// Atomically find and delete an object based on query. // Atomically find and delete an object based on query.
findAndDeleteSchema(name: string) { findAndDeleteSchema(name: string) {
return this._collection._mongoCollection.findAndRemove( return this._collection._mongoCollection.findOneAndDelete(
_mongoSchemaQueryFromNameQuery(name), _mongoSchemaQueryFromNameQuery(name)
[]
); );
} }
insertSchema(schema: any) { insertSchema(schema: any) {
return this._collection return this._collection
.insertOne(schema) .insertOne(schema)
.then(result => mongoSchemaToParseSchema(result.ops[0])) .then((result) => mongoSchemaToParseSchema(result.ops[0]))
.catch(error => { .catch((error) => {
if (error.code === 11000) { if (error.code === 11000) {
//Mongo's duplicate key error //Mongo's duplicate key error
throw new Parse.Error( throw new Parse.Error(
@@ -226,7 +225,7 @@ class MongoSchemaCollection {
addFieldIfNotExists(className: string, fieldName: string, fieldType: string) { addFieldIfNotExists(className: string, fieldName: string, fieldType: string) {
return this._fetchOneSchemaFrom_SCHEMA(className) return this._fetchOneSchemaFrom_SCHEMA(className)
.then( .then(
schema => { (schema) => {
// If a field with this name already exists, it will be handled elsewhere. // If a field with this name already exists, it will be handled elsewhere.
if (schema.fields[fieldName] != undefined) { if (schema.fields[fieldName] != undefined) {
return; return;
@@ -236,7 +235,7 @@ class MongoSchemaCollection {
// Make sure there are not other geopoint fields // Make sure there are not other geopoint fields
if ( if (
Object.keys(schema.fields).some( Object.keys(schema.fields).some(
existingField => (existingField) =>
schema.fields[existingField].type === 'GeoPoint' schema.fields[existingField].type === 'GeoPoint'
) )
) { ) {
@@ -248,7 +247,7 @@ class MongoSchemaCollection {
} }
return; return;
}, },
error => { (error) => {
// If error is undefined, the schema doesn't exist, and we can create the schema with the field. // If error is undefined, the schema doesn't exist, and we can create the schema with the field.
// If some other error, reject with it. // If some other error, reject with it.
if (error === undefined) { if (error === undefined) {