Skip to content

🏭 Nestjs starter template API with PostgreSQL via Prisma

License

Notifications You must be signed in to change notification settings

baakeydow/nestjs-prisma-starter-template

Repository files navigation

🏭 Nestjs starter template API with PostgreSQL via Prisma

Documentation

Changelog

Nest Docs

Prisma

PG

Prerequisites

Docker

Yarn

Init project

cp .env.example .env && yarn && docker-compose down && docker-compose up -d && sleep 7 && yarn prisma:generate && yarn build

Run

# restart DB
docker-compose down && docker-compose up -d && docker-compose logs -f

# development
yarn start

# watch mode
yarn start:dev

# production mode
yarn start:prod

Test

# unit tests
yarn test

# e2e tests
yarn test:e2e

# test coverage
yarn test:cov

Create user

curl --request POST 'http://localhost:8942/api/user/create' \
-H 'content-type: application/json' \
--data-raw '{"email": "baakey@21times2.com", "username": "baakey"}'

Retrieve specific user

curl 'http://localhost:8942/api/user/all?email=baakey@21times2.com' --compressed

References

CleanArchitecture
The Patterns Behind Scalable, Reliable, and Performant Large-Scale Systems

Tips

  • How to create new files ?

npx nest generate --help

  • How to run db migrations ?

npx prisma migrate --help

  • How to connect to the local database from terminal ?

PGPASSWORD=postgres psql -h 127.0.0.1 -p 5442 -d dtk -U postgres

  • How to use StandardDtkApiResponse ?
// Response with data
const responseWithData: StandardDtkApiResponse<{ message: string }> = {
  data: { message: 'hello' },
  error: null,
};

// Response with error
const responseWithError: StandardDtkApiResponse<null, { code: number, message: string }> = {
  data: null,
  error: { code: 404, message: 'not found' },
};

// Response with data and error
const responseWithDataAndError: StandardDtkApiResponse<
  { message: string, id: number },
  { code: number, message: string, details: Record<string, any> }
> = {
  data: { message: 'hello', id: 123 },
  error: { code: 500, message: 'server error', details: { foo: 'bar' } },
};