BETAThis is a new service – your feedback will help us to improve it.

Introducing databases

We've now built a working web app in Codenvy, which already has several useful features:

  • We can pass real data into our views and display it as part of a template
  • Code that is used in several parts of the site is reused, so only needs to be edited on one place
  • Static files like images, .css and .js files can be served.

However, our app is still read-only. Users can't change or modify it. For that, we need a database.

In this module, we're going to add a database to our app, start fetching data from it to display in the app, and finally add new data to the database using a form.

Eventually, whenever a user requests a view, our app will query the database, build the correct view based on that data, then send the view to the user.

What is a database?

A database stores data in a structured way.

There are many different kinds of database software. We're going to use one called MongoDB.

You may have heard of SQL or relational databases. These are capabale and popular, but we aren't going to use them on this course. MongoDB databases are non-relational.

Create a database

We could download the MongoDB software and run it inside Codenvy or on our own computer, but this is tricky and time-consuming.

Instead, we're going to use a free service called mLab, which will provide a blank MongoDB database for us to connect to, read and change.

Go to mLab, sign up for a new account and verify your email address.

From the mLab home page, create a new environment.

Under display name, put "my-environment". The other default settings are fine. Hit "Create new environment".

An environment is a collection of related databases. We only need one database, but we need to create an environment for it to live in first.

Next, hit "Create new" under the MongoDB Deployments section.

Choose the free sandbox plan for your database, and use AWS as the Cloud Provider.

Pick the Europe region to host your database—it makes sense to store data as close to your users as possible.

Call your database "my-app" and submit the order.

It will take a few moments for your database to be created.

Connecting to a database

In the MongoDB deployments section, click the name of your database to view its details on a new page.

You will see some connection instructions at the top of the page, including the connection string—a URL that looks like this:

mongodb://<dbuser>:<dbpassword>@ds151508.mlab.com:51508/my-app

Our database is available on the internet for anyone to connect to. This is bad for the security of our data, so later, we'll secure our database with a username and password.

Eventually, we'll write some code in our app that connects to the database using those details.

Your database's username and password will be used by your app, not by you. They should be different from your mLab user account details.

To do

  1. Go to mLab, sign up for a new account and verify your email address
  2. Create a new environment.
  3. Create a new database inside that environment.
  4. Wait for the database to be created.
Lessons last updated 12th July 2019. You can improve this lesson on Github.
Part of Databases
  1. Introducing databases
  2. Organising a databaseP
  3. Using a database driverP
  4. Creating modelsP
  5. Making forms workP
  6. Collecting form dataP
  7. Saving form dataP