# ๐ฎ Game Hub NestJS Backend


**A scalable backend API for a multiโgame platform**
Game Hub is a RESTful API backend built with NestJS and TypeORM that manages users, game items, online storage, a marketplace, and a unified wallet system. It currently supports games like Space Engineers and Valheim, providing a modular architecture that allows for easy integration of additional games in the future.
## ๐ Table of Contents
- [Features](#-features)
- [Architecture](#-architecture)
- [System Requirements](#-system-requirements)
- [Quick Start](#-quick-start)
- [Environment Configuration](#-environment-configuration)
- [Database Migrations](#-database-migrations)
- [Running the Application](#-running-the-application)
- [Testing](#-testing)
- [Project Structure](#-project-structure)
- [Contributing](#-contributing)
- [License](#-license)
## โจ Features
### ๐ Authentication
- **JWT-based authentication** for secure API access.
- **Steam OAuth integration** for seamless platform login, enhancing user experience.
- **Role-based access control** to enforce fineโgrained permissions for different user roles.
### ๐ฐ Unified Wallet System
- **Multi-game wallet** that can be shared or segmented per game/server, allowing for flexible financial management.
- **Support for multiple currencies**, including global and game-specific currencies.
- **Full transaction audit trail** to track all wallet activities.
- **Atomic secure transactions** leveraging database transactions for integrity.
### ๐ฎ Multi-Game Support
- **Space Engineers**: Manage items, online storage, and damage logs effectively.
- **Valheim**: Track characters, worlds, inventory, buildings, and skills.
- **Pluggable design** facilitates adding new games with minimal coupling, making it scalable.
### ๐๏ธ Hybrid Schema Architecture
- **Shared data** is maintained in a `public` schema, including users, wallets, currencies, and games.
- **Game-specific schemas** are used to isolate per-game entities, ensuring data integrity.
- **Cross-game aggregation** is enabled without duplicating identity data, which enhances performance.
### ๐ ๏ธ Developer Friendly
- **TypeScript** is used throughout the codebase, ensuring type safety and better maintainability.
- **TypeORM migrations** facilitate safe schema evolution and database management.
- **Centralized logging middleware** provides structured logs for easier debugging and monitoring.
## ๐ง Architecture
This project is implemented using the following technologies and tools:
- **Backend Framework**: NestJS (in TypeScript)
- **ORM**: TypeORM for database interactions.
- **Database**: PostgreSQL for data storage.
- **Authentication**: Passport.js combined with JWT and Steam OAuth for secure access.
- **Process Manager**: PM2 for managing application processes.
- **Reverse Proxy**: Nginx for handling incoming requests and load balancing.
- **Containerization**: Docker and Docker Compose for development and deployment.
### ๐๏ธ Module Layout
The application is structured into several modules, each serving a specific purpose:
AppModule โโโ Core โ โโโ ConfigModule (global) โ โโโ TypeOrmModule (global) โโโ Auth โ โโโ AuthModule โ โโโ UserModule โโโ Wallet โ โโโ WalletModule โโโ Games โ โโโ GameModule โ โโโ SpaceEngineersModule โ โโโ ValheimModule
## ๐ System Requirements
### Minimum
- **Node.js**: v20.0.0+
- **npm**: v8.0.0+
- **PostgreSQL**: v13+
- **Memory**: 1GB RAM
- **Disk**: 2GB free
### Recommended (Production)
- **Node.js**: v20 LTS
- **Memory**: 4GB+ RAM
- **CPU**: 2 cores+
- **Disk**: 10GB+ SSD
## ๐ Quick Start
To get started with the Game Hub backend, follow these simple steps:
### 1๏ธโฃ Clone the Repository
```bash
git clone https://github.com/your-username/game_hub_nest.git
cd game_hub_nest
cp .env.example .env
nano .env./start-dev.shcurl http://localhost:4000/health
open http://localhost:4000Environment variables are managed via the .env file. Key configurations include:
DB_HOST=localhost
DB_PORT=5432
DB_USER=your_db_user
DB_PASSWORD=your_db_password
DB_NAME=game_hub_db
DB_SSL=false # Set to true in production if using SSLJWT_SECRET=your_super_secret_jwt_key_here
JWT_EXPIRES_IN=7d
STEAM_API_KEY=your_steam_api_key_here
STEAM_RETURN_URL=http://localhost:4000/auth/steam/returnNODE_ENV=development
PORT=4000
HOST=0.0.0.0
CORS_ORIGINS=http://localhost:3000,http://localhost:5173Refer to the full list in .env.example for all environment variables.
NOTE: A dedicated safe migration script is referenced here, but it is not currently present in the repository. Use the manual commands below unless you add such a script.
# Generate a new migration (provide a name)
npm run migration:generate -- --name <MigrationName>
# Run pending migrations
npm run migration:run
# Revert the last migration
npm run migration:revert- Always back up your database before running migrations.
- Test migrations in a development environment first.
- Refer to
DATABASE_MIGRATION_GUIDE.mdfor detailed instructions. - Validate wallet schema changes against existing transaction integrity to avoid data loss.
- Multi-game and per-server wallet support.
- Support for multiple currencies (both global and game-specific).
- Full immutable transaction history for transparency.
- Extensible currency model to accommodate future needs.
- Manage item inventories and online storage efficiently.
- Track damage logs and player-related entities for enhanced gameplay analytics.
- Oversee character and skills management.
- Handle item, inventory, and building data, ensuring a rich user experience.
For testing, this project includes a dedicated test suite. To run the tests, use:
npm run testThis will execute all unit tests and provide feedback on your code quality and functionality.
The project is organized into the following directories:
src/: Contains the main application code (112 files).root/: Contains configuration and environment files (24 files).test/: Contains unit tests (6 files).config/: Contains additional configuration files (1 file).scripts/: Contains any auxiliary scripts (1 file).
Contributions are welcome! To contribute to the Game Hub project:
- Fork the repository.
- Create a new branch (
git checkout -b feature/new-feature). - Make your changes and commit (
git commit -m 'Add new feature'). - Push to the branch (
git push origin feature/new-feature). - Open a Pull Request.
This project is licensed under the MIT License. See the LICENSE file for details.
Feel free to explore the codebase, contribute, and help enhance the Game Hub experience!