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
The easiest way to connect a model to a data source is to use the LoopBack command line tool, slc loopback
. Follow these steps:
Use the data source generator,
slc loopback:datasource
, to create a new data source. For example:$ slc loopback:datasource $ Enter the data-source name: mysql $ Select the connector for mysql: MySQL (supported by StrongLoop)
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
.Install the corresponding connector with
npm
, for example:$ npm install --save loopback-connector-mysql
See Connectors for the list of connectors.
Use the model generator,
slc loopback:model
, to create a model. When prompted for the data source to attach to, select the one you just created.$ slc loopback:model $ Enter the model name: myModel $ Select the data-source to attach test2 to: mysql (mysql) $ Select model's base class: PersistedModel $ Expose test2 via the REST API? Yes $ Custom plural form (used to build REST URL): Let's add some test2 properties now. ...
You can also create models from an existing database; see Creating models for more information.
Connectors
The following LoopBack connectors are available:
Database connectors | ||
Connector | Module | Installation |
---|---|---|
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 |
Other connectors | ||
Email connector | Built in to LoopBack | Not required |
Push connector | loopback-component-push | npm install --save loopback-component-push |
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-push |
Installing a connector
Run npm install --save
for the connector module to add the dependency to package.json
; for example, to install the Oracle database connector:
$ npm install --save loopback-connector-oracle
This command adds the following entry to package.json
:
... "dependencies": { "loopback-connector-oracle": "latest" } ...
Creating a data source
Use the Data source generator to create a new data source:
$ 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; one of:
|
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 Only valid with MongoDB connector, PostgreSQL connector, and SQL Server connector. |
username | String | Username to connect to database |