Skip to content

Commit

Permalink
Merge branch 'main' into database
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohamed-khattab authored Feb 23, 2024
2 parents 9beb9bd + 34c79a7 commit 993103a
Show file tree
Hide file tree
Showing 13 changed files with 615 additions and 111 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Ignore file
.gitignore

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

Expand Down Expand Up @@ -122,9 +125,13 @@ dist
# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# VSCode
.vscode/

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

2 changes: 2 additions & 0 deletions docs/prisma-erd.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
216 changes: 216 additions & 0 deletions docs/schema.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
# Schema Documentation

## Overview

This is a a comprehensive overview of the database schema used in the platform, including entities, relationships, enums, diagrams and additional notes for proper usage and maintenance.

### Technologies Used

**Database Management System:** PostgreSQL

- PostgreSQL is scalable open-source relational database management system known for SQL compliance,
advanced concurrency control, extensibility, and strong security.

**ORM:** Prisma

- Prisma is a Node.js and Typescript ORM with an intuitive data model, automated migrations, type-safety,
and auto-completion. It defines the main configuration of the data models in a file called `schema.prisma`

## Prisma Components

We depend on Prisma as an ORM to connect our application to the database using the following components:

### Generator

Specifies how Prisma generates client code for interacting with the database, such as Prisma Client.

```prisma
generator client {
provider = "prisma-client-js"
}
```

### Data Source

Defines the database connection details, including the provider (in our case PostgreSQL), and URL, which is stored in an environment variable.

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

## Schema Description

### Entities

#### Structure

The schema consists of the following entities:

- **User:** Represents users of the system.

```prisma
model User {
id Int @id @default(autoincrement())
email String @unique
name String
password String
role UserRole @default(USER)
teams TeamMembers[]
}
```

- **Team:** Represents teams with members and projects.

```prisma
model Team {
id Int @id @default(autoincrement())
name String
members TeamMembers[]
projects Project[]
}
```

- **TeamMembers:** Represents the relationship between users and teams, including their roles.

```prisma
model TeamMembers {
teamId Int
userId Int
team Team @relation(fields: [teamId], references: [id], onDelete: Cascade)
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
role MemberRole @default(MEMBER)
@@id([teamId, userId])
}
```

- **Project:** Represents projects belonging to teams.

```prisma
model Project {
id Int @id @default(autoincrement())
name String
teamId Int
team Team @relation(fields: [teamId], references: [id], onDelete: Cascade)
databases Database[]
uuid String @default(uuid())
}
```

- **Database:** Represents databases associated with projects.

```prisma
model Database {
id String @id @default(uuid())
name String
createdAt DateTime @default(now())
storage Int
projectId Int
project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)
status status @default(INACTIVE)
}
```

### ERD Diagram

This ERD Diagram helps visualize the relationships between each entity in the schema and how they depend on each other
![ERD diagram of the schema](./prisma-erd.svg "ERD Diagram of the schema")

### Entity Details

#### User

- `id`: Auto-incremented unique identifier for the user.
- `email`: Unique email address of the user.
- `name`: Name of the user
- `password`: Hashed password of the user
- `role`: Role of the user, default: `USER`
- Relationships:
- `teams` Many-to-many relationship with Team entity through TeamMembers property

#### Team

- `id` Auto-incremented unique identifier for the team
- `name` Name of the team
- Relationships:
- `members` One-to-many relationship with team members
- `projects` One-to-many relationship with projects

#### TeamMembers

- `teamId` Foreign key referencing the id column of the Team table, pointing to the team that the member belongs to
- `userId` Foreign key referencing the id column of the User table, pointing to the user that is a member of the team
- `role` Role of the member in the team, default: `MEMBER`
- Relationships:
- `team` Many-to-one relationship with teams
- `user` Many-to-one relationship with users

#### Project

- `id` Auto-incremented unique identifier for the project
- `name` Name of the project
- `teamId` Foreign key referencing the id column of the Team table, pointing to the team that created the project
- `uuid` Unique UUID identifier for the project
- Relationships:
- `team` Many-to-one relationship with the team that creates the projects
- `databases` One-to-many relationship with databases associated with the project

#### Database

- `id` Unique UUID identifier for the database
- `name`Name of the database
- `createdAt` Timestamp indicating the creation time of the database
- `storage` Storage capacity of the database
- `projectId` Foreign key referencing the id column of the Project table, pointing to the project that the database belongs to
- Relationships:
- `project` Many-to-one relationship with the project

### Enums

#### UserRole

- `USER`: Regular user
- Regular user with standard access privileges on teams and projects.
- `ADMIN`: Admin user
- User with elevated access rights for system administration of the platform.

```prisma
enum UserRole {
USER
ADMIN
}
```

#### MemberRole

- `MEMBER`: Regular member
- Regular team member with access to the team's projects
- `LEADER`: Leader member
- The user that created the team and has the privileges to create and delete projects.

```prisma
enum MemberRole {
LEADER
MEMBER
}
```

#### Status

- `ACTIVE`: Active database
- Database that is online and fully responsive
- `INACTIVE`: Inactive database
- Database that hasn't been used for a while but still responsive
- `SLEEP`: Sleeping database
- Database that was `INACTIVE` for a certain amount of time and entered an offline mode, and it must be activated in order to be responsive for queries

```prisma
enum status {
ACTIVE
INACTIVE
SLEEP
}
```
62 changes: 34 additions & 28 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
"cookie-parser": "^1.4.6",
"dotenv": "^16.4.1",
"express": "^4.18.2",
"express-async-handler": "^1.2.0",
"jsonwebtoken": "^9.0.2",
"nodemon": "^3.0.3",
"prisma": "^5.8.1"
"prisma": "^5.9.1"
}
}
8 changes: 0 additions & 8 deletions prisma/migrations/20240207163006_uuid/migration.sql

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ CREATE TABLE "User" (
CREATE TABLE "Team" (
"id" SERIAL NOT NULL,
"name" TEXT NOT NULL,
"token" TEXT,
"expiry" TIMESTAMP(3),

CONSTRAINT "Team_pkey" PRIMARY KEY ("id")
);
Expand All @@ -40,6 +42,7 @@ CREATE TABLE "Project" (
"id" SERIAL NOT NULL,
"name" TEXT NOT NULL,
"teamId" INTEGER NOT NULL,
"uuid" TEXT NOT NULL,

CONSTRAINT "Project_pkey" PRIMARY KEY ("id")
);
Expand All @@ -59,6 +62,9 @@ CREATE TABLE "Database" (
-- CreateIndex
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");

-- CreateIndex
CREATE UNIQUE INDEX "Team_token_key" ON "Team"("token");

-- AddForeignKey
ALTER TABLE "TeamMembers" ADD CONSTRAINT "TeamMembers_teamId_fkey" FOREIGN KEY ("teamId") REFERENCES "Team"("id") ON DELETE CASCADE ON UPDATE CASCADE;

Expand Down
Loading

0 comments on commit 993103a

Please sign in to comment.