1 00:00:00,680 --> 00:00:04,160 We'll be using the Sequelize ORM in our project. 2 00:00:04,160 --> 00:00:08,930 It's one of the most popular ORMs for Node.js. 3 00:00:08,930 --> 00:00:10,720 If you go to Getting Started, 4 00:00:10,720 --> 00:00:14,640 it should show you what NPM packages you need to install. 5 00:00:14,640 --> 00:00:19,670 How to set up the database connection and how to set up a model. 6 00:00:21,150 --> 00:00:25,600 Then, theres the most common gotchas that you'll encounter when using promises. 7 00:00:27,190 --> 00:00:30,760 We're going to install sequelize and sequellite three. 8 00:00:32,110 --> 00:00:35,607 Since we're using a sequellite databases in our example app. 9 00:00:56,442 --> 00:00:59,826 A lot of code in this getting started document is good to use 10 00:00:59,826 --> 00:01:03,165 as a reference when building your own models. 11 00:01:03,165 --> 00:01:06,095 But when you're building any sizable application, 12 00:01:06,095 --> 00:01:10,975 writing all of this boilerplate code by hand can get tiresome. 13 00:01:10,975 --> 00:01:12,960 And you could easily make a mistake. 14 00:01:17,015 --> 00:01:23,100 Many development tools have what you call, a CLI or command line interface. 15 00:01:23,100 --> 00:01:28,660 That is a special piece of software that is used to aid in the development process. 16 00:01:28,660 --> 00:01:30,710 It generates a lot of boilerplate or 17 00:01:30,710 --> 00:01:33,620 standard code that you'd write all the time. 18 00:01:33,620 --> 00:01:35,112 Let's get that installed. 19 00:01:48,261 --> 00:01:52,570 You can run the Sequelize CLI by typing, 20 00:01:52,570 --> 00:01:56,790 node _modules/.bin/sequelize. 21 00:01:56,790 --> 00:02:04,084 Any decenct CLI will have a help command. 22 00:02:08,273 --> 00:02:12,422 If you ever to get stuck you can always run the CLI with the help flag. 23 00:02:16,639 --> 00:02:20,193 The two commands we'll be using are init, 24 00:02:20,193 --> 00:02:25,933 which initializes the project and model create, to create a model. 25 00:02:30,701 --> 00:02:34,330 We'll use the help command to see how we use it shortly. 26 00:02:34,330 --> 00:02:39,928 First, let's initialize our project with the init command. 27 00:02:43,920 --> 00:02:46,981 This will initialize all the configuration code, 28 00:02:46,981 --> 00:02:50,262 folders and helpers that we need for our application. 29 00:02:55,566 --> 00:03:02,702 It sets up four folders, Config, Migrations, models. 30 00:03:04,867 --> 00:03:07,540 And seeders. 31 00:03:07,540 --> 00:03:10,450 Let's look first at the config folder 32 00:03:10,450 --> 00:03:13,300 where we add code to configure out database connections. 33 00:03:13,300 --> 00:03:17,220 For development testing and production environments. 34 00:03:17,220 --> 00:03:21,990 In the newly created config folder, there is a JSON config file. 35 00:03:21,990 --> 00:03:24,355 This contains three configurations for 36 00:03:24,355 --> 00:03:27,770 the three main environments you need in any application. 37 00:03:29,470 --> 00:03:32,320 Your development environment when your programming your app. 38 00:03:33,930 --> 00:03:37,590 Testing, for running automated tests to make sure your code 39 00:03:37,590 --> 00:03:39,525 interacts correctly with the database. 40 00:03:39,525 --> 00:03:46,910 And production, for the live site, filled with real data your application needs. 41 00:03:46,910 --> 00:03:51,190 This is configured with boilerplate credentials for a MySQL Database but 42 00:03:51,190 --> 00:03:54,260 we want to use other relational databases. 43 00:03:54,260 --> 00:03:56,950 In this case, we're using SQLite. 44 00:03:56,950 --> 00:04:00,100 Remember, on the getting started documentation, 45 00:04:00,100 --> 00:04:04,060 there was a section saying how to set up a database connection. 46 00:04:04,060 --> 00:04:09,160 All the options here can be specified in the config file. 47 00:04:09,160 --> 00:04:10,660 Ideally in development, 48 00:04:10,660 --> 00:04:13,930 you want to use the same database you would use in production. 49 00:04:13,930 --> 00:04:18,210 But you could use a SQLite in a development and testing environment and 50 00:04:18,210 --> 00:04:20,200 PostgreSQL for production. 51 00:04:20,200 --> 00:04:22,850 For example, you could use SQLite so 52 00:04:22,850 --> 00:04:26,430 that you don't need to install a heavier database on your laptop. 53 00:04:26,430 --> 00:04:30,400 And PostgreSQL for a powerful Linux server in production. 54 00:04:30,400 --> 00:04:36,550 See how you can specify a dialect or the specific version of SQL we're using. 55 00:04:38,080 --> 00:04:40,817 We can use that for our environments. 56 00:04:52,298 --> 00:04:54,112 Since we're using SQL light, 57 00:04:54,112 --> 00:04:58,555 a file based database that doesn't require credentials or a host. 58 00:04:58,555 --> 00:05:02,594 We can use the storage key with the files path. 59 00:05:39,730 --> 00:05:42,413 The sequelize SLI created a few directories, 60 00:05:42,413 --> 00:05:45,382 including the config directory we just looked at. 61 00:05:48,499 --> 00:05:51,369 Another important folder is the models folder, 62 00:05:51,369 --> 00:05:56,350 which holds the code that maps JavaScript objects to database tables. 63 00:05:56,350 --> 00:05:57,260 In the models folder, 64 00:05:57,260 --> 00:06:02,625 there's an index Index JS file, this file will import any models we define. 65 00:06:02,625 --> 00:06:03,605 For example, 66 00:06:03,605 --> 00:06:08,835 we'll be creating an article model to map properties of a JavaScript object. 67 00:06:08,835 --> 00:06:12,285 That we can use in our programming code to columns 68 00:06:12,285 --> 00:06:15,170 in a database table named articles. 69 00:06:15,170 --> 00:06:20,530 The index file also exposes the sequalized package whenever we import models 70 00:06:20,530 --> 00:06:22,360 into our application code. 71 00:06:22,360 --> 00:06:27,060 This means, whenever we import models, we'll have this sequalized package. 72 00:06:27,060 --> 00:06:30,177 With all of the methods and functionality to use. 73 00:06:35,339 --> 00:06:39,490 Let's generate our first model, an article. 74 00:06:39,490 --> 00:06:44,190 To see how we can use the model:create command, we can run the sequelize command. 75 00:06:49,960 --> 00:06:58,590 And then, type in the command help:model:create. 76 00:06:58,590 --> 00:07:00,740 Here's an example of how to use it. 77 00:07:00,740 --> 00:07:05,800 You can specify the model name, which matches a table name in the database. 78 00:07:05,800 --> 00:07:10,890 In this example, sequelize will look for the information in the users table. 79 00:07:10,890 --> 00:07:14,630 An important distinction, is that the model name is singular and 80 00:07:14,630 --> 00:07:18,150 that the table name is plural and lowercase. 81 00:07:18,150 --> 00:07:22,520 This is a convention sequelize uses, it's automatic and 82 00:07:22,520 --> 00:07:24,410 you don't have to worry about it. 83 00:07:24,410 --> 00:07:30,030 Then, the mode attributes our properties, which match the column names in the table. 84 00:07:30,030 --> 00:07:33,220 Each attribute has the data type after it. 85 00:07:33,220 --> 00:07:38,760 In other words, in this example, this code creates a model for the users table. 86 00:07:38,760 --> 00:07:44,200 With three columns, first name, last name, and bio. 87 00:07:44,200 --> 00:07:47,850 Let's create our article generating command using this example. 88 00:07:49,330 --> 00:07:53,805 We need to start with the sequelize command model:create. 89 00:08:00,187 --> 00:08:02,085 Then the name of our model. 90 00:08:05,506 --> 00:08:08,237 Then we can specify the attributes. 91 00:08:12,180 --> 00:08:16,821 Which is a title, which is a string. 92 00:08:20,540 --> 00:08:21,638 Author. 93 00:08:23,340 --> 00:08:24,802 Which is a string. 94 00:08:27,034 --> 00:08:32,990 And body, which is a body of text or a large string. 95 00:08:32,990 --> 00:08:36,570 When we hit return, the model code is generated and 96 00:08:36,570 --> 00:08:38,760 a new JavaScript file is created. 97 00:08:42,080 --> 00:08:45,316 Let's take a peek inside the article doc j.s file. 98 00:08:49,361 --> 00:08:57,790 You can see the article defined with the title, author, and body. 99 00:08:57,790 --> 00:09:03,120 These attributes are the column names in the table storing all articles. 100 00:09:03,120 --> 00:09:05,980 This article model is exported. 101 00:09:05,980 --> 00:09:12,544 And this is what is included within the model code in the index JS file. 102 00:09:15,619 --> 00:09:20,790 Finally, we want the database to be updated with the models we've created. 103 00:09:20,790 --> 00:09:24,520 In other words, we want the articles table to be created. 104 00:09:25,730 --> 00:09:29,920 Once we've defined the model in JavaScript, we need to sync those changes. 105 00:09:30,950 --> 00:09:34,220 We can do this automatically, every time the service starts. 106 00:09:36,250 --> 00:09:38,290 Going back to the Sequelize home page, 107 00:09:38,290 --> 00:09:41,930 we can see that there's a sync method on Sequelize object. 108 00:09:41,930 --> 00:09:47,120 The sync method will automatically create or update any database tables. 109 00:09:47,120 --> 00:09:49,940 We call this method every time the service starts. 110 00:09:51,240 --> 00:09:55,740 So an ideal place to do this, would be when the server spins up. 111 00:09:55,740 --> 00:09:58,848 If you go to the bin slash www file and 112 00:09:58,848 --> 00:10:03,829 just rename it to doc js, so we can edit it in workspaces. 113 00:10:08,163 --> 00:10:11,394 This is the file that the express site generator creates for 114 00:10:11,394 --> 00:10:12,760 express applications. 115 00:10:14,240 --> 00:10:16,820 We can look down where the service starts listening. 116 00:10:19,770 --> 00:10:24,419 We could use the sync method first and once the database of dates are complete, 117 00:10:24,419 --> 00:10:26,900 we can start listening to the web server. 118 00:10:45,778 --> 00:10:47,100 And before I forget, 119 00:10:47,100 --> 00:10:51,080 let's remember to require the sequelize at the top of the file. 120 00:11:13,239 --> 00:11:15,254 Let's rename this back. 121 00:11:24,310 --> 00:11:28,605 When we went nmp start now, you should see the development.db 122 00:11:28,605 --> 00:11:32,180 file created at the root of the application folder. 123 00:11:35,010 --> 00:11:37,360 Now that we have the database code set up, 124 00:11:37,360 --> 00:11:41,340 we can now focus on the important parts of our application. 125 00:11:41,340 --> 00:11:44,020 We currently have no data in the database. 126 00:11:44,020 --> 00:11:47,220 In the next video, we'll work on the routes for data creation.