Page Contents

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:

  • memory
  • loopback-connector-oracle or just "oracle"
  • loopback-connector-mongodb or just "mongodb"
  • loopback-connector-mysql or just "mysql"
  • loopback-connector-postgresql or just "postgresql"
  • loopback-connector-soap or just "soap"
  • loopback-connector-mssql or just "mssql"
  • loopback-connector-rest or just "rest"

  • loopback-storage-service
name Name of the data source being defined.

Properties for database connectors

To connect a model to a data source, follow these steps:

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

  2. Edit server/datasources.json to 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.

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

    $ cd <your-app>
    $ npm install --save loopback-connector-mysql
    

    See Connectors for the list of connectors.

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

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.js or datasources.local.json
  • datasources._env_.js or datasources._env_.json, where env is the value of NODE_ENV environment variable (typically development or production). For example, datasources.production.json.

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'
  }
}