Skip to content

redbonzai/lucra-sports-api

Repository files navigation

Here is a README document that explains how your application works and provides clear instructions on how to set it up and operate it.


Minesweeper API

Overview

This project is a backend API for a Minesweeper game, built using NestJS, TypeORM, and PostgreSQL. It allows users to create and interact with Minesweeper games by exposing RESTful endpoints. The application features a modular architecture, custom repositories, and a TypeORM integration.

Features

  • Create a new game: Generate a Minesweeper board with specified rows and columns.
  • Retrieve all games: Get a list of all existing games.
  • Retrieve a specific game by ID: Fetch a game and its cells by its unique ID.
  • Seed Data for Testing: Use predefined seed data to facilitate testing.
  • GitHub Actions CI Pipeline: The CI pipeline runs unit tests, e2e tests, migrations, and code quality checks.

Getting Started

Prerequisites

Ensure you have the following installed:

  • Node.js (v22 )
  • Yarn
  • Docker and Docker Compose

Installation

  1. Clone the repository:

    git clone <repository-url>
    cd <repository-folder>
  2. Install dependencies:

    yarn install

Running the Application

You can run the application in different environments (development, testing, and production). The database configurations differ slightly across these environments.

  1. Development:

    • Set up the environment variables:

      Create a .env file in the root directory:

      DATABASE_URL=postgres://local:local@localhost:5432/local
    • Start the application:

      yarn start:dev
  2. Testing (with Docker):

    The CI pipeline and local testing environment are powered by Docker Compose, which sets up the PostgreSQL containers, runs migrations, and seeds the test database.

    • Spin up the containers:

      docker-compose up -d
    • Run the tests:

      yarn test
      yarn test:e2e
  3. Production:

    Build and start the production version of the app:

    yarn build
    yarn start

Project Structure

The project follows a modular structure:

  • src/modules: Contains feature modules like games, game-cell, etc.
  • src/entities: Contains TypeORM entities such as Game and GameCell.
  • src/repositories: Custom TypeORM repositories to encapsulate database logic.
  • src/migrations: Contains migration files generated by TypeORM.
  • src/seed: Seed SQL files to populate the database for testing.

API Endpoints

Games Controller

Method Endpoint Description
GET /games Retrieves all games
GET /games/:id Retrieves a game by its ID
POST /games Creates a new game

DTOs (Data Transfer Objects)

  • CreateGameDto: Defines the structure of the payload to create a new game.
  • GameResponseDto: Structures the response data for game retrieval.
  • FindOneGameParams: Defines the structure for query parameters when fetching a specific game.

Custom TypeORM Module

The project uses a custom TypeOrmExModule to register and manage custom repositories. This module allows for more flexible database interactions while maintaining clean code architecture.

Running Migrations

To generate and run migrations:

  • Generate a new migration:

    yarn migration:generate <path-to-migration-name>
  • Run migrations:

    yarn migration:run

Seeding the Database

For testing, a seed file is used to populate the database with initial data.

To apply the seed:

PGPASSWORD=local psql -h localhost -U local -p 5445 -d test_e2e_db -f ./src/seed/seed.sql

CI/CD Pipeline

The GitHub Actions CI pipeline is configured to perform the following:

  1. Install dependencies.
  2. Run lint checks and build the project.
  3. Run unit and e2e tests.
  4. Run migrations and apply seed data to a test PostgreSQL instance.

Issues and Debugging

  1. Database Connection Issues: Ensure that the PostgreSQL service is running and that the correct ports are bound, particularly in Docker Compose configurations.

  2. Failed Migrations: Make sure the correct DATABASE_URL is set in the environment variables and that migrations are running against the intended database.


About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published