# chicongst/nestjs-storage architecture

> Architecture diagram of the GitHub repository chicongst/nestjs-storage, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/chicongst/nestjs-storage
- Repository: https://github.com/chicongst/nestjs-storage

## Overview

##### 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.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Application Layer
    subgraph "Application"
        Config["Configuration Files"]
        AppModule["App Module/Entry Point"]
        StorageModule["Storage Module"]
        StorageService["Storage Service"]
        BlobStorageEngine["Blob Storage Engine"]
    end

    %% Adapters Layer
    subgraph "Adapters"
        AzureAdapter["Azure Adapter"]
        S3Adapter["S3 Adapter"]
        BaseStorageAdapter["Base Storage Adapter"]
    end

    %% External Services
    subgraph "External Services"
        ExternalAzure["Azure Blob Storage"]
        ExternalS3["AWS S3"]
    end

    %% Utility Libraries
    subgraph "Utility Libraries"
        Helpers["Helpers (FileName & FileURL)"]
        Constants["Constants"]
        Interfaces["Interfaces"]
        Types["Types"]
    end

    %% Relationships
    Config --> AppModule
    AppModule --> StorageModule
    StorageModule --> StorageService
    StorageService --> BlobStorageEngine

    StorageService --> AzureAdapter
    StorageService --> S3Adapter
    StorageService --> BaseStorageAdapter

    AzureAdapter --> ExternalAzure
    S3Adapter --> ExternalS3

    Helpers --> StorageService
    Constants --> StorageService
    Interfaces --> StorageService
    Types --> StorageService

    %% Styles
    classDef app fill:#fef3bd,stroke:#333,stroke-width:2px;
    classDef module fill:#ccf2ff,stroke:#333,stroke-width:2px;
    classDef adapter fill:#d1e7dd,stroke:#333,stroke-width:2px;
    classDef util fill:#e2ccff,stroke:#333,stroke-width:2px;
    classDef external fill:#ffcccc,stroke:#333,stroke-width:2px;

    class AppModule,Config app;
    class StorageModule,StorageService,BlobStorageEngine module;
    class AzureAdapter,S3Adapter,BaseStorageAdapter adapter;
    class Helpers,Constants,Interfaces,Types util;
    class ExternalAzure,ExternalS3 external;

    %% Click Events
    click AppModule "https://github.com/chicongst/nestjs-storage/blob/main/src/app.module.ts"
    click StorageModule "https://github.com/chicongst/nestjs-storage/blob/main/libs/storage.module.ts"
    click StorageService "https://github.com/chicongst/nestjs-storage/blob/main/libs/services/storage.service.ts"
    click BlobStorageEngine "https://github.com/chicongst/nestjs-storage/blob/main/libs/storage/blob-storage-engine.ts"
    click AzureAdapter "https://github.com/chicongst/nestjs-storage/blob/main/libs/adapters/azure.adapter.ts"
    click S3Adapter "https://github.com/chicongst/nestjs-storage/blob/main/libs/adapters/s3.adapter.ts"
    click BaseStorageAdapter "https://github.com/chicongst/nestjs-storage/blob/main/libs/adapters/base-storage.adapter.ts"
    click Helpers "https://github.com/chicongst/nestjs-storage/blob/main/libs/helpers/file-name.helper.ts"
    click Constants "https://github.com/chicongst/nestjs-storage/blob/main/libs/constants/provider.constants.ts"
    click Interfaces "https://github.com/chicongst/nestjs-storage/blob/main/libs/interfaces/storage-adapter.interface.ts"
    click Types "https://github.com/chicongst/nestjs-storage/blob/main/libs/types/account.type.ts"
    click Config "https://github.com/chicongst/nestjs-storage/blob/main/src/configs/env.config.ts"
```
