I have finally got the guts to do this and here are the steps I took:
- Back up the database
- Kill the server
- Edit model-config.json
- Edit the model definition JSON and model JS (old-model.json &
old-model.js). Be sure to rename them to new-model.json, etc
- Edit all relations/related models
- Edit all boot scripts/operation hooks/etc
Now that the Loopback code has been updated, the Mongo DB must be updated accordingly:
mongo>
//Make sure the new collection we are migrating to is empty
db.NewCollection.drop();
//Copy all entries from OldCollection to NewCollection
db.OldCollection.find().forEach( function(x){db.NewCollection.insert(x)} );
//Update relations, for hasOne related models
db.RelatedCollection.update( {}, { $rename: { 'oldCollectionId': 'newCollectionId'} }, { multi: true} );
//Update relations, for hasAndBelongsToMany related models
db.NewCollectionRelatedCollection.drop();
db.OldCollectionRelatedCollection.find().forEach( function(x){db.NewCollectionRelatedCollection.insert(x)} );
db.NewCollectionRelatedCollection.update( {}, { $rename: { 'oldCollectionId': 'newCollectionId'} }, { multi: true} );
//Feeling confident?
db.OldCollection.drop();
db.OldCollectionRelatedCollection.drop();
Little nervewracking, but after taking these steps and restarting the server, everything worked fine, save for a few places I forgot to change the naming. This was my first time "refactoring" something in a database and hopefully I have overlooked a much easier way to do this, but this is the best I've come up with so far.