Skip to content

Commit

Permalink
setup prisma, setup database, created a database schemas and migrate …
Browse files Browse the repository at this point in the history
…them using prisma
  • Loading branch information
Priyanshu9898 committed Dec 24, 2024
1 parent c2bb38b commit d3b4c29
Show file tree
Hide file tree
Showing 5 changed files with 287 additions and 0 deletions.
71 changes: 71 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"eslint": "^9",
"eslint-config-next": "15.1.1",
"postcss": "^8",
"prisma": "^6.1.0",
"tailwindcss": "^3.4.1",
"typescript": "^5"
}
Expand Down
105 changes: 105 additions & 0 deletions prisma/migrations/20241224233636_create_models/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
-- CreateEnum
CREATE TYPE "TransactionType" AS ENUM ('INCOME', 'EXPENSE');

-- CreateEnum
CREATE TYPE "RecurringInterval" AS ENUM ('DAILY', 'WEEKLY', 'MONTHLY', 'YEARLY');

-- CreateEnum
CREATE TYPE "TransactionStatus" AS ENUM ('PENDING', 'COMPLETED', 'FAILED');

-- CreateEnum
CREATE TYPE "AccountType" AS ENUM ('CURRENT', 'SAVINGS');

-- CreateTable
CREATE TABLE "users" (
"id" TEXT NOT NULL,
"clerkUserId" TEXT NOT NULL,
"email" TEXT NOT NULL,
"name" TEXT,
"imageUrl" TEXT,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "accounts" (
"id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"type" "AccountType" NOT NULL,
"balance" DECIMAL(65,30) NOT NULL DEFAULT 0,
"isDefault" BOOLEAN NOT NULL DEFAULT false,
"userId" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "transactions" (
"id" TEXT NOT NULL,
"type" "TransactionType" NOT NULL,
"amount" DECIMAL(65,30) NOT NULL,
"description" TEXT,
"date" TIMESTAMP(3) NOT NULL,
"category" TEXT NOT NULL,
"receiptUrl" TEXT,
"isRecurring" BOOLEAN NOT NULL DEFAULT false,
"recurringInterval" "RecurringInterval",
"nextRecurringDate" TIMESTAMP(3),
"lastProcessed" TIMESTAMP(3),
"status" "TransactionStatus" NOT NULL DEFAULT 'COMPLETED',
"userId" TEXT NOT NULL,
"accountId" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "budgets" (
"id" TEXT NOT NULL,
"amount" DECIMAL(65,30) NOT NULL,
"lastAlertSent" TIMESTAMP(3),
"userId" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateIndex
CREATE UNIQUE INDEX "users_clerkUserId_key" ON "users"("clerkUserId");

-- CreateIndex
CREATE UNIQUE INDEX "users_email_key" ON "users"("email");

-- CreateIndex
CREATE INDEX "accounts_userId_idx" ON "accounts"("userId");

-- CreateIndex
CREATE INDEX "transactions_userId_idx" ON "transactions"("userId");

-- CreateIndex
CREATE INDEX "transactions_accountId_idx" ON "transactions"("accountId");

-- CreateIndex
CREATE UNIQUE INDEX "budgets_userId_key" ON "budgets"("userId");

-- CreateIndex
CREATE INDEX "budgets_userId_idx" ON "budgets"("userId");

-- AddForeignKey
ALTER TABLE "accounts" ADD CONSTRAINT "accounts_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "transactions" ADD CONSTRAINT "transactions_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "transactions" ADD CONSTRAINT "transactions_accountId_fkey" FOREIGN KEY ("accountId") REFERENCES "accounts"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "budgets" ADD CONSTRAINT "budgets_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
3 changes: 3 additions & 0 deletions 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 (e.g., Git)
provider = "postgresql"
107 changes: 107 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

// Looking for ways to speed up your queries, or scale easily with your serverless or edge functions?
// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init

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

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

model User {
id String @id @default(uuid())
clerkUserId String @unique // clerk user id
email String @unique
name String?
imageUrl String?
transactions Transaction[]
accounts Account[]
budgets Budget[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@map("users")
}

model Account {
id String @id @default(uuid())
name String
type AccountType
balance Decimal @default(0) // will ask inital balance while creating an account
isDefault Boolean @default(false)
userId String
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
transactions Transaction[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@index([userId])
@@map("accounts")
}

model Transaction {
id String @id @default(uuid())
type TransactionType
amount Decimal
description String?
date DateTime
category String
receiptUrl String?
isRecurring Boolean @default(false) // Only used if isRecurring is true
recurringInterval RecurringInterval?
nextRecurringDate DateTime? // Next date for recurring transaction
lastProcessed DateTime? // Last time this recurring transaction was processed
status TransactionStatus @default(COMPLETED)
userId String
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
accountId String
account Account @relation(fields: [accountId], references: [id], onDelete: Cascade)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@index([userId])
@@index([accountId])
@@map("transactions")
}

enum TransactionType {
INCOME
EXPENSE
}

enum RecurringInterval {
DAILY
WEEKLY
MONTHLY
YEARLY
}

enum TransactionStatus {
PENDING
COMPLETED
FAILED
}

enum AccountType {
CURRENT
SAVINGS
}

model Budget {
id String @id @default(uuid())
amount Decimal
lastAlertSent DateTime? // Track when the last alert was sent
userId String @unique
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@index([userId])
@@map("budgets")
}

0 comments on commit d3b4c29

Please sign in to comment.