Skip to Content
Zod Config

Directory adapter

Load configuration from a directory of files, ordered by environment and host.

Loads configuration from a directory containing multiple configuration files, usually together with the script adapter or another file adapter. Inspired by node-config, the files in the config directory are loaded in the following order:

default.EXT
default-{instance}.EXT
{deployment}.EXT
{deployment}-{instance}.EXT
{short_hostname}.EXT
{short_hostname}-{instance}.EXT
{short_hostname}-{deployment}.EXT
{short_hostname}-{deployment}-{instance}.EXT
{hostname}.EXT
{hostname}-{instance}.EXT
{hostname}-{deployment}.EXT
{hostname}-{deployment}-{instance}.EXT
local.EXT
local-{instance}.EXT
local-{deployment}.EXT
local-{deployment}-{instance}.EXT

Where:

  • EXT is the file extension, e.g. ts, js, json;
  • instance is the NODE_APP_INSTANCE environment variable;
  • deployment is the NODE_CONFIG_ENV or NODE_ENV environment variable;
  • hostname is the HOST or HOSTNAME environment variable, or os.hostname();
  • short_hostname is the first part of the hostname.

This adapter is useful when you keep the configuration for each environment in version control.

Install

No extra dependency. The directory adapter is asynchronous, so use it with loadConfig.

Usage

import {  } from 'zod';
import {  } from 'zod-config';
import {  } from 'zod-config/directory-adapter';
import {  } from 'zod-config/script-adapter';
import path from 'path';

const  = .({
  : .().(/^\d+$/),
  : .(),
});

const  = [path.(, 'config-dir')];

const  = await ({
  : ,
  : ({
    : ,
    : [
      {
        // Restrict adapter to handle only ts files
        : ['.ts'],
        // Use the scriptAdapter for handling .ts files
        : (: string) =>
          ({
            : ,
          }),
      },
      // {
      //  Add here other adapters for other file types if needed
      // }
    ],
  }),
});

Options

OptionTypeDescription
pathsstring | string[]Directory or directories to load files from. Required.
adaptersAdapterSpecifier | AdapterSpecifier[]Which adapter reads which file extensions. Required.

Each AdapterSpecifier has:

FieldTypeDescription
extensionsstring[]File extensions this adapter handles, e.g. ['.ts']. An extension can appear in only one specifier.
adapterFactory(path: string) => AdapterCreates the adapter for one file.

Also accepts the shared adapter options: regex, silent, keyMatching and transform.

On this page