# dokuzkey/server architecture

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

- Interactive diagram: https://gitdiagram.com/dokuzkey/server
- Repository: https://github.com/dokuzkey/server

## Overview

##### Identify the Project Type and Purpose

- **Project Type:** The 9Key Server is a **backend server-side application** for the **9Key Password Manager**, which is a modern, secure, and cloud-based password management tool. It serves as an alternative to Bitwarden and 1Password.

- **Purpose:** The server handles authentication, database interactions, and manages password and note operations. It ensures data is end-to-end encrypted using a custom encryption module.

##### Analyze the File Structure

- **Top-Level Directories and Files:**
- **Configuration & Setup:**
- `.env.example`, `Dockerfile`, `config.ts`, `tsconfig.json`: Configuration and environment setup.
- `package.json`, `package-lock.json`: Node.js dependencies and scripts.
- `eslint.config.mjs`, `.prettierrc`: Code quality and formatting configurations.

- **Source Code (`src` Directory):**
- **Entry Point:** `src/index.ts`
- **Initialization:** `src/init/validity.ts`
- **Models:** `src/models/` containing `Note.ts`, `Password.ts`, `User.ts`
- **Modules:**
- `auth` for authentication (`login.ts`, `register.ts`)
- `database` for database connections (`connect.ts`)
- `notes` for CRUD operations on notes (`create.ts`, `delete.ts`, `edit.ts`, `fetch.ts`)
- `passwords` for CRUD operations on passwords (`create.ts`, `delete.ts`, `edit.ts`, `fetch.ts`)
- **Endpoints Configuration:** `src/validEndpoints.json`

- **Patterns and Architectural Choices:**
- **Modular Structure:** The `src/modules/` directory indicates a modular architecture, separating concerns like authentication, database, notes, and passwords.
- **MVC Pattern:** Presence of `models` suggests adherence to the Model-View-Controller (MVC) pattern, with models representing data structures.
- **TypeScript Usage:** Strong typing and scalability considerations.

- **Configuration and Deployment:**
- **Docker:** Presence of `Dockerfile` indicates containerization for consistent deployment environments.
- **Environment Management:** `.env.example` for environment variables setup.

##### Examine the README for Additional Insights

- **Project Description:** Emphasizes that 9Key Server is the backend for a password manager with a focus on security and modern aesthetics.
- **Clients Information:** References a separate frontend repository built with NextJS and TypeScript.
- **Setup Instructions:** Clarifies dependencies like Node.js and TypeScript Compiler.
- **Security Notice:** Highlights the infancy of the project and the focus on implementing robust encryption.
- **License Information:** Apache 2.0 License.

**a. Identify the Main Components:**

- **Frontend Application:**
- **dokuzKey/frontend:** Built with NextJS and TypeScript.

- **Backend Server:**
- **Authentication Module:** Handles user login and registration.
- **Database Module:** Manages connections and interactions with the database.
- **Notes Module:** CRUD operations for user notes.
- **Passwords Module:** CRUD operations for user passwords.
- **Encryption Module:** Ensures end-to-end encryption of data.

- **Database:**
- Stores user data, notes, and passwords securely.

- **External Services:**
- **Web Client:** Accessible via [sifre.org.tr](https://sifre.org.tr).

**b. Determine Relationships and Interactions:**

- **Frontend ↔ Backend:**
- Communication via API endpoints for authentication, notes, and passwords management.

- **Backend ↔ Database:**
- The Database Module connects to the database to perform CRUD operations.

- **Encryption Module:**
- Integrated within the Backend to encrypt/decrypt data before storing or retrieving from the database.

- **External Web Client:**
- Interfaces with the Backend Server to provide user access.

**c. Highlight Architectural Patterns and Design Principles:**

- **Modular Architecture:** Separation of concerns through distinct modules (auth, database, notes, passwords).
- **MVC Pattern:** Utilization of models to represent data structures.
- **Containerization:** Use of Docker for deployment consistency.
- **Security-First Approach:** Emphasis on encryption and secure data handling.

**d. Include Relevant Technologies, Frameworks, or Libraries:**

- **Programming Language:** TypeScript
- **Framework:** Node.js for the backend; NextJS for the frontend.
- **Database:** Not explicitly mentioned, but inferred to be a relational or NoSQL database based on modules.
- **Build and Deployment:** Docker for containerization.
- **Code Quality Tools:** ESLint and Prettier for linting and formatting.

##### Elements

- **Clear Labels:** Each component (e.g., Authentication Module, Database) should be distinctly labeled.
- **External Services:** Distinguish external entities like the web client.

##### Final Recommendations

- **Use a Layered Approach:** Start with high-level components and progressively detail sub-components.
- **Incorporate Documentation:** Reference the README and module functionalities to validate component interactions.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global entities
    ExternalWebClient["External Web Client"]:::external
    Frontend["Frontend Application"]:::frontend

    %% Backend Server
    subgraph "Backend Server"
        EntryPoint["Entry Point"]:::backend_module
        AuthModule["Authentication Module"]:::backend_module
        DBModule["Database Module"]:::backend_module
        NotesModule["Notes Module"]:::backend_module
        PasswordsModule["Passwords Module"]:::backend_module
        EncryptionModule["Encryption Module"]:::backend_module
        Models["Models"]:::backend_module
        EndpointsConfig["Endpoints Configuration"]:::backend_module
    end

    %% Database
    Database["((Database))"]:::database

    %% Connections
    ExternalWebClient --> Frontend
    Frontend -->|API Calls| EntryPoint
    EntryPoint --> AuthModule
    EntryPoint --> NotesModule
    EntryPoint --> PasswordsModule
    EntryPoint --> EndpointsConfig
    AuthModule --> EncryptionModule
    NotesModule --> EncryptionModule
    PasswordsModule --> EncryptionModule
    EncryptionModule --> DBModule
    DBModule --> Database
    AuthModule --> Models
    NotesModule --> Models
    PasswordsModule --> Models

    %% Click Events
    click AuthModule "https://github.com/dokuzkey/server/tree/main/src/modules/auth/"
    click DBModule "https://github.com/dokuzkey/server/tree/main/src/modules/database/"
    click NotesModule "https://github.com/dokuzkey/server/tree/main/src/modules/notes/"
    click PasswordsModule "https://github.com/dokuzkey/server/tree/main/src/modules/passwords/"
    click Models "https://github.com/dokuzkey/server/tree/main/src/models/"
    click EntryPoint "https://github.com/dokuzkey/server/blob/main/src/index.ts"
    click EndpointsConfig "https://github.com/dokuzkey/server/blob/main/src/validEndpoints.json"

    %% Styles
    classDef external fill:#f9f,stroke:#333,stroke-width:2px;
    classDef frontend fill:#bbf,stroke:#333,stroke-width:2px;
    classDef backend_module fill:#bfb,stroke:#333,stroke-width:2px;
    classDef database fill:#f96,stroke:#333,stroke-width:2px;
```
