Using StrongLoop to create a Node.js MySQL application
6 min read
Using StrongLoop to create a Node.js MySQL application
The StrongLoop API Platform is built on top of the open source LoopBack.io, a highly-extensible, Node.js API framework. With LoopBack, you can quickly create scalable APIs and database mappings. StrongLoop also has built-in Mobile Backend as a Service (MBaaS) features like push and offline sync, and it has graphical tools for clustering, profiling and monitoring Node apps. StrongLoop runs on top of the Express Node.js web application framework and conforms to the Swagger 2.0 specification.
The recently published tutorial Getting Started with Bluemix and StrongLoop presents videos summarizing the strengths of each product and demonstrating a sample application; the tutorial Getting Started with Node.js LoopBack Framework and IBM Cloudant examines how to leverage IBM Cloudant as a backend data store with the LoopBack framework and StrongLoop Arc tools.
Continuing with same theme as the previous tutorials, this brief tutorial shows the steps of integrating data from a MySQL database with Node.js using the StrongLoop API. We make no assumptions about tooling other than the command line! Once complete, you will be able to browse the generated APIs:
From there you will be able to POST data with the StrongLoop API Explorer and review the results in your database browser of choice.
Before you begin
Beyond the command line, below are the prerequisite tools:
The Node.js executable npm, the package manager for Node.js
Of course you’ll need a Bluemix account. If you don’t already have one, click the button below:
Before you can use StrongLoop in your app, you must install the StrongLoop command line tools. These include
slc, the command shell for the StrongLoop Process Manager,
strong-pm, and the LoopBack API Framework, with both a CLI and a GUI. To install the StrongLoop tools, execute the following
To verify whether LoopBack is installed successfully, type
It should display the version of your StrongLoop installation.
Create a skeletal LoopBack application
Change to your development directory.
Create a new application entitled ‘<alias>-loopback-example’ (e.g. amyla-loopback-example) by using the following command.
Enter the application name and the installation directory to complete the initialization of your new StrongLoop project. You now have a complete Node.js StrongLoop application. Try to run your application and open the explorer page.
If an error similar to “Cannot find module ‘loopback'” occurs, run
npm install to install the prerequisite modules in the
node_modules directory. Click the show/hide link to show the StrongLoop APIs that come available with your new application:
Install the StrongLoop MySQL connector
Next, we will add MySQL support for the MySQL database to the project, by installing the LoopBack MySQL connector. Run the following command to install the StrongLoop MySQL connector:
Configuring the Datasource
Now the appropriate database support is added, you must configure the datasource with your database connection. Create and configure a new datasource named ‘AccountDb’.
Enter to accept the default datasource name. For the database type, select MySQL.
Create a Node.js Application and MySQL Service
Now we have a working StrongLoop application running on our localhost, and we are about to add a MySQL connection to the application, we must first create the MySQL instance that we want to connect to.
If you have already an existing MySQL database instance that you want to use, you can skip this step and use the MySQL credentials of your existing service instead.
I will create a new MySQL database instance in Bluemix. If you do not have a Bluemix account, sign up for a free Bluemix trial.
Login to the Bluemix console at console.ng.bluemix.net.
Go to the Dashboard.
Under ‘Cloud Foundry Apps’ click ‘CREATE APP’.
Select ‘SDK for Node.js’ and click ‘CONTINUE’.
Enter the application name ‘<alias>-loopback-example’ at ‘APP NAME’ and click ‘FINISH’.
Wait until the app is finished staging.
Once the message ‘Your app is running’ is displayed, click on the Overview page.
Click on ‘ADD A SERVICE OR API’ or if you previously already created the MySQL service, click ‘BIND A SERVICE OR API’.
Search for ‘MySQL’ or scroll down to the Data & Analytics services.
Find and select the ‘ClearDB MySQL Database’ service.
Accept the defaults or rename the service and press ‘USE’ and confirm to ‘restage’ the application.
Once you created the service, in the left menu of the application detail page, go to ‘Environmental Variables’ to see the MySQL service credentials. Or go to the Overview page, and in the service window, click the ‘Show Credentials’ link.
Configure the Datasource
In your application directory, open the
server/datasources.json file, and add your datasource configuration. Copy the credentials for the ClearDB MySQL service or use your own MySQL Database credentials.
Add the credentials to the
Create the Data Model
Now the database is successfully added and the datasource configured, you can create the data model. The data model describes the schema or the data objects in your application. Each data object will have its own model in StrongLoop. The complete schema is the collection of models. Here we create a data model for the Account object.
Follow the prompts to configure the data model. For the model’s ‘base class’ use ‘PersistedModel’ and choose to expose the model via the REST API. Add the model properties as follows:
For Properties on the data model, create the following three properties.
We now have the database support, the data source and the data model. With these, we can use Object Relation Mapping (ORM) to generate the database schema in MySQL from the data model, using the auto-migrate tool in StrongLoop.
Add a script to create the schema in the database.
Create a bin directory inside the
./server folder in your project root.
server/bin directory, create a file called
automigrate.js with the following code.
Create the MySQL Schema
Run the automigrate.js script with the below command to create the table in MySQL.
After running the above script, connect to your MySQL database with your favorite MySQL client and you should see the table ‘account’ with the three columns: ’email’, ‘createdAt’ and ‘lastModifiedAt’. StrongLoop also added an ‘id’ column. The table has data for the two entries that we created in the script to insert the data:
Run the application
and browse to http://localhost:3000/explorer. StrongLoop added a series of APIs that are Swagger compliant to your application to access the Account resource:
We can also use the explorer page to test each API. Click the ‘POST /Accounts’ link to insert new data. Enter a new JSON record and press ‘Try it out!’.
Your StrongLoop application to manage Accounts is done! We just need to still upload and deploy it to Bluemix.
Deploy the StrongLoop app to Bluemix
Go to the Bluemix Dashboard and click on the project(that you created earlier).
Click on start coding.
Download the code of the starter application in Bluemix to your localhost, or clone the Git repository to your localhost.
Copy the manifest.yml file of the Bluemix starter application to the root directory of your local StrongLoop application ‘<alias>-loopback-example’. This file contains information used by Cloud Foundry to deploy the Node.js application to Bluemix.
Copy the hidden
.cfignorefile to the root directory of your local StrongLoop application ‘<alias>-loopback-example’.
Modify the local StrongLoop ‘package.json’ file to add the ‘cfenv’ dependency and change the start script parameter (see code snippet below)
server/server.jsfile of your local StrongLoop application to include the ‘host’ and ‘port’ read by
cfenv. Bluemix makes these parameters available as environment variables. In the code snippet below I modified the
server.jsgenerated by StrongLoop to reflect these changes.
node_modulesfolder from the root directory of your local StrongLoop application.
Code snippet for package.json:
Code snippet for
Connect to Bluemix and login to Bluemix:
Push your application to Bluemix using the
cf push command:
cf push should kick off a
git push and build and deploy on Bluemix. Check your Bluemix Dashboard or your JazzHub ‘Build & Deploy’ page to confirm your application is now running successfully on Bluemix!