Welcome to LostData
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 as a "Model Database". It provides a platform for managing detailed profiles of models, including personal information, pricing, content guidelines, and more. The system features a Node.js backend with an Express server, a MongoDB database for data persistence, and a dynamic single-page frontend built with vanilla JavaScript and styled with TailwindCSS.
The application supports role-based access control, distinguishing between regular users ('chatter') and administrators ('admin'). Administrators have extended privileges, including creating, updating, and deleting model entries, as well as importing and exporting data in JSON and CSV formats. The project also includes a separate Python-based command-line tool for managing a local JSON version of the database, which operates independently of the main web application.
System Architecture
The application follows a classic three-tier architecture: a client-side frontend, a server-side backend with a REST API, and a database.
Sources: server.js, public/index.html, APP/models/model.model.js
This diagram shows the overall architecture, including the main web application stack and the separate Python CLI tool.
Core Technologies
The application is built upon several key technologies and libraries defined in its package.json file.
| Dependency | Description |
|---|---|
express | Web framework for Node.js, used to build the server and API. |
mongoose | Object Data Modeling (ODM) library for MongoDB and Node.js. |
cors | Middleware to enable Cross-Origin Resource Sharing. |
express-session | Middleware for managing user sessions for authentication. |
multer | Middleware for handling multipart/form-data, used for file uploads. |
csv-parser | Stream-based parser for converting CSV data to JSON. |
json2csv | Converter to transform JSON data back into CSV format. |
dotenv | Loads environment variables from a .env file. |
Sources: package.json:2-10
Backend System
The backend is a Node.js application responsible for business logic, data persistence, and serving the frontend.
Sources: server.js
Web Server & Middleware
The main entry point, server.js, initializes an Express application and configures several middleware components that process incoming requests before they reach the route handlers.
This diagram illustrates the request processing pipeline through the configured Express middleware.
Sources: server.js:14-22
Database & Data Model
The application uses MongoDB as its database, with Mongoose managing the connection and data schema.
Database Configuration
The MongoDB connection URI is specified in 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. The connection is established in server.js.
Sources: APP/config/db.config.js:2-3, server.js:25
Mongoose Schema
The Model schema defines the structure for documents stored in the database. It includes fields for model attributes and an automated history-tracking mechanism.
This diagram shows the fields and methods defined in the Mongoose schema for a model.
The schema includes the following key fields:
| Field | Type | Required | Description |
|---|---|---|---|
id | String | Yes | A unique identifier for the model. |
name | String | Yes | The name of the model. |
profile | String | Yes | A detailed profile description. |
pricing | String | Yes | Pricing information for services. |
dosAndDonts | String | Yes | A list of allowed and disallowed activities. |
history | Array | No | An array of objects tracking changes to the document. |
A pre('save') hook automatically adds an entry to the history array whenever a document is modified, capturing the timestamp, action, and modified fields.
Sources: APP/models/model.model.js:3-26
REST 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.
| Method | Endpoint | Protection | Description |
|---|---|---|---|
POST | /api/login | None | Authenticates a user and creates a session. |
POST | /api/logout | None | Destroys the user's session. |
GET | /api/models | None | Retrieves a sorted list of all models. |
GET | /api/models/:id | None | 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:27-130
Authentication & Authorization
Authentication is handled using express-session. A user's role is determined at login based on their email address: if the email ends with @admin.com, the role is set to 'admin'; otherwise, it is 'chatter'.
An isAdmin middleware function protects sensitive routes by checking if req.session.user.role is 'admin'. If not, it returns a 403 Forbidden status.
Sources: server.js:32-34, server.js:41-44
This diagram shows the logic of the isAdmin middleware used to protect admin-only endpoints.
Frontend System
The user interface is a single-page application provided by public/index.html. It uses vanilla JavaScript to interact with the backend API, render data, and handle user input.
Sources: public/index.html
Key Features
- Login: Users can log in with an email address. The UI adapts based on the returned role ('admin' or 'chatter').
- Model Display: Models are fetched from
/api/modelsand displayed in a grid. - Search: A search input filters the displayed models in real-time based on name or profile content.
- Modal View: Clicking a model opens a modal with detailed information.
- Admin Controls: For admin users, buttons for adding, editing, and deleting models are visible.
- Data Management: Admins have access to buttons for exporting data to JSON/CSV and importing from a CSV file.
Data Flow: Fetching and Rendering Models
The following diagram illustrates how the frontend fetches and displays the list of models.
Sources: public/index.html:103-114, server.js:51-58
Ancillary Scripts
Database Seeding
The seed.js script is a standalone utility for populating the MongoDB database with initial data. It connects to the database and uses Model.insertMany() to insert a predefined array of model objects. This is useful for setting up a new development environment with test data.
Sources: seed.js
Python CLI Tool
The repository includes model_database.py, a Python script that provides a command-line interface for managing model data. This tool is completely separate from the Node.js/MongoDB stack and persists its data in a local models.json file. It supports adding, removing, viewing, and listing models through an interactive prompt.
Sources: model_database.py