From 4517620d9e6ac85564e6cdb9dd6b3bfa77658ed8 Mon Sep 17 00:00:00 2001 From: Toni Koskinen Date: Thu, 26 Mar 2026 13:45:33 +0200 Subject: [PATCH] Finalize template --- .gitignore | 4 ++ apps/api/Dockerfile.dev | 17 +++++ apps/api/src/app/app.module.ts | 3 +- .../controllers/posts/posts.controller.ts | 25 ++++++++ apps/api/src/modules/users/posts.module.ts | 7 +++ apps/web-app/Dockerfile.dev | 16 +++++ apps/web-app/src/app/nx-welcome.ts | 1 + docker-compose.yml | 63 +++++++++++++++++++ .../20260326110513_init/migration.sql | 15 +++++ prisma/migrations/migration_lock.toml | 3 + prisma/schema.prisma | 2 - prisma/seed.sql | 5 ++ 12 files changed, 158 insertions(+), 3 deletions(-) create mode 100644 apps/api/Dockerfile.dev create mode 100644 apps/api/src/modules/users/controllers/posts/posts.controller.ts create mode 100644 apps/api/src/modules/users/posts.module.ts create mode 100644 apps/web-app/Dockerfile.dev create mode 100644 docker-compose.yml create mode 100644 prisma/migrations/20260326110513_init/migration.sql create mode 100644 prisma/migrations/migration_lock.toml create mode 100644 prisma/seed.sql diff --git a/.gitignore b/.gitignore index 2c9c516..d3a7e1b 100644 --- a/.gitignore +++ b/.gitignore @@ -36,6 +36,10 @@ npm-debug.log yarn-error.log testem.log /typings +pnpm-debug.log +pnpm-error.log +package-lock.json +.pnpm-store/ # System Files .DS_Store diff --git a/apps/api/Dockerfile.dev b/apps/api/Dockerfile.dev new file mode 100644 index 0000000..160aab9 --- /dev/null +++ b/apps/api/Dockerfile.dev @@ -0,0 +1,17 @@ +FROM node:20-bookworm-slim + +WORKDIR /app +RUN corepack enable + +# Dependencies (from workspace root) +COPY package.json pnpm-lock.yaml ./ +RUN pnpm install + +# Monorepo structure +COPY nx.json tsconfig.base.json ./ +COPY apps/api ./apps/api +COPY libs ./libs +COPY prisma ./prisma + +ENV PORT=3000 +CMD ["pnpm", "nx", "serve", "api"] diff --git a/apps/api/src/app/app.module.ts b/apps/api/src/app/app.module.ts index 8662803..5fb5220 100644 --- a/apps/api/src/app/app.module.ts +++ b/apps/api/src/app/app.module.ts @@ -1,9 +1,10 @@ import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; +import { PostsModule } from '../modules/users/posts.module'; @Module({ - imports: [], + imports: [PostsModule], controllers: [AppController], providers: [AppService], }) diff --git a/apps/api/src/modules/users/controllers/posts/posts.controller.ts b/apps/api/src/modules/users/controllers/posts/posts.controller.ts new file mode 100644 index 0000000..519eebd --- /dev/null +++ b/apps/api/src/modules/users/controllers/posts/posts.controller.ts @@ -0,0 +1,25 @@ +import { Controller, Get, NotFoundException, Param } from '@nestjs/common'; +import { Post } from '@shared/prisma-generated/src/types'; +import { prisma } from '@shared/prisma-generated/src/prisma'; + +@Controller('posts') +export class PostsController { + @Get() + async findAll(): Promise { + const posts = await prisma.post.findMany(); + return posts; + } + + @Get(':id') + async findById(@Param('id') id: string): Promise { + const post = await prisma.post.findUnique({ + where: { id }, + }); + + if (!post) { + throw new NotFoundException(`Post with id ${id} not found`); + } + + return post; + } +} \ No newline at end of file diff --git a/apps/api/src/modules/users/posts.module.ts b/apps/api/src/modules/users/posts.module.ts new file mode 100644 index 0000000..92e3d64 --- /dev/null +++ b/apps/api/src/modules/users/posts.module.ts @@ -0,0 +1,7 @@ +import { Module } from '@nestjs/common'; +import { PostsController } from './controllers/posts/posts.controller'; + +@Module({ + controllers: [PostsController], +}) +export class PostsModule {} diff --git a/apps/web-app/Dockerfile.dev b/apps/web-app/Dockerfile.dev new file mode 100644 index 0000000..97303f9 --- /dev/null +++ b/apps/web-app/Dockerfile.dev @@ -0,0 +1,16 @@ +FROM node:20-bookworm-slim + +WORKDIR /app +RUN corepack enable + +# Dependencies (from workspace root) +COPY package.json pnpm-lock.yaml ./ +RUN pnpm install + +# Monorepo structure +COPY nx.json tsconfig.base.json ./ +COPY apps/web-app ./apps/web-app +COPY libs ./libs + +ENV HOSTNAME=0.0.0.0 +CMD ["pnpm", "nx", "serve", "web-app"] \ No newline at end of file diff --git a/apps/web-app/src/app/nx-welcome.ts b/apps/web-app/src/app/nx-welcome.ts index 6786b9e..9939409 100644 --- a/apps/web-app/src/app/nx-welcome.ts +++ b/apps/web-app/src/app/nx-welcome.ts @@ -514,6 +514,7 @@ import { CommonModule } from '@angular/common';

