nest icon indicating copy to clipboard operation
nest copied to clipboard

Programatic Configuration: @Configuration class decorator

Open thomrick opened this issue 9 years ago • 6 comments

Like Spring does it could be great to allow programatic configuration classes.

These classes are responsible to load configuration properties and can be injected. For example theses classes could have access to file system property files (json, properties or what else) or remote ones like S3 bucket.

It could help to defined default server properties like default api path, listened ports or for remote access providers token. We also can imagine context property classes by module. A module that is not a sub module where define configuration class is can't be injected in modules classes.

It also could be awesome to have something like Spring Load Config with to hot server reload with external configuration change hooks.

thomrick avatar May 09 '17 21:05 thomrick

I started to work on @Configuration For now I have something like this: Decorator:

import 'reflect-metadata';
import {ConfigurationMetadata} from '../../interfaces/configuration-metadata.interface';
import {isNil, isObject} from '../shared.utils';
import {FILE_METADATA, IN_MEMORY_METADATA, URL_METADATA} from '../../constants';

export const Configuration = (metadata?: ConfigurationMetadata): ClassDecorator => {
  return isObject(metadata) ? ConfigurationFrom(metadata) : InMemoryConfiguration();
};

const InMemoryConfiguration = (): ClassDecorator => {
  return (target: object) => {
    Reflect.defineMetadata(IN_MEMORY_METADATA, true, target);
  }
};

const ConfigurationFrom = (metadata: ConfigurationMetadata): ClassDecorator => {
  if (!isNil(metadata.file))
    return FileConfiguration(metadata.file);
  else if (!isNil(metadata.url))
    return RemoteConfiguration(metadata.url);
};

const FileConfiguration = (filename: string): ClassDecorator => {
  return (target: object) => {
    Reflect.defineMetadata(FILE_METADATA, filename, target);
  }
};

const RemoteConfiguration = (url: string): ClassDecorator => {
  return (target: object) => {
    Reflect.defineMetadata(URL_METADATA, url, target);
  }
};

Helper classes:

export interface IConfiguration {
  getProperty(name: string): string;
}

export abstract class AbstractConfiguration implements IConfiguration {
  constructor(protected properties: {[key: string]: string} = {}) {}
  public getProperty(name: string): string {
    return this.properties[name];
  }
}

export abstract class AbstractInMemoryConfiguration extends AbstractConfiguration{
  constructor() {
    super();
    this.declare();
  }
  protected abstract declare(): void;
}

And a use case:

class TestConfiguration extends AbstractInMemoryConfiguration {
    protected declare(): void {
      this.properties['KEY'] = 'VALUE';
    }
  }

Regarding the @Configuration decorator I can declare it like this:

@Configuration()
class TestConfiguration extends AbstractInMemoryConfiguration {  }

@Configuration(file: 'filename')
class TestConfiguration extends AbstractFileConfiguration {  }

@Configuration(url: 'http://domain/path')
class TestConfiguration extends AbstractRemoteConfiguration {  }

Does it seem to be good for you guys ?

thomrick avatar May 15 '17 17:05 thomrick

Hi @ThomRick, Thank you so much for the invested time, but I just don't want to exaggerate. Maybe in the future, but not now :slightly_smiling_face: Kamil

kamilmysliwiec avatar Sep 15 '17 21:09 kamilmysliwiec

@kamilmysliwiec could you leave it open? It's very nice feature.

cojack avatar Sep 16 '17 06:09 cojack

Are you sure we really need the abstraction layer over configuration stuff? I think now it's easy to fetch the remote configuration / load config using fs package just before creating the Nest instance. If the configuration affects components and should be a part of the DI container - just use async components. What @ThomRick proposed looks really good, but I'm trying to keep in mind that learning curve is already big enough :smiley:

kamilmysliwiec avatar Sep 16 '17 07:09 kamilmysliwiec

You convinced me.

cojack avatar Sep 16 '17 09:09 cojack

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Sep 25 '19 11:09 lock[bot]