Skip to content

lithium-apps/extended-env

Repository files navigation

Contributors Forks Stargazers Issues License GitHub Build


Logo

@lithium/extended-env

Extend env module for AdonisJS

Request Feature · Report Bug · Request Modification

Table of Contents
  1. About The Project
  2. Install the package
  3. Use the package
  4. Contact

About the project

The environment module extend the behavior of the default AdonisJS env module by adding support for Scaleway Secrets.

We welcome contributions and improvements to this module. Don't hesitate to submit features and improvements ;)

(back to top)

Built With

  • TypeScript

(back to top)

Install the package

For now the package isn't published to npm, but you can install it from the GitHub registry and can be installed in any project.

  1. You need to create a .npmrc file at the root of your project with the following content:

    @lithium-apps:registry=https://npm.pkg.github.com
  2. For the login process you need to set a personal access token with the read:packages scope. Then you can login to the GitHub registry with the following command:

    pnpm login --registry=https://npm.pkg.github.com --scope=@lithium-apps
  3. You can now install the packages using the following command:

    pnpm install @lithium-apps/extended-env

(back to top)

Use the package

  1. Replace the import of the default AdonisJS env module by the extended one in start/env.ts:

    // import { Env } from '@adonisjs/core/env'; -- Replace this line
    import { Env } from '@lithium-apps/extended-env'; 
  2. Use the new modules when defining your schema:

    import { Env } from '@lithium-apps/extended-env'; 
    
    export default await Env.create(new URL('../', import.meta.url), {
        NODE_ENV: Env.schema.enum(['development', 'production', 'test'] as const),
        PORT: Env.schema.number(),
        APP_KEY: Env.schema.string(),
        HOST: Env.schema.string({ format: 'host' }),
        LOG_LEVEL: Env.schema.enum(['fatal', 'error', 'warn', 'info', 'debug', 'trace', 'silent']),
    
        /*
        |----------------------------------------------------------
        | Variables for configuring database connection
        |----------------------------------------------------------
        */
        SECRET_DATABASE: Env.scaleway.database.optional({
            host: 'DB_HOST',
            port: 'DB_PORT',
            username: 'DB_USER',
            password: 'DB_PASSWORD',
            dbname: 'DB_DATABASE',
        }),
    
        DB_HOST: Env.schema.string.optionalWhen(() => Env.scaleway.exists('SECRET_DATABASE'), { format: 'host' }),
        DB_PORT: Env.schema.number.optionalWhen(() => Env.scaleway.exists('SECRET_DATABASE')),
        DB_USER: Env.schema.string.optionalWhen(() => Env.scaleway.exists('SECRET_DATABASE')),
        DB_DATABASE: Env.schema.string.optionalWhen(() => Env.scaleway.exists('SECRET_DATABASE')),
        DB_PASSWORD: Env.schema.string.optional(),
    });

Contact

(back to top)