# juanjogondev/fastypest architecture

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

- Interactive diagram: https://gitdiagram.com/juanjogondev/fastypest
- Repository: https://github.com/juanjogondev/fastypest

## Overview

##### Main Components
- Fastypest Core Module: This is the central piece of the library (see src/core/fastypest.ts and src/core/index.ts). It provides the API (init, restoreData) used in test setups.
- Configuration Module: Located in src/core/config.ts, this component deals with the initial setup and connection details (using TypeORM) for the library.
- SQL Script Processor: Under src/core/sql-script, this module handles the execution of SQL queries. It includes:
- The main SQL-script handler (src/core/sql-script/sql-script.ts) that controls the flow.
- Database-specific query definitions stored as JSON files (e.g., cockroachdb.json, mysql.query.json, postgres.query.json) which detail how restoration is performed per database type.
- Additional types and interfaces (src/core/sql-script/types) to enforce the structure of these operations.
- Testing and Seeding Artifacts: In the tests folder, you have configuration (setup and ORM config files), seed definitions, entity samples, and Jest configuration. This shows how FASTYPEST is intended to be integrated with a test runner (like Jest) and demonstrates the use-case in which the library is resetting the database state between test runs.

##### Relationships and Interactions
- The Test Runner (Jest) initializes the FASTYPEST instance via the Fastypest class by providing a TypeORM connection.
- Once initialized (via init()), FASTYPEST uses the configuration module to access connection details.
- During test execution, after tests run, the restoreData method is called. This method internally interacts with the SQL Script Processor, which:
- Determines the correct SQL queries to use (depending on the target database—MySQL, PostgreSQL, etc.) by referring to the corresponding JSON query files.
- Executes these queries to quickly reset the database to its seeded state.
- The entire flow is wrapped with supporting scripts (like the ones in the “scripts” directory) and tool configurations (.github workflows, Docker Compose, etc.) that facilitate coding standards, CI/CD, and deployment for package updates.

##### Architectural Patterns & Technologies
- Modular Design: The separation into core, SQL script modules, and configuration promotes reusability and clarity.
- Adapter/Strategy Pattern: The way different database query JSON files are managed suggests an underlying strategy to choose which set of queries to run depending on the connected database.
- Integration with TypeORM: By requiring a TypeORM connection, FASTYPEST is clearly designed to plug into existing ORM-driven applications.
- Test-Oriented Design: The integration with Jest and the focus on database seeding and restoration underline that this library acts as a utility in test suites rather than as a full-stack service.

##### Final Touches
- Extensibility point (e.g., how additional databases can be supported by adding new JSON query files and possibly new strategies in the SQL Script Processor).

## Diagram (Mermaid)

```mermaid
graph TD
    subgraph "Internal Modules"
        A["Fastypest Core"]:::internal
        B["Configuration Module"]:::internal
        subgraph "SQL Script Processor"
            C["SQL Script Handler"]:::internal
            D["Query Definitions"]:::internal
            E["Types & Interfaces"]:::internal
        end
    end

    subgraph "External Systems"
        F["Test Runner/Jest Setup"]:::external
        G["MySQL Database"]:::external
        H["PostgreSQL Database"]:::external
        I["CockroachDB Database"]:::external
    end

    F -->|"init"| A
    A -->|"loadsConfig"| B
    A -->|"triggerRestoreData"| C
    C -->|"fetchesQueries"| D
    C -->|"validatesData"| E
    C -->|"executes"| G
    C -->|"executes"| H
    C -->|"executes"| I

    click A "https://github.com/juanjogondev/fastypest/blob/main/src/core/fastypest.ts"
    click B "https://github.com/juanjogondev/fastypest/blob/main/src/core/config.ts"
    click C "https://github.com/juanjogondev/fastypest/blob/main/src/core/sql-script/sql-script.ts"
    click D "https://github.com/juanjogondev/fastypest/tree/main/src/core/sql-script/queries"
    click E "https://github.com/juanjogondev/fastypest/tree/main/src/core/sql-script/types"
    click F "https://github.com/juanjogondev/fastypest/tree/main/tests"

    classDef internal fill:#AED6F1,stroke:#1F618D,stroke-width:2px;
    classDef external fill:#F9E79F,stroke:#B7950B,stroke-width:2px;
```
