Skip to content

milon27/nestpress-prisma

Repository files navigation

Nestpress boilerplate with Authentication module

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

How to run

  • make sure you have docker and pnpm install on your system as node package manager

  • run database on docker(you should have docker install on your machine)

    • run bash .script/reload.local.sh [use git-bash terminal in windows or for mac/linux default terminal is fine]
  • install all dependencies and run the app

    pnpm i
    # rename .env.example (if available) to .env then update db connection string (DATABASE_URL) and other env variable if needed
    npx prisma migrate deploy
    npx prisma generate
    
    # reload / reopen vscode to restart TS server
    npm run dev # Api will run on port 4000
    # health check: http://localhost:4000/v1
    

Update npm dependencies

# Updates all dependencies in package.json
pnpm up

# run test to check the app is working fine or not
npm run test

Run/Write test

  • create .env.test file from .env.example and update values
  • all test file will be in src/__test__ directory
    • on each test file we will have beforeAll() and afterAll() it will clear db+redis.
  • run npm run test for integration test
  • for testing we are using vitest, supertest

API endpoints and doc

  • BASE url: http://localhost:4000/v1
  • Swagger Doc url: [Not Implemented]
  • in .doc folder a postman json file postman-collection.json is available import it on your postman
    • you need add an environment in postman with variable url=http://localhost:4000

Code Snippet on vscode

  • 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

How to push code

  • setup husky Git hooks npx husky install it 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 dev branch

Developer guide (code style)

  • Set up vscode

    • Install Prettier extension on vscode, then select default formatter to prettier instead of none
    • Install eslint extension
    • Install prisma vscode extension
  • Folder Structure

    • All folder name must be singular, e.g. /login not /logins

    • Each feature will have a module folder inside feature folder

      • we might have 2 types of module - 1. singular 2. multi-module
      • e.g. singular (user module check src/feature/user)
        • user module folder structure
        • src/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 module folder structure
        • src/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]
  • File Name Convention (all lower case,separated by -)

    • All file name must be singular, e.g. user.router.ts not users.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
  • 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 capital I
    • 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({})
  • 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

About

An express boilerplate structured like nest.js, with completed authentication with refresh access token used ORM as prisma.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published