Technology Stack
Relevant source files
The following files were used as context for generating this wiki page:The LostData project is a full-stack web application built primarily on a Node.js backend, a MongoDB database, and a vanilla JavaScript frontend. The system is designed to manage a database of "models," providing functionalities for creating, reading, updating, and deleting (CRUD) model profiles. It includes role-based access control, data import/export features, and a separate Python-based command-line tool for local data management.
The overall architecture separates concerns between the client-side interface, the server-side business logic and API, and the database persistence layer. The backend is powered by the Express.js framework, which handles API requests, user sessions, and serves the static frontend files.
High-Level Architecture
The diagram below illustrates the main components of the application and their interactions.
Backend
The backend is built with Node.js and the Express.js framework. It is responsible for serving the frontend, providing a RESTful API for data management, handling user authentication and sessions, and interacting with the MongoDB database.
Sources: server.js, package.json
Core Dependencies
The backend relies on several key npm packages for its functionality, as defined in package.json.
| Package | Version | Description |
|---|---|---|
express | ^5.1.0 | Web application framework for Node.js, used for routing and middleware. |
mongoose | ^8.19.0 | MongoDB object modeling tool (ODM) used to define schemas and interact with the database. |
cors | ^2.8.5 | Middleware to enable Cross-Origin Resource Sharing. Configured to allow all origins (*). |
dotenv | ^17.2.3 | Loads environment variables from a .env file into process.env. |
express-session | ^1.18.2 | Middleware for managing user sessions. Used for role-based access control. |
multer | ^2.0.2 | Middleware for handling multipart/form-data, used for file uploads (CSV import). |
csv-parser | ^3.2.0 | Parses CSV file streams. Used in conjunction with multer for the CSV import feature. |
json2csv | ^6.0.0-alpha.2 | Converts JSON data to CSV format for the data export feature. |
Sources: package.json:2-11, server.js:2-10
API Endpoints
The server.js file defines a REST API for managing model data. Access to modification endpoints (POST, PUT, DELETE) and data import/export is restricted to users with an 'admin' role.
| Method | Path | Protection | Description |
|---|---|---|---|
POST | /api/login | None | Creates a user session and assigns a role. |
POST | /api/logout | None | Destroys the user session. |
GET | /api/models | None | Retrieves a 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:28-142
The following sequence diagram shows the flow for an admin user updating a model.
Sources: server.js:80-89, public/index.html
Database
The application uses MongoDB as its database, with Mongoose serving as the Object Data Modeling (ODM) library to interface with it.
Sources: APP/config/db.config.js, APP/models/index.js, server.js:24
Configuration
The database connection URI is managed through an environment variable MONGODB_URI, with a fallback to a local MongoDB instance.
// APP/config/db.config.js
module.exports = {
url: process.env.MONGODB_URI || "mongodb://127.0.0.1:27017/model_db"
};
Sources: APP/config/db.config.js:1-3
Schema Definition
The Model schema defines the structure for documents in the models collection. It includes fields for model profile information, pricing, rules, and a history log to track changes.
The diagram below represents the Mongoose schema for a Model.
Sources: APP/models/model.model.js:2-15
The schema fields are detailed in the table below:
| 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 description. | |
pricing | String | Yes | Pricing information for services. | |
dosAndDonts | String | Yes | A list of allowed and disallowed activities. | |
extraTabs | Array | No | [] | An array for additional information tabs. |
history | Array | No | [] | An array of objects logging changes (timestamp, action, user, changes). |
Sources: APP/models/model.model.js:2-15
A pre-save hook is implemented to automatically add an entry to the history array whenever a document is modified.
Sources: APP/models/model.model.js:18-27
Frontend
The frontend is a single-page application (SPA) consisting of a single index.html file, styled with Tailwind CSS (via output.css), and powered by vanilla JavaScript. It communicates with the backend via the fetch API to perform all data operations.
Sources: public/index.html, Index.html
Key frontend functionalities include:
- User Login/Logout: Controls access to admin features.
- Model Display: Fetches and displays all models in a grid.
- Search: Filters models based on user input.
- Modal View: Shows detailed information for a selected model.
- Admin Controls: Buttons for adding, editing, and deleting models are visible only to admin users.
- Data Management: Provides UI for exporting data to JSON/CSV and importing from CSV.
Sources: public/index.html:10-170
Auxiliary Tooling
Python CLI Tool
A standalone Python script, model_database.py, provides a command-line interface for managing a local models.json file. This tool is separate from the main Node.js/MongoDB stack and appears to be for local, offline data management or initial data preparation. It supports adding, removing, viewing, and listing models in an interactive session.
Sources: model_database.py
Database Seeding
The seed.js script is 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 insert a predefined array of model objects. This is a common practice for setting up a development or production environment with default data.
Sources: seed.js