Part 4 — Connecting A Database

In Part 4 we’ll go over setting up a database and connecting it to your app. We’ll use a NoSQL database — MongoDB. We’ll store user accounts and the comments users post in the forum in our database.

**Note: The forum page is optional. If you don’t plan to create a forum page simply store user account info in the database.**

To set up your database, follow these steps:

1. Sign up for a MongoDB Atlas account here.

2. Choose a provider and region where the free tier is available and select that free tier. Be sure to opt out of any features like backups that will cost extra.

3. Go to your Cluster view and click on CONNECT.

4. Setup connection security by whitelisting your connection IP address and creating a MongoDB User. Remember this username and password for the next step.

5. For the connection method, select “Connect Your Application” and copy the connection string.

Next, we need to create the file in our directory where we will connect our database. Let’s call it “kyodieModels.js”.

In this file, declare a variable called MONGO_URI and assign to your connection string. Make sure you have replaced <password> in your connection string with your MongoDB user password created earlier and wrap the entire string with quotes.

Then, use Mongoose’s connect method with MONGO_URI and an object passed in as parameters. In the object create keys for the name of your database and parsing requirements. With promises, add a console.log to confirm that you have connected to the database.

When you run your app and see that log in your console you know you have officially connected to your database!

kyodieModels.js file

NoSql databases are not beholden to schemas (unlike SQL databases). This makes noSQL more flexible because the database can contain documents with different kinds of data that don’t neatly fit into a schema. Still, this can cause trouble — it can be confusing and prone to errors if data is not organized in a predictable structure.

Mongoose alleviates some of this trouble by letting developers create schemas and use models created off those schemas to interact with the DB. Keeping our interactions with the database structured and predictable.

We will create three schemas. One for user authentication and one for posting comments on the forum page. We will “connect” these two schemas so we can keep track of which user posts each comment.

This “connection” is called denormalization. Denormalization can make certain retrieval of data easier and faster at the cost of losing consistency — The same information now lives in multiple places so if we need to insert or update data it makes it more costly and difficult as well as taking up more space due to having redundant data. We also do not have one source of truth but have multiple sources which can be problematic.

To create a schema, first declare a variable called “Schema” and assign it to the method “mongoose.Schema”. This allows is to use mongoose to create new Schema’s.

Next, declare a variable called “commentSchema” and assign it to the definition of a new schema. In this schema, create keys for title, comment, and user_id. Title and comment are input fields the user will complete when posting a comment. The users User_id number will be taken from storage after they login. We will go over authentication (login, signup) in part 5. The values of each key should be the data type expected in order to post / save something to the database. If the comment key is expecting a String and a user types a Number, the comment will post to the database as a string and not a number value. See all the data types you can use here.

Next, create a model. We will use this model when interacting with the database.

Repeat the above two steps to create user and session Schemas and models. Notice in the user schema we’ve made the userName and password keys required — we need both of those to create a user. The session schema will be used to keep track of cookies given to a user when they login. We will go over cookies and sessions with authentication in Part 5 as well.

Lastly, export each model you’ve created. These models are used to define the way we interact (post, update, delete) with the database.

Congratulations! In Part 4 you have successfully connected your database to your app! Now we can use it to create users and post comments.

->Continue to Part 5 now!

--

--