Custom Configuration Parameters in AdonisJS

AdonisJS stores its configuration values in config folder in root. For e.g. you will set database environment values in database.js, authentication params in auth.js and so on.

You can update or set new configuration parameters required by your application in those files. You could also create a new configuration file for your application variables and refer to those values from services, controllers, or from any place where it makes sense.

Let’s see how -

  • Create a new file called todo.js under config folder
  • Introduce your predefined values in the file

    "use strict";
    /** @type {import('@adonisjs/framework/src/Env')} */
    const Env = use("Env");
    module.exports = {
    todo: {
      endDays: 10
    }
    };
    
  • Use your values from anywhere - just refer to todo.todo.endDays

    todo.end_date = todayDate + todo.todo.endDays;
    

I used todo.todo because -

  • the first todo refers to the file, the second refers to the variable group name within the file
  • there may be a different entity using the same todo.js file. So it is may be a good idea to group entity-specific configuration params under the entity name
comments powered by Disqus