DEV Community

Chintan Shah
Chintan Shah

Posted on

Stop Writing Database Configs From Scratch: envconfig-kit v0.2.0

I wrote about making your app crash at startup instead of in production. That post was about envconfig-kit v0.1.0.

Here's what's new in envconfig-kit v0.2.0.

What's New

Framework Presets

Pre-configured schemas for common patterns. Stop writing the same database, Redis, and auth configs over and over:

import { env, presets } from 'envconfig-kit'

const config = env({
  ...presets.server(),      // PORT, HOST, NODE_ENV
  ...presets.database(),   // DATABASE_URL, DATABASE_HOST, etc.
  ...presets.redis(),      // REDIS_URL, REDIS_HOST, etc.
  ...presets.auth(),       // AUTH_JWT_SECRET, etc.
  ...presets.aws(),        // AWS_ACCESS_KEY_ID, etc.
  ...presets.smtp(),       // SMTP_HOST, SMTP_PORT, etc.
  ...presets.cors(),       // CORS_ORIGINS, CORS_METHODS, etc.
})
Enter fullscreen mode Exit fullscreen mode

Custom prefixes work too:

...presets.database('DB_')  // DB_URL, DB_HOST, etc.
...presets.redis('CACHE_')  // CACHE_URL, CACHE_HOST, etc.
Enter fullscreen mode Exit fullscreen mode

New Validators

Enum Type - Type-safe environment values:

const config = env({
  NODE_ENV: { 
    type: 'enum', 
    values: ['development', 'staging', 'production'] as const,
    default: 'development'
  },
})
// config.NODE_ENV is typed as 'development' | 'staging' | 'production'
Enter fullscreen mode Exit fullscreen mode

Array Type - Parse comma-separated values:

const config = env({
  ALLOWED_HOSTS: { type: 'array' },  // "a,b,c" → ["a", "b", "c"]
  TAGS: { type: 'array', separator: '|' },  // custom separator
})
Enter fullscreen mode Exit fullscreen mode

JSON Type - Parse JSON strings:

const config = env({
  FEATURE_FLAGS: { type: 'json' },
})
// FEATURE_FLAGS='{"darkMode":true}' → { darkMode: true }
Enter fullscreen mode Exit fullscreen mode

Regex Type - Custom pattern validation:

const config = env({
  API_VERSION: { 
    type: 'regex', 
    pattern: /^v\d+\.\d+$/ 
  },
})
// Only accepts "v1.0", "v2.1", etc.
Enter fullscreen mode Exit fullscreen mode

Integer Type - Whole numbers only:

const config = env({
  PORT: { type: 'integer' },  // Stricter than 'number'
})
Enter fullscreen mode Exit fullscreen mode

Secret Masking

Mark secrets and they'll be masked automatically in error messages:

const config = env({
  API_KEY: { type: 'string', secret: true },
  DATABASE_PASSWORD: { type: 'string', secret: true },
})
Enter fullscreen mode Exit fullscreen mode

When validation fails, error messages show API_KEY=**** instead of the actual value. No more accidentally logging secrets.

Custom Validation

Add your own validation logic:

const config = env({
  PORT: { 
    type: 'number',
    validate: (value) => value >= 1024 && value <= 65535
  },
  MAX_CONNECTIONS: {
    type: 'integer',
    validate: (value) => value > 0 && value <= 1000,
    default: 100
  },
})
Enter fullscreen mode Exit fullscreen mode

Real-World Example

Here's how you'd configure a typical API with v0.2.0:

import { env, presets } from 'envconfig-kit'

export const config = env({
  // Server (preset)
  ...presets.server(),

  // Database (preset)
  ...presets.database(),

  // Redis (preset)
  ...presets.redis(),

  // Auth (preset)
  ...presets.auth(),

  // External services
  STRIPE_KEY: { type: 'string', secret: true },
  SENDGRID_KEY: { type: 'string', secret: true },

  // CORS (preset)
  ...presets.cors(),

  // Feature flags with enum
  FEATURE_MODE: {
    type: 'enum',
    values: ['basic', 'premium', 'enterprise'] as const,
    default: 'basic'
  },

  // Custom validation
  MAX_CONNECTIONS: {
    type: 'integer',
    validate: (value) => value > 0 && value <= 1000,
    default: 100
  },
})
Enter fullscreen mode Exit fullscreen mode

That's it. Everything is typed, validated, and your app crashes at startup if anything is wrong.

Breaking Changes

None. All new features are additive and backward compatible.

Install

npm install envconfig-kit@0.2.0
Enter fullscreen mode Exit fullscreen mode

Or update existing:

npm install envconfig-kit@latest
Enter fullscreen mode Exit fullscreen mode

What's Next

Still zero dependencies. Still ~3KB. Still works with CommonJS and ESM.


GitHub | npm

Top comments (2)

Collapse
 
isocyanideisgood profile image
Abhijeet Bhale

This seems to be very useful, might use it in some on my project 👍🏻

Collapse
 
chintanshah35 profile image
Chintan Shah

Thanks! Hope it saves you some time. Feel free to reach out if you have any questions or feedback.