Skip to content

Commit

Permalink
Merge pull request #14 from helloitsdave/dockerised-be
Browse files Browse the repository at this point in the history
[backend] feature: Dockerised db + app
  • Loading branch information
helloitsdave authored Jan 20, 2024
2 parents a53670f + 96a913c commit d9bbe7c
Show file tree
Hide file tree
Showing 12 changed files with 339 additions and 34 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/backend-docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Build Backend Docker

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

defaults:
run:
working-directory: ./backend

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Use Node.js 18.x
uses: actions/setup-node@v3
with:
node-version: 18.x

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1

- name: Start service in docker
run: |
npm run docker:up
- name: Stop service in docker
run: |
npm run docker:down
8 changes: 8 additions & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Use an official Node.js runtime based on Alpine Linux as the base image
FROM node:18-alpine

# Install bash
RUN apk add --no-cache bash

# Set the working directory inside the container
WORKDIR /usr/src/app

Expand All @@ -19,6 +22,11 @@ COPY tsconfig.json ./
# Copy the application code into the container
COPY . .

COPY wait-for-it.sh wait-for-it.sh

# Make the script executable
RUN chmod +x wait-for-it.sh

# Build TypeScript code
RUN npm run build

Expand Down
17 changes: 17 additions & 0 deletions backend/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
version: "3"
services:
postgres:
image: postgres:13
environment:
POSTGRES_USER: test
POSTGRES_PASSWORD: test
POSTGRES_DB: test
ports:
- "5432:5432"
app:
build: .
command: ["sh", "-c", "./wait-for-it.sh postgres:5432 -- npx prisma migrate deploy && npm run seed && npm start"]
ports:
- "5001:5001"
environment:
DATABASE_URL: "postgresql://test:test@postgres:5432/test"
16 changes: 8 additions & 8 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 15 additions & 11 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,27 @@
"start:local": "npx nodemon",
"start": "node dist/index.js",
"build": "tsc",
"docker:build": "docker build -t notes-app-be .",
"docker:run": "docker run -p 5001:5001 notes-app-be"
"docker:app:build": "docker build -t notes-app-be .",
"docker:app:up": "docker run -p 5001:5001 notes-app-be",
"docker:up": "docker-compose up -d",
"docker:down": "docker-compose down",
"docker:prisma": "npx prisma migrate dev --name init",
"seed": "npx ts-node prisma/seed.ts"
},
"author": "",
"license": "ISC",
"devDependencies": {
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21",
"@types/cors": "2.8.17",
"@types/express": "4.17.21",
"@types/node": "^20.10.6",
"nodemon": "^3.0.2",
"prisma": "^5.7.1",
"ts-node": "^10.9.2",
"typescript": "^5.3.3"
"nodemon": "3.0.2",
"prisma": "5.7.1",
"ts-node": "10.9.2",
"typescript": "5.3.3"
},
"dependencies": {
"@prisma/client": "^5.7.1",
"cors": "^2.8.5",
"express": "^4.18.2"
"@prisma/client": "5.7.1",
"cors": "2.8.5",
"express": "4.18.2"
}
}
8 changes: 8 additions & 0 deletions backend/prisma/migrations/20240119233910_test/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- CreateTable
CREATE TABLE "Note" (
"id" SERIAL NOT NULL,
"title" TEXT NOT NULL,
"content" TEXT NOT NULL,

CONSTRAINT "Note_pkey" PRIMARY KEY ("id")
);
3 changes: 3 additions & 0 deletions backend/prisma/migrations/migration_lock.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "postgresql"
62 changes: 62 additions & 0 deletions backend/prisma/seed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// prisma/seed.js

import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

const seed = [
{
"title": "Meeting Notes",
"content": "Discussed project timelines and goals."
},
{
"title": "Shopping List",
"content": "Milk, eggs, bread, and fruits."
},
{
"title": "Recipe",
"content": "Ingredients: Chicken, tomatoes, onions, garlic."
},
{
"title": "Ideas",
"content": "Brainstorming ideas for the next feature release. 🚀"
},
{
"title": "Personal Goals",
"content": "Exercise for 30 minutes daily. Read a book every week."
},
{
"title": "Fête d'anniversaire",
"content": "Préparer une surprise pour la fête d'anniversaire."
},
{
"title": "日本旅行",
"content": "計画: 東京、京都、大阪を訪れる。"
},
{
"title": "Семейный ужин",
"content": "Приготовить вкусный ужин для всей семьи."
},
{
"title": "Coding Project",
"content": "Implement new features using React and Express."
}
]

async function main() {
// Seed data here
await prisma.note.createMany({
data: seed,
});

// Add more data as needed...
}

main()
.catch((e) => {
console.error(e);
// process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});
Loading

0 comments on commit d9bbe7c

Please sign in to comment.