Migrate Schema.js to adaptive mongo collection.
This commit is contained in:
@@ -54,7 +54,7 @@ export default class MongoCollection {
|
|||||||
return this._mongoCollection.findAndModify(query, [], update, { new: true }).then(document => {
|
return this._mongoCollection.findAndModify(query, [], update, { new: true }).then(document => {
|
||||||
// Value is the object where mongo returns multiple fields.
|
// Value is the object where mongo returns multiple fields.
|
||||||
return document.value;
|
return document.value;
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
insertOne(object) {
|
insertOne(object) {
|
||||||
@@ -68,6 +68,10 @@ export default class MongoCollection {
|
|||||||
return this._mongoCollection.update(query, update, { upsert: true });
|
return this._mongoCollection.update(query, update, { upsert: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
updateOne(query, update) {
|
||||||
|
return this._mongoCollection.updateOne(query, update);
|
||||||
|
}
|
||||||
|
|
||||||
updateMany(query, update) {
|
updateMany(query, update) {
|
||||||
return this._mongoCollection.updateMany(query, update);
|
return this._mongoCollection.updateMany(query, update);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,16 +28,6 @@ DatabaseController.prototype.connect = function() {
|
|||||||
return this.adapter.connect();
|
return this.adapter.connect();
|
||||||
};
|
};
|
||||||
|
|
||||||
// Returns a promise for a Mongo collection.
|
|
||||||
// Generally just for internal use.
|
|
||||||
DatabaseController.prototype.collection = function(className) {
|
|
||||||
if (!Schema.classNameIsValid(className)) {
|
|
||||||
throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME,
|
|
||||||
'invalid className: ' + className);
|
|
||||||
}
|
|
||||||
return this.adapter.collection(this.collectionPrefix + className);
|
|
||||||
};
|
|
||||||
|
|
||||||
DatabaseController.prototype.adaptiveCollection = function(className) {
|
DatabaseController.prototype.adaptiveCollection = function(className) {
|
||||||
return this.adapter.adaptiveCollection(this.collectionPrefix + className);
|
return this.adapter.adaptiveCollection(this.collectionPrefix + className);
|
||||||
};
|
};
|
||||||
@@ -68,9 +58,9 @@ DatabaseController.prototype.validateClassName = function(className) {
|
|||||||
DatabaseController.prototype.loadSchema = function(acceptor = returnsTrue) {
|
DatabaseController.prototype.loadSchema = function(acceptor = returnsTrue) {
|
||||||
|
|
||||||
if (!this.schemaPromise) {
|
if (!this.schemaPromise) {
|
||||||
this.schemaPromise = this.collection('_SCHEMA').then((coll) => {
|
this.schemaPromise = this.adaptiveCollection('_SCHEMA').then(collection => {
|
||||||
delete this.schemaPromise;
|
delete this.schemaPromise;
|
||||||
return Schema.load(coll);
|
return Schema.load(collection);
|
||||||
});
|
});
|
||||||
return this.schemaPromise;
|
return this.schemaPromise;
|
||||||
}
|
}
|
||||||
@@ -79,9 +69,9 @@ DatabaseController.prototype.loadSchema = function(acceptor = returnsTrue) {
|
|||||||
if (acceptor(schema)) {
|
if (acceptor(schema)) {
|
||||||
return schema;
|
return schema;
|
||||||
}
|
}
|
||||||
this.schemaPromise = this.collection('_SCHEMA').then((coll) => {
|
this.schemaPromise = this.adaptiveCollection('_SCHEMA').then(collection => {
|
||||||
delete this.schemaPromise;
|
delete this.schemaPromise;
|
||||||
return Schema.load(coll);
|
return Schema.load(collection);
|
||||||
});
|
});
|
||||||
return this.schemaPromise;
|
return this.schemaPromise;
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -168,12 +168,12 @@ function schemaAPITypeToMongoFieldType(type) {
|
|||||||
// '_metadata' is ignored for now
|
// '_metadata' is ignored for now
|
||||||
// Everything else is expected to be a userspace field.
|
// Everything else is expected to be a userspace field.
|
||||||
class Schema {
|
class Schema {
|
||||||
collection;
|
_collection;
|
||||||
data;
|
data;
|
||||||
perms;
|
perms;
|
||||||
|
|
||||||
constructor(collection) {
|
constructor(collection) {
|
||||||
this.collection = collection;
|
this._collection = collection;
|
||||||
|
|
||||||
// this.data[className][fieldName] tells you the type of that field
|
// this.data[className][fieldName] tells you the type of that field
|
||||||
this.data = {};
|
this.data = {};
|
||||||
@@ -184,8 +184,8 @@ class Schema {
|
|||||||
reloadData() {
|
reloadData() {
|
||||||
this.data = {};
|
this.data = {};
|
||||||
this.perms = {};
|
this.perms = {};
|
||||||
return this.collection.find({}, {}).toArray().then(mongoSchema => {
|
return this._collection.find({}).then(results => {
|
||||||
for (let obj of mongoSchema) {
|
for (let obj of results) {
|
||||||
let className = null;
|
let className = null;
|
||||||
let classData = {};
|
let classData = {};
|
||||||
let permsData = null;
|
let permsData = null;
|
||||||
@@ -231,7 +231,7 @@ class Schema {
|
|||||||
return Promise.reject(mongoObject);
|
return Promise.reject(mongoObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.collection.insertOne(mongoObject.result)
|
return this._collection.insertOne(mongoObject.result)
|
||||||
.then(result => result.ops[0])
|
.then(result => result.ops[0])
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
if (error.code === 11000) { //Mongo's duplicate key error
|
if (error.code === 11000) { //Mongo's duplicate key error
|
||||||
@@ -268,7 +268,7 @@ class Schema {
|
|||||||
'schema is frozen, cannot add: ' + className);
|
'schema is frozen, cannot add: ' + className);
|
||||||
}
|
}
|
||||||
// We don't have this class. Update the schema
|
// We don't have this class. Update the schema
|
||||||
return this.collection.insert([{_id: className}]).then(() => {
|
return this._collection.insertOne({ _id: className }).then(() => {
|
||||||
// The schema update succeeded. Reload the schema
|
// The schema update succeeded. Reload the schema
|
||||||
return this.reloadData();
|
return this.reloadData();
|
||||||
}, () => {
|
}, () => {
|
||||||
@@ -280,10 +280,9 @@ class Schema {
|
|||||||
}).then(() => {
|
}).then(() => {
|
||||||
// Ensure that the schema now validates
|
// Ensure that the schema now validates
|
||||||
return this.validateClassName(className, true);
|
return this.validateClassName(className, true);
|
||||||
}, (error) => {
|
}, () => {
|
||||||
// The schema still doesn't validate. Give up
|
// The schema still doesn't validate. Give up
|
||||||
throw new Parse.Error(Parse.Error.INVALID_JSON,
|
throw new Parse.Error(Parse.Error.INVALID_JSON, 'schema class name does not revalidate');
|
||||||
'schema class name does not revalidate');
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -296,7 +295,7 @@ class Schema {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
update = {'$set': update};
|
update = {'$set': update};
|
||||||
return this.collection.findAndModify(query, {}, update, {}).then(() => {
|
return this._collection.updateOne(query, update).then(() => {
|
||||||
// The update succeeded. Reload the schema
|
// The update succeeded. Reload the schema
|
||||||
return this.reloadData();
|
return this.reloadData();
|
||||||
});
|
});
|
||||||
@@ -354,12 +353,12 @@ class Schema {
|
|||||||
// We don't have this field. Update the schema.
|
// We don't have this field. Update the schema.
|
||||||
// Note that we use the $exists guard and $set to avoid race
|
// Note that we use the $exists guard and $set to avoid race
|
||||||
// conditions in the database. This is important!
|
// conditions in the database. This is important!
|
||||||
var query = {_id: className};
|
var query = { _id: className };
|
||||||
query[key] = {'$exists': false};
|
query[key] = { '$exists': false };
|
||||||
var update = {};
|
var update = {};
|
||||||
update[key] = type;
|
update[key] = type;
|
||||||
update = {'$set': update};
|
update = {'$set': update};
|
||||||
return this.collection.findAndModify(query, {}, update, {}).then(() => {
|
return this._collection.upsertOne(query, update).then(() => {
|
||||||
// The update succeeded. Reload the schema
|
// The update succeeded. Reload the schema
|
||||||
return this.reloadData();
|
return this.reloadData();
|
||||||
}, () => {
|
}, () => {
|
||||||
@@ -422,14 +421,14 @@ class Schema {
|
|||||||
|
|
||||||
// for non-relations, remove all the data.
|
// for non-relations, remove all the data.
|
||||||
// This is necessary to ensure that the data is still gone if they add the same field.
|
// This is necessary to ensure that the data is still gone if they add the same field.
|
||||||
return database.collection(className)
|
return database.adaptiveCollection(className)
|
||||||
.then(collection => {
|
.then(collection => {
|
||||||
var mongoFieldName = this.data[className][fieldName].startsWith('*') ? '_p_' + fieldName : fieldName;
|
let mongoFieldName = this.data[className][fieldName].startsWith('*') ? `_p_${fieldName}` : fieldName;
|
||||||
return collection.update({}, { "$unset": { [mongoFieldName] : null } }, { multi: true });
|
return collection.updateMany({}, { "$unset": { [mongoFieldName]: null } });
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
// Save the _SCHEMA object
|
// Save the _SCHEMA object
|
||||||
.then(() => this.collection.update({ _id: className }, { $unset: {[fieldName]: null }}));
|
.then(() => this._collection.updateOne({ _id: className }, { $unset: { [fieldName]: null } }));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user