loopback-example-database

    A tutorial for basic database related features.

    • 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.

    For example, to view the MySQL example:

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

    • Node
    • LoopBack CLI tools; see
    1. cd loopback-example-database
    2. npm install
    3. npm start

    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
    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:

    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
      • createdAt
        • Date
        • Not required
      • lastModifiedAt
        • Date
        • Not required
    1. lb model Account
    2. ... # follow the prompts

    6. Create the collection with sample data - Automigration

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

    1. mkdir bin

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

    1. node bin/automigrate.js

    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:

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

    • 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. ]

    Create a script name . 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. "dataType": "int",
    20. "dataLength": null,
    21. "dataPrecision": 10,
    22. "dataScale": 0,
    23. "nullable": "N"
    24. }
    25. },
    26. "email": {
    27. "type": "String",
    28. "length": 512,
    29. "precision": null,
    30. "scale": null,
    31. "mysql": {
    32. "columnName": "email",
    33. "dataType": "varchar",
    34. "dataLength": 512,
    35. "dataPrecision": null,
    36. "dataScale": null,
    37. "nullable": "Y"
    38. }
    39. },
    40. "createdat": {
    41. "type": "Date",
    42. "required": false,
    43. "length": null,
    44. "precision": null,
    45. "scale": null,
    46. "mysql": {
    47. "columnName": "createdAt",
    48. "dataType": "datetime",
    49. "dataLength": null,
    50. "dataPrecision": null,
    51. "dataScale": null,
    52. "nullable": "Y"
    53. }
    54. },
    55. "lastmodifiedat": {
    56. "type": "Date",
    57. "required": false,
    58. "length": null,
    59. "precision": null,
    60. "scale": null,
    61. "mysql": {
    62. "columnName": "lastModifiedAt",
    63. "dataType": "datetime",
    64. "dataLength": null,
    65. "dataPrecision": null,
    66. "dataScale": null,
    67. "nullable": "Y"
    68. }
    69. }
    70. }

    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 .Then run:

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

    You should see:


    Tags: example_app