# alexandrajs/sandstorm architecture

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

- Interactive diagram: https://gitdiagram.com/alexandrajs/sandstorm
- Repository: https://github.com/alexandrajs/sandstorm

## Overview

##### Project type and purpose
- Sandstorm is an open‐source Node.js library (no standalone frontend or microservice layers). It exposes a programmatic API for defining schemas, instantiating models, querying a MongoDB backend, and enforcing type/validation rules.

##### File structure
- Top‐level folders:
- src – the core implementation
- bench – performance benchmarks against model, schema, types
- test – unit and integration tests (includes a docker‐compose.yml to spin up MongoDB)
- Key source files in src:
- sandstorm.js (the main class that users instantiate)
- schema.js (registry + schema‐definition logic)
- model.js (model instance, set/merge/save/delete operations)
- cursor.js (abstraction over MongoDB cursors)
- types/* and properties/* (typecasting + validation logic)
- index.js – library entrypoint, exports Sandstorm API.
- package.json – declares dependencies (e.g., mongodb driver, promises).

##### README for insights
- Users register schemas via Sandstorm.register(), creating an internal registry.
- They create models, call set/merge, then save() – under the hood executed via MongoDB driver.
- Queries via Sandstorm.find() return Cursor instances with toArray(), delete(), etc.

##### Main components
a. Client Application code (the “User”)
b. Sandstorm API Layer
· index.js (exports)
· sandstorm.js (core singleton/class)
c. Schema Registry/Subsystem
· schema.js
· types/ (string, number, boolean, objectId, mixed, etc.)
· properties/ (validation + casting strategies per type)
d. Model Layer
· model.js (Model instances)
· cursor.js (Query results abstraction)
e. Persistence Layer
· MongoDB driver (external service)
· MongoDB instance (spun up in tests or provided by user)
f. Benchmark & Test Harness (optional side components)
· bench scripts (bench/*.js)
· test suite (integration via docker-compose + unit tests)

##### Relationships and interactions
- The Client calls Sandstorm.register() → Schema Registry builds internal Schema + Type/Property handlers
- Client calls Sandstorm.create() → Model instance linked to a schema
- Model.set()/merge() → invokes Properties to cast/validate fields
- Model.save()/delete() → uses MongoDB driver to persist or remove documents
- Client calls Sandstorm.find() → returns Cursor → executes a Mongo query → cursor.js wraps driver cursor
- Cursor.toArray(), Cursor.delete() → further interactions with the driver

##### Architectural patterns
- Registry pattern for schemas
- Factory pattern for creating property/type handlers
- Strategy pattern for validation (per‐type logic in properties/*)
- Wrapper/Adapter around the MongoDB driver (cursor.js, model.js)
- Modular code organization for extensibility (types and properties directories)

- Clear component labels (with file names)
- Optional: note protocols (e.g., “uses MongoDB wire protocol”)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Client
    Client["Client Application"]:::external

    %% API Layer
    subgraph "Sandstorm API Layer"
        direction TB
        Index["index.js"]:::core
        Sandstorm["sandstorm.js"]:::core
    end

    %% Schema Registry
    subgraph "Schema Registry/Subsystem"
        direction TB
        Schema["schema.js"]:::core
        subgraph "Types"
            direction TB
            TypesIndex["index.js"]:::types
            ArrayType["array.js"]:::types
            BooleanType["boolean.js"]:::types
            DateType["date.js"]:::types
            MixedType["mixed.js"]:::types
            NumberType["number.js"]:::types
            ObjectType["object.js"]:::types
            ObjectIdType["objectId.js"]:::types
            StringType["string.js"]:::types
        end
        subgraph "Properties"
            direction TB
            PropsIndex["index.js"]:::properties
            ArrayProp["array.js"]:::properties
            BooleanProp["boolean.js"]:::properties
            DateProp["date.js"]:::properties
            MixedProp["mixed.js"]:::properties
            ModelProp["model.js"]:::properties
            NumberProp["number.js"]:::properties
            ObjectProp["object.js"]:::properties
            ObjectIdProp["objectId.js"]:::properties
            StringProp["string.js"]:::properties
        end
    end

    %% Model Layer
    subgraph "Model Layer"
        direction TB
        Model["model.js"]:::core
        Cursor["cursor.js"]:::core
    end

    %% Persistence Layer
    subgraph "Persistence Layer"
        direction TB
        Driver["MongoDB Driver"]:::external
        MongoDB["MongoDB Instance"]:::external
    end

    %% Benchmark Harness
    subgraph "Benchmark Harness"
        direction TB
        BenchSchema["bench/schema.js"]:::bench
        BenchModel["bench/model.js"]:::bench
        BenchProps["bench/properties.js"]:::bench
        BenchTypes["bench/types.js"]:::bench
    end

    %% Test Suite
    subgraph "Test Suite"
        direction TB
        TestSchema["test/schema.js"]:::test
        TestModel["test/model.js"]:::test
        TestCursor["test/cursor.js"]:::test
        TestSandstorm["test/sandstorm.js"]:::test
        TestTypes["test/types.js"]:::test
        TestCommon["test/common.js"]:::test
        TestCompose["test/docker-compose.yml"]:::test
    end

    %% Index to implement sandstorm
    Client -->|"uses"| Index
    Index -->|"instantiates"| Sandstorm
    Sandstorm -->|"register() calls"| Schema
    Schema -->|"loads"| TypesIndex
    TypesIndex --> ArrayType
    TypesIndex --> BooleanType
    TypesIndex --> DateType
    TypesIndex --> MixedType
    TypesIndex --> NumberType
    TypesIndex --> ObjectType
    TypesIndex --> ObjectIdType
    TypesIndex --> StringType
    Schema -->|"loads"| PropsIndex
    PropsIndex --> ArrayProp
    PropsIndex --> BooleanProp
    PropsIndex --> DateProp
    PropsIndex --> MixedProp
    PropsIndex --> ModelProp
    PropsIndex --> NumberProp
    PropsIndex --> ObjectProp
    PropsIndex --> ObjectIdProp
    PropsIndex --> StringProp
    Sandstorm -->|"create()/find()"| Model
    Model -->|"wraps"| Cursor
    Model -->|"persist via"| Driver
    Cursor -->|"queries via"| Driver
    Driver -->|"connects to"| MongoDB

    %% Click Events
    click Index "https://github.com/alexandrajs/sandstorm/blob/master/index.js"
    click Sandstorm "https://github.com/alexandrajs/sandstorm/blob/master/src/sandstorm.js"
    click Schema "https://github.com/alexandrajs/sandstorm/blob/master/src/schema.js"
    click TypesIndex "https://github.com/alexandrajs/sandstorm/blob/master/src/types/index.js"
    click ArrayType "https://github.com/alexandrajs/sandstorm/blob/master/src/types/array.js"
    click BooleanType "https://github.com/alexandrajs/sandstorm/blob/master/src/types/boolean.js"
    click DateType "https://github.com/alexandrajs/sandstorm/blob/master/src/types/date.js"
    click MixedType "https://github.com/alexandrajs/sandstorm/blob/master/src/types/mixed.js"
    click NumberType "https://github.com/alexandrajs/sandstorm/blob/master/src/types/number.js"
    click ObjectType "https://github.com/alexandrajs/sandstorm/blob/master/src/types/object.js"
    click ObjectIdType "https://github.com/alexandrajs/sandstorm/blob/master/src/types/objectId.js"
    click StringType "https://github.com/alexandrajs/sandstorm/blob/master/src/types/string.js"
    click PropsIndex "https://github.com/alexandrajs/sandstorm/blob/master/src/properties/index.js"
    click ArrayProp "https://github.com/alexandrajs/sandstorm/blob/master/src/properties/array.js"
    click BooleanProp "https://github.com/alexandrajs/sandstorm/blob/master/src/properties/boolean.js"
    click DateProp "https://github.com/alexandrajs/sandstorm/blob/master/src/properties/date.js"
    click MixedProp "https://github.com/alexandrajs/sandstorm/blob/master/src/properties/mixed.js"
    click ModelProp "https://github.com/alexandrajs/sandstorm/blob/master/src/properties/model.js"
    click NumberProp "https://github.com/alexandrajs/sandstorm/blob/master/src/properties/number.js"
    click ObjectProp "https://github.com/alexandrajs/sandstorm/blob/master/src/properties/object.js"
    click ObjectIdProp "https://github.com/alexandrajs/sandstorm/blob/master/src/properties/objectId.js"
    click StringProp "https://github.com/alexandrajs/sandstorm/blob/master/src/properties/string.js"
    click Model "https://github.com/alexandrajs/sandstorm/blob/master/src/model.js"
    click Cursor "https://github.com/alexandrajs/sandstorm/blob/master/src/cursor.js"
    click Driver "https://github.com/alexandrajs/sandstorm/blob/master/package.json"
    click TestCompose "https://github.com/alexandrajs/sandstorm/blob/master/test/docker-compose.yml"
    click BenchSchema "https://github.com/alexandrajs/sandstorm/blob/master/bench/schema.js"
    click BenchModel "https://github.com/alexandrajs/sandstorm/blob/master/bench/model.js"
    click BenchProps "https://github.com/alexandrajs/sandstorm/blob/master/bench/properties.js"
    click BenchTypes "https://github.com/alexandrajs/sandstorm/blob/master/bench/types.js"
    click TestSchema "https://github.com/alexandrajs/sandstorm/blob/master/test/schema.js"
    click TestModel "https://github.com/alexandrajs/sandstorm/blob/master/test/model.js"
    click TestCursor "https://github.com/alexandrajs/sandstorm/blob/master/test/cursor.js"
    click TestSandstorm "https://github.com/alexandrajs/sandstorm/blob/master/test/sandstorm.js"
    click TestTypes "https://github.com/alexandrajs/sandstorm/blob/master/test/types.js"
    click TestCommon "https://github.com/alexandrajs/sandstorm/blob/master/test/common.js"

    %% Styles
    classDef core fill:#bbdefb,stroke:#1e88e5,color:#0d47a1;
    classDef types fill:#c8e6c9,stroke:#388e3c,color:#1b5e20;
    classDef properties fill:#c8e6c9,stroke:#388e3c,color:#1b5e20;
    classDef external fill:#ffe0b2,stroke:#fb8c00,color:#e65100;
    classDef bench fill:#e0e0e0,stroke:#9e9e9e,color:#424242;
    classDef test fill:#e0e0e0,stroke:#9e9e9e,color:#424242;
```
