diff --git a/src/app/account/page.tsx b/src/app/account/page.tsx index 4e98014..0023029 100644 --- a/src/app/account/page.tsx +++ b/src/app/account/page.tsx @@ -7,7 +7,7 @@ import { Label } from '@/components/ui/label'; import { Input } from '@/components/ui/input'; import { URL_SIGN_IN } from '@/lib/constants'; import generateSampleData from '@/lib/actions/generateSampleData'; -import { prismaClient } from '@/prisma'; +import prisma from '@/prisma'; import { ServerActionTrigger } from '@/components/form/serverActionTrigger'; import accountDelete from '@/lib/actions/accountDelete'; @@ -20,21 +20,21 @@ export default async function AccountPage() { } let paymentCount = 0; - paymentCount = await prismaClient.payment.count({ + paymentCount = await prisma.payment.count({ where: { userId: user.id, }, }); let entityCount = 0; - entityCount = await prismaClient.entity.count({ + entityCount = await prisma.entity.count({ where: { userId: user.id, }, }); let categoryCount = 0; - categoryCount = await prismaClient.category.count({ + categoryCount = await prisma.category.count({ where: { userId: user.id, }, diff --git a/src/app/categories/page.tsx b/src/app/categories/page.tsx index f7bee73..73c6251 100644 --- a/src/app/categories/page.tsx +++ b/src/app/categories/page.tsx @@ -1,5 +1,5 @@ import { getUser } from '@/auth'; -import { prismaClient } from '@/prisma'; +import prisma from '@/prisma'; import React from 'react'; import CategoryPageClientContent from '@/components/categoryPageClientComponents'; import categoryCreateUpdate from '@/lib/actions/categoryCreateUpdate'; @@ -9,7 +9,7 @@ export default async function CategoriesPage() { const user = await getUser(); - const categories = await prismaClient.category.findMany({ + const categories = await prisma.category.findMany({ where: { userId: user?.id, }, diff --git a/src/app/entities/page.tsx b/src/app/entities/page.tsx index f5b7d0a..e39176c 100644 --- a/src/app/entities/page.tsx +++ b/src/app/entities/page.tsx @@ -1,4 +1,4 @@ -import { prismaClient } from '@/prisma'; +import prisma from '@/prisma'; import { getUser } from '@/auth'; import React from 'react'; import EntityPageClientContent from '@/components/entityPageClientComponents'; @@ -9,7 +9,7 @@ export default async function EntitiesPage() { const user = await getUser(); - const entities = await prismaClient.entity.findMany({ + const entities = await prisma.entity.findMany({ where: { userId: user?.id, }, @@ -23,7 +23,7 @@ export default async function EntitiesPage() { ], }); - const categories = await prismaClient.category.findMany({ + const categories = await prisma.category.findMany({ where: { userId: user?.id, }, diff --git a/src/app/page.tsx b/src/app/page.tsx index 54450d0..72c34da 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -1,7 +1,7 @@ import React from 'react'; import { Category, Entity, EntityType } from '@prisma/client'; import { Scope, ScopeType } from '@/lib/types/scope'; -import { prismaClient } from '@/prisma'; +import prisma from '@/prisma'; import { getUser } from '@/auth'; import DashboardPageClient from '@/components/dashboardPageClientComponents'; @@ -25,7 +25,7 @@ export default async function DashboardPage(props: { searchParams?: { scope: Sco const scope = Scope.of(props.searchParams?.scope || ScopeType.ThisMonth); // get all payments in the current scope - const payments = await prismaClient.payment.findMany({ + const payments = await prisma.payment.findMany({ where: { userId: user?.id, date: { @@ -108,6 +108,7 @@ export default async function DashboardPage(props: { searchParams?: { scope: Sco userId: '', name: 'Other', type: EntityType.Entity, + defaultCategoryId: null, createdAt: new Date(), updatedAt: new Date(), }, diff --git a/src/app/payments/page.tsx b/src/app/payments/page.tsx index bbec906..f1ffa66 100644 --- a/src/app/payments/page.tsx +++ b/src/app/payments/page.tsx @@ -1,5 +1,5 @@ import { getUser } from '@/auth'; -import { prismaClient } from '@/prisma'; +import prisma from '@/prisma'; import React from 'react'; import PaymentPageClientContent from '@/components/paymentPageClientComponents'; import paymentCreateUpdate from '@/lib/actions/paymentCreateUpdate'; @@ -9,7 +9,7 @@ export default async function PaymentsPage() { const user = await getUser(); - const payments = await prismaClient.payment.findMany({ + const payments = await prisma.payment.findMany({ where: { userId: user?.id, }, @@ -23,7 +23,7 @@ export default async function PaymentsPage() { ], }); - const entities = await prismaClient.entity.findMany({ + const entities = await prisma.entity.findMany({ where: { userId: user?.id, }, @@ -37,7 +37,7 @@ export default async function PaymentsPage() { ], }); - const categories = await prismaClient.category.findMany({ + const categories = await prisma.category.findMany({ where: { userId: user?.id, }, diff --git a/src/auth.ts b/src/auth.ts index 26cdb6c..ae752a6 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -1,9 +1,9 @@ import { Lucia } from 'lucia'; import { PrismaAdapter } from '@lucia-auth/adapter-prisma'; import { cookies } from 'next/headers'; -import { prismaClient } from '@/prisma'; +import prisma from '@/prisma'; -const adapter = new PrismaAdapter(prismaClient.session, prismaClient.user); +const adapter = new PrismaAdapter(prisma.session, prisma.user); export const lucia = new Lucia(adapter, { sessionCookie: { diff --git a/src/lib/actions/accountDelete.ts b/src/lib/actions/accountDelete.ts index 28cf745..952e31c 100644 --- a/src/lib/actions/accountDelete.ts +++ b/src/lib/actions/accountDelete.ts @@ -1,7 +1,7 @@ import { ActionResponse } from '@/lib/types/actionResponse'; import { URL_SIGN_IN } from '@/lib/constants'; import { getUser, lucia } from '@/auth'; -import { prismaClient } from '@/prisma'; +import prisma from '@/prisma'; import { cookies } from 'next/headers'; export default async function accountDelete(): Promise { @@ -17,31 +17,31 @@ export default async function accountDelete(): Promise { }; } - await prismaClient.payment.deleteMany({ + await prisma.payment.deleteMany({ where: { userId: user.id, }, }); - await prismaClient.entity.deleteMany({ + await prisma.entity.deleteMany({ where: { userId: user.id, }, }); - await prismaClient.category.deleteMany({ + await prisma.category.deleteMany({ where: { userId: user.id, }, }); - await prismaClient.session.deleteMany({ + await prisma.session.deleteMany({ where: { userId: user.id, }, }); - await prismaClient.user.delete({ + await prisma.user.delete({ where: { id: user.id, }, diff --git a/src/lib/actions/categoryCreateUpdate.ts b/src/lib/actions/categoryCreateUpdate.ts index a7796d5..11bdd5f 100644 --- a/src/lib/actions/categoryCreateUpdate.ts +++ b/src/lib/actions/categoryCreateUpdate.ts @@ -1,6 +1,6 @@ import { z } from 'zod'; import { ActionResponse } from '@/lib/types/actionResponse'; -import { prismaClient } from '@/prisma'; +import prisma from '@/prisma'; import { getUser } from '@/auth'; import { URL_SIGN_IN } from '@/lib/constants'; import { categoryFormSchema } from '@/lib/form-schemas/categoryFormSchema'; @@ -25,7 +25,7 @@ export default async function categoryCreateUpdate({ // create/update category try { if (id) { - await prismaClient.category.update({ + await prisma.category.update({ where: { id: id, }, @@ -42,7 +42,7 @@ export default async function categoryCreateUpdate({ message: `'${name}' updated`, }; } else { - await prismaClient.category.create({ + await prisma.category.create({ data: { userId: user.id, name: name, diff --git a/src/lib/actions/categoryDelete.ts b/src/lib/actions/categoryDelete.ts index 11cc496..8773fe3 100644 --- a/src/lib/actions/categoryDelete.ts +++ b/src/lib/actions/categoryDelete.ts @@ -1,5 +1,5 @@ import { ActionResponse } from '@/lib/types/actionResponse'; -import { prismaClient } from '@/prisma'; +import prisma from '@/prisma'; import { getUser } from '@/auth'; import { URL_SIGN_IN } from '@/lib/constants'; @@ -25,7 +25,7 @@ export default async function categoryDelete(id: number): Promise } // check that entity is associated with user - const entity = await prismaClient.entity.findFirst({ + const entity = await prisma.entity.findFirst({ where: { id: id, userId: user.id, @@ -40,7 +40,7 @@ export default async function entityDelete(id: number): Promise // delete entity try { - await prismaClient.entity.delete({ + await prisma.entity.delete({ where: { id: entity.id, userId: user.id, diff --git a/src/lib/actions/generateSampleData.ts b/src/lib/actions/generateSampleData.ts index 86db509..09ea36b 100644 --- a/src/lib/actions/generateSampleData.ts +++ b/src/lib/actions/generateSampleData.ts @@ -1,4 +1,4 @@ -import { prismaClient } from '@/prisma'; +import prisma from '@/prisma'; import type { Category, Entity } from '@prisma/client'; import { EntityType } from '@prisma/client'; import { getUser } from '@/auth'; @@ -19,12 +19,12 @@ export default async function generateSampleData(): Promise { } // Categories: create sample data - const categories: Category[] = await prismaClient.category.findMany({where: {userId: user.id}}); - if (await prismaClient.category.count({where: {userId: user.id}}) == 0) { + const categories: Category[] = await prisma.category.findMany({where: {userId: user.id}}); + if (await prisma.category.count({where: {userId: user.id}}) == 0) { console.log('Creating sample categories...'); - categories.push(await prismaClient.category.create({ + categories.push(await prisma.category.create({ data: { userId: user.id, name: 'Groceries', @@ -32,7 +32,7 @@ export default async function generateSampleData(): Promise { }, })); - categories.push(await prismaClient.category.create({ + categories.push(await prisma.category.create({ data: { userId: user.id, name: 'Drugstore items', @@ -40,7 +40,7 @@ export default async function generateSampleData(): Promise { }, })); - categories.push(await prismaClient.category.create({ + categories.push(await prisma.category.create({ data: { userId: user.id, name: 'Going out', @@ -48,7 +48,7 @@ export default async function generateSampleData(): Promise { }, })); - categories.push(await prismaClient.category.create({ + categories.push(await prisma.category.create({ data: { userId: user.id, name: 'Random stuff', @@ -56,7 +56,7 @@ export default async function generateSampleData(): Promise { }, })); - categories.push(await prismaClient.category.create({ + categories.push(await prisma.category.create({ data: { userId: user.id, name: 'Salary', @@ -69,12 +69,12 @@ export default async function generateSampleData(): Promise { console.log(categories); // Entities: create sample data - const entities: Entity[] = await prismaClient.entity.findMany({where: {userId: user.id}}); - if (await prismaClient.entity.count({where: {userId: user.id}}) == 0) { + const entities: Entity[] = await prisma.entity.findMany({where: {userId: user.id}}); + if (await prisma.entity.count({where: {userId: user.id}}) == 0) { console.log('Creating sample entities...'); - entities.push(await prismaClient.entity.create({ + entities.push(await prisma.entity.create({ data: { userId: user.id, name: 'Main Account', @@ -82,7 +82,7 @@ export default async function generateSampleData(): Promise { }, })); - entities.push(await prismaClient.entity.create({ + entities.push(await prisma.entity.create({ data: { userId: user.id, name: 'Company', @@ -90,7 +90,7 @@ export default async function generateSampleData(): Promise { }, })); - entities.push(await prismaClient.entity.create({ + entities.push(await prisma.entity.create({ data: { userId: user.id, name: 'Supermarket 1', @@ -98,7 +98,7 @@ export default async function generateSampleData(): Promise { }, })); - entities.push(await prismaClient.entity.create({ + entities.push(await prisma.entity.create({ data: { userId: user.id, name: 'Supermarket 2', @@ -106,7 +106,7 @@ export default async function generateSampleData(): Promise { }, })); - entities.push(await prismaClient.entity.create({ + entities.push(await prisma.entity.create({ data: { userId: user.id, name: 'Supermarket 3', @@ -114,7 +114,7 @@ export default async function generateSampleData(): Promise { }, })); - entities.push(await prismaClient.entity.create({ + entities.push(await prisma.entity.create({ data: { userId: user.id, name: 'Supermarket 4', @@ -129,14 +129,14 @@ export default async function generateSampleData(): Promise { // Payments: create sample data console.log('Creating sample payments...'); - if (await prismaClient.payment.count({where: {userId: user.id}}) == 0) { + if (await prisma.payment.count({where: {userId: user.id}}) == 0) { for (let i = 0; i < 4; i++) { const date = new Date(); date.setDate(1); date.setMonth(date.getMonth() - i); - await prismaClient.payment.create({ + await prisma.payment.create({ data: { userId: user.id, amount: 200000, @@ -164,7 +164,7 @@ export default async function generateSampleData(): Promise { const date = new Date( new Date().getTime() - Math.floor(Math.random() * 10000000000)); - await prismaClient.payment.create({ + await prisma.payment.create({ data: { userId: user.id, amount: Math.floor( diff --git a/src/lib/actions/paymentCreateUpdate.ts b/src/lib/actions/paymentCreateUpdate.ts index a321062..4a66d33 100644 --- a/src/lib/actions/paymentCreateUpdate.ts +++ b/src/lib/actions/paymentCreateUpdate.ts @@ -1,6 +1,6 @@ import { z } from 'zod'; import { ActionResponse } from '@/lib/types/actionResponse'; -import { prismaClient } from '@/prisma'; +import prisma from '@/prisma'; import { getUser } from '@/auth'; import { URL_SIGN_IN } from '@/lib/constants'; import { paymentFormSchema } from '@/lib/form-schemas/paymentFormSchema'; @@ -29,7 +29,7 @@ export default async function paymentCreateUpdate({ // create/update payment try { if (id) { - await prismaClient.payment.update({ + await prisma.payment.update({ where: { id: id, }, @@ -50,7 +50,7 @@ export default async function paymentCreateUpdate({ message: `Payment updated`, }; } else { - await prismaClient.payment.create({ + await prisma.payment.create({ data: { userId: user.id, amount: amount, diff --git a/src/lib/actions/paymentDelete.ts b/src/lib/actions/paymentDelete.ts index 3401e92..a3bfd16 100644 --- a/src/lib/actions/paymentDelete.ts +++ b/src/lib/actions/paymentDelete.ts @@ -1,5 +1,5 @@ import { ActionResponse } from '@/lib/types/actionResponse'; -import { prismaClient } from '@/prisma'; +import prisma from '@/prisma'; import { getUser } from '@/auth'; import { URL_SIGN_IN } from '@/lib/constants'; @@ -25,7 +25,7 @@ export default async function paymentDelete(id: number): Promise } // check that payment is associated with user - const payment = await prismaClient.payment.findFirst({ + const payment = await prisma.payment.findFirst({ where: { id: id, userId: user.id, @@ -40,7 +40,7 @@ export default async function paymentDelete(id: number): Promise // delete payment try { - await prismaClient.payment.delete({ + await prisma.payment.delete({ where: { id: payment.id, userId: user.id, diff --git a/src/lib/actions/signIn.ts b/src/lib/actions/signIn.ts index 5168179..0e34e3d 100644 --- a/src/lib/actions/signIn.ts +++ b/src/lib/actions/signIn.ts @@ -5,12 +5,12 @@ import { cookies } from 'next/headers'; import { signInFormSchema } from '@/lib/form-schemas/signInFormSchema'; import { ActionResponse } from '@/lib/types/actionResponse'; import { URL_HOME } from '@/lib/constants'; -import { prismaClient } from '@/prisma'; +import prisma from '@/prisma'; export default async function signIn({username, password}: z.infer): Promise { 'use server'; - const existingUser = await prismaClient.user.findFirst({ + const existingUser = await prisma.user.findFirst({ where: { username: username.toLowerCase(), }, diff --git a/src/lib/actions/signUp.ts b/src/lib/actions/signUp.ts index 1b40aff..d7ea888 100644 --- a/src/lib/actions/signUp.ts +++ b/src/lib/actions/signUp.ts @@ -6,7 +6,7 @@ import { cookies } from 'next/headers'; import { signUpFormSchema } from '@/lib/form-schemas/signUpFormSchema'; import { ActionResponse } from '@/lib/types/actionResponse'; import { URL_HOME } from '@/lib/constants'; -import { prismaClient } from '@/prisma'; +import prisma from '@/prisma'; export default async function signUp({username, password}: z.infer): Promise { 'use server'; @@ -14,7 +14,7 @@ export default async function signUp({username, password}: z.infer { + return new PrismaClient(); +}; + +declare global { + // noinspection ES6ConvertVarToLetConst + var prismaGlobal: undefined | ReturnType; +} + +const prisma = globalThis.prismaGlobal ?? prismaClientSingleton(); + +export default prisma; + +if (process.env.NODE_ENV !== 'production') { + globalThis.prismaGlobal = prisma +}