diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..aa6952e --- /dev/null +++ b/.env.example @@ -0,0 +1,2 @@ +DATABASE_URL=postgresql://postgres:postgres@localhost:5432/postgres +DATABASE_URL_NON_POOLING=postgresql://postgres:postgres@localhost:5432/postgres diff --git a/README.md b/README.md index 702f160..a262a1b 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,6 @@ ## To do -- [ ] Add database schema - [ ] Add user authentication - [ ] Add user roles - [ ] Add user permissions diff --git a/prisma/migrations/20240621082614_initial_schema/migration.sql b/prisma/migrations/20240621082614_initial_schema/migration.sql new file mode 100644 index 0000000..bf156f7 --- /dev/null +++ b/prisma/migrations/20240621082614_initial_schema/migration.sql @@ -0,0 +1,36 @@ +-- CreateEnum +CREATE TYPE "ModuleType" AS ENUM ('TYPE1', 'TYPE2', 'TYPE3'); + +-- CreateTable +CREATE TABLE "User" ( + "id" TEXT NOT NULL, + "name" TEXT NOT NULL, + "email" TEXT NOT NULL, + "deleted" BOOLEAN NOT NULL DEFAULT false, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "User_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "Module" ( + "id" DOUBLE PRECISION NOT NULL, + "type" "ModuleType" NOT NULL, + "text" TEXT NOT NULL, + "deleted" BOOLEAN NOT NULL DEFAULT false, + "createdById" TEXT NOT NULL, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "Module_pkey" PRIMARY KEY ("id") +); + +-- CreateIndex +CREATE UNIQUE INDEX "User_email_key" ON "User"("email"); + +-- CreateIndex +CREATE UNIQUE INDEX "Module_id_key" ON "Module"("id"); + +-- AddForeignKey +ALTER TABLE "Module" ADD CONSTRAINT "Module_createdById_fkey" FOREIGN KEY ("createdById") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; diff --git a/prisma/migrations/migration_lock.toml b/prisma/migrations/migration_lock.toml new file mode 100644 index 0000000..fbffa92 --- /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 (i.e. Git) +provider = "postgresql" \ No newline at end of file diff --git a/prisma/prisma.ts b/prisma/prisma.ts new file mode 100644 index 0000000..bb75fe7 --- /dev/null +++ b/prisma/prisma.ts @@ -0,0 +1,9 @@ +import { PrismaClient } from '@prisma/client'; + +const globalForPrisma = global as unknown as { prisma: PrismaClient }; + +export const prisma = globalForPrisma.prisma || new PrismaClient(); + +if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma; + +export default prisma; diff --git a/prisma/schema.prisma b/prisma/schema.prisma new file mode 100644 index 0000000..f31019b --- /dev/null +++ b/prisma/schema.prisma @@ -0,0 +1,36 @@ +generator client { + provider = "prisma-client-js" +} + +datasource db { + provider = "postgresql" + url = env("DATABASE_URL") // uses connection pooling + directUrl = env("DATABASE_URL_NON_POOLING") // uses a direct connection +} + +enum ModuleType { + TYPE1 + TYPE2 + TYPE3 +} + +model User { + id String @id @default(cuid()) + name String + email String @unique + deleted Boolean @default(false) + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt + Module Module[] +} + +model Module { + id Float @id @unique + type ModuleType + text String + deleted Boolean @default(false) + createdById String + createdBy User @relation(fields: [createdById], references: [id]) + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt +}