Gathering detailed insights and metrics for @megaorm/config
Gathering detailed insights and metrics for @megaorm/config
Gathering detailed insights and metrics for @megaorm/config
Gathering detailed insights and metrics for @megaorm/config
npm install @megaorm/config
Typescript
Module System
Node Version
NPM Version
TypeScript (99.74%)
JavaScript (0.26%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
1 Commits
1 Watchers
1 Branches
1 Contributors
Updated on Dec 18, 2024
Latest Version
1.1.0
Package Id
@megaorm/config@1.1.0
Unpacked Size
67.41 kB
Size
11.70 kB
File Count
6
NPM Version
10.8.3
Node Version
20.16.0
Published on
Dec 18, 2024
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
5
This package is designed to help with loading, managing, validating, and ensuring the existence of configuration files in a Node.js project root.
To install this package, run the following command:
1npm install @megaorm/config
This will install the package and make the Config
class available for use in your Node.js project.
Config
class to load a custom configuration file.1const { Config } = require('@megaorm/config'); 2 3class MyConfig extends Config { 4 static file = 'myconfig.json'; // Custom config file 5 static default = { key: 'value' }; // Default configuration 6} 7 8module.exports = { MyConfig };
load
method loads the configuration file (.json
or .js
), applying any registered validators, and uses the default configuration if the file is missing.1const { MyConfig } = require('./MyConfig'); 2 3MyConfig.load().then((config) => console.log(config));
This method looks for the configuration file in your root folder, loads it, and caches it. The next time you execute
load
, it will resolve with the cached configuration.
1const { MyConfig } = require('./MyConfig'); 2const { ConfigError } = require('@megaorm/config'); 3 4MyConfig.register((config) => { 5 if (!config.option) { 6 throw new ConfigError('This option is required'); 7 } 8 9 // You must always return the config for the next validator 10 return config; 11});
Validators ensure your configuration is valid. You can use them to assign default values or throw an error if a required option is missing or the type is invalid.
reload()
method.1const { MyConfig } = require('./MyConfig'); 2 3MyConfig.reload().then((config) => console.log(config));
Use this method to reload the original config and cache it again. It's useful in case you made changes to your config object and decided to load the original one.
resolveSync()
and resolve()
methods resolve the project root directory. They work by traversing backward from the current working directory to locate your project's root directory.1const { MyConfig } = require('./MyConfig'); 2 3console.log(MyConfig.resolveSync()); // Outputs your project root 4 5MyConfig.resolve().then((root) => console.log(root)); // Outputs the project root
Both of these methods cache the absolute path to your project root and return it whenever you need it.
exist(path)
checks if a file or directory exists at a given path.mkdir(path)
ensures a directory exists, creating it if necessary.mkfile(path, content)
ensures a file exists and creates it with the specified content if necessary.1const { MyConfig } = require('./MyConfig'); 2const { resolve } = require('path'); 3 4// Project root 5const root = MyConfig.resolveSync(); 6 7// Ensure `myConfig.json` file exists 8MyConfig.exist(resolve(root, 'myConfig.json')).then(() => 9 console.log('My Config exists in the root directory') 10); 11 12// Ensure the `config` directory exists in the root folder 13MyConfig.mkdir(resolve(root, 'config')).then(() => 14 console.log('Directory created or already exists') 15); 16 17// Ensure `db.json` exists in the `config` directory 18MyConfig.mkfile(resolve(root, 'config/db.json'), '{"database": "main"}').then( 19 () => console.log('Config file created or already exists') 20);
You can also use
existMany(paths)
to ensure multiple paths exist.
validate
method runs all registered validators on the configuration object. It ensures the configuration is in the correct state before being used.1const { MyConfig } = require('./MyConfig'); 2const { resolve } = require('path'); 3 4// Register Validators 5MyConfig.register((config) => { 6 // Ensure config is an object 7 if (typeof config !== 'object') { 8 throw new Error('Invalid configuration'); 9 } 10 11 // Return the config object for the next validator 12 return config; 13}); 14 15MyConfig.register((config) => { 16 // Ensure the name option is defined and valid 17 if (typeof config.name !== 'string') { 18 throw new Error('Invalid name'); 19 } 20 21 // Return the config object for the next validator 22 return config; 23}); 24 25// Resolve the root 26const root = MyConfig.resolveSync(); 27 28// Load JSON Config 29MyConfig.loadJSON(resolve(root, 'test.json')).then((config) => { 30 // Validate the config 31 console.log(MyConfig.validate(config)); 32});
The
load
andreload
methods execute thevalidate
method after loading or reloading the configuration the first time to ensure your configuration is valid.
.js
configuration file..json
configuration file.1const { MyConfig } = require('./MyConfig');
2const { resolve } = require('path');
3
4// Resolve the root
5const root = MyConfig.resolveSync();
6
7// Load JS configuration
8MyConfig.loadJS(resolve(root, 'config.js')).then((config) =>
9 console.log(config)
10);
11
12// Load JSON configuration
13MyConfig.loadJSON(resolve(root, 'config.json')).then((config) =>
14 console.log(config)
15);
This code demonstrates how I implemented the MegaConfig class in MegaORM. It provides an overview of how the Config class should be used.
1import { Config, ConfigError } from '@megaorm/config';
2import { isBool, isChildOf, isObj, isStr } from '@megaorm/test';
3import { MegaCluster } from '@megaorm/core/MegaCluster';
4
5
6/**
7 * MegaConfig is a specialized configuration manager for MegaORM.
8 * It handles validation and defaulting for various configuration properties,
9 * including paths, TypeScript settings, and cluster details.
10 */
11export class MegaConfig extends Config {
12 /**
13 * The default name of the configuration file.
14 */
15 protected static file: string = 'mega.config.js';
16}
17
18/**
19 * Ensures the configuration is an object before proceeding.
20 */
21MegaConfig.register((config: MegaORMConfig) => {
22 if (!isObj(config)) {
23 throw new ConfigError(
24 `Invalid config: Expected an object but received ${typeof config}.`
25 );
26 }
27 return config;
28});
29
30/**
31 * Ensures that `config.cluster` is an instance of `MegaCluster`.
32 */
33MegaConfig.register((config: MegaORMConfig) => {
34 if (!isChildOf(config.cluster, MegaCluster)) {
35 throw new ConfigError(
36 `Invalid config.cluster: Expected an instance of MegaCluster but received ${typeof config.cluster}.`
37 );
38 }
39 return config;
40});
41
42/**
43 * Ensures that `config.default` is a string.
44 */
45MegaConfig.register((config: MegaORMConfig) => {
46 if (!isStr(config.default)) {
47 throw new ConfigError(
48 `Invalid config.default: Expected a valid default pool name but received ${typeof config.default}.`
49 );
50 }
51 return config;
52});
53
54/**
55 * Ensures `config.paths` is an object.
56 */
57MegaConfig.register((config: MegaORMConfig) => {
58 if (!isObj(config.paths)) config.paths = {};
59
60 return config;
61});
62
63/**
64 * Set default values for the `paths` property in the configuration.
65 */
66MegaConfig.register((config: MegaORMConfig) => {
67 if (!isStr(config.paths.models)) config.paths.models = 'models';
68 if (!isStr(config.paths.seeders)) config.paths.seeders = 'seeders';
69 if (!isStr(config.paths.commands)) config.paths.commands = 'commands';
70 if (!isStr(config.paths.generators)) config.paths.generators = 'generators';
71
72 return config;
73});
74
75/**
76 * Ensures `config.typescript` is an object.
77 */
78MegaConfig.register((config: MegaORMConfig) => {
79 if (!isObj(config.typescript)) config.typescript = {} as any;
80
81 return config;
82});
83
84/**
85 * Set default values for the `typescript` property in the configuration.
86 */
87MegaConfig.register((config: MegaORMConfig) => {
88 if (!isBool(config.typescript.enabled)) config.typescript.enabled = false;
89 if (!isStr(config.typescript.src)) config.typescript.src = 'src';
90 if (!isStr(config.typescript.dist)) config.typescript.dist = 'dist';
91
92 return config;
93});
The Config
class provides a comprehensive and flexible way to manage configuration files while ensuring they are validated, loaded efficiently, and accessible throughout your Node.js project.
No vulnerabilities found.
No security vulnerabilities found.