Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@adonisjs/config

Package Overview
Dependencies
Maintainers
2
Versions
44
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@adonisjs/config

Config management for AdonisJS framework

  • 1.0.13
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
14K
decreased by-54.4%
Maintainers
2
Weekly downloads
 
Created
Source

Config

circleci-image npm-image license-image

Extremely simple module to decouple application config from the file system, which has handful of benefits.

  1. Can rely on more sources to feed configuration.
  2. Easy to define fake values during testing.
  3. A much nicer API to read nested values.

Table of contents

Usage

Install the package from npm as follows

npm i @adonisjs/config

# yarn
yarn add @adonisjs/config

and then use the class as follows:

import { Config } from '@adonisjs/config/build/standalone'

const initialConfiguration = {
  app: {
    name: 'adonis',
  },
  database: {
    connection: 'mysql',
  },
  logger: {
    level: 'debug',
  },
}

const config = new Config(initialConfiguration)

config.get('app.name') // adonis
config.get('database.connection') // mysql
config.get('database.user', 'root') // root

Usage with AdonisJs

The @adonisjs/core includes this module and hence there is no need to install it seperately. However, here are the instructions to setup the provider.

export const providers = [
  '@adonisjs/config',
]

After this, you have to register the typings file inside files array for Typescript to pick the ambient module.

All this hassle is required, since this module is never meant to be installed standalone.

tsconfig.json

{
  "files": ["./node_modules/@adonisjs/config/build/adonis-typings/config.d.ts"]
}

Why not simply create the config files?

Majority of projects create config files next to the source files or inside a dedicated config directory and require those files wherever needed.

However, with AdonisJs, we make the process of config management a little bit better over manually requiring config files and it has handful of benefits.

AdonisJs recommends to save all configuration inside the config directory and then behind the scenes it read those files and feed it's content to the Config class and later the application developer can get rid of importing config files and rely on the Config class instance instead.

Multiple config sources

We virtually decouple the config from the filesystem, which means your app can read the configuration from anywhere and pass it to the Config class. For example:

const { db } from 'some-db-module'
import { Config } from '@adonisjs/config'

const settings = await db.table('settings').select('*')

const config = new Config({}) // start with empty store
settings.forEach((row) => {
  config.set(row.key, JSON.parse(row.value))
})

Easy to fake during tests

Now since, you are not requiring the config files directly inside your application code, you can easily provide fake values during tests.

Config file
export const db = {
  connection: 'pg'
}
Config module
import { Config } from '@adonisjs/config'
import { db } from './config/database'

export default new Config({ db })
Application code
import { Db } from 'some-db-module'
import config from './config'

const db = new Db(config.get('db'))

class UserController {
  async store () {
    // perform insert
  }
}
Test code
import config from './config'
config.set('db.connection', 'sqlite')

// now run tests and connection will be sqlite over pg

Reading nested values

Reading nested values in Javascript isn't fun. You have to ensure that top level object is actually an object before accessing it's child.

However, with this module, you can pull nested values without worrying about the intermediate parents being undefined or null.

config.get('database.mysql.connection.host', '127.0.0.1')

The get method will return 127.0.0.1 if any of the parents or the value of host itself is non-existent.

API

Following are the autogenerated files via Typedoc

Keywords

FAQs

Package last updated on 13 Apr 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc