Codebase Structure
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". It is built with a Node.js and Express backend, a MongoDB database, and a vanilla JavaScript single-page frontend. The system provides role-based access control, distinguishing between regular users ('chatter') and 'admin' users, with admins having full CRUD (Create, Read, Update, Delete) capabilities over the model data, as well as import/export functionality.
The codebase is organized into a backend server, a database modeling layer, and a static frontend. An alternative Python-based command-line tool for managing a similar database in a JSON file is also present in the repository, suggesting a potential prototype or separate utility.
Overall Architecture
The application follows a classic three-tier architecture: a client-side frontend, a server-side backend API, and a database.
The diagram below illustrates the high-level interaction between these components.
This diagram shows the flow from the user's browser, through the Express.js backend API, to the MongoDB database, and back.
Sources: server.js, public/index.html, APP/models/index.js
Backend (Node.js / Express)
The backend is a single Express application defined in server.js. It handles API requests, user sessions, database interactions, and serving the frontend static files.
Sources: server.js, package.json
Server Initialization and Middleware
The main server.js file sets up the Express application and configures necessary middleware:
cors: Enables Cross-Origin Resource Sharing for all origins.express.json&express.urlencoded: Parses incoming JSON and URL-encoded payloads.express-session: Manages user sessions for authentication. A user's role ('admin' or 'chatter') is stored in the session upon login.multer: Handles file uploads, specifically for CSV import.express.static: Serves the static frontend files from thepublicdirectory.
The server connects to MongoDB using Mongoose, with the connection URI specified in an environment variable or a default value from APP/config/db.config.js.
Sources: server.js:2-26, APP/config/db.config.js:2
API Endpoints
All API routes are defined directly within server.js. Access to modification endpoints (POST, PUT, DELETE) and data export/import is restricted to admin users via the isAdmin middleware.
| Method | Path | Admin Only | Description |
|---|---|---|---|
POST | /api/login | No | Creates a user session and assigns a role based on email address. |
POST | /api/logout | No | Destroys the user session. |
GET | /api/models | No | Retrieves a list of all models, sorted by name. |
GET | /api/models/:id | No | Retrieves a single model by its _id. |
POST | /api/models | Yes | Creates a new model and logs the creation event in its history. |
PUT | /api/models/:id | Yes | Updates an existing model and logs the update event in its history. |
DELETE | /api/models/:id | Yes | Deletes a model by its _id. |
GET | /api/export/json | Yes | Exports all model data as a JSON file. |
GET | /api/export/csv | Yes | Exports all model data as a CSV file. |
POST | /api/import/csv | Yes | Imports model data from an uploaded CSV file using multer. |
Sources: server.js:28-140
Authentication and Authorization
Authentication is handled via a simple session-based mechanism. A user logs in by POSTing their email to /api/login. The server determines the user's role based on the email format (@admin.com for admins) and stores it in the session.
The isAdmin middleware function checks for an active session and the 'admin' role before allowing access to protected routes.
// server.js:41-44
const isAdmin = (req, res, next) => {
if (req.session.user && req.session.user.role === 'admin') next();
else res.status(403).json({ message: 'Admin required' });
};
Sources: server.js:28-44
Database Layer (Mongoose)
The application uses Mongoose as an Object Data Modeling (ODM) library to interact with MongoDB.
Sources: APP/models/index.js, APP/models/model.model.js, APP/config/db.config.js
Database Configuration
The MongoDB connection string is managed by APP/config/db.config.js. It prioritizes the MONGODB_URI environment variable and falls back to a local database instance if the variable is not set.
Sources: APP/config/db.config.js:2
Model Schema
The data structure for a "model" is defined in APP/models/model.model.js. This schema includes fields for personal details, pricing information, and content guidelines. A history array is maintained to track changes to each document.
The diagram below represents the Model entity relationship model.
This ER diagram shows the fields and types defined in the Mongoose schema for the Model collection.
Sources: APP/models/model.model.js:2-15
A pre('save') middleware hook automatically adds an entry to the history array whenever a document is updated, capturing the timestamp, action, and modified fields.
Sources: APP/models/model.model.js:17-26
Data Seeding
The seed.js script is provided to populate the database with initial data. It connects to the database and uses Model.insertMany() to bulk-insert an array of model objects. This is useful for setting up a development or production environment with a baseline dataset.
Sources: seed.js
Frontend (public/index.html)
The frontend is a single HTML file that contains all the necessary HTML, CSS (via a link to output.css), and JavaScript to create a dynamic user interface. It communicates with the backend via fetch API calls.
Sources: public/index.html
UI and Client-Side Logic
The UI allows users to:
- Log in with an email.
- View a grid of all models.
- Search for models by name or profile content.
- Click on a model to view its details in a modal window.
- (Admins only) Add, edit, and delete models.
- (Admins only) Export data to JSON/CSV and import from CSV.
The following sequence diagram illustrates the flow for a user logging in and fetching the list of models.
This diagram shows the sequence of API calls from login to displaying data on the frontend.
Sources: public/index.html:84-110, server.js:28-32, 48-55
Alternative Implementation (model_database.py)
The repository includes a Python script, model_database.py, which provides a command-line interface for managing a model database stored in a local models.json file. This appears to be a separate, parallel implementation or an early prototype.
The ModelDatabase class in the script encapsulates all logic for loading, saving, adding, removing, and viewing models.
This class diagram outlines the structure of the Python-based database management tool.
Sources: model_database.py:4-80
Summary
The LostData codebase is a well-structured, full-stack application using a Node.js/Express/MongoDB stack. It features a clear separation between the frontend client, backend API server, and database layer. Key features include role-based authentication, full CRUD functionality for a "Model" resource, and data import/export capabilities. The inclusion of a separate Python script for local JSON file management suggests an evolution from a simpler, file-based system to the current database-driven web application.
Sources: server.js, public/index.html, model_database.py