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:

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

    3. Create a data source

    Data source info

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

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

    4. Configure the data source

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

    Model Info

    • Model name: Account
    • Attach Account to: accountDS (mssql)
    • Base class: PersistedModel
    • Expose via REST: Yes
    • Custom plural form: Leave blank
    • Properties:
      • email
        • String
        • Not required
        • Date
        • Not required
      • lastModifiedAt
        • Date
    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: Fri Oct 23 2015 16:25:50 GMT-0700 (PDT),
    3. lastModifiedAt: Fri Oct 23 2015 16:25:50 GMT-0700 (PDT),
    4. id: 1 }
    5. Created: { email: 'jane.doe@ibm.com',
    6. createdAt: Fri Oct 23 2015 16:25:50 GMT-0700 (PDT),
    7. lastModifiedAt: Fri Oct 23 2015 16:25:50 GMT-0700 (PDT),
    8. id: 2 }

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

    • GET /Accounts
    • Try it out!You should see:
    1. [
    2. {
    3. "email": "john.doe@ibm.com",
    4. "createdAt": "2015-10-23T23:25:50.000Z",
    5. "lastModifiedAt": "2015-10-23T23:25:50.000Z",
    6. "id": 1
    7. },
    8. {
    9. "email": "jane.doe@ibm.com",
    10. "createdAt": "2015-10-23T23:25:50.000Z",
    11. "lastModifiedAt": "2015-10-23T23:25:50.000Z",
    12. "id": 2
    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:

      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