- create SSH_PRIVATE_KEY or SSH_PASSWORD in github action secret
- crate DOT_ENV_PROD in github variable
- update IP address and app name in .github/workflows/prod-cicd.yml
- api will run on 400x e.g. 4004
How to use this template using nestpress cli.
# create project
npx nestpress@latest init
# create author, blog, category module (by default it will create single module)
# create a author module on src/feature/ directory
npx nestpress@latest module author
# create a blog multi module on src/feature/ directory [-m for multi module]
npx nestpress@latest module blog -m
# create a category module as blog sub module on src/feature/blog directory
npx nestpress@latest module category blog
# for more use of nestpress cli checkout this: https://www.npmjs.com/package/nestpress- make sure you have docker and pnpm install on your system as node package manager
- run mysql database and redis on docker(you should have docker install on your machine)
- run
docker compose -f docker-compose.local.yml up --buildthis will run the api server, database and redis on docker[use git-bash terminal in windows or for mac/linux default terminal is fine] - you can view the server on
http://localhost:4000
- run
-
install all dependencies and run the app
pnpm i # rename .env.dev to .env then update db connection string (DATABASE_URL) and other env variable if needed pnpm: db:migrate # reload / reopen vscode to restart TS server # run the test pnpm test # run the app pnpm dev # Api will run on port 4000
# Updates all dependencies in package.json (not important, it might break the project)
pnpm all:update
# run test to check the app is working fine or not
pnpm testTo maintain stability and compatibility within the project, do not update the following package:
| Package | Current Version | Reason |
|---|---|---|
zod |
3.25.70 |
This version is fully compatible with our Express 5 validation middleware. Updating may break request validation or cause runtime errors (e.g., Cannot set property query of #<IncomingMessage>). |
- create .env.test file from .env.dev and update values
- all test file will be in
src/__test__directory- on each test file we will have
beforeAll()andafterAll()it will cleardb+redis.
- on each test file we will have
- run
npm run testfor integration test - for testing we are using
vitest, supertest
- run
pnpm dbml:generate - copy content from
resources/dbml/schema.dbmland puthttps://dbdiagram.io/dto visualize
- BASE url: http://localhost:4000/v1
- Swagger Doc url: [Not Implemented]
- in
.docfolder a postman json filepostman-collection.jsonis available import it on your postman, or maybe there is live link just open that login into your postman and export collection.- you need add an environment in postman with variable
url=http://localhost:4000
- you need add an environment in postman with variable
- generate a router boilerplate
- create a file on a module called user.router.ts
npr+User(this is the module name in PascalCase)+tab
- generate a controller boilerplate
- create a file on a module called user.controller.ts
npc+User(this is the module name in PascalCase)+tab
- setup husky Git hooks
npx husky installit will generate a husky.sh file in.husky/_folder - create new branch
git checkout -b feature-branch - add files
git add . - commit files
git commit -m 'message'[here husky wil check the linting, it will throw error and stop commit if there is any linting error, it will ignore warning] - push code
git push origin feature-branch - always create Pull Request on
devbranch
-
Set up vscode
- Install Prettier extension on vscode, then select default formatter to prettier instead of none
- Install eslint extension
- Install Code Spell Checker extension
-
Folder Structure
- All folder name must be singular, e.g.
/loginnot/logins - Each feature will have a module folder inside
featurefolder- we might have 2 types of module - 1. singular 2. multi-module
- e.g. singular (user module check
src/feature/user)user modulefolder structuresrc/feature/user-> dto/create-user.dto.ts-> interface/user.interface.ts-> user.controller.ts-> user.router.ts-> user.service.ts
- e.g. multi-module (auth module check
src/feature/auth)auth modulefolder structuresrc/feature/auth-> auth.router.ts(this is the main router where all the sub singular module will connect)-> login-register[act as single module]-> logout[act as single module]-> verify-email[act as single module]
- All folder name must be singular, e.g.
-
File Name Convention (all lower case,separated by -)
- All file name must be singular, e.g.
user.router.tsnotusers.router.ts - Some other file names
- app.router.ts | my-app.router.ts
- app.controller.ts | my-app.controller.ts
- app.service.ts | my-app.service.ts
- create-user.dto.ts
- logged-in-user.interface.ts
- anything.something.ts
- All file name must be singular, e.g.
-
Class, interface and Function name convention
- Class:
class MyClass{}(mostly we will use functional programming, so try to ignore class) - Interface:
interface IMyInterface{}should start with capitalI - Function:
function myFunction(){}should be in camelCase - Variable name:
const aName=""should be in camelCase - Object name:
const UserService={}should be in PascalCase - Router Name:
const UserRouter = Router() - Controller Name:
const UserController = {} - DTO Name:
const CreateUserDto = z.object({})
- Class:
-
Import/Export Module
- on app.ts and for all *.router.ts file use default export
- for all other case always use named export avoid using default export
- e.g. for controller, service, dto, interface always go with named export
@author milon27.com