Overview
Configure data sources in /server/datasources.json. You can set up as many data sources as you want in this file.
For example:
{
"db": {
"name": "db",
"connector": "memory"
},
"myDB": {
"name": "myDB",
"connector": "mysql",
"host": "demo.strongloop.com",
"port": 3306,
"database": "demo",
"username": "demo",
"password": "L00pBack"
}
}
To access data sources in application code, use app.datasources._datasourceName_.
Standard properties
All data sources support a few standard properties. Beyond that, specific properties and defaults depend on the connector being used.
| Property | Description |
|---|---|
| connector |
LoopBack connector to use; one of:
|
| name | Name of the data source being defined. |
Properties for database connectors
To connect a model to a data source, follow these steps:
-
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-corp: MySQL (supported by StrongLoop)$ slc loopback:datasource ? Enter the data-source name: mysql-corp ? Select the connector for mysql-corp: MySQL (supported by StrongLoop)Respond to the prompts to set data source properties such as database credentials. To change the data source properties, edit
server/datasources.json. -
Edit
server/datasources.jsonto 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 the specific connector documentation in Database connectors.
-
Install the corresponding connector as a dependency of your app with
npm, for example:$ cd <your-app> $ npm install --save loopback-connector-mysqlSee Connectors for the list of connectors.
-
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.
Note:
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.
Environment-specific configuration
You can override values set in datasources.json in the following files:
datasources.local.jsordatasources.local.jsondatasources._env_.jsordatasources._env_.json, whereenvis the value ofNODE_ENVenvironment variable (typicallydevelopmentorproduction). For example,datasources.production.json.
Important:
The additional files can override the top-level data-source options with string and number values only. You cannot use objects or array values.
Example data sources:
datasources.json
{
// the key is the datasource name
// the value is the config object to pass to
// app.dataSource(name, config).
db: {
connector: 'memory'
}
}
datasources.production.json
{
db: {
connector: 'mongodb',
database: 'myapp',
user: 'myapp',
password: 'secret'
}
}