Blog made using Nestjs + Mikro-orm codebase(backend) containing real world examples (CRUD, auth (password based and oauth), advanced patterns, etc)
NOTE: Starting April 18,2022 , the repo has ditched most promises for observables. You can check the latest promised version code at commit
Getting started
Clone this repo. Edit the env file and pass in your credentials
Installation
Install dependencies
npm install
Database
The example codebase uses MikroORM with a Postgres database.
Copy sample env file and adjust the connection settings and other settings(jwt,redis,mail,etc) respectively on sample env file
Note
: Env files are kept in env folder. The config validation allows 4 environment ['dev', 'prod', 'test','stage']. The env file name
should be of format .env.[environment] Ex. (.env.dev). The env to use should be provided while running any script as NODE_ENV=dev npm run dev
Start local Postgres server and run NODE_ENV=dev make migrate
to apply migrations
Now you can start the application witt NODE_ENV=dev npm run start
.
Note: If you are using windows, SET NODE_ENV=dev npm run start
Features covered:
π i18n - Internationalization𧡠piscina - threads for cpu extensive tasksπ¬ Twillio - sms supportπ± NestJS β latest versionπ TypeScript - Type checkingβοΈ Dotenv - Supports environment variablesπ Authentication - JWT, RSA256, oauthπ¬ Authorization - RBAC with caslπͺ MikroORM - Database ORMπͺ PostgreSQL - Open-Source Relational Databaseπ§ Configuration - Single config for allπ Swagger - API Documentationπ³ Docker Compose - Container Orchestrationπ Helmet - secure HTTP headersπ΄ Insomnia - Insomnia config for endpointsπ ESLint β Pluggable JavaScript linterπ Prettier - Opinionated Code Formatter
NPM scripts
npm run start
- Start applicationnpm run start:watch
- Start application in watch modenpm run test
- run Jest test runnernpm run start:prod
- Build application
Additionally, you can also see the scripts in makefile
Setup
- First if you don't want to use any libs from like redis, mailer etc. replace them from the app.module.tasks
- You will also need to remove the config from
validate.config.ts
from lineload: []
- Also remove the unwanted config variables from the env file
- You will also need to remove the config from
- Make sure you create a env file under
env
directory with name like.env.something
.The portion after .env is theNODE_ENV
value which will be required while running the app - Also make sure you have ssl files inside
src/resources/ssl
if you tend to use ssl. Replace the sample files with your ssl files but keep the name same. Additionally
Migration and seeding
Migrations are used to update the database schema. The migration files are stored in migrations
directory.
env=dev make migrate # applies migration for dev env
Seeding is used to insert data into the database. The seeding files are stored in common/database/seeders
directory.
env=dev USER_PASSWORD=Test@1234 make seed # seeds data for dev env with user password set as Test@1234
Start application
NODE_ENV=[env name] npm run start
(without ssl)NODE_ENV=[env name] SSL=true npm run start
(with ssl)- Test api by browsing to
http://localhost:[port]/v1/user
- View automatically generated swagger api docs by browsing to
http://localhost:[port]/docs
Authentication
This applications uses JSON Web Token (JWT) to handle authentication. The token is passed with each request using the Authorization
header with Token
scheme. The JWT authentication middleware handles the validation and authentication of the token.
Deployment
You need to have docker and docker-compose installed. Also since we are using makefiles for deployment, you need to have make installed.
env=dev make deploy # deploys dev environment
env=prod make deploy # deploys prod environment
The password for redis
and rabbitmq
is test@1234
can be changed in the make file under deploy
script
sample env
The sample env is generated using sample-env