Page Contents

Overview

Models, data sources, and connectors

 

LoopBack models connect to backend systems such as databases via data sources that provide create, retrieve, update, and delete (CRUD) functions. LoopBack also generalizes other backend services, such as REST APIs, SOAP web services, and storage services, and so on, as data sources.

Data sources are backed by connectors that implement the data exchange logic using database drivers or other client APIs. In general, applications don’t use connectors directly, rather they go through data sources using the DataSource  and  PersistedModel APIs.

Basic procedure

To connect a model to a data source, follow these steps:

  1. Use the  data source generator  to create a new data source.

    For example: 

    $ apic create --type datasource
    ? Enter the data-source name: mysql-corp
    ? Select the connector for mysql: MySQL (supported by StrongLoop)
    
    $ lb datasource
    ? Enter the data-source name: mysql-corp
    ? Select the connector for mysql: MySQL (supported by StrongLoop)
    

    Follow the prompts to name the datasource and select the connector to use. This adds the new data source to datasources.json.

  2. Edit server/datasources.json to add the necessary authentication credentials: typically hostname, username, password, and database name.

    For example:

    server/datasources.json

    "mysql-corp": {
      "name": "mysql-corp",
      "connector": "mysql",
      "host": "your-mysql-server.foo.com",
      "user": "db-username",
      "password": "db-password",
      "database": "your-db-name"
    }
    

    For information on the properties that each connector supports, see documentation for the specific connector under Connectors reference.

  3. Install the corresponding connector as a dependency of your app with npm.

    For example: 

    $ cd <your-app>
    $ npm install --save loopback-connector-mysql
    

    See Connectors reference for the list of connectors.

  4. Use the model generator to create a model.

    $ lb model
    ? Enter the model name: myModel
    ? Select the data-source to attach myModel to: mysql (mysql)
    ? Select model's base class: PersistedModel
    ? Expose myModel via the REST API? Yes
    ? Custom plural form (used to build REST URL):
    Let's add some test2 properties now.
    ...
    
    ```shell
    $ apic create --type model
    ? Enter the model name: myModel
    ? Select the data-source to attach myModel to: mysql (mysql)
    ? Select model's base class: PersistedModel
    ? Expose myModel via the REST API? Yes
    ? Custom plural form (used to build REST URL):
    Let's add some test2 properties now.
    ...
    ```
    

    When prompted for the data source to attach to, select the one you just created. 

You can also create models from an existing database; see Creating models for more information.

Connectors

The following table lists commercially-supported LoopBack connectors. For more information, see Database connectors and Non-database connectors.

Connector Module Install with npm install --save ...
Database connectors
IBM Cloudant loopback-connector-cloudant loopback-connector-cloudant
IBM DashDB loopback-connector-dashdb loopback-connector-dashdb
IBM DB2 loopback-connector-db2 loopback-connector-db2
IBM DB2 for z/OS loopback-connector-db2z loopback-connector-db2z
IBM Informix loopback-connector-informix loopback-connector-informix
Memory connector Built in to LoopBack Not required; suitable for development and debugging only.
MongoDB loopback-connector-mongodb loopback-connector-mongodb
MySQL loopback-connector-mysql loopback-connector-mysql
Oracle loopback-connector-oracle loopback-connector-oracle
PostgreSQL loopback-connector-postgresql loopback-connector-postgresql
SQL Server loopback-connector-mssql loopback-connector-mssql
SQLite 3.x loopback-connector-sqlite3 loopback-connector-sqlite3
Other connectors
Email connector Built in to LoopBack

Not required

Push connector loopback-component-push loopback-component-push
Remote connector loopback-connector-remote loopback-connector-remote
REST loopback-connector-rest loopback-connector-rest
SOAP loopback-connector-soap loopback-connector-soap
Storage connector loopback-component-storage loopback-component-storage

Installing a connector

Run npm install --save <connector-module> in your application root directory to add the dependency to package.json; for example, to install the Oracle database connector:

$ cd <your-app>
$ npm install --save loopback-connector-oracle

This command adds the following entry to package.json

package.json

...
"dependencies": {
  "loopback-connector-oracle": "latest"
}
...

Creating a data source

Use the data source generator to create a new data source:

$ apic create --type datasource
$ lb datasource

Follow the prompts to add the desired data source.

You can also create a data source programmatically; see Advanced topics: data sources for more information.

Data source properties

Data source properties depend on the specific data source being used. However, data sources for database connectors (Oracle, MySQL, PostgreSQL, MongoDB, and so on) share a common set of properties, as described in the following table.

Property Type Description
connector String Connector name; for example:
  • "memory"
  • "loopback-connector-mongodb" or "mongodb"
  • "loopback-connector-mysql" or "mysql"
  • "loopback-connector-oracle" or "oracle"
  • "loopback-connector-postgresql" or "postgresql"
  • "loopback-connector-rest" or "rest"
  • "loopback-connector-mssql" or "mssql"
database String Database name
debug Boolean If true, turn on verbose mode to debug database queries and lifecycle.
host String Database host name
password String Password to connect to database
port Number Database TCP port
url String Combines and overrides host, port, user, password, and database properties. Only valid with certain connectors; see connector documentation.
username String Username to connect to database
Tags: data_sources