Translation last updated: 17 Nov 2015.
This documentation is for LoopBack 2.x.
For the latest information, see the LoopBack documentation in English.
Skip to end of metadata
Go to start of metadata

Overview

 

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 generatorslc loopback:datasource, to create a new data source.  For example: 

    Follow the prompts to name the datasource and select the connector to use.  See Connecting models to data sources for more information.  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

    For information on the specific properties that each connector supports, see:

  3. Install the corresponding connector as a dependency of your app with npm, for example: 

    See Connectors for the list of connectors.

  4. Use the model generatorslc loopback:model, to create a model.  When prompted for the data source to attach to, select the one you just created. 

    Icon

    The model generator lists the memory connector, "no data source," and data sources listed in datasources.json. That's why you created the data source first in step 1.

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

Connectors

The following LoopBack connectors are available:

Database connectors
ConnectorModuleInstallation
Memory connectorBuilt in to LoopBackNot required; suitable for development and debugging only.
MongoDBloopback-connector-mongodbnpm install --save loopback-connector-mongodb
MySQLloopback-connector-mysqlnpm install --save loopback-connector-mysql
Oracleloopback-connector-oraclenpm install --save loopback-connector-oracle
PostgreSQLloopback-connector-postgresqlnpm install --save loopback-connector-postgresql
SQL Serverloopback-connector-mssqlnpm install --save loopback-connector-mssql
Other connectors
Email connectorBuilt in to LoopBack

Not required

Push connector loopback-component-pushnpm install --save loopback-component-push
Remote connectorloopback-connector-remotenpm install --save loopback-connector-remote
RESTloopback-connector-restnpm install --save loopback-connector-rest
SOAPloopback-connector-soapnpm install --save loopback-connector-soap
Storage connectorloopback-component-storage  npm install --save loopback-component-storage
Icon

In addition to the connectors listed above that StrongLoop provides, Community connectors developed and maintained by the LoopBack community enable you to connect to CouchDB, Neo4j, Elasticsearch, and many others.  See Community connectors for more information.

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:

This command adds the following entry to package.json

/package.json

Creating a data source

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

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.

PropertyTypeDescription
connectorString

Connector name; one of:

  • "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"
databaseStringDatabase name
debugBooleanIf true, turn on verbose mode to debug database queries and lifecycle.
hostStringDatabase host name
passwordStringPassword to connect to database
portNumberDatabase TCP port
urlString

Combines and overrides hostportuserpassword, and database properties.

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

usernameStringUsername to connect to database
  • No labels