ez-dev-tools-tj
v1.1.0
Published
A comprehensive Node.js utility library.
Downloads
9
Readme
EZDevTools
EZDevTools is a powerful Node.js utility library designed to simplify and enhance development by providing a comprehensive suite of tools, including state management, task queuing, logging, data handling, configuration management, caching, metrics collection, and more.
Features
- State Management: Manage application state with validation, middleware support, and more.
- Task Queuing: Manage and process tasks with priority and retry mechanisms.
- Logging: Flexible logging with support for multiple transports (file, HTTP).
- Data Handling: Transform, filter, validate, and fetch data with ease.
- Configuration Management: Dynamic and layered configuration management.
- Caching: Simple in-memory or Redis-based caching with TTL support.
- Metrics Collection: Collect and expose metrics for monitoring and alerting.
- Utility Functions: Useful utility functions such as UUID generation, file handling, and more.
- Database Connectivity: Easy connections to MongoDB, PostgreSQL, MySQL, Redis, and Sequelize ORM.
- Integration with External Services: Integrate with external services like Sentry, ELK, Prometheus, AWS, Google Cloud, and Azure.
- Internationalization: Simple i18n support using
i18next
. - Documentation Generation: Auto-generate documentation using
jsdoc
.
Installation
To install the package, run the following command:
npm install ez-dev-tools-js
Usage
Below are some examples of how to use the different modules provided by EZDevTools:
1. State Management (EZState)
const { EZState } = require('ez-dev-tools-js');
const state = new EZState();
state.setState({ key: 'value' });
console.log(state.getState()); // { key: 'value' }
2. Task Queuing (EZQueue)
const { EZQueue } = require('ez-dev-tools-js');
const queue = new EZQueue();
queue.enqueue(async () => { console.log('Task 1'); });
queue.enqueue(async () => { console.log('Task 2'); });
queue.processQueue();
queue.on('taskComplete', task => { console.log('Task completed', task); });
queue.on('taskError', error => { console.error('Task error', error); });
3. Logging (EZLogger)
const { EZLogger } = require('ez-dev-tools-js');
const logger = new EZLogger();
logger.info('This is an info message');
// Adding transports
logger.addTransport({ type: 'file', filename: 'logs.txt' });
logger.addTransport({ type: 'http', url: 'http://example.com/logs' });
4. Data Handling (EZData)
const { EZData } = require('ez-dev-tools-js');
const data = EZData.transform([1, 2, 3], x => x * 2);
console.log(data); // [2, 4, 6]
EZData.fetchData('https://api.example.com/data').then(data => console.log(data));
const validData = EZData.validate({ key: 'value' }, { key: Joi.string().required() });
console.log(validData);
5. Configuration Management (EZConfig)
const { EZConfig } = require('ez-dev-tools-js');
const config = new EZConfig();
config.setConfig('key', 'value');
console.log(config.getConfig('key')); // 'value'
config.watchConfig(newConfig => { console.log('Config changed', newConfig); });
6. Caching (EZCache)
const { EZCache } = require('ez-dev-tools-js');
const cache = new EZCache({ store: 'redis', redisConfig: { host: 'localhost', port: 6379 } });
cache.set('key', 'value');
cache.get('key').then(value => console.log(value)); // 'value'
cache.delete('key');
cache.clear();
7. Metrics Collection (EZMetrics)
const { EZMetrics } = require('ez-dev-tools-js');
const metrics = new EZMetrics();
const counter = metrics.createCounter('requests', 'Number of requests');
counter.inc();
metrics.getMetrics().then(metrics => console.log(metrics));
8. Utility Functions (EZUtils)
const { EZUtils } = require('ez-dev-tools-js');
console.log(EZUtils.generateUUID());
await EZUtils.delay(1000);
const obj = { a: 1, b: { c: 2 } };
const clone = EZUtils.deepClone(obj);
console.log(clone);
const filePath = 'test.txt';
EZUtils.writeFile(filePath, 'Hello, world!');
console.log(EZUtils.readFile(filePath));
9. Database Connectivity (EZDatabase)
const { EZDatabase } = require('ez-dev-tools-js');
// MongoDB connection
const db = await EZDatabase.connectMongo('mongodb://localhost:27017', 'testdb');
// PostgreSQL connection
const pool = await EZDatabase.connectPostgres({ user: 'user', host: 'localhost', database: 'testdb', password: 'pass', port: 5432 });
// MySQL connection
const connection = await EZDatabase.connectMySQL({ host: 'localhost', user: 'user', database: 'testdb', password: 'pass' });
// Redis connection
const redisClient = EZDatabase.connectRedis({ host: 'localhost', port: 6379 });
// Sequelize connection
const { sequelize, DataTypes } = EZDatabase.connectSequelize({ dialect: 'sqlite', storage: ':memory:' });
const User = sequelize.define('User', {
username: DataTypes.STRING,
birthday: DataTypes.DATE
});
await sequelize.sync();
10. Integration with External Services (EZIntegration)
const { EZIntegration } = require('ez-dev-tools-js');
// Sending error to Sentry
await EZIntegration.sendToSentry('YOUR_SENTRY_DSN', new Error('Test error'));
// Sending logs to ELK
await EZIntegration.sendToELK('http://localhost:9200/logs', { message: 'Test log' });
// Sending metrics to Prometheus
await EZIntegration.sendToPrometheus('http://localhost:9091/metrics', { metric: 'Test metric' });
// Uploading files to AWS S3
await EZIntegration.uploadToS3('my-bucket', 'my-key', 'file content');
// Uploading files to Google Cloud Storage
await EZIntegration.uploadToGCS('my-bucket', 'my-filename', 'path/to/local/file');
// Uploading files to Azure Blob Storage
await EZIntegration.uploadToAzure('my-container', 'my-blob', 'path/to/local/file');
11. Internationalization (EZI18n)
const { EZI18n } = require('ez-dev-tools-js');
// Initializing i18n
await EZI18n.init({
lng: 'en',
resources: {
en: {
translation: {
key: 'value'
}
}
}
});
// Translating a key
console.log(EZI18n.t('key')); // 'value'
// Changing language
EZI18n.changeLanguage('es');
console.log(EZI18n.t('key')); // 'valor' (if translation exists)
12. Documentation Generation (EZDocs)
const { EZDocs } = require('ez-dev-tools-js');
// Generating documentation
EZDocs.generateDocs(['src/EZState/index.js'], 'docs');