npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@goodandco/nest-config

v2.0.1

Published

Config package

Downloads

3

Readme

Installation

$ npm install @goodandco/nest-config

Using

First add this line to the top of yours main.ts file:


global.__baseDir = __dirname.replace('/dist', '');

If you have different root dir for your compiled code, then specify related directory instead of /dist

set up configuration yaml in <projectRoot/config/config.yaml. This is the root config file. According to NODE_ENV and existance of <projectRoot>/config/config.<NODE_ENV>.yaml they will be merged with NODE_ENV's config prefer.

Your yaml config file <projectRoot>/config/config.yaml

app:
  mongo:
    connection:
      type: 'mongodb'
      url: 'mongodb://${MONGO_USERNAME|root}:${MONGO_PASSWORD|root}@${MONGO_HOSTNAME|localhost}:27017/${MONGO_DATABASE|scheduler}?authSource=admin'
      useUnifiedTopology: false
      useNewUrlParser: true

This syntax means that it if MONGO_USERNAME for example is set up, it will use value from it, instead it going to use value after | sign. It this case it is root.

In case of using production and other specific environments it is possible to add related config file <projectRoot>/config/config.production.yaml.

In this case you have to run your application with related NODE_ENV=production. Module merges env related config into common config: config.production.yaml into config.yaml.

So, lets imaging that our production mongo config contains different attributes:

    retryWrites: false
    replicaSet: 'rs0'
    readPreference: 'secondaryPreferred'
    sslCA: '/path/to/my/rds-combined-ca-bundle.pem'

So <projectRoot>/config/config.production.yaml will look like:

app:
  mongo:
    connection:
      retryWrites: false
      replicaSet: 'rs0'
      readPreference: 'secondaryPreferred'
      sslCA: '/path/to/my/rds-combined-ca-bundle.pem'

Other props it takes from basic config file <projectRoot>/config/config.yaml.

Don't forget to add this into your main.ts:

global.__baseDir = __dirname.replace('/dist', '');

Your app.module.ts:


import { ConfigService } from '@nestjs/config';
import { ConfigModule  } from '@goodandco/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { MongoConnectionOptions } from 'typeorm/driver/mongodb/MongoConnectionOptions';
import { TConfig } from 'src/types/config';

@Module({
  imports: [
    ConfigModule.forRoot<TConfig>({}),
    TypeOrmModule.forRootAsync({
      useFactory: (configService: ConfigService) => ({
        ...configService.get<MongoConnectionOptions>('app.mongo.connection'),
        entities: [],
      }),
      imports: [ConfigModule],
      inject: [ConfigService],
    })

In the same way you be able to use ConfigService from any part of your app.

Your be able to specify type of your config structure. Here in example it is described in TConfig definition. Or you can use any or Record<string, any>. Both these options are not recommended.

Once you load your config it stores as singleton and will use afterwards during ConfigService initiation. Also there are could be cases when you need access to your config in decorator level. So it means that config required before the app has been inited. For that reasons you can use next approach with ConfigLoader.config method:

import { TConfig } from '../shared';
import { ConfigLoader } from '@goodandco/nest-config'
const {
  app: {
    consumer: { topic: kafkaTopic },
  },
} = ConfigLoader.config<TConfig>();

@Controller('consumer')
export class ConsumerController {
  ...
  
  @MessagePattern(kafkaTopic)
  async consume(
    @Payload() dto: DTO
  ) {
    try {
      await this.consumerService.process(dto);
   ...
}

License

Nest-Config is MIT licensed.