Hello there, Welcome web-app 👋 + HMR is working!

diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..0c0d284 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,63 @@ +services: + db: + image: postgres:18-alpine + environment: + POSTGRES_USER: webapp + POSTGRES_PASSWORD: webpass + POSTGRES_DB: webappdb + PGDATA: /var/lib/postgresql/data + ports: + - "5432:5432" + volumes: + - webapp_pgdata:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U webapp -d webappdb"] + interval: 10s + timeout: 5s + retries: 5 + + api: + build: + context: . + dockerfile: ./apps/api/Dockerfile.dev + command: sh -c "pnpm install && pnpm prisma generate && pnpm prisma migrate deploy && pnpm nx serve api" + ports: + - "3000:3000" + environment: + - NODE_ENV=development + - DATABASE_URL=postgresql://webapp:webpass@db:5432/webappdb + - NX_DAEMON=false + - NX_WORKSPACE_DATA_DIRECTORY=/tmp/nx-workspace-data-api + - NX_CACHE_DIRECTORY=/tmp/nx-cache-api + stop_grace_period: 5s + depends_on: + db: + condition: service_healthy + volumes: + - .:/app + - api_node_modules:/app/node_modules + + web: + build: + context: . + dockerfile: ./apps/web-app/Dockerfile.dev + command: sh -c "pnpm install && pnpm nx serve web-app --host 0.0.0.0 --port 4200" + ports: + - "4200:4200" + environment: + - NODE_ENV=development + - NX_DAEMON=false + - NX_WORKSPACE_DATA_DIRECTORY=/tmp/nx-workspace-data-web + - NX_CACHE_DIRECTORY=/tmp/nx-cache-web + stop_grace_period: 5s + volumes: + - .:/app + - web_node_modules:/app/node_modules + depends_on: + api: + condition: service_started + +volumes: + webapp_pgdata: + api_node_modules: + web_node_modules: \ No newline at end of file diff --git a/prisma/migrations/20260326110513_init/migration.sql b/prisma/migrations/20260326110513_init/migration.sql new file mode 100644 index 0000000..9c01077 --- /dev/null +++ b/prisma/migrations/20260326110513_init/migration.sql @@ -0,0 +1,15 @@ +-- CreateEnum +CREATE TYPE "PostStatus" AS ENUM ('DRAFT', 'PUBLISHED', 'ARCHIVED'); + +-- CreateTable +CREATE TABLE "posts" ( + "id" TEXT NOT NULL, + "title" TEXT NOT NULL, + "content" TEXT, + "status" "PostStatus" NOT NULL DEFAULT 'DRAFT', + "authorName" TEXT NOT NULL, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "posts_pkey" PRIMARY KEY ("id") +); diff --git a/prisma/migrations/migration_lock.toml b/prisma/migrations/migration_lock.toml new file mode 100644 index 0000000..044d57c --- /dev/null +++ b/prisma/migrations/migration_lock.toml @@ -0,0 +1,3 @@ +# Please do not edit this file manually +# It should be added in your version-control system (e.g., Git) +provider = "postgresql" diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 4a14a2c..9a2a0d2 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -1,8 +1,6 @@ // This is your Prisma schema file, // learn more about it in the docs: https://pris.ly/d/prisma-schema -// Get a free hosted Postgres database in seconds: `npx create-db` - generator client { provider = "prisma-client" output = "../libs/prisma-generated/src/lib/generated" diff --git a/prisma/seed.sql b/prisma/seed.sql new file mode 100644 index 0000000..fe8c139 --- /dev/null +++ b/prisma/seed.sql @@ -0,0 +1,5 @@ +INSERT INTO "posts" ("id","title","content","status","authorName","createdAt","updatedAt") +VALUES + ('post_1','Hello Prisma','First seeded post','DRAFT','Alice',NOW(),NOW()), + ('post_2','Published post','Seeded and published','PUBLISHED','Bob',NOW(),NOW()) +ON CONFLICT ("id") DO NOTHING; \ No newline at end of file