evp-catalyst
v1.0.1
Published
This package provides a collection of shared utilities for Node.js applications, designed to standardize common tasks like logging, event dispatching, and database interactions.
Readme
EVP Catalyst Shared Utilities
This package provides a collection of shared utilities for Node.js applications, designed to standardize common tasks like logging, event dispatching, and database interactions.
Installation
To install the package, use npm:
npm install evp-catalystUsage
Below are examples of how to use the core utilities provided by this package.
Logger
The logger is a pre-configured Pino instance that includes transports for both console output and CloudWatch (in production). It automatically injects tracing IDs.
import logger from 'evp-catalyst/dist/utils/logger';
logger.info('This is an informational message.');
logger.error({ err: new Error('Something went wrong') }, 'An error occurred.');Event Dispatcher
The EventDispatcher is a singleton class for sending events to an AWS SNS topic.
Prerequisites: Ensure the EVENTS_TOPIC_ARN environment variable is set.
import { EventDispatcher } from 'evp-catalyst/dist/events/dispatcher';
async function sendMyEvent() {
try {
const dispatcher = EventDispatcher.getInstance();
await dispatcher.dispatch(
'user.created', // EventType
'user', // ResourceType
{ userId: 123, name: 'John Doe' } // Payload
);
console.log('Event dispatched successfully!');
} catch (error) {
console.error('Failed to dispatch event:', error);
}
}
sendMyEvent();MongoDB Utilities
A collection of helpers for MongoDB interactions.
import { generateUUID } from 'evp-catalyst/dist/utils/mongodb';
const newId = generateUUID();
console.log('Generated UUID:', newId);Tracing Context
Utilities for managing correlation, causation, and tracing IDs using async_hooks.
import { runWithTracing, getCorrelationId } from 'evp-catalyst/dist/utils/tracing-context';
function myBusinessLogic() {
// The tracing context is automatically available here
const correlationId = getCorrelationId();
console.log(`The correlation ID is: ${correlationId}`);
}
// Run your function within a tracing context
runWithTracing(myBusinessLogic);