Search code examples
loopbackjsloopback

Loopback - Setting Up environment specific configuration


Hi can I get some help with setting environment specific configuration. I have two files for datasource

  1. server/datasources.json
  2. server/datasources.test.json

I use the script "SET NODE_ENV=test && mocha test/**/*.test.js" on WIndows to run my test cases and set the node environment to test. Loopback does not load server/datasource.test.json instead the datasource from server/datasource.json is loaded.

I have confirmend the environment using process.env.NODE_ENV which logs "test

I have tried to change server/datasource.json to server/datasource.local.json, But then I get an error WARNING: Main config file "datasources.json" is missing.

I dont understand what I am doing wrong.Am I supposed to create all the config files for the test environment like *.test.json. Or is there a different config file where I have to define envrionment specific files.

Please check this repo https://github.com/dhruv004/sample-loopback-example From the code If you run npm run test It loads data from local.json which is the data source for development environment.It should load data from test.json(datasource for test environment)


Solution

  • Looking on your repository, I can see this note from LoopBack documentation particulary relevant for you:

    A LoopBack application can load multiple configuration files, that can potentially conflict with each other. The value set by the file with the highest priority will always take effect. The priorities are:

    1. Environment-specific configuration, based on the value of NODE_ENV; for example, server/config.staging.json.
    2. Local configuration file; for example, server/config.local.json.
    3. Default configuration file; for example, server/config.json.

    In your model-config.json all models have datasource set to db so in your case LoopBack application loads first datasources.test.json. It cannot find datasource db there (only testdb), so it falls back to datasources.json. There it finds datasource db and it uses it. Try renaming testdb in datasources.test.json to db and it will take a precedense.