Loading
Lesson 08
Courses / Quick Start Introduction to Sails
Setting Up the Migrate Strategy in Sails

Video Transcript

Hello everybody in this lecture we're going to look at the RESTful routes automatically provided by sales from the blueprint API. We're gonna use Postman. You can use any RESTful REST client that you'd like. In my case I'm gonna use Postman. So I already installed Postman and I'm gonna open up. If you're using Postman the first time you opened up you if you don't have an account you can just say skip it. I don't want to create an account at this time kind of thing if you don't want to create one. Anyway here we are I'm gonna close this and clear everything here. I don't want this and take this out. Just I hit the sidebar. Chrome apps are being deprecated as we mentioned before so we're using the Postman app. Let's go. So we have a resource called company remember. So the app is gonna be running at HTTP localhost at the part 1337. So let's make sure we have the server running. So I'm gonna go to my terminal and I already had my server running. If you want to run a server again you can again type npm start and the server should run. So because we added a model we are gonna have this warning here. So let me make this bigger so you can see. So as I mentioned before this is about the migrate strategy. We did not specify in the models.js file remember config slash models.js had a migrate configuration thing. So because we created a model now sales is asking hey how do you want me to deal with the tables in the database and all that stuff. So it has three strategies for migrate. So the first one is save. So never auto migrate my databases. I will do it myself. Basically this is the choice you want to choose for production. That is don't do anything with the database keep it as it is don't matter with it. Now have source number two alter. Auto migrate. So if you change something in schema and you know change the table schema some resource schema and if you choose this strategy it's going to try to auto migrate and keep the existing data and they see it's experimental or you can have a migrate strategy number three which is drop. So it's going to drop all the data and rebuild everything from scratch. So be careful if you use this one it's going to drop everything. All your data is going to be gone. Alright so in summary you want to use one. Save always for production. Don't change anything to database. If you are in development you can try using alter if you're always changing your stuff. Alright so let's go with alter. So we're doing development. Oh let's try again. NPM start. And press 2. Okay so I didn't choose anything and they automatically choose migrate safe. Okay so let's try again. I'll try alter. There we go. But notice that every time I rerun restart a server I have to state which migrate you want to use. So that's kind of annoying. So that's why we have to set it up in that file models.js. So let's go back to the text editor here and let's go to config slash models.js. Okay so in the models.js file all the way down here we have this migrate here. So let's uncomment this out. So we're going to say explicitly this is my great strategy I want to use. In this case it's using alter. Remember use safe for production. So because we're doing development I'm going to use alter. Alright but keep that in mind. Don't forget use safe for production. Okay I'm going to save it and next time we run sales we left the server. It's no longer going to ask us what migration you want to use because we have explicitly stated now that we want to use this migrate strategy. Alright so let's go with alter and go back to the terminal let's clear this and do npm start again. Okay no prompt good very good. So that's it we got the server running. Now we can finally start playing around with our API.
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: