
Join the Conversation!
Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.
"Please login to view comments"
Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.
By logging in, you'll unlock full access to this and other free tutorials on JSM Pro.
Why? Logging in lets us personalize your learning experience, track your progress, and keep you in the loop with new workshops, coding tips, and platform updates.
You'll also be the first to know about upcoming launches, events, and exclusive discounts.
No spam—just helpful content to level up your skills.
If that sounds fair, go ahead and log in to continue →
Enter your name and email to get instant access
##Looks like we found a thief monkey By the way, I liked the trick how you reached till here. You have a good sense of humor. You will improve a lot if you join our course with this passion.
var
(function-scoped, outdated)let
(block-scoped, modern and recommended)const
(block-scoped, cannot be reassigned)_
, or $
let let = 5;
is invalid)myVar
and myvar
are different)string
, number
, boolean
, null
, undefined
, bigint
, symbol
Objects
, Arrays
, Functions
Subscribing gives you access to a brief, insightful summary of each lecture to stay on track.
00:00:02 Now is the time to set up our database.
00:00:04 You could use something older like Postgres or maybe something modern like Neon, which is a serverless platform that allows you to host Postgres databases online.
00:00:14 Then you could hook it up with an ORM like Drizzle and it would all work.
00:00:18 But in this course, I'll use MongoDB Atlas.
00:00:21 allowing us to very easily create our database and host it on the cloud, completely for free.
00:00:27 Now, it is important to mention that up to this point, this course was completely database agnostic, which means that the concepts we learned could be
00:00:35 applicable to any database.
00:00:36 And if you wanted to use SQL, that's totally okay.
00:00:40 I would still say follow through with the video, watch me implement MongoDB in a non-SQL database, See how it all works.
00:00:49 And then later, try to modify it on your own and switch it to an SQL database.
00:00:54 You'll learn so much more that way.
00:00:56 Or maybe in the future, I split this video in two and build it with other databases in mind as well.
00:01:02 Who knows?
00:01:03 But for the time being, head over to MongoDB Atlas and click try free.
00:01:07 Create your account.
00:01:09 And once you're in the dashboard, create a new project.
00:01:12 Name it something like subscription tracker.
00:01:17 Click Next, make yourself the project owner, and when you click Create, it'll take some time, and then you'll have to create your database cluster,
00:01:25 basically your database.
00:01:27 Click Create, and choose M0, which is the free version, which gives you about half a gigabyte of storage, and trust me, that is more than enough for storing
00:01:36 JSON information.
00:01:37 The rest of the option is okay, so just click Create Deployment.
00:01:41 Now, this is very important.
00:01:42 You can choose your username.
00:01:44 I'll do jsm right here.
00:01:46 And then you'll also have to choose your password.
00:01:48 Make sure to write this password down as you'll need to use it later on within the code.
00:01:52 Click create and choose a connection method.
00:01:55 In this case, we'll connect through a MongoDB driver like Node.js.
00:01:59 So choose the first option and copy the installation command.
00:02:03 npm install mongodb.
00:02:05 Back in the code, you can open up a new terminal and run npm install mongoDB.
00:02:14 And let's also install mongoose, which is an ORM for mongoDB, allowing you to more easily create your database models and schemas and press enter.
00:02:23 While that is installing, let's wait for a cluster to provision.
00:02:27 Basically means your database is setting up.
00:02:30 We can go back and then click drivers one more time to see if it's provisioned.
00:02:34 If not, let's give it a minute.
00:02:35 Once that is done, you'll be given your connection string, which you can copy and add to your code base, but make sure to replace the DB password with
00:02:43 the original password for your username.
00:02:45 So back into the code, let me head over to our ENV development local and add new environment variables for our database.
00:02:55 I'll call it DBURI and simply paste the string that I just copied right here.
00:03:00 MongoDB, SRV, JSM.
00:03:03 And for my password, I chose just JSM as well.
00:03:07 Hopefully you chose something more secure.
00:03:09 Now we can head over into config envs and we can also export const the DBURI right here.
00:03:16 Once you've done that, create a new folder, which we can call database.
00:03:25 And within it, create a new file called mongodb.js.
00:03:32 Within it, we can import mongoose, coming from mongoose, and we can import the environment variables which we need.
00:03:40 So that's going to be the DBURI, as well as NodeENV, coming from dot dot slash config, forward slash ENV dot JS.
00:03:50 We can first check if there is no DBURI, Then we can throw a new error, something like throw new error, please define the MongoDB URI environment variable
00:04:06 inside .env.local.
00:04:08 And of course, this is .env.
00:04:11 either development or production local.
00:04:16 So I'll make it super specific, just so we know that it has to be .env.
00:04:21 and then development production.local.
00:04:24 If we pass that if statement, that means that we are ready to connect.
00:04:27 So let's say const connectToDatabase and that'll be equal to an asynchronous function, which has a try and a catch block.
00:04:39 In the catch, we get an error and obviously something went wrong.
00:04:43 So let's simply console log that error saying error connected to database, show that error, and then exit the process with a code of one,
00:04:53 which means failure.
00:04:54 But we're trying to make something go right.
00:04:56 And that something is a wait.
00:04:59 mongoose.connect.
00:05:01 And we simply want to connect to this specific DBURI, which we got from MongoDB Atlas.
00:05:07 And I believe that's it.
00:05:08 We don't have to pass anything else.
00:05:10 Now we have to call this function from somewhere.
00:05:13 So let's export it by saying export default connected database.
00:05:17 And let's head over into the app.js.
00:05:21 Where do you think we'll call it?
00:05:23 Well, right here within app.listen, as soon as we start our application on localhost something, we can also try to connect to database,
00:05:35 make sure to import this function from the top, await it because it is an asynchronous function, and therefore you have to make this one asynchronous as well.
00:05:44 If you've done that correctly, and if you head over into connect to MongoDB, Alongside this await, once we pass it, we can also console log something.
00:05:56 Maybe something like console logged, connected to database in NodeENV mode.
00:06:03 So this is going to be either production or development.
00:06:06 So now, if you open up your terminal, you can see subscription tracker API running on, connected to database in undefined mode.
00:06:14 Oh, that's definitely not good, but I think I know why that is.
00:06:18 We have only defined it in production, but I'll also add it to development right here under environment, and I'll say noDNV is equal to development.
00:06:29 If I do it like that, make a change to any file and save, it'll say connect to the database in development mode, which is exactly what we wanted.
00:06:38 Great.
00:06:39 Now that we have the routes and the database, let's go ahead and create some models.