23.47.2 • Published 5 months ago

@digital-alchemy/boilerplate v23.47.2

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

@digital-alchemy/boilerplate

Description

This library is the required entrypoint for using the rest of the libraries published from this repository. Boilerplate provides a batteries included bootstrapping workflow for NestJS applications, as well as several utility classes.

Extra Reading

LinkDescription
LoggerAPI documentation for AutoLogService
Usage exampleSmall terminal app in an external repo utilizing this boilerplate
Lifecycle eventsStartup lifecycle event flow for boilerplate & nestjs

Bootstrap

@QuickScript minimal applications

The @QuickScript annotation is based off the NestJS @Module annotation, and it represents the code entrypoint for the application. When the code is run, the @QuickScript annotation will automatically bootstrap an application, and run the exec method if provided.

This annotation is intended for more minimal applications. A few files, all business logic and no specialized build needs. More complex applications should utilize the manual bootstrapping flow in order to get more customization.

import { QuickScript } from "@digital-alchemy/boilerplate";

@QuickScript()
class ExampleScript {
  exec() {
    console.log("Hello world");
  }
}

Manual bootstrap

The manual bootstrapping process requires the manual definition of a root application module separate from the Bootstrap call. @ApplicationModule should be used to annotate the root module.

import { ApplicationModule } from "@digital-alchemy/boilerplate";

@ApplicationModule({
  application: "my-application",
  controllers: [...],
  imports: [...],
  providers: [...]
})
export class MyApplication {}

// Provide different bootstrapping options based on build environment files
Bootstrap(MyApplication, {
  config: { ... },
  http: false,
  logging:{
    prettyLog: true
  }
})

Configuration

The boilerplate library has an opinionated configuration system that is based off rc. It's intended to provide an EVEN LAZIER way of configuration applications working through the NestJS direct injection system. The @InjectConfig annotation can be used to retrieve configuration values for providers.

Some metadata is should be provided to help the configuration system. In smaller applications, it is convenient to provide this inline with the @InjectConfig annotation. Larger applications, and libraries should place metadata definitions at the module level so the information is central and can be reused.

Inline definition

import { QuickScript } from "@digital-alchemy/boilerplate";

@QuickScript()
class ExampleScript {
  constructor(
    @InjectConfig("SPECIAL_STRING", {
      type: "string",
      description: "A human readable description for why this exists",
      default: "so special"
    }) private readonly specialString: string
  ){}
}

Module level definition

import { QuickScript } from "@digital-alchemy/boilerplate";

@QuickScript({
  configuration: {
    SPECIAL_STRING: {
      type: "string",
      description: "A human readable description for why this exists",
      default: "so special"
    }
  }
})
class ExampleScript {
  constructor(
    @InjectConfig("SPECIAL_STRING") private readonly specialString: string
  ){}
}

Value resolution

The configuration loader merges values on a per-property basis. Values provided in sources lower in this list override values from higher.

  • Inline constructor definitions
    • private readonly prop: string = "hello world"
  • Metadata definitions
  • Bootstrap overrides
  • File based configurations
    • /etc/{name}/config
    • /etc/{name}/config.json
    • /etc/{name}/config.ini
    • /etc/{name}/config.yaml
    • /etc/{name}rc
    • /etc/{name}rc.json
    • /etc/{name}rc.ini
    • /etc/{name}rc.yaml
    • cwd()/.{name}rc
    • Recursively to system root: cwd()/../.{name}rc
    • ~/.config/{name}
    • ~/.config/{name}.json
    • ~/.config/{name}.ini
    • ~/.config/{name}.yaml
    • ~/.config/{name}/config
    • ~/.config/{name}/config.json
    • ~/.config/{name}/config.ini
    • ~/.config/{name}/config.yaml
  • Environment variables
  • Command line switches

Configuration options

The following configuration options are utilized by this library.

; default configuration
[libs.boilerplate]
  CACHE_PROVIDER=memory
  CACHE_TTL=86_400
  LOG_LEVEL=info
  CACHE_HOST=localhost
  CACHE_PORT=6379

CACHE_PROVIDER

Cache storage provider. default: memory

optionnote
memorystore in memory of node process, cleared when process dies
redisconnect to an external redis instance, data will persist after process dying

CACHE_TTL

Configuration property for redis connection

LOG_LEVEL

Minimum log level to output. default: info

  • silent
  • info
  • warn
  • debug
  • error

CACHE_HOST

Address for redis instance, default: localhost

CACHE_PORT

Port for redis instance, default: 6379

Switches

Flags and switches that are intended to be used on the command line when launching the app.

SCAN_CONFIG

# Usage example
node ./dist/apps/sampler-app/main.js --scan-config | tee ./configuration.json

Scan all modules and providers to gather all available configuration metadata, output as json, and exit. Implemented for use with the config-builder app

CONFIG

# Usage example
node ./dist/apps/sampler-app/main.js --config /path/to/config/file

If provided, the automatic configuration file lookup & merge process will be skipped in favor of loading this single file. All rules relating to environment variables and other command line switches will operate normally.

23.34.2

8 months ago

23.40.0

7 months ago

23.40.2

7 months ago

23.40.3

7 months ago

23.40.5

7 months ago

23.40.6

7 months ago

23.27.1

10 months ago

23.27.0

10 months ago

23.39.0

7 months ago

23.39.2

7 months ago

23.39.1

7 months ago

23.47.0

5 months ago

23.47.1

5 months ago

23.47.2

5 months ago

23.28.0

10 months ago

23.28.1

10 months ago

23.17.0

1 year ago

23.16.1

1 year ago

23.16.0

1 year ago

23.19.0

12 months ago

23.17.2

1 year ago

23.16.3

1 year ago

23.18.0

12 months ago

23.17.1

1 year ago

23.16.2

1 year ago

23.17.4

1 year ago

23.19.1

12 months ago

23.17.3

1 year ago

23.16.4

1 year ago

23.17.5

1 year ago

23.19.1-dev.0

12 months ago

23.19.1-dev.1

12 months ago

23.20.0

12 months ago

23.21.0

11 months ago

23.25.0

11 months ago

23.13.1

1 year ago

23.15.0

1 year ago

23.14.1

1 year ago

23.14.0

1 year ago

23.14.3

1 year ago

23.14.2

1 year ago

23.13.3

1 year ago

23.14.4

1 year ago

23.13.4

1 year ago

23.13.0

1 year ago

23.12.4

1 year ago

23.12.3

1 year ago

23.12.2

1 year ago

23.12.1

1 year ago

23.12.0

1 year ago