Skip to content

Commit

Permalink
[RELEASE] Mongodb (#178)
Browse files Browse the repository at this point in the history
* fix: ajust compose and env.example (#173)

* fix: ajust compose and env.example

* feat: insert new base workflows

* fix: ajust docker compose and dockerfile

* chore: remove db url from .env.example

* feat: add new connection with mongo

* feat: ajust mongo shemas and add integrate with prisma

* fix: ajust husky to run prettier on files

* fix: run pnpm in docker-compose

* chore: ajust env.example

* chore: add comando to push tables to mongo

* chore: remove teste.js

* fix: ajust stript push db

---------

Co-authored-by: Paulo Victor <pilutechinformatica@gmail.com>
  • Loading branch information
k1nha and PiluVitu authored Sep 26, 2024
1 parent 8628a5a commit d5b212d
Show file tree
Hide file tree
Showing 18 changed files with 96 additions and 188 deletions.
9 changes: 3 additions & 6 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
HOSTNAME='localhost'
PORT=3000
SECRET_KEY='321'
DATABASE_URL="postgresql://postgres:password@localhost:5432/octopost"

MONGO_CONTAINER_PORT="27017"

DATABASE_URL="mongodb://localhost:27017/octopost"

MONGO_INITDB_ROOT_USERNAME
MONGO_INITDB_ROOT_PASSWORD
ME_CONFIG_MONGODB_ADMINUSERNAME
ME_CONFIG_MONGODB_ADMINPASSWORD
ME_CONFIG_MONGODB_URL
21 changes: 0 additions & 21 deletions .github/workflows/auto-assign.yml

This file was deleted.

11 changes: 11 additions & 0 deletions .github/workflows/base-workflows.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: Base Actions

on:
issue_comment:
types: [created]
pull_request:

jobs:
assignes:
uses: devhatt/workflows/.github/workflows/auto-assign.yml@main
secrets: inherit
1 change: 1 addition & 0 deletions .husky/pre-push
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
. "$(dirname -- "$0")/_/husky.sh"

pnpm prisma:generate
pnpm format:fix
pnpm test
pnpm build
28 changes: 21 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
FROM node:22 AS build
FROM node:22 AS base

ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"

WORKDIR /usr/src/app

RUN npm install -g pnpm@8
RUN corepack enable && corepack prepare pnpm@8.6.0 --activate

COPY ./package.json ./pnpm-lock.yaml ./

COPY ./ .

RUN pnpm install --frozen-lockfile -r
FROM base AS prod-deps

RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile

FROM base AS build

RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile
RUN npx prisma generate
RUN pnpm build

FROM node:22-alpine3.19

ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"


RUN corepack enable && corepack prepare pnpm@8.6.0 --activate


WORKDIR /usr/src/app
RUN npm install -g pnpm@8
COPY --from=build /usr/src/app/package.json ./package.json
COPY --from=build /usr/src/app/build ./build
COPY --from=build /usr/src/app/node_modules ./node_modules
COPY --from=prod-deps /usr/src/app/node_modules ./node_modules

EXPOSE 3000

CMD ["pnpm", "run", "start"]
CMD ["pnpm", "start"]
68 changes: 34 additions & 34 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,34 +1,42 @@
services:
db-dev: &db
container_name: octopost-db-dev
# This config is for MongoDB v4
# It's a Replica Set (required for Prisma Client)
mongo:
profiles:
- dev
image: mongo:latest
build:
context: ./mongodb_replica
args:
MONGO_VERSION: 4
environment:
MONGO_REPLICA_HOST: 127.0.0.1
MONGO_REPLICA_PORT: 27017
# Use "mongosh" instead of "mongo" for v5+
MONGO_COMMAND: 'mongo'

ports:
- '27017:27017'
environment:
MONGO_INITDB_ROOT_USERNAME: $MONGO_INITDB_ROOT_USERNAME
MONGO_INITDB_ROOT_PASSWORD: $MONGO_INITDB_ROOT_PASSWORD
restart: unless-stopped
healthcheck:
# Use "mongosh" instead of "mongo" for v5+
test:
[
'CMD',
'mongo',
'admin',
'--port',
'27017',
'--eval',
"db.adminCommand('ping').ok",
]
interval: 5s
timeout: 2s
retries: 20
volumes:
- db:/var/lib/mongodb/mydata
networks:
- octopost

mongo-express:
image: mongo-express
depends_on: [db-dev]
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: $ME_CONFIG_MONGODB_ADMINUSERNAME
ME_CONFIG_MONGODB_ADMINPASSWORD: $ME_CONFIG_MONGODB_ADMINPASSWORD
ME_CONFIG_MONGODB_URL: $ME_CONFIG_MONGODB_URL
ME_CONFIG_BASICAUTH: false
links:
- db-dev
- mongo1_data:/var/lib/mongodb/mydata
networks:
- octopost

api:
build:
context: ./
Expand All @@ -37,21 +45,13 @@ services:
profiles:
- prod
ports:
- 3000:3000
depends_on:
- db-prod
- '${PORT}:${PORT}'
networks:
- octopost

db-prod:
<<: *db
container_name: octopost-db
profiles:
- prod
volumes:
mongo1_data:

networks:
octopost:
driver: bridge

volumes:
db:
12 changes: 12 additions & 0 deletions mongodb_replica/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
ARG MONGO_VERSION

FROM mongo:${MONGO_VERSION}

# we take over the default & start mongo in replica set mode in a background task
ENTRYPOINT mongod --port $MONGO_REPLICA_PORT --replSet rs0 --bind_ip 0.0.0.0 & MONGOD_PID=$!; \
# we prepare the replica set with a single node and prepare the root user config
INIT_REPL_CMD="rs.initiate({ _id: 'rs0', members: [{ _id: 0, host: '$MONGO_REPLICA_HOST:$MONGO_REPLICA_PORT' }] })"; \
# we wait for the replica set to be ready and then submit the command just above
until ($MONGO_COMMAND admin --port $MONGO_REPLICA_PORT --eval "$INIT_REPL_CMD"); do sleep 1; done; \
# we are done but we keep the container by waiting on signals from the mongo task
echo "REPLICA SET ONLINE"; wait $MONGOD_PID;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"docs:lint": "redocly lint --extends minimal ./openapi/main.yaml",
"docker-dev": "docker compose --profile dev up",
"docker-prod": "docker compose --profile prod up --build",
"prisma:generate": "npx prisma generate"
"prisma:generate": "npx prisma generate",
"prisma:push": "npx prisma db push"
},
"devDependencies": {
"@commitlint/cli": "19.3.0",
Expand Down
64 changes: 0 additions & 64 deletions prisma/migrations/20240502234532_init_db/migration.sql

This file was deleted.

This file was deleted.

This file was deleted.

2 changes: 0 additions & 2 deletions prisma/migrations/20240624191001_add_in_my_pc/migration.sql

This file was deleted.

This file was deleted.

23 changes: 0 additions & 23 deletions prisma/migrations/20240722010704_rename_columns/migration.sql

This file was deleted.

3 changes: 0 additions & 3 deletions prisma/migrations/migration_lock.toml

This file was deleted.

16 changes: 8 additions & 8 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ generator client {
}

datasource db {
provider = "postgresql"
provider = "mongodb"
url = env("DATABASE_URL")
}

model User {
id String @id @default(uuid())
id String @id @default(auto()) @map("_id") @db.ObjectId
email String @unique
name String?
username String @unique
Expand All @@ -30,14 +30,14 @@ model User {
}

model Account {
id String @id @default(uuid())
id String @id @default(auto()) @map("_id") @db.ObjectId
name String?
username String?
socialMediaUserId String? @map("social_media_user_id") //Referes to the social media user id -> Twitter ID
avatarUrl String? @map("avatar_url")
userId String? @map("user_id")
userId String? @map("user_id") @db.ObjectId
favorite Boolean
socialMediaId Int @map("social_media_id")
socialMediaId String @map("social_media_id") @db.ObjectId
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
Expand All @@ -49,20 +49,20 @@ model Account {
}

model Token {
id Int @id @default(autoincrement())
id String @id @default(auto()) @map("_id") @db.ObjectId
authToken String? @map("auth_token")
token String?
issuedAt DateTime? @map("issued_at")
expiresIn Int? @map("expire_in")
accountId String @unique @map("account_id")
accountId String @unique @map("account_id") @db.ObjectId
account Account? @relation(fields: [accountId], references: [id])
@@map("tokens")
}

model SocialMedia {
id Int @id @default(autoincrement())
id String @id @default(auto()) @map("_id") @db.ObjectId
name String
description String?
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* istanbul ignore file -- @preserve */

import { app } from '@/config/app';
import { env } from '@/config/env';

Expand Down
Loading

0 comments on commit d5b212d

Please sign in to comment.