Skip to content

Commit

Permalink
update docs and prisma schema (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
elvincheng3 authored Sep 21, 2023
1 parent 0b4d657 commit 13379db
Show file tree
Hide file tree
Showing 9 changed files with 1,208 additions and 61 deletions.
46 changes: 39 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,59 @@

NextJS, NestJS, Prisma, PostgreSQL

## Overview of Yarn Commands

- `dev`: start the frontend
- `format`: run prettier formatter
- `lint`: run eslint
- `test`: run all tests
- `tsc`: run ts checker
- `migrate`: run prisma migrate and make changes to local db
- `gen-client`: regenerate client used to communicate with backend in `apps/web`
- `dev:db:up`: start up the database
- `dev:db:down`: stop the database
- `backend:docker:build`: build the docker image for the backend
- `backend:docker:run`: start the backend and database stack
- `backend:docker:down`: stop the backend and database stack

## Getting Started (Local Development)

1. Create a .env file in the project root and configure environment variables using the example.
2. Install dependencies in both `apps/web` and `apps/server`. We currently use Yarn as our package manager.
1. Create a .env file in `apps/web` and `apps/server` and configure environment variables using the provided example files.
2. Install yarn dependencies. We currently use Yarn and Yarn workspaces to manage dependencies.

```bash
yarn install
```

3. Generate the Prisma Client in the `apps/server` directory using the following command from within the `apps/server` directory:

```bash
yarn prisma generate
```

4. To run the development client and server together:
4. Start up your local backend and database stack:

```bash
yarn backend:docker:run
```

5. Migrate your local database using Prisma Migrate in order to set up database schemas.

```bash
yarn migrate
```

6. Run the frontend client locally. The environment variables should be configured to use our local stack:

```bash
yarn dev
```

The client should be hosted at [http://localhost:3000](http://localhost:3000).

The server should be hosted at [http://localhost:4000](http://localhost:4000).
The server should be hosted at [http://localhost:8080](http://localhost:8080).

TODO: Database setup and initialization using Prisma Migrate.
7. The database should be located at [http://localhost:5432](http://localhost:5432). pgadmin is accessible from [http://localhost:5050](http://localhost:5050), and the credentials are `admin@admin.com:pgadmin4`. To connect to the database using pgadmin, create a new server connection with the host set to `host.docker.internal`, port set to `5432`, and username and password set to `user` and `pass` respectively.

## Regenerating Client

Expand All @@ -33,10 +65,10 @@ When making changes to our backend, we need to regenerate our frontend client to
1. Start instance of backend server hosted at [http://localhost:4000](http://localhost:4000) by running the following command in the server directory:

```bash
yarn start:dev
yarn backend:docker:up
```

2. In the client directory, run the following command:
2. Run the following command:

```bash
yarn gen-client
Expand Down
4 changes: 2 additions & 2 deletions apps/server/docker-compose.db.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
version: "3"
version: '3'
services:
db:
image: postgres:13
restart: always
ports:
- "5432:5432"
- '5432:5432'
volumes:
- ./pgdata:/var/lib/postgresql/data
environment:
Expand Down
6 changes: 3 additions & 3 deletions apps/server/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: "3"
version: '3'
services:
app:
restart: on-failure
Expand All @@ -13,7 +13,7 @@ services:
NODE_ENV: development
PORT: 8080
ports:
- "8080:8080"
- '8080:8080'
command: node dist/main.js
volumes:
- ./src:/apps
Expand All @@ -24,7 +24,7 @@ services:
image: postgres:13
restart: always
ports:
- "5432:5432"
- '5432:5432'
volumes:
- ./pgdata:/var/lib/postgresql/data
environment:
Expand Down
111 changes: 111 additions & 0 deletions apps/server/prisma/migrations/20230921191946_base/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
-- CreateTable
CREATE TABLE "Department" (
"id" UUID NOT NULL,
"name" VARCHAR(255) NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,

CONSTRAINT "Department_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "Employee" (
"id" UUID NOT NULL,
"firstName" VARCHAR(255) NOT NULL,
"lastName" VARCHAR(255) NOT NULL,
"email" VARCHAR(255) NOT NULL,
"pswdHash" VARCHAR(255),
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
"departmentId" UUID NOT NULL,

CONSTRAINT "Employee_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "Position" (
"id" UUID NOT NULL,
"name" VARCHAR(255) NOT NULL,
"single" BOOLEAN NOT NULL DEFAULT false,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,

CONSTRAINT "Position_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "SignatureField" (
"id" UUID NOT NULL,
"name" VARCHAR(255) NOT NULL,
"order" INTEGER NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
"signerPositionId" UUID,
"formTemplateId" UUID NOT NULL,

CONSTRAINT "SignatureField_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "Signature" (
"id" UUID NOT NULL,
"order" INTEGER NOT NULL,
"signed" BOOLEAN NOT NULL DEFAULT false,
"signedDocLink" VARCHAR(255),
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
"signerPositionId" UUID,
"userSignedById" UUID,
"formInstanceId" UUID NOT NULL,

CONSTRAINT "Signature_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "FormInstance" (
"id" UUID NOT NULL,
"name" VARCHAR(255) NOT NULL,
"formDocLink" VARCHAR(255) NOT NULL,
"completed" BOOLEAN NOT NULL DEFAULT false,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
"originatorId" UUID NOT NULL,
"formTemplateId" UUID NOT NULL,

CONSTRAINT "FormInstance_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "FormTemplate" (
"id" UUID NOT NULL,
"name" VARCHAR(255) NOT NULL,
"formDocLink" VARCHAR(255) NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,

CONSTRAINT "FormTemplate_pkey" PRIMARY KEY ("id")
);

-- AddForeignKey
ALTER TABLE "Employee" ADD CONSTRAINT "Employee_departmentId_fkey" FOREIGN KEY ("departmentId") REFERENCES "Department"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "SignatureField" ADD CONSTRAINT "SignatureField_signerPositionId_fkey" FOREIGN KEY ("signerPositionId") REFERENCES "Position"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "SignatureField" ADD CONSTRAINT "SignatureField_formTemplateId_fkey" FOREIGN KEY ("formTemplateId") REFERENCES "FormTemplate"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Signature" ADD CONSTRAINT "Signature_signerPositionId_fkey" FOREIGN KEY ("signerPositionId") REFERENCES "Position"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Signature" ADD CONSTRAINT "Signature_userSignedById_fkey" FOREIGN KEY ("userSignedById") REFERENCES "Employee"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Signature" ADD CONSTRAINT "Signature_formInstanceId_fkey" FOREIGN KEY ("formInstanceId") REFERENCES "FormInstance"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "FormInstance" ADD CONSTRAINT "FormInstance_originatorId_fkey" FOREIGN KEY ("originatorId") REFERENCES "Employee"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "FormInstance" ADD CONSTRAINT "FormInstance_formTemplateId_fkey" FOREIGN KEY ("formTemplateId") REFERENCES "FormTemplate"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
3 changes: 3 additions & 0 deletions apps/server/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"
20 changes: 10 additions & 10 deletions apps/server/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ generator client {
}

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

// `Departments` represent the various departments that employees could work in.
model Department {
id String @id @default(uuid()) @db.Uuid
name String @db.VarChar(255)
id String @id @default(uuid()) @db.Uuid
name String @db.VarChar(255)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
Expand Down Expand Up @@ -69,7 +69,7 @@ model SignatureField {
signerPositionId String? @db.Uuid
formTemplate FormTemplate @relation(fields: [formTemplateId], references: [id])
formTemplateId String
formTemplateId String @db.Uuid
}

// `Signatures` represent the signatures required on a form. Each signature has a
Expand All @@ -90,10 +90,10 @@ model Signature {
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
signerPosition Position? @relation(fields: [signerPositionId], references: [id])
signerPositionId String? @db.Uuid
userSignedBy Employee? @relation(fields: [userSignedById], references: [id])
userSignedById String? @db.Uuid
signerPosition Position? @relation(fields: [signerPositionId], references: [id])
signerPositionId String? @db.Uuid
userSignedBy Employee? @relation(fields: [userSignedById], references: [id])
userSignedById String? @db.Uuid
formInstance FormInstance @relation(fields: [formInstanceId], references: [id])
formInstanceId String @db.Uuid
}
Expand All @@ -118,7 +118,7 @@ model FormInstance {
originator Employee @relation(fields: [originatorId], references: [id])
originatorId String @db.Uuid
formTemplate FormTemplate @relation(fields: [formTemplateId], references: [id])
formTemplateId String
formTemplateId String @db.Uuid
}

// A `FormTemplate` is a reference for a form that is used when creating forms initiated by users.
Expand Down
17 changes: 3 additions & 14 deletions apps/web/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
"extends": "../../tsconfig.json",
"compilerOptions": {
"target": "ESNext",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
Expand All @@ -23,13 +19,6 @@
}
]
},
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx",
".next/types/**/*.ts"
],
"exclude": [
"node_modules"
]
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"]
}
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
"apps/web"
],
"scripts": {
"dev": "yarn --cwd apps/web dev & yarn --cwd apps/server start:dev",
"dev": "yarn --cwd apps/web dev",
"format": "yarn workspaces run prettier . --write",
"lint": "yarn workspaces run lint",
"test": "yarn workspaces run test --passWithNoTests",
"tsc": "yarn workspaces run tsc -p tsconfig.json",
"migrate": "yarn --cwd apps/server prisma migrate dev",
"gen-client": "yarn --cwd apps/web openapi --input http://localhost:4000/api-json --output ./src/client --client axios",
"dev:db:up": "docker compose -f apps/server/docker-compose.db.yml up -d",
"dev:db:down": "docker compose -f apps/server/docker-compose.db.yml down",
"backend:docker:build": "docker compose -f apps/server/docker-compose.yml build",
Expand All @@ -24,6 +26,9 @@
"author": "",
"license": "ISC",
"dependencies": {
"@yarnpkg/cli": "^4.0.0-rc.51",
"@yarnpkg/core": "^4.0.0-rc.51",
"@yarnpkg/plugin-git": "^3.0.0-rc",
"@yarnpkg/plugin-workspace-tools": "^4.0.0-rc.51"
}
}
Loading

1 comment on commit 13379db

@vercel
Copy link

@vercel vercel bot commented on 13379db Sep 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.