Connect Loopback to MongoDB

Connecting A Database To Loopback:

Prerequisites: We need to install a database into our Final Project folder. Follow the instructions from below. Go to the next step only if you already have a database source setup

Step 1: Create Database Connector

  • In this step we will be setting up the connection from our mongodb you have installed to our loopback project we installed.

From within you loopback project, final-project-bk folder run:

$ lb datasource

This will being a series of commands.

  • Enter datasource name: Type mongo and click enter

  • Select database type: We are presented with a list of database choices. Select MongoDb using the up or down arrows.

  • Enter Other Credentials: We will have another bunch of options, for host, port and user. These are for if we were using a cloud database. We are BUT we can fill out later manually in the datasources.json file. So just leave blank for now:

    • Connection url:

    • Host:

    • Port:

    • User:

    • Password:

    • Database:

  • Allow supported Features: Select yes to allow MongoDB v3.1.0 supported features

Note: Pressing enter by default selects Yes, that is why the Y is capitalized when you see (Y/n)

  • Install loopback-connector: Select yes to install loopback-connector-mongodb@^1.4). This will install what loopback needs to connect to mongodb.

Note: Pressing enter by default selects Yes, that is why the Y is capitalized when you see (Y/n)

A successful install will result in :

Step 2: Connect Loopback to MongoDB

In the MongoAtlas Cluster Dashboard:

Click on Connect

Select connect application

Copy the short srv connection string

Now back in your loopback final project navigate to the following file: server/datasources.json

Paste the copied connection string as a value of the “url” property of the mongo datasource object

Connection string example:

mongodb+srv://admin:<password>@movie-app-7lh3q.mongodb.net/test?retryWrites=true&w=majority

Replace the following portions of the string with the database credentials you made earlier:

  • username (default: admin)

  • password (default: <PASSWORD>)

  • database name (default: test)

Fill in the rest of the property values inside of the mongo datasource object

Step 3: Additional Setup Steps:

Remove the following line of code

Navigate to ./Final-Project-bk/server/datasources.json and remove the following line of code:

"useNewUrlParser": true

Disable Access Control

Most applications need to control who (or what) can access data or call services . Typically, this involves requiring users to login to access protected data, or requiring authorization tokens for other applications to access protected data. With access control we can control which users have access to these respective API endpoints on the backend through requiring an access token as part of the requests.

For simplicity sake we will disable access control, though we will later in the course we will re-enable this feature.

Disable authentication:

  • Navigate to ./Final-Project-bk/server/boot/authentication.js and comment out the code on line number 3:

module.exports = function enableAuthentication(server) {
  // Disble authentication
  // server.enableAuth();
};

Step 4: Start Server

Lets start the server now and check that we can start the server and connect to the database From within your final-project-bk

node .

Loopback Data Source Setup Recap

Setup a connection using the loopback command line program to connect to our local mongo database to our loopback project. All the settings for our projects data sources are in datasources.json or Final-Project-bk/server/datasources.json. In here is where we could change the url to point to a cloud database and even have multiple databases. We will be added a database soon but first we have to create one.

Screen Shot 2017-10-04 at 52207 PM.jpg

Last updated