Chinese Documentation : Connecting models to data sources

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:

  1. 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 .

  2. Install the corresponding connector with npm, for example: 

    $ npm install --save loopback-connector-mysql

    See Connectors for the list of connectors.

  3. 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.

    $ 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
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
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-push
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 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

/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.

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

Attachments: