7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial

Next lesson playing in 5 seconds

  • Overview
  • Transcript

5.1 Hooking Into the Bootstrap Process

In this lesson we can see how to hook into the Sails bootstrap process in order to migrate the event data store from sails-disk to MongoDB.

5.1 Hooking Into the Bootstrap Process

Hi folks. In this lesson we're gonna see how we can hook into the Sails bootstrap process. At the moment all of our event data is still stored in the SailsDisk DB. We've got MongoDB for our user model, so really we want to move all of our event data to Mongo as well. There isn't any official tool or method to use to transfer all of our event data from SailsDisk to Mongo, but we can Sails Bootstrap process to make Sails do this for us automatically by using the bootstrap to perform a one time copy of the data. So the first thing we need to do is create a new connection for MongoDB in config/connections. And we'll call this one eventMongoDBserver... And we'll up date the database to use the events database instead. So we do have an event model already which sales created for us when we created the event API back near the start of the course. So let's temporarily create a new file in the model's folder and we'll call it eventnew.js. Let's just save this one as Event new. So we need to tell our new model to use the new event Mongo DB server connection and we also want to restrict data not specified by attributes to be ignored. >> We can also specify the Mongo DB connection using the table name configuration property. If we don't do that, then by default Sails will store all of our events in a MongoDB collection called Event New. So now let's add the attributes for the event model. So we have the simple attributes browser OS and type, and we just make that sure these are all strings, and we specify that they're all actually required. So now we can make use of the bootstrap to copy the data from the original event model, which is saved in SailsDisk, to the event new model which is going to store all it's data in Mongo. The bootstrap is a configuration file in the config directory. And let's open it up to take a quick look. So we can see that there's a very small amount of code in here already. There's a module called bootstrap, and that will get past a callback function, and at the end of the bootstrap method, the callback gets invoked. And there's a comment here to say that it's very important that this callback gets triggered. Because otherwise, Sails will never lift. Because it will still wait for the bootstrap. So we just need to make sure that we don't delete that, and any code that we want to add should be added above that. So let's read the shortcut Read route to read through the existing data from SailsDisk. Then let's use the shortcut Create route of our new event model to insert the data in Mongo. So we're gonna use the find method of the existing event model. So that's the SailsDisk one. And now we can use the create method of the event new model. So when we invoke the find method, the callback function that we passed through exec will have all of the events' pastware. So we actually want to pass this to the create method of the event new model so that the events will be created. In the call back for the exec method on the create method, that will actually pass back confirmation of the records that have been created, so let's just log that to the console. So, even though we're using different databases here, SailsDisk and Mongo, we don't need to worry about whether we're using the correct syntax for each type of database. We just use the waterline methods, find and create, and waterline will take care of it for us. So now, let's try lifting our app at this point, And what we're seeing here is confirmation that all of our events were copied into Mongo. So I mentioned that this was a one time only operation. So before we forget, let's go back to bootstrap and just comment out all of this code. Fantastic. We just need to make sure we leave the callback to be executed. And we can then delete the Event or JS file from the Models folder. So this is the old, empty one that we created near the start of the course. Let's get rid of that entirely. And let's rename the EventNew to just be Event. And as we aren't using SailsDisk at all anymore, we can switch it off by commenting out the local disk DB section in config/connections And we can even remove the localdiskdb.db file from the .temp directory. So now that we have our production data safely stored in Mongo, we can update the migrate option in config/models from alter to safe. So in this lesson, we saw how to hook into the Sails bootstrap process by adding code inside the method exposed by config/bootstrap. We used this feature to migrate the event data that we stored earlier in the course from the default local disk DB to Mongo. Thanks for watching.

Back to the top