# oktadev/okta-node-sql-server-example architecture

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

- Interactive diagram: https://gitdiagram.com/oktadev/okta-node-sql-server-example
- Repository: https://github.com/oktadev/okta-node-sql-server-example

## Overview

##### Project Type and Purpose
- Type: Full-stack web application (SPA front-end + RESTful back end).
- Purpose: A calendar/event management app secured with Okta, persisting data in SQL Server, presented by a Vue.js SPA and server-rendered views.

##### File Structure
Top-level folders:
- client/ – Vue.js single-page app (App.vue, index.js).
- src/ – Hapi server code:
– server.js + src/index.js: bootstraps the Hapi server.
– src/routes/ (API endpoints for events & auth).
– src/plugins/ (SQL connector + Okta auth plugin).
– src/data/ (data-access layer with raw SQL scripts & helper functions).
– src/views/ (EJS templates for server-rendered pages).
– src/config.js (env variables: ports, SQL, Okta).
- tasks/ – utility scripts (initdb.js + SQL to create schema).
- test/ – unit/integration tests.
- .env.sample – placeholder for environment configuration.

##### Extract Insights from README
- Stack: Node.js (Hapi), SQL Server, Vue.js, Okta OIDC.
- Execution flow:
– Developer sets up .env with DB and Okta credentials.
– tasks:initdb runs SQL scripts to create “calendar” database and events table.
– npm run dev spins up Hapi server (serves API + views) and possibly bundles the Vue client.
– User accesses http://localhost:8080; SPA communicates with back end.

##### Main Components & Their Roles
A. Client (Vue.js SPA)
– Runs in the browser.
– Handles user interface: event list, add/edit/delete forms, login/out.
– Communicates over HTTPS with the server’s REST API.
B. Hapi Server (Node.js)
– Serves static assets (Vue bundle) and EJS-rendered pages (login callback).
– Exposes REST endpoints under /api/events and /api/auth (in src/routes).
– Applies security via Okta plugin (src/plugins/auth.js).
– Connects to SQL Server via SQL plugin (src/plugins/sql.js).
C. SQL Server Database
– Stores calendar and event data.
– Initialized via tasks/initdb.js and SQL scripts in src/data/events.
D. External Service: Okta OIDC Provider
– Manages user authentication and token issuance.
– Server uses Okta SDK (plugin) to validate tokens and protect routes.
E. Development & Operational Utilities
– tasks/initdb: CLI task to set up the database schema.
– test suite: validates routes, plugins, and data layer.

##### Defining Relationships and Data Flows
1. User → Browser → Vue.js SPA
2. SPA → (1) GET/POST /api/events → Hapi Server → SQL Plugin → SQL Server
(2) redirects to Okta for login → Okta → callback → Hapi auth plugin
3. Hapi Server → Renders EJS views for login/callback pages
4. tasks:initdb CLI → Direct SQL Connection → SQL Server (one-time schema setup)
5. Tests → Mock server + database stubs

##### Architectural Patterns & Design Principles
- Layered Architecture:
– Presentation (client + views),
– API/Controller (routes),
– Plugin Services (auth, sql),
– Data Access Layer (raw SQL scripts + utils).
- Plugin-based extensibility: Hapi plugins for authentication and database.
- Separation of concerns:
– UI vs. API vs. Data
– Configuration in .env (via src/config.js)
- Security by externalizing auth to Okta (OAuth2 / OIDC).

## Diagram (Mermaid)

