Configuration

Application wide config is stored in the src/config directory.

.
├── config
│   ├── registry.ts
│   ├── bindings.ts
│   ├── constants.ts
│   ├── container.ts
│   ├── env.ts
│   └── interfaces.ts
.

Some files are optional, so take the time to learn which ones suit your project and when to use them.



registry.ts

(optional) A registry file is required when we need to wait for a value to resolve such as database connection. This augments our already defined containter.ts

bindings.ts (required)

This file is required to get around the classes as identifiers problem.

/**
 * ($b)inding/s
 */
export const $b = {
  //
  Environment: Symbol.for('EnvironmentConfig'),
  // Cassandra: Symbol.for('Cassandra'),
  // Mongo: Symbol.for('Mongo'),

  // Repositories
  UsersRepository: Symbol.for('UsersRepository')
}

constants.ts (optional)

If your service has any top level types, they should probably go in here.

export enum UserRolesOptions {
  ADMINISTRATOR = 'administrator',
  USER = 'default'
}

container.ts (required)

InversifyJS provides a feature rich Dependency Inversion libary. It’s worth reading over

Don’t fret, the starter repos come with a couple of stubs to help you on your feet.

env.ts (required)

Quote

A litmus test for whether an app has all config correctly factored out of the > code is whether the codebase could be made open source at any moment, without > compromising any credentials. (Source: https://12factor.net/config)

Enso relies on dotenv-safe to provide this functionality.

A great feature of this libary is that it compares your .env to a commited .env.example file. This helps surface any enviroment configuration issues much faster when working on a rapidly evolving codebase.

Check out a sample env.ts from the step by step install.

interfaces.ts (require)

Similar to constants.ts if we have any top level interfaces, Or if we want to supplement any base interfaces, they should probably live in this file.

export interface IEnvironmentConfig {
  ENVIRONMENT: string
  PORT: number
  JWT_KEY_SALT: string
  JWT_KEY_SECRET: string
}

Additional reading

  • Martin Fowler, Patterns of Enterprise Application Architecture

References