A brief tutorial on using the LoopBack MySQL connector.Note: This page was generated from the loopback-example-database/README.md.

loopback-example-database

A tutorial for basic database related features.

Overview

Topics covered

  • Data sources
    • Creating
    • Configuring
  • Models
    • Creating
  • Automigration
  • Discovery

Database specific tutorials

Database specific tutorials are on separate branches. The master branch containsthe tutorial for MongoDB.

BranchConnector
masterMongoDB
mssqlMicrosoft SQL Server
mysqlMySQL
oracleOracle
postgresqlPostgreSQL

For example, to view the MySQL example:

  1. git clone https://github.com/strongloop/loopback-example-database
  2. cd loopback-example-database
  3. git checkout mysql

Prerequisites

Before starting this tutorial, make sure you have the following installed:

  • Node
  • LoopBack CLI tools; see lb

Running the example

  1. git clone https://github.com/strongloop/loopback-example-database
  2. cd loopback-example-database
  3. npm install
  4. npm start

Tutorial - MySQL

1. Create a new LoopBack app

App info

  • Name: loopback-example-database
  • Dir to contain the project: loopback-example-database
  1. lb app loopback-example-database
  2. ... # follow the prompts

2. Install the LoopBack MySQL connector

  1. cd loopback-example-database
  2. npm install --save loopback-connector-mysql

3. Create a data source

Data source info

  • Data source name: accountDS
  • Select the connector for accountDS: MySQL
  1. lb datasource accountDS
  2. ... # follow the prompts

This creates a new data source named accountDS that uses the MySQL connector.

4. Configure the data source

For the purposes of this example, we will use a preconfigured StrongLoop MySQLserver. Edit server/datasources.json to set the MySQL configs:

  1. {
  2. ...
  3. "accountDS": {
  4. "name": "accountDS",
  5. "connector": "mysql",
  6. "host": "demo.strongloop.com",
  7. "port": 3306,
  8. "database": "loopback-example-mysql",
  9. "username": "demo",
  10. "password": "L00pBack"
  11. }
  12. }

Feel free to use your own local MySQL instance. Simply change the configsabove to match your own.

5. Create a new model

Model Info

  • Model name: Account
  • Attach Account to: accountDS (mysql)
  • Base class: PersistedModel
  • Expose via REST: Yes
  • Custom plural form: Leave blank
  • Properties:
    • email
      • String
      • Not required
    • createdAt
      • Date
      • Not required
    • lastModifiedAt
      • Date
      • Not required
  1. lb model Account
  2. ... # follow the prompts

6. Create the collection with sample data - Automigration

With the account model configured, we can generate the correspondingMySQL table using the info from the Account metadata in common/models/account.jsonvia auto-migration.

Start by creating a dir to store general-purpose scripts:

  1. mkdir bin

Inside that dir, create a script named automigrate.js.To create the Account collection and create two sample accounts, run:

  1. node bin/automigrate.js

WARNING

The automigrate function creates a new collection if it doesn’t exist. Ifthe collection already exists, it will be destroyed and it’s data will bedeleted. If you want to keep this data, use autoupdate instead.

You should see:

  1. Created: { email: 'john.doe@ibm.com',
  2. createdAt: Thu Oct 22 2015 17:58:09 GMT-0700 (PDT),
  3. lastModifiedAt: Thu Oct 22 2015 17:58:09 GMT-0700 (PDT),
  4. id: 562986213ea33440575c6588 }
  5. Created: { email: 'jane.doe@ibm.com',
  6. createdAt: Thu Oct 22 2015 17:58:09 GMT-0700 (PDT),
  7. lastModifiedAt: Thu Oct 22 2015 17:58:09 GMT-0700 (PDT),
  8. id: 562986213ea33440575c6587 }

7. View data using the explorer

Projects scaffolded via slc loopback come with loopback-component-explorerpreconfigured. From the project root, start the server:

  1. node .

Then to view the existing account data, browse to localhost:3000/explorer andclick:

  • GET /Accounts
  • Try it out!You should see:
  1. [
  2. {
  3. "email": "john.doe@ibm.com",
  4. "createdAt": "2015-10-23T00:58:09.280Z",
  5. "lastModifiedAt": "2015-10-23T00:58:09.280Z",
  6. "id": "562986213ea33440575c6587"
  7. },
  8. {
  9. "email": "jane.doe@ibm.com",
  10. "createdAt": "2015-10-23T00:58:09.280Z",
  11. "lastModifiedAt": "2015-10-23T00:58:09.280Z",
  12. "id": "562986213ea33440575c6588"
  13. }
  14. ]

Try out some of the other endpoints to get a feel for how explorer works.

8. Add a script to perform discover the database schema

Discoveryis the process of reverse engineering a LoopBack model from an existingdatabase schema.

Create a script name discover-schema.js. Then run this script todiscover the schema from the existing Account table:

  1. node bin/discover-schema

You should see:

  1. {
  2. "name": "Account",
  3. "options": {
  4. "idInjection": false,
  5. "mysql": {
  6. "schema": "loopback-example-mysql",
  7. "table": "Account"
  8. }
  9. },
  10. "properties": {
  11. "id": {
  12. "type": "Number",
  13. "required": true,
  14. "length": null,
  15. "precision": 10,
  16. "scale": 0,
  17. "id": 1,
  18. "mysql": {
  19. "columnName": "id",
  20. "dataType": "int",
  21. "dataLength": null,
  22. "dataPrecision": 10,
  23. "dataScale": 0,
  24. "nullable": "N"
  25. }
  26. },
  27. "email": {
  28. "type": "String",
  29. "required": false,
  30. "length": 512,
  31. "precision": null,
  32. "scale": null,
  33. "mysql": {
  34. "columnName": "email",
  35. "dataType": "varchar",
  36. "dataLength": 512,
  37. "dataPrecision": null,
  38. "dataScale": null,
  39. "nullable": "Y"
  40. }
  41. },
  42. "createdat": {
  43. "type": "Date",
  44. "required": false,
  45. "length": null,
  46. "precision": null,
  47. "scale": null,
  48. "mysql": {
  49. "columnName": "createdAt",
  50. "dataType": "datetime",
  51. "dataLength": null,
  52. "dataPrecision": null,
  53. "dataScale": null,
  54. "nullable": "Y"
  55. }
  56. },
  57. "lastmodifiedat": {
  58. "type": "Date",
  59. "required": false,
  60. "length": null,
  61. "precision": null,
  62. "scale": null,
  63. "mysql": {
  64. "columnName": "lastModifiedAt",
  65. "dataType": "datetime",
  66. "dataLength": null,
  67. "dataPrecision": null,
  68. "dataScale": null,
  69. "nullable": "Y"
  70. }
  71. }
  72. }
  73. }

9. Add a script to discover and build models

When retrieving the scheme is not enough, you can discover and build LoopBackmodels in one step.

Create a sript named discover-and-build-models.js.Then run:

  1. node bin/discover-and-build-models

You should see:

  1. Found: [ { id: 1,
  2. email: 'john.doe@ibm.com',
  3. createdat: Fri Oct 23 2015 15:07:51 GMT-0700 (PDT),
  4. lastmodifiedat: Fri Oct 23 2015 15:07:51 GMT-0700 (PDT) },
  5. { id: 2,
  6. email: 'jane.doe@ibm.com',
  7. createdat: Fri Oct 23 2015 15:07:51 GMT-0700 (PDT),
  8. lastmodifiedat: Fri Oct 23 2015 15:07:51 GMT-0700 (PDT) } ]

See the official docsfor more info.


More LoopBack examples

Tags: example_app