```mermaid
flowchart TD
    Client["Client (Vue.js SPA)"]:::frontend
    click Client "https://github.com/oktadev/okta-node-sql-server-example/tree/main/client/"

    subgraph "Hapi Server" 
        classDef backend fill:#cfc,stroke:#080;
        Bootstrap["Server Bootstrap\n(src/server.js, src/index.js)"]:::backend
        click Bootstrap "https://github.com/oktadev/okta-node-sql-server-example/blob/main/src/server.js"
        Config["Configuration\n(src/config.js)"]:::backend
        click Config "https://github.com/oktadev/okta-node-sql-server-example/blob/main/src/config.js"

        subgraph "Routes\n(src/routes)"
            Events["/api/events\n(events.js)"]:::backend
            click Events "https://github.com/oktadev/okta-node-sql-server-example/blob/main/src/routes/api/events.js"
            AuthRoute["/api/auth\n(auth.js)"]:::backend
            click AuthRoute "https://github.com/oktadev/okta-node-sql-server-example/blob/main/src/routes/auth.js"
            IndexRoute["Index Routes\n(index.js)"]:::backend
            click IndexRoute "https://github.com/oktadev/okta-node-sql-server-example/blob/main/src/routes/index.js"
        end

        subgraph "Plugins\n(src/plugins)"
            AuthPlugin["Auth Plugin\n(src/plugins/auth.js)"]:::backend
            click AuthPlugin "https://github.com/oktadev/okta-node-sql-server-example/blob/main/src/plugins/auth.js"
            SQLPlugin["SQL Plugin\n(src/plugins/sql.js)"]:::backend
            click SQLPlugin "https://github.com/oktadev/okta-node-sql-server-example/blob/main/src/plugins/sql.js"
        end

        subgraph "Data Access\n(src/data)"
            DataIndex["index.js"]:::backend
            click DataIndex "https://github.com/oktadev/okta-node-sql-server-example/blob/main/src/data/index.js"
            DataUtils["utils.js"]:::backend
            click DataUtils "https://github.com/oktadev/okta-node-sql-server-example/blob/main/src/data/utils.js"
            EventsData["Events Data Layer\n(src/data/events)"]:::backend
            click EventsData "https://github.com/oktadev/okta-node-sql-server-example/blob/main/src/data/events/index.js"
        end

        subgraph "Views\n(src/views)"
            ViewIndex["index.ejs"]:::backend
            click ViewIndex "https://github.com/oktadev/okta-node-sql-server-example/blob/main/src/views/index.ejs"
            Layout["layout.ejs"]:::backend
            click Layout "https://github.com/oktadev/okta-node-sql-server-example/blob/main/src/views/layout.ejs"
            NavPartial["navigation.ejs"]:::backend
            click NavPartial "https://github.com/oktadev/okta-node-sql-server-example/blob/main/src/views/partials/navigation.ejs"
        end
    end

    DB["SQL Server Database"]:::db
    Okta["Okta OIDC Provider"]:::external
    InitDB["Database Init Task\n(tasks/initdb.js)"]:::utility
    click InitDB "https://github.com/oktadev/okta-node-sql-server-example/blob/main/tasks/initdb.js"
    Tests["Test Suite\n(test/)"]:::utility
    click Tests "https://github.com/oktadev/okta-node-sql-server-example/tree/main/test/"

    NoteConfig[".env Configuration\n(.env.sample)"]:::utility

    Client -->|HTTPS: /api/events| Events
    Client -->|HTTPS: /api/auth| AuthRoute
    Events -->|"calls"| SQLPlugin
    SQLPlugin -->|"TDS/1433"| DB
    AuthRoute -->|"validates tokens"| AuthPlugin
    AuthPlugin -->|"OAuth2/OIDC"| Okta
    AuthRoute -->|"renders callback"| ViewIndex
    Bootstrap -->|"loads"| Config
    Bootstrap -->|"registers"| Events & AuthRoute & IndexRoute
    Bootstrap -->|"registers"| AuthPlugin & SQLPlugin
    InitDB -->|"executes SQL"| DB
    Tests -->|"mocks plugins & routes"| Bootstrap

    classDef frontend fill:#ccf,stroke:#036;
    classDef backend fill:#cfc,stroke:#292;
    classDef db fill:#fcf,stroke:#933;
    classDef external fill:#eee,stroke:#666,stroke-dasharray: 5 5;
    classDef utility fill:#ffc,stroke:#aa0;
```
