Page Contents

Overview

</figure>  

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)
    
    $ slc loopback: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.

    $ 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.
    ...
    
    $ slc loopback: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.

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

Not required

Push connector loopback-component-push npm install --save loopback-component-push
Remote connector loopback-connector-remote npm install --save loopback-connector-remote
REST loopback-connector-rest npm install --save loopback-connector-rest
SOAP loopback-connector-soap npm install --save loopback-connector-soap
Storage connector loopback-component-storage npm install --save 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
$ slc loopback: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 hostportuserpassword, and database properties.

Only valid with MongoDB connector, PostgreSQL connector, and SQL Server connector.

username String Username to connect to database
Tags: data_sources