How chicongst/nestjs-storage works
Project Type & Purpose
• This is a NestJS module designed to abstract storage functionalities using different providers (such as Azure Blob Storage or AWS S3).
• The module provides adapters, helper functions, interfaces, and services to interact with external storage services in a consistent way.
• From the README snippets, you see usage examples where the module is configured via “StorageModule.forRoot” to choose a disk type (“azure” or “s3”) and supply the corresponding credentials and settings.
Analyzing the File Structure
• Key directories:
• “libs”: Contains the reusable library code for storage, including:
▸ Adapters: Implements platform-specific logic (azure.adapter.ts, s3.adapter.ts, base-storage.adapter.ts).
▸ Constants: Includes provider constants and file-type constants.
▸ Helpers: Functions to manage file names and URLs.
▸ Interfaces: Definitions for expected file types, storage adapter interfaces, and multer output.
▸ Services: The storage service (storage.service.ts) that likely serves as the business logic layer orchestrating all storage operations.
▸ Storage: Contains the engine (blob-storage-engine.ts) possibly handling detailed storage procedures.
▸ Types: Contains TypeScript types that standardize account structures, blob details, upload headers, etc.
▸ A storage module (storage.module.ts) that integrates these components as a NestJS dynamic module.
• “src”: Contains the NestJS-specific application code such as:
▸ Main app module (app.module.ts) and related controllers/services.
▸ Configuration files (“env.config.ts”, “storage.config.ts”) that set up runtime parameters and tie in the storage module.
• Other configuration files (like .eslintrc.js, .prettierrc, etc.) indicate a well-defined development setup, but the core architecture remains around the libs and src directories.
Key Components & Their Relationships
• Storage Module (in libs/storage.module.ts):
• Serves as the encapsulating NestJS module.
• It integrates the Storage Service and registers storage adapters as providers.
• Storage Service (libs/services/storage.service.ts):
• Acts as the facade through which the application interacts with the storage engines.
• Adapters (within libs/adapters):
• Distinct implementations for Azure and S3 (and a base adapter for shared functionality).
• These interface with external storage services.
• Engine (libs/storage/blob-storage-engine.ts):
• Likely encapsulates the low-level operations necessary to communicate with the storage providers.
• Helpers, Interfaces, and Types:
• Type safety, reusable helper logic, and proper communication between configuration and service layers.
• App Module (src/app.module.ts):
• Bootstraps the NestJS application and imports the configured storage module from the configuration file (src/configs/storage.config.ts).
Architectural Patterns and Design Principles
• Modular design: Separation of concerns is evident; the storage-related functionality is encapsulated within its own module and library code.
• Dependency Injection: Utilized by NestJS to inject configuration and external adapters into services.
• Adapter Pattern: Abstracts multiple storage providers (Azure and S3) behind common interfaces, allowing flexible swapping or addition of new providers.
• Configuration-as-Code: Environment and storage configurations are clearly separated in config files, facilitating easy runtime changes.