This API-Template was designed and built with NestJS and TypeORM. It follows the Hexagonal Architecture pattern, which is a software architecture pattern that is used to create loosely coupled application components. The Hexagonal Architecture pattern is also known as the Ports and Adapters pattern.
This api template was designed to help developers quickly build applications that are scalable, maintainable, and easy to use. Furthermore, the api use and requires unit testing to ensure that the application works as expected. It is designed to be used in production, and it is also open source.
- Authentication: This API uses JWT (jsonwebtoken) for authentication.
- Backend: NestJS, TypeORM, TypeScript
- Authentication: JWT (jsonwebtoken)
- Database: It supports PostgreSQL and others based on TypeORM. (Currently PostgreSQL)
- ORM: TypeORM
- Testing: Jest for unit testing
- Documentación: Swagger for API documentation.
src/
│
├── context/ # Context of the project
│ ├── userProfile/ # UserProfile Entity Folder
│ │ ├── application/ # Entity UseCases
│ │ │ ├── use-case/
│ │ ├── domain/ # Domain Entity
│ │ └── infraestructure/ # Infraestructure of the entity
│ ├── seeds/ # Seeders Folder
│ ├── services/ # Global Services
│ └── shared/ # Global Shared Modules
└──
- Clone the repository
git clone https://github.com/ing-cordova/nest-typeorm-hexagonal.git
- Navigate to the repository
cd nest-typeorm-hexagonal
- Install dependencies
npm install
- Set up your database: By default, the application uses a PostgreSQL database. You can change the database configuration in the
.env
file. You can use the following databases:
- MySQL or MariaDB
- PostgreSQL or CockroachDB
- SQLite
- Microsoft SQL Server
- Oracle
- SAP Hana
- Google Cloud Spanner
- MongoDB (experimental)
To use a different database, you need to install the right driver for your environment:
npm install DRIVER_NAME --save
Database | Driver |
---|---|
MySQL or MariaDB | mysql or mysql2 |
PostgreSQL or CockroachDB | pg |
SQLite | sqlite3 |
Microsoft SQL Server | mssql |
Oracle | oracledb |
SAP Hana | @sap/hana-client and hdb-pool |
Google Cloud Spanner | @google-cloud/spanner |
MongoDB | mongodb@^5.2.0 |
- For Google Cloud Spanner, you need to set the
GOOGLE_APPLICATION_CREDENTIALS
environment variable with the path to your JSON key file. - For SAP Hana, you need to set the
SPANNER_EMULATOR_HOST
environment variable if you want to use the emulator.
- Set up environment variables
source .env
npm run generate:module module-name
After you run the command, you will see a new folder in the src/context
directory with the name of the module you entered. Also, you have to include into the src/app.module
file the new module.
- Generate the migrations (at the project will be a initial migration, you can create more migrations based on the entities you create)
npm run typeorm:generate -- ./src/migrations/initial-migration
- Run the migrations
npm run typeorm:run
2.1 If you want to revert the migrations
npm run typeorm:revert
- Run the seeders
npm run seed
- Run the application
npm run start
- Run unit tests
npm run test
The Swagger documentation for this API is available at http://localhost:3000/api
Feel free to explore the code and contribute to this project. If you have any questions or suggestions, don't hesitate to contact me.