# tyshkovskii/typed-async-storage architecture

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

- Interactive diagram: https://gitdiagram.com/tyshkovskii/typed-async-storage
- Repository: https://github.com/tyshkovskii/typed-async-storage

## Overview

Below are step-by-step guidelines tailored to this project:

##### Project Type and Purpose
- The project is an npm package intended to provide a typed and validated storage layer on top of AsyncStorage.
- It is focused on validating data via PropTypes and ensuring that each storage instance adheres to a specific schema, either for single or multiple storage items.

##### Analyzing the File Structure
- The top-level directories/files include configuration files (.eslintrc.js, .github/ etc.), tests, mocks, and the core source under the src directory.
- Within src, you have key modules:
- createStorage.js – likely contains the factory function that sets up storage instances.
- storageWrapper.js – probably encapsulates and abstracts the AsyncStorage API calls.
- validation.js – handles schema validation using PropTypes.
- helpers.js – contains utility functions that support the operations.
- index.js – exposes the public API of the library.
- Testing files (in the test folder) mirror these components to ensure proper function.

##### Key Components
- Consumer/Application Code: Represents the client code that imports and uses the library.
- API Entry Point: The index.js file that exposes the createStorage function.
- Storage Creation Module (createStorage.js): This module sets up new storage instances based on the provided schema, name, and AsyncStorage instance.
- Storage Wrapper (storageWrapper.js): Wraps AsyncStorage functionalities (set, get, merge, remove, clear) ensuring schema consistency.
- Validation Module (validation.js): Implements PropTypes-based validations to ensure that the storage schema is met.
- Helper Utilities (helpers.js): Auxiliary functions used by other modules.
- External Dependency – AsyncStorage: The native AsyncStorage module coming from '@react-native-community/async-storage'.
- Testing & Mocks (test & __mocks__ folders): Represent quality control and simulation of external components.

##### Relationships and Interactions
- The consumer calls the API (via the exported index.js) to create or use storage.
- The createStorage module receives configuration details (name, schema, AsyncStorage, and optional flag for multiple storages) and uses the helper utilities.
- The Storage Wrapper orchestrates calls to AsyncStorage, ensuring every operation (set, get, etc.) passes through the Validation module.
- The Validation module checks that the stored/loaded data conforms to the provided schema via PropTypes.
- Data flows from the storage instance (wrapped AsyncStorage) back to the consumer, with each operation validated appropriately.

##### Architectural Patterns and Design Principles
- Separation of concerns: Different modules handle creation, wrapping, utility functions, and validation.
- Modularity: Each part of the functionality is encapsulated in its own file/module, making the library highly maintainable.
- Dependency abstraction: The design abstracts the AsyncStorage API behind its own layers, allowing easier validation and potential future replacement.
- Testability: A clear separation into modules supports unit testing (as evidenced by the test directory).

## Diagram (Mermaid)

```mermaid
flowchart TD
    Client["Client Application"]:::client
    API["API Entry Point (index.js)"]:::core
    StorageCreate["Storage Creation Module (createStorage.js)"]:::core
    Helpers["Helper Utilities (helpers.js)"]:::core
    StorageWrapper["Storage Wrapper Module (storageWrapper.js)"]:::core
    Validation["Validation Module (validation.js)"]:::core
    AsyncStorage["AsyncStorage (External)"]:::external

    Client -->|"calls"| API
    API -->|"invokes"| StorageCreate
    StorageCreate -->|"utilizes"| Helpers
    StorageCreate -->|"creates"| StorageWrapper
    StorageWrapper -->|"validates with"| Validation
    StorageWrapper -->|"performs operations"| AsyncStorage

    subgraph "Testing & Mocks"
        Tests["Tests"]:::tests
        Mocks["Mocks"]:::tests
        Tests ---|"uses"| Mocks
    end

    API ---|"tested by"| Tests

    classDef client fill:#f9f,stroke:#333,stroke-width:2px;
    classDef core fill:#bbf,stroke:#333,stroke-width:2px;
    classDef external fill:#ff9,stroke:#333,stroke-width:2px;
    classDef tests fill:#bfb,stroke:#333,stroke-width:2px;

    click API "https://github.com/tyshkovskii/typed-async-storage/blob/master/src/index.js"
    click StorageCreate "https://github.com/tyshkovskii/typed-async-storage/blob/master/src/createStorage.js"
    click StorageWrapper "https://github.com/tyshkovskii/typed-async-storage/blob/master/src/storageWrapper.js"
    click Validation "https://github.com/tyshkovskii/typed-async-storage/blob/master/src/validation.js"
    click Helpers "https://github.com/tyshkovskii/typed-async-storage/blob/master/src/helpers.js"
    click AsyncStorage "https://github.com/tyshkovskii/typed-async-storage/blob/master/__mocks__/@react-native-community/async-storage.js"
    click Tests "https://github.com/tyshkovskii/typed-async-storage/tree/master/test"
    click Mocks "https://github.com/tyshkovskii/typed-async-storage/tree/master/__mocks__"
```
