Prerequisite: Install command-line tools as described in Installation.
Recommended: Read LoopBack core concepts.
Page Contents
You’re going to take the app from the previous section and connect it to MySQL.
Note:
If you followed the previous steps in the tutorial, go to Add a data source.
If you’re just jumping in, follow the steps below to catch up…
Get the app (in the state following the last article) from GitHub and install all its dependencies:
$ git clone https://github.com/strongloop/loopback-getting-started.git
$ cd loopback-getting-started
$ git checkout lb2-step1
$ npm install
Add a data source
Now you’re going to define a data source using the Data source generator:
$ slc loopback:datasource
The generator will prompt you to name the data source:
[?] Enter the data-source name:
Enter mysqlDs and hit Enter.
Next, the generator will prompt you for the type of data source:
[?] Select the connector for mysqlDS: (Use arrow keys)
other
In-memory db (supported by StrongLoop)
MySQL (supported by StrongLoop)
PostgreSQL (supported by StrongLoop)
Oracle (supported by StrongLoop)
Microsoft SQL (supported by StrongLoop)
MongoDB (supported by StrongLoop)
(Move up and down to reveal more choices)
Press the down-arrow key to highlight MySQL, then hit Enter.
The tool adds the data source definition to the server/datasources.json
file, which will now look as shown below. Notice the “mysqlDs” data source you just added, as well as in-memory data source named “db,” which is there by default.
{
"db": {
"name": "db",
"connector": "memory"
},
"mysqlDs": {
"name": "mysqlDs",
"connector": "mysql"
}
}
Install MySQL connector
Now add the loopback-connector-mysql module and install the dependencies:
$ npm install loopback-connector-mysql --save
Configure data source
Important:
If you have a MySQL database server that you can use, please use it. Create a new database called “getting_started.” If you wish, you can use a different database name. Just make sure the mysqlDs.database
property in datasources.json
matches it (see below).
If not, you can use the StrongLoop MySQL server running on demo.strongloop.com
. However, be aware that it is a shared resource. There is a small chance that two users may run the script that creates sample data (see Add some test data and view it, below) at the same time and may run into race condition. For this reason, we recommend you use your own MySQL server if you have one.
Next, you need to configure the data source to use the desired MySQL server.
Edit /server/datasources.json
and after the line
` “connector”: “mysql”`
add host
, port
, database
, username
, and password
properties.
To use the StrongLoop MySQL server: running on demo.strongloop.com
, then enter the values shown below.
To use your own MySQL server: enter the hostname, port number, and login credentials for your server.
{
"db": {
"name": "db",
"connector": "memory"
},
"mysqlDs": {
"name": "mysqlDs",
"connector": "mysql",
"host": "demo.strongloop.com",
"port": 3306,
"database": "getting_started",
"username": "demo",
"password": "L00pBack"
}
}
Connect CoffeeShop model to MySQL
Now you’ve created a MySQL data source and you have a CoffeeShop model; you just need to connect them. LoopBack applications use the model-config.json file to link models to data sources. Edit /server/model-config.json
and look for the CoffeeShop entry:
...
"CoffeeShop": {
"dataSource": "db",
"public": true
}
...
Change the dataSource
property from db
to mysqlDs
. This attaches the CoffeeShop model to the MySQL datasource you just created and configured:
...
"CoffeeShop": {
"dataSource": "mysqlDs",
"public": true
}
...
Add some test data and view it
Now you have a CoffeeShop model in LoopBack, how do you create the corresponding table in MySQL database?
You could try executing some SQL statements directly…but LoopBack provides a Node API to do it for you automatically using a process called auto-migration. For more information, see Creating a database schema from models.
The loopback-getting-started
module contains the create-sample-models.js
script to demonstrate auto-migration. If you’ve been following along from the beginning (and didn’t just clone this module), then you’ll need to copy it from below or from GitHub . Put it in the application’s /server/boot
directory so it will get executed when the application starts.
Note:
The auto-migration script below is an example of a boot script that LoopBack executes when an application initially starts up. Use boot scripts for initialization and to perform any other logic your application needs to perform when it starts. See Defining boot scripts for more information.
module.exports = function(app) {
app.dataSources.mysqlDs.automigrate('CoffeeShop', function(err) {
if (err) throw err;
app.models.CoffeeShop.create([{
name: 'Bel Cafe',
city: 'Vancouver'
}, {
name: 'Three Bees Coffee House',
city: 'San Mateo'
}, {
name: 'Caffe Artigiano',
city: 'Vancouver'
}, ], function(err, coffeeShops) {
if (err) throw err;
console.log('Models created: \n', coffeeShops);
});
});
};
This will save some test data to the data source.
Note:
The boot script containing the auto-migration command will run each time you run your application. Since automigrate()
first drops tables before trying to create new ones, it won’t create duplicate tables.
Now run the application:
$ node .
In the console, you’ll see this:
...
Browse your REST API at http://0.0.0.0:3000/explorer
Web server listening at: http://0.0.0.0:3000/
Models created: [ { name: 'Bel Cafe',
city: 'Vancouver',
id: 1 },
{ name: 'Three Bees Coffee House',
city: 'San Mateo',
id: 3 },
{ name: 'Caffe Artigiano',
city: 'Vancouver',
id: 2 } ]
You can also use the API Explorer:
- Browse to http://0.0.0.0:3000/explorer/ (you may need to use http://localhost:3000/explorer, depending on your browser and OS).
- Click GET /CoffeeShops Find all instance of the model matched by filter…
- Click Try it out!
- You’ll see the data for the three coffee shops created in the above script.
Next: In Extend your API, you’ll learn how to add a custom method to your model.