Finish implementation of delete field from schema

This commit is contained in:
Drew Gross
2016-02-10 14:11:42 -08:00
parent b0c4b8f6ce
commit 0a0d4f65ef
2 changed files with 66 additions and 8 deletions

View File

@@ -458,20 +458,32 @@ Schema.prototype.deleteField = function(fieldName, className, database, prefix)
});
}
let p = null;
if (schema.data[className][fieldName].startsWith('relation')) {
//For relations, drop the _Join table
p = database.dropCollection(prefix + '_Join:' + fieldName + ':' + className);
return database.dropCollection(prefix + '_Join:' + fieldName + ':' + className)
//Save the _SCHEMA object
.then(() => this.collection.update({ _id: className }, { $unset: {[fieldName]: null }}));
} else {
//for non-relations, remove all the data. This is necessary to ensure that the data is still gone
//if they add the same field.
p = Promise.resolve();
return new Promise((resolve, reject) => {
database.collection(prefix + className, (err, coll) => {
if (err) {
reject(err);
} else {
return coll.update({}, {
"$unset": { [fieldName] : null },
}, {
multi: true,
})
//Save the _SCHEMA object
.then(() => this.collection.update({ _id: className }, { $unset: {[fieldName]: null }}))
.then(resolve)
.catch(reject);
}
});
});
}
return p.then(() => {
//Save the _SCHEMA object
return Promise.resolve();
});
});
});
}