Minimata is a robust and scalable CRM (Customer Relationship Management) system with minimal reliance on third-party tools. It leverages PostgreSQL as the database and Prisma as the (Object-Relational Mapping) ORM for efficient data interaction. The system features custom authentication, role-based access control, secure password storage, and session management, all implemented from scratch to maximize control and minimize external dependencies. Ad-ons include integration with Neon for enhanced scalability and serverless capabilities.
Minimata Project Root
├── README.md # Project documentation
├── package.json # Monorepo config, root scripts
├── .env # Environment variables
├── turbo.json # Turborepo pipeline config
├── apps/ # All deployable applications
│ ├── web/ # Next.js Front-End SPA
│ │ ├── __tests__/ # Unit/Component Tests
│ │ ├── public/ # Static assets
│ │ ├── src/
│ │ │ ├── app/ # Next.js app directory (routing)
│ │ │ │ ├── api/ # Next.js API routes (replaces Express)
│ │ │ │ │ ├── auth/ # Authentication API endpoints
| | | | | | ├── approve/
| | | | | | │ └── route.ts # Approve API (migrated from Express)
| | | | | | ├── me/
| | | | | | │ └── route.ts # Get authenticated user info (Next.js API)
| | | | | | ├── new-users/
| | | | | | └── route.ts # List new users (migrated from Express)
│ │ │ │ │ ├── contact/ # Contact form processing endpoint
│ │ │ │ │ ├── accounts/ # CRUD endpoints for accounts/CRM
│ │ │ │ │ └── middleware.ts # Optional middleware for API routes
│ │ │ │ ├── contact/ # Contact page and components
│ │ │ │ ├── dashboard/ # Dashboard route (role-based)
│ │ │ │ ├── login/ # Login page
│ │ │ │ ├── register/ # Register page
│ │ │ │ ├── unauthorized/ # Unauthorized page
│ │ │ │ ├── page.tsx # Home page
│ │ │ │ ├── layout.tsx # App-wide layout
│ │ │ │ ├── tailwind.css # Global styles
│ │ │ │ └── favicon.ico # Favicon
│ │ │ ├── components/ # Shared UI components
│ │ │ ├── hooks/ # Custom React hooks
│ │ │ ├── utils/ # Shared utilities
│ │ │ ├── prisma/ # Prisma client and schema
│ │ │ └── types/ # Local types
│ │ ├── package.json # Next.js app dependencies
│ │ └── tsconfig.json # TS config for web app
├── packages/ # Shared libraries
│ ├── ui/ # Shared UI components (lib)
│ ├── utils/ # Shared utilities (lib)
│ ├── types/ # Shared TypeScript types
│ └── db/ # Prisma client and helpers
│ ├── prisma/
│ ├── package.json
├── scripts/ # Dev/ops scripts
├── tests/ # End-to-end/integration tests
README.md
— Project overview, setup, and instructions.package.json
— Project dependencies and scripts..env
— Environment variables (e.g., database connection string, JWT secrets).prisma/
— Database schema and migrations.
schema.prisma
— Database schema (Prisma SDL).migrations/
— Database migration files.scripts/
— Utility scripts for development/ops tasks.tests/
— General test suites.
client/
— Client-side E2E/integration tests.src/server/
src/server/
auth/
auth.service.ts
— Authentication logic (registration, login, logout).auth.controller.ts
— API endpoints for authentication.auth.model.ts
— Authentication data structures.middleware/
— Express/Node middleware (JWT, RBAC, error handler, CSRF, etc.).routes/
— API routes and handlers (user, account, contact, KPI, etc.).utils/
— Utility functions (password, JWT, CORS, logger, Prisma client singleton, etc.).validators/
— Input validation and sanitization logic.server.ts
— Server entry point.client/
client/
src/
components/
— Reusable UI components.context/
— React context providers.pages/
— Application pages.utils/
— Client-side utilities.services/
, styles/
, App.tsx
as needed)public/
— Static assets for classic React.apps/web/
apps/web/
public/
— Static assets for Next.js app.__tests__/
— Next.js-specific unit/integration tests.src/
app/
— Next.js 13+ app directory (layouts, routing, server components).
(auth)/login
— Login page/route.(auth)/register
— Registration page/route.(protected)/dashboard
— Protected dashboard page/route.(public)/contact
— Public contact page/route.(public)/unauthorized
— Unauthorized page/route.components/
— Shared UI components.hooks/
— Custom React hooks.pages/
api/
— Next.js API routes (hybrid API).
auth/
— API routes for authentication.utils/
— Client-side utilities (e.g., sanitization, API helpers).src/types/
— Shared type definitions.
express/
— Express request/response extensions (e.g., req.user
type).prisma/
schema.prisma
— Main DB schema.migrations/
— All migration folders.Other
Backend:
Frontend:
Dev Tools:
Logging:
API & Docs:
Testing:
Deployment & Scaling:
Deployment (Exclusive Feature):
Update the package list and upgrade existing packages:
sudo apt update
sudo apt upgrade
Using nvm (Node Version Manager) is recommended for managing multiple Node.js versions:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.bashrc
nvm install --lts
nvm use --lts
Alternatively, install directly:
sudo apt install nodejs npm
sudo apt install postgresql postgresql-contrib
npm install -g prisma
sudo service postgresql start
or via Docker:
# Create Container
docker-compose up -d
# Verify Initialization Logs
docker logs postgres_container # Look for message similar to "PostgreSQL init process complete; ready for start up."
# Access the PostgreSQL Container
docker exec -it postgres_container bash
# Open PostgeSQL Database
sudo -u postgres psql
# Create Username and Password
CREATE USER your_username WITH PASSWORD 'your_password'; # Bypass if using .env
# Create Database with Owner
CREATE DATABASE your_database WITH OWNER your_username; # Bypass if using .env
# Grant Privileges to User
GRANT ALL PRIVILEGES ON DATABASE your_database TO your_username; # Bypass if using .env
# List Roles
\du
# List DBs
\l
# Exit PostgreSQL
\q
# Exit Docker Postgres Container
exit
If not already navigate your project directory:
cd <project_directory>
# Generate Default JSON Package
npm init -y
# Install dependencies
npm install # Or yarn
# Generate Secure JWT Secret
npx generate-jwt-secret --length 32
Create a .env
file in the project root directory and populate it with necessary environment variables, including the PostgreSQL connection string. Ensure this file is excluded from version control (add it to .gitignore). Example:
DATABASE_URL=postgresql://your_username:your_password@localhost:5432/your_database?schema=public
JWT_SECRET=your_secure_jwt_secret
# Create Prisma Directory
mkdir -p prisma
# Create Prisma Schema File (Model Definitions)
touch prisma/schema.prisma
# Generate Prisma Client
npx prisma generate
# Run Migrations
npx prisma migrate dev --name init
npm run start:server
# From New Terminal
npm run start:client
http://localhost:3000
(or the port specified in your server configuration) to access the application.Backend:
/register
endpoint for user data (email, password, name, role). Hash password (Argon2, scrypt) and store in User table./login
endpoint for credentials. Verify password hash, generate JWT (and refresh token if needed), return to client.Frontend:
/login
./register
.How it works:
prisma/schema.prisma
: User, Account, Contact, KpiDefinition, KpiValue (with fields/relationships).npx prisma migrate dev --name <migration_name>
to create/update tables.How it works:
body('email').isEmail()
) to route handlers.validationResult(req)
to check for errors and return them if present./api/auth/register
and /api/auth/login
.src/server/middleware/rbac.ts
to enforce role-based access for protected routes.How it works:
import { rbac } from './middleware/rbac';
app.use('/admin', rbac(['admin']));
req.user.roles
is set by your authentication/JWT middleware.How it works:
src/server/utils/logger.ts
.src/server/server.ts
to use Winston as its output stream.tests/
directory, with separate files for unit tests and integration tests.npm test
or npx jest
./api/docs
and is generated from JSDoc comments in your route files.src/server/utils/swagger.ts
and integrated in src/server/server.ts
.How it works:
/api/docs
.src/server/server.cluster.ts
file uses the Node.js cluster module to spawn multiple worker processes, leveraging all available CPU cores for maximum throughput and resilience.How it works:
ts-node src/server/server.cluster.ts
(or compile and run the JS output).pm2 start dist/server/server.cluster.js -i max
(for max CPU scaling).PM2 Example Commands:
# Install PM2 globally
npm install -g pm2
# Start the clustered server with max workers
pm2 start dist/server/server.cluster.js -i max --name minimata-api
# Monitor logs and processes
pm2 logs
pm2 monit
# Reload with zero downtime
pm2 reload minimata-api
# Stop and delete
pm2 stop minimata-api
pm2 delete minimata-api
This roadmap provides a structured approach to building the CRM, allowing for phased development and ensuring a robust and scalable solution while minimizing third-party dependencies.