# hassanuos/nodejs-dynodb-audit-log architecture

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

- Interactive diagram: https://gitdiagram.com/hassanuos/nodejs-dynodb-audit-log
- Repository: https://github.com/hassanuos/nodejs-dynodb-audit-log

## Overview

##### Project type and purpose
- This is a small Node.js “audit‐log” utility. It takes incoming JSON objects, computes what has changed compared to the last stored version in DynamoDB, and writes a new audit record (while marking the prior one as “archived”). It is not a full‐stack web app but rather a backend scripting/tooling component intended to run either via CLI or as part of a larger pipeline.

##### File structure
- Top‐level files:
- index.js – the entry point of the audit‐log workflow
- DynamoDBWrapper.js – encapsulates all interactions with DynamoDB (CRUD, table creation)
- CreateTable.js – a standalone script to bootstrap the audit‐log table schema
- compareJSON.js / compareJSONNew.js – modules that diff two JSON objects to produce “old_value” and “new_value” pairs
- currentDateTime.js – returns the current timestamp in the required format
- package.json / package-lock.json – declare dependencies (AWS SDK, etc.)

##### README for additional insights
- Purpose is to maintain an append‐only audit trail in a DynamoDB table, keyed by ref_id.
- Uses AWS DynamoDB SDK (or local DynamoDB for testing).
- Audit entries carry fields: id, ref_id, latest_object, old_value, new_value, new_date, old_date, entry_status.

a. Main components:
1. Input Producer (could be a REST service, CLI, or upstream system that fed JSON records)
2. Audit‐Log Service (the Node.js script)
– index.js as orchestrator
– DynamoDBWrapper module
– JSON diff modules (compareJSON.js / compareJSONNew.js)
– Date util module
3. AWS DynamoDB
4. (Optional) Local DynamoDB instance or AWS CLI for setup/testing

b. Relationships & interactions:
- Input Producer → index.js: sends new JSON object + ref_id
- index.js → DynamoDBWrapper.getLatest(ref_id): fetch last audit entry
- index.js → compareJSON(oldObject, newObject): compute diffs
- index.js → DynamoDBWrapper.writeNewEntry(diffResult): create new audit record
- index.js → DynamoDBWrapper.archiveOldEntry(entry_id): set entry_status = 0 on prior record
- DynamoDBWrapper → DynamoDB: issues GetItem / PutItem / UpdateItem calls
- CreateTable.js → DynamoDB: CreateTable on startup or via AWS CLI

c. Architectural patterns & principles:
- Single Responsibility: each module handles one concern (DB access, JSON diff, timestamp formatting).
- Orchestration: index.js coordinates modules in a linear workflow.
- Idempotency & append‐only: previous records are not deleted, only flagged inactive.

d. Key technologies:
- Node.js
- AWS SDK for JavaScript (v2 or v3)
- AWS DynamoDB (local or remote)
- JSON diff algorithm (custom in compareJSON)

## Diagram (Mermaid)

```mermaid
flowchart TB
    Producer["Input Producer"]:::external

    subgraph "Audit-Log Service"
        Orchestrator["index.js\n(Orchestrator)"]:::internal
        Wrapper["DynamoDBWrapper.js"]:::internal
        DiffOld["compareJSON.js"]:::internal
        DiffNew["compareJSONNew.js"]:::internal
        DateUtil["currentDateTime.js"]:::internal
    end

    DynamoDB["DynamoDB"]:::db

    Provision["CreateTable.js"]:::provisioning

    subgraph "Project Docs & Manifests"
        Manifest["package.json"]:::doc
        Lock["package-lock.json"]:::doc
        Readme["README.md"]:::doc
    end

    Producer -->|"JSON + ref_id"| Orchestrator
    Orchestrator -->|"getLatest(ref_id)\nGetItem"| Wrapper
    Wrapper -->|"GetItem"| DynamoDB
    Orchestrator -->|"Diff old vs new"| DiffOld
    Orchestrator -->|"Diff old vs new"| DiffNew
    Orchestrator -->|"timestamp"| DateUtil
    Orchestrator -->|"writeNewEntry\nPutItem"| Wrapper
    Wrapper -->|"PutItem"| DynamoDB
    Orchestrator -->|"archiveOldEntry\nUpdateItem"| Wrapper
    Wrapper -->|"UpdateItem"| DynamoDB
    Provision -->|"CreateTable"| DynamoDB

    click Orchestrator "https://github.com/hassanuos/nodejs-dynodb-audit-log/blob/master/index.js"
    click Wrapper "https://github.com/hassanuos/nodejs-dynodb-audit-log/blob/master/DynamoDBWrapper.js"
    click Provision "https://github.com/hassanuos/nodejs-dynodb-audit-log/blob/master/CreateTable.js"
    click DiffOld "https://github.com/hassanuos/nodejs-dynodb-audit-log/blob/master/compareJSON.js"
    click DiffNew "https://github.com/hassanuos/nodejs-dynodb-audit-log/blob/master/compareJSONNew.js"
    click DateUtil "https://github.com/hassanuos/nodejs-dynodb-audit-log/blob/master/currentDateTime.js"
    click Manifest "https://github.com/hassanuos/nodejs-dynodb-audit-log/blob/master/package.json"
    click Lock "https://github.com/hassanuos/nodejs-dynodb-audit-log/blob/master/package-lock.json"
    click Readme "https://github.com/hassanuos/nodejs-dynodb-audit-log/blob/master/README.md"

    classDef external fill:#CCCCCC,stroke:#333,stroke-width:1px
    classDef internal fill:#D0E6FF,stroke:#333,stroke-width:1px
    classDef db fill:#AAFFAA,stroke:#333,stroke-width:1px
    classDef provisioning fill:#FFDD99,stroke:#333,stroke-width:1px
    classDef doc fill:#E0CCFF,stroke:#333,stroke-width:1px
```
