# nestjs/azure-storage architecture

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

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

## Overview

##### Project Type and Purpose
- This is an open-source NestJS module (“@nestjs/azure-storage”) that plugs into any Nest application and provides Azure Blob Storage integration.
- It offers a Dynamic Module (`AzureStorageModule`), a service (`AzureStorageService`), an interceptor (`AzureStorageFileInterceptor`), a decorator (`AzureCredentialsDecorator`), and a CLI schematic to automate setup.

##### High-Level File-Structure Analysis
- Top-level:
- index.ts – re-exports public API
- lib/ – contains core module, service, interceptor, decorator, constants and tests
- schematics/ – contains Nest CLI installation scripts that add config and imports to the user’s project
- config files: tsconfig.json, tslint.json, jest.config.js, CI config, etc.
- Patterns:
- Nest Dynamic Module pattern (`withConfig`, `withConfigAsync`)
- Dependency Injection for service and interceptor
- Nest CLI schematics for code generation

##### System Design Components
a. Components
- NestJS Application (User’s app)
- AzureStorageModule (Dynamic Nest Module)
- Configuration Provider (injects accountName, sasKey, containerName)
- AzureStorageService (wraps Azure Blob Storage SDK calls)
- AzureMulterService (handles multer storage logic)
- AzureStorageFileInterceptor (intercepts file uploads in controllers)
- AzureCredentialsDecorator (injects credential metadata into routes)
- Azure Blob Storage (external service)
- Nest CLI Schematic (external tooling that edits user code)
b. Interactions / Data Flow
1. Developer runs `nest add @nestjs/azure-storage` → Nest CLI reads schematics/install → updates `.env`, `app.module.ts`, `.gitignore`.
2. At runtime, the Nest Application imports `AzureStorageModule.withConfig(...)` → Nest’s DI container registers:
– CONFIG token → config factory/provider
– AzureStorageService → depends on CONFIG
– AzureMulterService → depends on CONFIG
– AzureStorageFileInterceptor → injected into controller pipeline
3. Client sends HTTP POST `/azure/upload` with multipart/form-data →
– Request hits Controller → `@UseInterceptors(AzureStorageFileInterceptor)` →
– Interceptor uses AzureMulterService to buffer/stream the file →
– Interceptor calls AzureStorageService.upload() →
– Service calls Azure Blob Storage SDK → Blob container receives file → returns a storage URL →
– Interceptor attaches `file.storageUrl` to the controller handler.
c. Architectural Patterns
- Dynamic Module Pattern (configurable import)
- Dependency Injection (services, interceptors, config)
- Interceptor Pattern (file upload pipeline)
- Decorator Pattern (credentials injection)
- CLI Schematic (code generation plugin)

- Labels showing file locations and key methods

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "NestJS Application"
        Module["AzureStorageModule\n(lib/azure-nest-storage.module.ts)"]:::module
        Config["CONFIG Provider\n(lib/azure-storage.constant.ts)"]:::config
        Service["AzureStorageService\n(lib/azure-storage.service.ts)"]:::service
        Multer["AzureMulterService\n(lib/azure-multer.service.ts)"]:::service
        Interceptor["AzureStorageFileInterceptor\n(lib/azure-storage-file.interceptor.ts)"]:::interceptor
        Decorator["AzureCredentialsDecorator\n(lib/azure-credentials.decorator.ts)"]:::decorator
        Controller["UserController\n(@UseInterceptors)"]:::controller
    end

    Blob["Azure Blob Storage"]:::external
    Schematic["Nest CLI Schematic"]:::schematic

    Controller -->|"@UseInterceptors"| Interceptor
    Interceptor -->|"buffer/stream file"| Multer
    Interceptor -->|"calls upload()"| Service
    Service -->|"REST API call"| Blob

    Config -.->|"injects (DI)"| Service
    Config -.->|"injects (DI)"| Multer
    Module -.->|"uses CONFIG_TOKEN"| Config
    Schematic -.->|"modifies app.module.ts, .env, .gitignore"| Module

    click Module "https://github.com/nestjs/azure-storage/blob/master/lib/azure-nest-storage.module.ts"
    click Config "https://github.com/nestjs/azure-storage/blob/master/lib/azure-storage.constant.ts"
    click Service "https://github.com/nestjs/azure-storage/blob/master/lib/azure-storage.service.ts"
    click Multer "https://github.com/nestjs/azure-storage/blob/master/lib/azure-multer.service.ts"
    click Interceptor "https://github.com/nestjs/azure-storage/blob/master/lib/azure-storage-file.interceptor.ts"
    click Decorator "https://github.com/nestjs/azure-storage/blob/master/lib/azure-credentials.decorator.ts"
    click Schematic "https://github.com/nestjs/azure-storage/blob/master/schematics/install/index.ts"

    classDef module fill:#ADD8E6,stroke:#000
    classDef service fill:#ADD8E6,stroke:#000
    classDef interceptor fill:#ADD8E6,stroke:#000
    classDef decorator fill:#ADD8E6,stroke:#000
    classDef controller fill:#ADD8E6,stroke:#000
    classDef config fill:#90EE90,stroke:#000
    classDef schematic fill:#90EE90,stroke:#000
    classDef external fill:#FFA500,stroke:#000
```
