A high-performance backend server for the Four Cubes Management System, modernized to run on Bun for optimal performance while maintaining full backward compatibility with the legacy frontend.
- Bun-Native Performance: Optimized server using Bun's native
Bun.serve()API for maximum performance - Express Fallback: Compatible Express.js server for environments where Bun isn't available
- PostgreSQL Database: Managed with Prisma ORM
- Bundle Management: Create, track, and manage steel bundle inventory
- Label Generation: QR code-based label printing system
- Die Mutation Tasks: New endpoint for managing obsolete/defective bundles
- Legacy Frontend Compatible: All existing API endpoints maintained
- Bun >= 1.1.30 (recommended) or Node.js >= 16.x (fallback)
- PostgreSQL database
- Yarn (for dependency management)
git clone https://github.com/abhalala/fcms-server.git
cd fcms-servercurl -fsSL https://bun.sh/install | bash# Using Yarn (recommended for canvas compatibility)
yarn install
# Or using Bun (may have issues with native modules like canvas)
bun installCreate a .env file in the root directory:
DATABASE_URL="postgresql://username:password@host:port/database"
HOST="localhost"
PORT=3000yarn generate
# or
bun generatenpx prisma migrate deploybun run dev
# or
bun --watch ./src/index.bun.tsbun run dev:expressbun run dev:node
# or
yarn dev:nodebun run start
# or
bun ./src/index.bun.tsbun run start:express
# or for Node.js
bun run start:nodebun run studio
# or
yarn studiodocker build -t fcms-server .docker run -d \
--name fcms-server \
-p 3000:3000 \
-e DATABASE_URL="postgresql://username:password@host:port/database" \
-e HOST="0.0.0.0" \
-v $(pwd)/data:/app/data \
-v $(pwd)/cache:/app/cache \
fcms-serverCreate a docker-compose.yml:
version: '3.8'
services:
fcms-server:
build: .
ports:
- "3000:3000"
environment:
DATABASE_URL: "postgresql://fcms:password@db:5432/fcms_db"
HOST: "0.0.0.0"
volumes:
- ./data:/app/data
- ./cache:/app/cache
depends_on:
- db
db:
image: postgres:14
environment:
POSTGRES_USER: fcms
POSTGRES_PASSWORD: password
POSTGRES_DB: fcms_db
volumes:
- postgres_data:/var/lib/postgresql/data
ports:
- "5432:5432"
volumes:
postgres_data:Then run:
docker-compose up -dhttp://localhost:3000/api
Check server status.
Response:
{
"status": 200
}Get the current bundle number counter.
Response:
{
"currentNumber": "123"
}Set the bundle number counter.
Request Body:
{
"number": "150"
}Create a new bundle.
Request Body:
{
"cutlength": "12.5",
"quantity": "100",
"weight": "250.5",
"cast_id": "CAST123",
"vs_no": "VS001",
"po_no": "PO2024001",
"location": "1"
}Response:
{
"uid": "clx...",
"sr_no": "25A123",
"length": 12.5,
"quantity": 100,
"weight": 250.5,
...
}Get recent bundles.
Response:
{
"recentBundles": [...]
}Get bundle details by UID.
Modify an existing bundle.
Print bundle label (layout: 0 or 1).
Get all variants with normalized range values.
Response:
{
"variants": [
{
"s_no": "VS001",
"series": "A-Series",
"range": "{\"start\":10,\"end\":20}"
}
]
}Move bundles from stock to sold.
Request Body:
{
"moveData": "25A123,25A124,25A125",
"ref": "INV-2024-001"
}Response:
{
"done": true
}Die mutation tasks are used to mark bundles as obsolete, defective, or otherwise removed from active inventory.
Process bundles for die mutation (batch operation).
Request Body:
{
"bundles": ["25A123", "25B456", "25C789"],
"reason": "defective",
"notes": "Material defects found during inspection"
}Valid reasons: defective, lost, damaged, other
Response:
{
"success": true,
"processed": 3,
"failed": 0,
"results": [
{
"sr_no": "25A123",
"status": "mutated",
"uid": "clx..."
}
],
"errors": []
}Get all mutated bundles (paginated).
Response:
{
"bundles": [...],
"total": 42,
"limit": 100,
"offset": 0
}Permanently delete a mutated bundle.
Response:
{
"success": true,
"deleted": {
"uid": "clx...",
"sr_no": "25A123"
}
}The server supports two runtime modes:
-
Bun-Native Mode (
src/index.bun.ts)- Uses Bun's
Bun.serve()API - Optimized route handlers in
src/routes-bun/ - Parallel request processing
- Bun's fast file I/O for bundle number management
- ~2-3x faster than Express mode
- Uses Bun's
-
Express Mode (
src/index.ts)- Traditional Express.js server
- Standard route handlers in
src/routes/ - Compatible with Node.js runtime
- Full compatibility layer for legacy environments
The Bun-native server includes several performance optimizations:
- Parallel Processing: Database queries execute in parallel where possible
- Fast File I/O: Uses
Bun.file()andBun.write()for optimal file operations - Lazy Module Loading: Canvas module loads only when needed (for label generation)
- Zero-Copy JSON: Bun's native JSON parsing is significantly faster
- Efficient Request Handling: Direct Response objects without middleware overhead
Managed by Prisma ORM with three main models:
- Bundle: Active inventory bundles
- soldBundle: Sold/moved bundles with reference tracking
- Variant: Steel section variants with specifications
Status enum: STOCK, ORDERED, SOLD, RETURNED
The server maintains 100% API compatibility with the existing frontend:
- All original endpoints preserved
- Same request/response formats
- Same port (3000) by default
- CORS enabled for cross-origin requests
- Existing authentication flows maintained
The frontend can use this Bun-based backend as a drop-in replacement:
- No frontend code changes required
- Same API endpoints and data formats
- Improved response times
- Better concurrency handling
# With Bun
bun test
# With Node
yarn test# Health check
curl http://localhost:3000/api
# Get variants
curl http://localhost:3000/api/variant/all
# Die mutation example
curl -X POST http://localhost:3000/api/die-mutation/tasks \
-H "Content-Type: application/json" \
-d '{"bundles": ["25A123"], "reason": "defective"}'The canvas npm module (used for label generation) has native dependencies. It works in both environments but:
- In Docker: System dependencies are installed automatically
- In Development: Works with existing Node.js installation
- Lazy Loading: Module loads only when
/api/bundle/print/endpoint is called
If label generation isn't needed, the server runs without canvas installed.
When updating the schema:
# Create migration
npx prisma migrate dev --name migration_name
# Apply in production
npx prisma migrate deployPreliminary benchmarks show significant improvements with Bun:
| Operation | Node.js + Express | Bun + Express | Bun Native |
|---|---|---|---|
| Simple GET | ~5ms | ~3ms | ~1ms |
| JSON Response | ~8ms | ~4ms | ~2ms |
| DB Query | ~15ms | ~12ms | ~10ms |
| Parallel Ops | ~50ms | ~30ms | ~20ms |
Results may vary based on system configuration and database performance
Private
- bhalalansh anshb@duck.com
This is a private project. For issues or questions, contact the repository owner.
Built with β‘ Bun for optimal performance