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}.EXTWhere:
EXTis the file extension, e.g.ts,js,json;instanceis theNODE_APP_INSTANCEenvironment variable;deploymentis theNODE_CONFIG_ENVorNODE_ENVenvironment variable;hostnameis theHOSTorHOSTNAMEenvironment variable, oros.hostname();short_hostnameis 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
| Option | Type | Description |
|---|---|---|
paths | string | string[] | Directory or directories to load files from. Required. |
adapters | AdapterSpecifier | AdapterSpecifier[] | Which adapter reads which file extensions. Required. |
Each AdapterSpecifier has:
| Field | Type | Description |
|---|---|---|
extensions | string[] | File extensions this adapter handles, e.g. ['.ts']. An extension can appear in only one specifier. |
adapterFactory | (path: string) => Adapter | Creates the adapter for one file. |
Also accepts the shared adapter options: regex, silent, keyMatching and transform.