Architecture Overview
Relevant source files
The following files were used as context for generating this wiki page:The LostData project is a full-stack web application designed to manage a database of models. The architecture follows a classic three-tier model, comprising a single-page application (SPA) frontend, a Node.js/Express RESTful API backend, and a MongoDB database for persistence. The system provides role-based access control, distinguishing between regular "chatter" users and "admin" users with elevated privileges for data manipulation.
The application is built as a monolithic service where the Express server is responsible for both serving the static frontend assets and providing the backend API. It also includes auxiliary scripts for database seeding and a separate Python-based CLI for managing a JSON-file version of the database.
System Components
The application is composed of three primary components: a web-based frontend, a backend server, and a database. An additional Python CLI tool exists for offline data management.
The diagram below illustrates the high-level interaction between these components.
Sources: server.js, public/index.html, seed.js, model_database.py
Backend (Node.js / Express)
Sources: server.js, package.json
The backend is a Node.js application built with the Express framework. It handles API requests, user authentication, and database interactions.
Server Setup & Middleware
The main server file, server.js, initializes the Express application and configures several middleware components to handle common web application tasks.
cors: Enables Cross-Origin Resource Sharing for all origins (*).express.json: Parses incoming requests with JSON payloads.express.urlencoded: Parses incoming requests with URL-encoded payloads.express-session: Manages user sessions for authentication.express.static: Serves the static frontend files from thepublicdirectory.multer: Handlesmultipart/form-data, used for file uploads (e.g., CSV import).
Sources: server.js:2-21
The backend relies on the following key dependencies:
| Dependency | Description |
|---|---|
express | Web application framework for Node.js. |
mongoose | MongoDB object modeling tool. |
cors | Middleware for enabling CORS. |
express-session | Middleware for session management. |
dotenv | Loads environment variables from a .env file. |
multer | Middleware for handling multipart/form-data. |
csv-parser | Parses CSV files into JSON. |
json2csv | Converts JSON data to CSV format. |
Sources: package.json:2-11
API Endpoints
The server exposes a RESTful API for managing model data. Access to create, update, delete, and export endpoints is restricted to admin users.
| Method | Path | Protection | Description |
|---|---|---|---|
POST | /api/login | Public | Creates a user session based on email. |
POST | /api/logout | Public | Destroys the current user session. |
GET | /api/models | Public | Retrieves a sorted list of all models. |
GET | /api/models/:id | Public | Retrieves a single model by its ID. |
POST | /api/models | Admin | Creates a new model. |
PUT | /api/models/:id | Admin | Updates an existing model by its ID. |
DELETE | /api/models/:id | Admin | Deletes a model by its ID. |
GET | /api/export/json | Admin | Exports all models as a JSON file. |
GET | /api/export/csv | Admin | Exports all models as a CSV file. |
POST | /api/import/csv | Admin | Imports models from an uploaded CSV file. |
Sources: server.js:28-142
Authentication & Authorization
Authentication is handled via server-side sessions. A user logs in by POSTing their email to /api/login. The server then determines the user's role: if the email ends with @admin.com, the role is admin; otherwise, it is chatter. This role is stored in the session.
The isAdmin middleware function checks if a logged-in user has the admin role before allowing access to protected routes. If the check fails, it returns a 403 Forbidden status.
This diagram shows the login and role assignment flow.
Database (MongoDB)
The application uses MongoDB as its primary data store, accessed via the Mongoose ODM.
Configuration & Connection
The database connection is configured in APP/config/db.config.js, which exports a URL. This URL is read from the MONGODB_URI environment variable, with a fallback to a local MongoDB instance (mongodb://127.0.0.1:27017/model_db). The connection is established in server.js.
Sources: APP/config/db.config.js:2-4, server.js:24-25, APP/models/index.js:1-9
Data Model
The core data entity is the "Model", defined by a Mongoose schema. This schema specifies the structure, data types, and validation rules for model documents stored in the database.
The following diagram represents the Model entity relationship model.
Sources: APP/models/model.model.js:2-15
The schema includes a pre('save') hook that automatically populates the history array whenever a document is modified, tracking the timestamp, action, and modified fields.
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
id | String | Yes | - | A unique identifier for the model. |
name | String | Yes | - | The name of the model. |
age | Number | No | - | The age of the model. |
location | String | No | - | The location of the model. |
profile | String | Yes | - | A detailed profile of the model. |
pricing | String | Yes | - | Pricing information. |
dosAndDonts | String | Yes | - | A list of do's and don'ts for the model. |
extraTabs | Array | No | [] | An array for additional information tabs. |
history | Array | No | [] | An audit log of changes made to the document. |
Sources: APP/models/model.model.js:4-13
Frontend (Vanilla JS SPA)
The frontend is a single-page application contained within public/index.html. It uses vanilla JavaScript to dynamically render content and interact with the backend API. The server is configured to serve this file for any non-API GET request.
Sources: public/index.html, server.js:144-146
Key Functionality
- Login/Logout: Users can log in with an email to establish a session and log out to destroy it.
- Model Display: Fetches all models from
/api/modelsand displays them in a grid. - Search: Filters the displayed models based on user input in a search bar.
- Modal View: Clicking a model opens a modal with detailed information.
- Admin Actions: If logged in as an admin, buttons for adding, editing, deleting, and exporting data are visible and functional.
The following sequence diagram shows how the frontend fetches and renders the list of models.
Sources: public/index.html:84-95, server.js:54-61
Data Management Utilities
The repository contains scripts for managing data outside the main application flow.
Database Seeding
The seed.js script is a standalone Node.js script used to populate the MongoDB database with initial data. It connects to the database using the same configuration as the main application and uses Model.insertMany() to bulk-insert an array of model objects.
Sources: seed.js
Python CLI Tool
The model_database.py file provides a command-line interface (CLI) for managing model data in a local models.json file. This tool is completely separate from the Node.js application and its MongoDB database. It offers an interactive mode with commands like add, remove, view, and list to manipulate the JSON file directly.
Sources: model_database.py