Migrate Schema.js to adaptive mongo collection.

This commit is contained in:
Nikita Lutsenko
2016-03-07 20:56:26 -08:00
parent a163327ac9
commit fb5b8fb58f
3 changed files with 25 additions and 32 deletions

View File

@@ -54,7 +54,7 @@ export default class MongoCollection {
return this._mongoCollection.findAndModify(query, [], update, { new: true }).then(document => {
// Value is the object where mongo returns multiple fields.
return document.value;
})
});
}
insertOne(object) {
@@ -68,6 +68,10 @@ export default class MongoCollection {
return this._mongoCollection.update(query, update, { upsert: true });
}
updateOne(query, update) {
return this._mongoCollection.updateOne(query, update);
}
updateMany(query, update) {
return this._mongoCollection.updateMany(query, update);
}

View File

@@ -28,16 +28,6 @@ DatabaseController.prototype.connect = function() {
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) {
return this.adapter.adaptiveCollection(this.collectionPrefix + className);
};
@@ -68,9 +58,9 @@ DatabaseController.prototype.validateClassName = function(className) {
DatabaseController.prototype.loadSchema = function(acceptor = returnsTrue) {
if (!this.schemaPromise) {
this.schemaPromise = this.collection('_SCHEMA').then((coll) => {
this.schemaPromise = this.adaptiveCollection('_SCHEMA').then(collection => {
delete this.schemaPromise;
return Schema.load(coll);
return Schema.load(collection);
});
return this.schemaPromise;
}
@@ -79,9 +69,9 @@ DatabaseController.prototype.loadSchema = function(acceptor = returnsTrue) {
if (acceptor(schema)) {
return schema;
}
this.schemaPromise = this.collection('_SCHEMA').then((coll) => {
this.schemaPromise = this.adaptiveCollection('_SCHEMA').then(collection => {
delete this.schemaPromise;
return Schema.load(coll);
return Schema.load(collection);
});
return this.schemaPromise;
});

View File

@@ -168,12 +168,12 @@ function schemaAPITypeToMongoFieldType(type) {
// '_metadata' is ignored for now
// Everything else is expected to be a userspace field.
class Schema {
collection;
_collection;
data;
perms;
constructor(collection) {
this.collection = collection;
this._collection = collection;
// this.data[className][fieldName] tells you the type of that field
this.data = {};
@@ -184,8 +184,8 @@ class Schema {
reloadData() {
this.data = {};
this.perms = {};
return this.collection.find({}, {}).toArray().then(mongoSchema => {
for (let obj of mongoSchema) {
return this._collection.find({}).then(results => {
for (let obj of results) {
let className = null;
let classData = {};
let permsData = null;
@@ -231,7 +231,7 @@ class Schema {
return Promise.reject(mongoObject);
}
return this.collection.insertOne(mongoObject.result)
return this._collection.insertOne(mongoObject.result)
.then(result => result.ops[0])
.catch(error => {
if (error.code === 11000) { //Mongo's duplicate key error
@@ -268,7 +268,7 @@ class Schema {
'schema is frozen, cannot add: ' + className);
}
// 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
return this.reloadData();
}, () => {
@@ -280,10 +280,9 @@ class Schema {
}).then(() => {
// Ensure that the schema now validates
return this.validateClassName(className, true);
}, (error) => {
}, () => {
// The schema still doesn't validate. Give up
throw new Parse.Error(Parse.Error.INVALID_JSON,
'schema class name does not revalidate');
throw new Parse.Error(Parse.Error.INVALID_JSON, 'schema class name does not revalidate');
});
}
@@ -296,7 +295,7 @@ class Schema {
}
};
update = {'$set': update};
return this.collection.findAndModify(query, {}, update, {}).then(() => {
return this._collection.updateOne(query, update).then(() => {
// The update succeeded. Reload the schema
return this.reloadData();
});
@@ -354,12 +353,12 @@ class Schema {
// We don't have this field. Update the schema.
// Note that we use the $exists guard and $set to avoid race
// conditions in the database. This is important!
var query = {_id: className};
query[key] = {'$exists': false};
var query = { _id: className };
query[key] = { '$exists': false };
var update = {};
update[key] = type;
update = {'$set': update};
return this.collection.findAndModify(query, {}, update, {}).then(() => {
return this._collection.upsertOne(query, update).then(() => {
// The update succeeded. Reload the schema
return this.reloadData();
}, () => {
@@ -422,14 +421,14 @@ class Schema {
// for non-relations, remove all the data.
// 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 => {
var mongoFieldName = this.data[className][fieldName].startsWith('*') ? '_p_' + fieldName : fieldName;
return collection.update({}, { "$unset": { [mongoFieldName] : null } }, { multi: true });
let mongoFieldName = this.data[className][fieldName].startsWith('*') ? `_p_${fieldName}` : fieldName;
return collection.updateMany({}, { "$unset": { [mongoFieldName]: null } });
});
})
// Save the _SCHEMA object
.then(() => this.collection.update({ _id: className }, { $unset: {[fieldName]: null }}));
.then(() => this._collection.updateOne({ _id: className }, { $unset: { [fieldName]: null } }));
});
}