N-FIN-2: add models to schema

This commit is contained in:
Markus Thielker 2024-03-08 22:18:49 +01:00
parent c18b086c7c
commit 252bdfc2a3
No known key found for this signature in database
2 changed files with 130 additions and 0 deletions

View file

@ -0,0 +1,70 @@
-- CreateEnum
CREATE TYPE "EntityType" AS ENUM ('Entity', 'Account');
-- CreateTable
CREATE TABLE "entities" (
"id" SERIAL NOT NULL,
"user_id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"type" "EntityType" NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL,
CONSTRAINT "entities_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "payments" (
"id" SERIAL NOT NULL,
"user_id" TEXT NOT NULL,
"amount" INTEGER NOT NULL,
"currency" TEXT NOT NULL DEFAULT 'EUR',
"date" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"payor_id" INTEGER NOT NULL,
"payee_id" INTEGER NOT NULL,
"note" TEXT,
"category_id" INTEGER,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL,
CONSTRAINT "payments_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "categories" (
"id" SERIAL NOT NULL,
"user_id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"color" TEXT NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL,
CONSTRAINT "categories_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE UNIQUE INDEX "entities_user_id_name_key" ON "entities"("user_id", "name");
-- CreateIndex
CREATE UNIQUE INDEX "categories_user_id_name_key" ON "categories"("user_id", "name");
-- AddForeignKey
ALTER TABLE "lucia_session" ADD CONSTRAINT "lucia_session_userId_fkey" FOREIGN KEY ("userId") REFERENCES "lucia_user"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "entities" ADD CONSTRAINT "entities_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "lucia_user"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "payments" ADD CONSTRAINT "payments_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "lucia_user"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "payments" ADD CONSTRAINT "payments_payor_id_fkey" FOREIGN KEY ("payor_id") REFERENCES "entities"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "payments" ADD CONSTRAINT "payments_payee_id_fkey" FOREIGN KEY ("payee_id") REFERENCES "entities"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "payments" ADD CONSTRAINT "payments_category_id_fkey" FOREIGN KEY ("category_id") REFERENCES "categories"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "categories" ADD CONSTRAINT "categories_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "lucia_user"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

View file

@ -16,6 +16,10 @@ model User {
username String @unique
password String
entities Entity[]
payments Payment[]
categories Category[]
@@map("lucia_user")
}
@ -28,3 +32,59 @@ model Session {
@@map("lucia_session")
}
model Entity {
id Int @id @default(autoincrement())
userId String @map("user_id")
user User @relation(fields: [userId], references: [id])
name String
type EntityType
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
paymentsAsPayor Payment[] @relation("PayorEntity")
paymentsAsPayee Payment[] @relation("PayeeEntity")
@@unique(fields: [userId, name])
@@map("entities")
}
enum EntityType {
Entity
Account
}
model Payment {
id Int @id @default(autoincrement())
userId String @map("user_id")
user User @relation(fields: [userId], references: [id])
amount Int
currency String @default("EUR")
date DateTime @default(now())
payor Entity @relation("PayorEntity", fields: [payorId], references: [id])
payorId Int @map("payor_id")
payee Entity @relation("PayeeEntity", fields: [payeeId], references: [id])
payeeId Int @map("payee_id")
note String?
category Category? @relation(fields: [categoryId], references: [id])
categoryId Int? @map("category_id")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
@@map("payments")
}
model Category {
id Int @id @default(autoincrement())
userId String @map("user_id")
user User @relation(fields: [userId], references: [id])
name String
color String
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
payments Payment[]
@@unique(fields: [userId, name])
@@map("categories")
}