Loading
Lesson 05
Courses / Quick Start Introduction to Sails
Setting Up the Data Source in Sails

Video Transcript

Welcome back to Quick Start Introduction to Sales. So from now on, we want to start building our RESTful API. So let's build an API that serves information about companies. For example, a company has a name, has a founding date, and an employee account. But before we jump into the model, let's get our database all set up. By default, Sales is going to have a default database, the so-called local disk, using the Sales disk adapter. So let's take a look at those configuration files to get more acquaintance with that database configuration. So let's go to our project. I'm going to open here my text editor. So we're going to have a look at the directory called config. So under config, we have this file called connections.js. So connections.js is where you set up your connections, that is the different types of databases. In the case of the sales, by default, it's going to set up this connection called local disk db. This is an arbitrary name that you can choose, and it's using what's called a sales adapter. So sales adapter is going to take care of configuring database and setting it up, everything for you. So all you have to provide is the database username and all that stuff. But in the case for the local disk db, there's not much configuration going on. All you have to say is which adapter you're going to use. Okay, so this sets up this connection, local disk db, that's the name of this connection. It's an arbitrary name. We can have multiple connections, for example. If you scroll down, there's this some MySQL server connection. Comment it out, then you can uncomment if you want to set up a MySQL connection. For example, in this case, if you want to set up MySQL, you would use the sales MySQL adapter. Just as a reminder, you have to install this guy using npm install dash dash save sales and MySQL. So you can see it's using the sales MySQL adapter. Then you set up the connection using the host, user, password, and the name of the database. So you can use any database you want. There's also support for MongoDB using the sales Mongo adapter. So you can set up some MongoDB server, that's the name of the connection. So you can use MongoDB. And it uses the sales Mongo adapter that you can use. You can install using npm install sales Mongo and so on. So you can have multiple connections that you can use. Okay, postgres and others. You can take a look here at the website for more information. Now, once you have that connection set up, in this case, let's just use this local disk db. We have to tell what's the default database of this application. That's in the file called models.js. So under the config directory slash models.js, you can say what's your app's default connection right here. This case is commented out. So in this case, it's going to be local disk db by default, even if it's commented out. So this is where you set up which connection you want to use by default if it's not specified. So you can choose which one you want to use by default. Also in this same file, models.js, there's the migrate option. We're going to have this... Sales is going to ask us what kind of migration strategy we want to adopt once we create the models and we lift the server. So if you keep commenting it out, it's going to complain about that unless you determine what kind of migration... migration strategy you want to use. Let's just leave it at that, but keep that in mind that you're going to have to change that later to adopt the safe choice for your production or development of different kind of environment. You can add more information if you go to their documentation. Anyway, so let's just remember that in the models.js, you can set your default connection and you can set the migration. Okay, the migrate strategy. Now, with that in mind, we can also have environment-specific configuration for what connection you want to use. In that case, go to config slash end. For example, under the development environment, you can determine what connection you want to use. In this case, if I comment this out, it's using some MongoDB server. It can be anything. You can see my SQL connection or whatever you want to say. All right. So this guy is going to say, under the development environment, please use this connection. So you can have different kinds of connections. For example, in production, you might want to use some other kind of database. You want to use MySQL or something else. So you can have different kinds of databases for different kinds of environments. Okay. So let's wrap this up and once again, review the database configuration. First, you go to config slash connections to set up your connections. You can choose a sales adapter for your corresponding database type, MySQL, sales and MySQL, sales Mongo, sales postgres, and so on. And then you just set up the connection with the username, password, database name, host board, and that kind of stuff. Okay. Once you got the connection set up, you can go to the models.js where you can specify the default connection for everything if not specified. And then you can also specify the migration. How to handle the data when the server lifts. Are you going to delete the database and start over? Are you going to keep the records and build a new thing? Are you going to alter the records, keep them as they are, and just add more data? That kind of stuff. All right. And then you can set up specific environment configuration and config slash m slash the name of your environment. In this case, there are two environments already by default, development and production. So you can say, I want to use this connection for production. Oh, no. I want to use this different auto-connection for development. Okay. With that in mind, this is it for this lecture. In the next lecture, we're going to start building our first model. See you then.
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: