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.
To connect a model to a data source, follow these steps:
Use the data source generator,
slc loopback:datasource, to create a new data source. For example:
server/datasources.jsonto add the necessary authentication credentials: typically hostname, username, password, and database name. For example:
For information on the specific properties that each connector supports, see:
Install the corresponding connector as a dependency of your app with
npm, for example:
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.
You can also create models from an existing database; see Creating models for more information.
The following LoopBack connectors are available:
|Memory connector||Built in to LoopBack||Not required; suitable for development and debugging only.|
|Email connector||Built in to LoopBack|
Installing a connector
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
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.
Connector name; one of:
|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|
Combines and overrides
|username||String||Username to connect to database|