qmemory
Version:
A comprehensive production-ready Node.js utility library with MongoDB document operations, user ownership enforcement, Express.js HTTP utilities, environment-aware logging, and in-memory storage. Features 96%+ test coverage with comprehensive error handli
92 lines (83 loc) • 4.97 kB
JavaScript
/**
* My NPM Module
* A simple Node.js module with database utilities
*
* This serves as the main entry point for the NPM module, organizing and re-exporting
* functionality from specialized modules in the /lib directory. This approach follows
* the barrel pattern, providing a clean public API while maintaining internal code
* organization.
*
* Design rationale: By separating concerns into different modules and re-exporting
* them here, we achieve better maintainability, testability, and allow consumers
* to import everything from a single location while keeping the internal structure
* flexible for future changes.
*/
// Import organized modules - each module handles a specific domain of functionality
// This separation allows for easier testing, maintenance, and selective importing
const {
sendNotFound,
sendConflict,
sendInternalServerError,
sendServiceUnavailable // helper for 503 responses
} = require('./lib/http-utils'); // Central location for HTTP helpers promotes consistency
const { ensureMongoDB, ensureUnique } = require('./lib/database-utils'); // Database helpers keep controllers clean
const {
performUserDocOp,
findUserDoc,
deleteUserDoc,
userDocActionOr404,
fetchUserDocOr404,
deleteUserDocOr404,
listUserDocs,
createUniqueDoc,
updateUserDoc, // helper to update user documents safely
validateDocumentUniqueness, // helper for uniqueness validation
hasUniqueFieldChanges // helper for change detection
} = require('./lib/document-ops'); // Higher level document handling utilities
const { MemStorage, storage } = require('./lib/storage'); // in-memory storage class and singleton instance
const { greet, add, isEven } = require('./lib/utils'); // basic utility functions for common operations
const { logFunctionEntry, logFunctionExit, logFunctionError } = require('./lib/logging-utils'); // centralized logging patterns
// Export all functions for use as a module
// This barrel export pattern provides a single import point for consumers
// while maintaining internal organization. Each category is grouped logically
// to help consumers understand the available functionality at a glance.
module.exports = { // re-exposes modules so consumers import from one place
// HTTP utilities - Express.js response helpers re-exposed from http-utils
// Centralized HTTP response handling reduces duplication across controllers
sendNotFound, // 404 response helper re-exported for convenience
sendConflict, // 409 conflict helper re-exported for barrel pattern
sendInternalServerError, // 500 response helper re-exported for consistency
sendServiceUnavailable, // 503 response helper re-exported for unified API
// Database utilities - MongoDB connection and validation helpers re-exposed from database-utils
// These functions provide robust database interaction patterns with proper error handling
ensureMongoDB, // initializes MongoDB connection for consumers
ensureUnique, // duplicate check helper re-exported for unified API
// Document operations - High-level document manipulation utilities re-exposed from document-ops
// These encapsulate common CRUD patterns for user-owned documents,
// reducing boilerplate in application controllers and ensuring consistent behavior
performUserDocOp, // wrapper for generic document operations
findUserDoc, // fetches a document scoped to the current user
deleteUserDoc, // removes a user owned document safely
userDocActionOr404, // triggers action or returns 404 if not found
fetchUserDocOr404, // retrieves a document or fails with 404
deleteUserDocOr404, // deletes a document or fails with 404
listUserDocs, // lists documents for a specific user
createUniqueDoc, // creates a document enforcing unique fields
updateUserDoc, // update with uniqueness checks re-exported for convenience
validateDocumentUniqueness, // helper for uniqueness validation re-exported
hasUniqueFieldChanges, // helper for change detection re-exported
// Storage - In-memory storage implementation and singleton instance re-exposed from storage module
// Provides both the class for custom instantiation and a ready-to-use singleton
MemStorage, // storage class allowing separate instances
storage, // shared singleton instance re-exported for convenience
// Basic utilities - Common helper functions for everyday operations re-exposed from utils
// Simple, reusable functions for string formatting, math, and data validation
greet, // generates a greeting string
add, // performs simple integer addition
isEven, // checks numeric parity
// Logging utilities - Centralized logging patterns for consistent debugging re-exposed from logging-utils
// Standardized logging functions for function entry, exit, and error tracking
logFunctionEntry, // logs when a function begins execution
logFunctionExit, // logs when a function completes execution
logFunctionError // logs errors with context
};