Documentation Index
Fetch the complete documentation index at: https://mintlify.com/andreguti9190/Sistema-de-gestion-de-ventas--Nodejs/llms.txt
Use this file to discover all available pages before exploring further.
Database Schema
The Sales Management System uses MySQL with a relational schema designed around sales, inventory, and client management.Overview
The database consists of 7 main tables:- roles - User role definitions
- users - System user accounts
- categories - Product categorization
- products - Inventory items
- clients - Customer information
- sales - Sales transactions
- sale_details - Line items for each sale
Schema Diagram
Table Definitions
roles
Defines user roles for access control.| Column | Type | Constraints | Description |
|---|---|---|---|
| id | INT | PRIMARY KEY, AUTO_INCREMENT | Unique role identifier |
| name | VARCHAR(50) | NOT NULL, UNIQUE | Role name (admin, employee) |
users
Stores system user accounts with role-based access.| Column | Type | Constraints | Description |
|---|---|---|---|
| id | BINARY(16) | PRIMARY KEY | UUID stored as binary |
| name | VARCHAR(100) | NOT NULL | User’s full name |
| VARCHAR(150) | NOT NULL, UNIQUE | User email address | |
| password | VARCHAR(255) | NOT NULL | Hashed password |
| role_id | INT | NOT NULL, FOREIGN KEY | Reference to roles.id |
| created_at | TIMESTAMP | DEFAULT CURRENT_TIMESTAMP | Account creation time |
| updated_at | TIMESTAMP | AUTO UPDATE | Last modification time |
User passwords should be hashed using bcrypt or similar before storage.
categories
Organizes products into categories.| Column | Type | Constraints | Description |
|---|---|---|---|
| id | INT | PRIMARY KEY, AUTO_INCREMENT | Unique category identifier |
| name | VARCHAR(100) | NOT NULL, UNIQUE | Category name (min 3 chars) |
| created_at | TIMESTAMP | DEFAULT CURRENT_TIMESTAMP | Creation timestamp |
products
Stores inventory items with pricing and stock information.| Column | Type | Constraints | Description |
|---|---|---|---|
| id | BINARY(16) | PRIMARY KEY | UUID for product |
| name | VARCHAR(150) | NOT NULL | Product name (min 3 chars) |
| description | TEXT | - | Detailed description (min 10 chars) |
| price | DECIMAL(10,2) | NOT NULL | Price with 2 decimal places |
| stock | INT | NOT NULL, DEFAULT 0 | Available quantity |
| category_id | INT | FOREIGN KEY | Reference to categories.id |
| is_deleted | BOOLEAN | DEFAULT FALSE | Soft delete flag |
| created_at | TIMESTAMP | DEFAULT CURRENT_TIMESTAMP | Creation time |
| updated_at | TIMESTAMP | AUTO UPDATE | Last update time |
clients
Stores customer information.| Column | Type | Constraints | Description |
|---|---|---|---|
| id | BINARY(16) | PRIMARY KEY | UUID for client |
| name | VARCHAR(100) | NOT NULL | Client’s full name |
| VARCHAR(150) | NOT NULL, UNIQUE | Client email address |
sales
Records sales transactions.| Column | Type | Constraints | Description |
|---|---|---|---|
| id | BINARY(16) | PRIMARY KEY | UUID for sale |
| clients_id | BINARY(16) | NOT NULL, FOREIGN KEY | Reference to clients.id |
| created_at | TIMESTAMP | DEFAULT CURRENT_TIMESTAMP | Sale timestamp |
sale_details
Stores line items for each sale transaction.| Column | Type | Constraints | Description |
|---|---|---|---|
| id | BINARY(16) | PRIMARY KEY | UUID for sale detail |
| sale_id | BINARY(16) | NOT NULL, FOREIGN KEY | Reference to sales.id |
| product_id | BINARY(16) | NOT NULL, FOREIGN KEY | Reference to products.id |
| quantity | INT | NOT NULL | Quantity sold |
ON DELETE CASCADE ensures that when a sale is deleted, all its line items are automatically removed.Relationships
One-to-Many Relationships
-
roles → users: One role has many users
- Enforced by:
users.role_id→roles.id
- Enforced by:
-
categories → products: One category has many products
- Enforced by:
products.category_id→categories.id - Constraint: Cannot delete category if products exist with that category
- Enforced by:
-
clients → sales: One client has many sales
- Enforced by:
sales.clients_id→clients.id - Constraint: Cannot delete client if they have sales
- Enforced by:
-
sales → sale_details: One sale has many line items
- Enforced by:
sale_details.sale_id→sales.id - Cascade: Deleting a sale deletes all its details
- Enforced by:
-
products → sale_details: One product appears in many sales
- Enforced by:
sale_details.product_id→products.id - Constraint: Cannot delete product if it appears in sales
- Enforced by:
UUID Usage
The system uses BINARY(16) to store UUIDs efficiently:- Tables using UUIDs: users, clients, products, sales, sale_details
- Tables using integers: roles, categories
Working with UUIDs
MySQL provides functions to convert between UUID strings and binary:src/model/categorie.model.js:33).
Stock Management
When a sale is created:- Product stock is validated (must have sufficient quantity)
- Stock is decremented by the quantity sold
- Transaction is recorded in
sale_details
Soft Deletes
Theproducts table uses a soft delete pattern:
is_deletedfield marks products as deleted without removing them- Preserves referential integrity with existing sales
- Products can be filtered in queries:
WHERE is_deleted = FALSE
Indexes
Primary Keys (Automatically Indexed)
- All
idcolumns are indexed as primary keys
Unique Indexes
roles.nameusers.emailcategories.nameclients.email
Foreign Key Indexes (Automatically Created)
users.role_idproducts.category_idsales.clients_idsale_details.sale_idsale_details.product_id
Database Size Considerations
| Table | Estimated Size per Row |
|---|---|
| roles | ~60 bytes |
| users | ~500 bytes |
| categories | ~120 bytes |
| products | ~300 bytes |
| clients | ~270 bytes |
| sales | ~50 bytes |
| sale_details | ~50 bytes |
Actual size varies based on data length. TEXT fields in products can significantly increase row size.
Next Steps
Architecture
Learn how the API interacts with the database
API Reference
See how to query and modify data
Error Handling
Handle foreign key constraint errors
Configuration
Configure database connection