A monorepository template based on Nrwl/Nx set up for Angular with Angular Material, NestJS and Prisma using PostgreSQL.
Inspired by this article: Building a REST API with NestJS and Prisma
- NodeJS >= 18.12.0 < 19
- Docker Compose
- npx (optional)
- Angular frontend application
/apps/psk-ui
at http://localhost:4200/ - NestJS backend application
/apps/psk-api
at http://localhost:3000/api - npm library
@psk/psk-lib
at/libs/psk-lib
- Dockerized PostgreSQL database
psk
configured in apps/psk-api/.env and docker-compose.yml at localhost:5432/psk
Install dependencies within repository folder:
npm ci
Boot Docker container containing sample database for sample backend app:
docker-compose up -d
Configure env vars for backend app by creating apps/psk-api/.env with e. g.:
DATABASE_URL="postgresql://psk_user:mYsEcReTpAsSwOrD@localhost:5432/psk?schema=public"
Run backend app:
npm run serve-api
In another CLI instance, run frontend app:
npm run serve-ui
This command also executes npm run generate-api-lib
which reads dist/apps/psk-api/api-swagger-spec.json
to generate and store interfaces, modules and services in @psk/psk-lib
consumed by psk-ui
.
See sample UI at http://localhost:4200/
See API Docs at http://localhost:3000/api/docs
Run unit tests:
npm run test:watch psk-ui
npm run test:watch psk-api
npx nx g @nx/angular:application
Add new NestJS app:
npx nx g @nx/nest:application
In apps/psk-api/prisma/schema.prisma, e. g.
model MyEntity {
id String @id @default(uuid())
name String
}
To apply schema changes to the Prisma init and migration process, run:
npx dotenv -e apps/{YOUR_APP_NAME}/.env -- npx prisma migrate dev --schema apps/{YOUR_APP_NAME}/prisma/schema.prisma
Because of the Nx folder structure dotenv-cli is required to pass environment variable from apps/psk-api/.env to Prisma CLI.
This will also run @brakebein/prisma-generator-nestjs-dto
in order to generate all DTOs from the prism schema in apps/psk-api/generated
.
Use nestjs-prisma-crud to generate new CRUD templates for entities:
npx nx g -c nestjs-prisma-crud-schematics crud-resource MyEntity
Then, import the generated DTOs in apps/psk-api/generated
to the generated NestJS CRUD services.
Generate modules and services to corresponding library for Angular apps to consume:
npm run generate-api-lib
This is configured in psk-api-openapi-gen.json.
npx nx g @nx/node:library