This is the backend for the flashcards web application, built using NestJS. The backend uses PostgreSQL as the database.
- Installation
- Environment Configuration
- Running the Application
- API Documentation
- Testing
- Project Structure
- Flashcards Backend Architecture Guide
- Contributing
- License
- Docker and Docker Compose installed
- Node.js and npm installed
git clone https://github.com/yourusername/flashcards-backend.git
cd flashcards-backend
npm install
Create a .env
file in the root directory with the following content:
DATABASE_HOST=localhost
DATABASE_PORT=5432
DATABASE_USER=yourusername
DATABASE_PASSWORD=yourpassword
DATABASE_NAME=flashcardsdb
Start the backend and database using Docker Compose:
docker-compose up --build
-
Start the PostgreSQL database on your local machine and ensure it matches the configuration in the
.env
file. -
Run migrations and seed data:
npm run migration:run
npm run seed:run
- Start the backend:
npm run start:dev
The API documentation is available via Swagger. Once the backend is running, you can access it at:
http://localhost:3000/api
To run the backend tests, use:
npm run test
src/app.module.ts
: Main application module.src/entities
: Entity definitions.src/flashcards
: Flashcards module, including controllers, services, and repositories.src/seeds
: Seed scripts for database seeding.src/migrations
: Database migration files.
Entities represent the core data structure of the application, directly mapping to the database tables. Each entity is a class decorated with @Entity
and contains properties representing the columns of the table.
Location: src/entities
Example:
Flashcard
entity represents the flashcards in the system.Deck
entity represents the collections of flashcards.
DTOs (Data Transfer Objects) are used to define the structure of the data sent to and received from the API. They are simple classes that use decorators from the class-validator
library to enforce data validation rules.
Location: src/flashcards/dtos
Example:
CreateFlashcardDto
is used when creating a new flashcard.UpdateFlashcardDto
is used when updating an existing flashcard.
Repositories encapsulate the data access logic, providing methods to interact with the database. They extend the TypeORM Repository
class and can include custom query methods specific to the application's needs.
Location: src/flashcards/repositories
Example:
FlashcardsRepository
contains methods for querying and manipulatingFlashcard
entities in the database.
Services contain the business logic and coordinate between repositories and controllers. They use dependency injection to access repositories and perform operations like creating, reading, updating, and deleting data.
Location: src/flashcards
Example:
FlashcardsService
handles the business logic for managing flashcards, such as creating, updating, and retrieving flashcards.
Controllers define the API endpoints and handle HTTP requests. They use decorators from the @nestjs/common
module to define routes and HTTP methods. Controllers delegate the actual processing to services.
Location: src/flashcards
Example:
FlashcardsController
defines endpoints for creating, updating, and retrieving flashcards.
Modules group related components together, making the application modular and easier to maintain. Each module imports necessary components and exports them if needed by other modules. Modules also configure dependency injection for their components.
Location: src/flashcards
Example:
FlashcardsModule
groups the controllers, services, and repositories related to flashcards.
The main application module (AppModule
) initializes the application. It imports other modules and sets up global configurations like database connections using TypeOrmModule
and environment configurations using ConfigModule
.
Location: src/app.module.ts
Migrations manage database schema changes, allowing you to evolve the database schema over time. Migration files are created using TypeORM CLI and define changes such as creating, altering, or dropping tables and columns.
Location: src/migrations
Example:
CreateFlashcardsTable
migration file defines the schema for theflashcards
table.CreateDecksTable
migration file defines the schema for thedecks
table.
Seeds populate the database with initial data. Seed scripts are used to insert predefined data into the database, which is useful for setting up initial application state or for testing purposes.
Location: src/seeds
Example:
InitialSeeder
script inserts initial data into theflashcards
anddecks
tables.
Factories are used to generate bulk data for testing or seeding. Factory scripts define how to create entities with random or predefined data, making it easier to populate the database with meaningful data for development and testing.
Location: src/factories
Example:
FlashcardFactory
defines how to create flashcards with random data.DeckFactory
defines how to create decks with random data.
Docker and Docker Compose are used to containerize the application and manage dependencies. Dockerfiles define the environment and dependencies for individual services, while Docker Compose files define and run multi-container Docker applications, orchestrating the services together.
Location: Root directory
Example:
Dockerfile
defines the environment and dependencies for the backend service.docker-compose.yml
defines the services, networks, and volumes for the application, orchestrating the backend service and the PostgreSQL database.
Contributions are welcome! Please follow these steps to contribute:
- Fork the repository.
- Create a new branch for your feature or bugfix.
- Commit your changes with a descriptive message.
- Push your changes to your fork.
- Create a pull request with a description of your changes.
This project is licensed under the MIT License. See the LICENSE file for details.