N-FIN-61: fix prisma client instantiation

This commit is contained in:
Markus Thielker 2024-03-24 16:16:02 +01:00
parent 7731d0143e
commit 00b4e51aee
No known key found for this signature in database
17 changed files with 82 additions and 66 deletions

View file

@ -7,7 +7,7 @@ import { Label } from '@/components/ui/label';
import { Input } from '@/components/ui/input'; import { Input } from '@/components/ui/input';
import { URL_SIGN_IN } from '@/lib/constants'; import { URL_SIGN_IN } from '@/lib/constants';
import generateSampleData from '@/lib/actions/generateSampleData'; import generateSampleData from '@/lib/actions/generateSampleData';
import { prismaClient } from '@/prisma'; import prisma from '@/prisma';
import { ServerActionTrigger } from '@/components/form/serverActionTrigger'; import { ServerActionTrigger } from '@/components/form/serverActionTrigger';
import accountDelete from '@/lib/actions/accountDelete'; import accountDelete from '@/lib/actions/accountDelete';
@ -20,21 +20,21 @@ export default async function AccountPage() {
} }
let paymentCount = 0; let paymentCount = 0;
paymentCount = await prismaClient.payment.count({ paymentCount = await prisma.payment.count({
where: { where: {
userId: user.id, userId: user.id,
}, },
}); });
let entityCount = 0; let entityCount = 0;
entityCount = await prismaClient.entity.count({ entityCount = await prisma.entity.count({
where: { where: {
userId: user.id, userId: user.id,
}, },
}); });
let categoryCount = 0; let categoryCount = 0;
categoryCount = await prismaClient.category.count({ categoryCount = await prisma.category.count({
where: { where: {
userId: user.id, userId: user.id,
}, },

View file

@ -1,5 +1,5 @@
import { getUser } from '@/auth'; import { getUser } from '@/auth';
import { prismaClient } from '@/prisma'; import prisma from '@/prisma';
import React from 'react'; import React from 'react';
import CategoryPageClientContent from '@/components/categoryPageClientComponents'; import CategoryPageClientContent from '@/components/categoryPageClientComponents';
import categoryCreateUpdate from '@/lib/actions/categoryCreateUpdate'; import categoryCreateUpdate from '@/lib/actions/categoryCreateUpdate';
@ -9,7 +9,7 @@ export default async function CategoriesPage() {
const user = await getUser(); const user = await getUser();
const categories = await prismaClient.category.findMany({ const categories = await prisma.category.findMany({
where: { where: {
userId: user?.id, userId: user?.id,
}, },

View file

@ -1,4 +1,4 @@
import { prismaClient } from '@/prisma'; import prisma from '@/prisma';
import { getUser } from '@/auth'; import { getUser } from '@/auth';
import React from 'react'; import React from 'react';
import EntityPageClientContent from '@/components/entityPageClientComponents'; import EntityPageClientContent from '@/components/entityPageClientComponents';
@ -9,7 +9,7 @@ export default async function EntitiesPage() {
const user = await getUser(); const user = await getUser();
const entities = await prismaClient.entity.findMany({ const entities = await prisma.entity.findMany({
where: { where: {
userId: user?.id, 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: { where: {
userId: user?.id, userId: user?.id,
}, },

View file

@ -1,7 +1,7 @@
import React from 'react'; import React from 'react';
import { Category, Entity, EntityType } from '@prisma/client'; import { Category, Entity, EntityType } from '@prisma/client';
import { Scope, ScopeType } from '@/lib/types/scope'; import { Scope, ScopeType } from '@/lib/types/scope';
import { prismaClient } from '@/prisma'; import prisma from '@/prisma';
import { getUser } from '@/auth'; import { getUser } from '@/auth';
import DashboardPageClient from '@/components/dashboardPageClientComponents'; 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); const scope = Scope.of(props.searchParams?.scope || ScopeType.ThisMonth);
// get all payments in the current scope // get all payments in the current scope
const payments = await prismaClient.payment.findMany({ const payments = await prisma.payment.findMany({
where: { where: {
userId: user?.id, userId: user?.id,
date: { date: {
@ -108,6 +108,7 @@ export default async function DashboardPage(props: { searchParams?: { scope: Sco
userId: '', userId: '',
name: 'Other', name: 'Other',
type: EntityType.Entity, type: EntityType.Entity,
defaultCategoryId: null,
createdAt: new Date(), createdAt: new Date(),
updatedAt: new Date(), updatedAt: new Date(),
}, },

View file

@ -1,5 +1,5 @@
import { getUser } from '@/auth'; import { getUser } from '@/auth';
import { prismaClient } from '@/prisma'; import prisma from '@/prisma';
import React from 'react'; import React from 'react';
import PaymentPageClientContent from '@/components/paymentPageClientComponents'; import PaymentPageClientContent from '@/components/paymentPageClientComponents';
import paymentCreateUpdate from '@/lib/actions/paymentCreateUpdate'; import paymentCreateUpdate from '@/lib/actions/paymentCreateUpdate';
@ -9,7 +9,7 @@ export default async function PaymentsPage() {
const user = await getUser(); const user = await getUser();
const payments = await prismaClient.payment.findMany({ const payments = await prisma.payment.findMany({
where: { where: {
userId: user?.id, 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: { where: {
userId: user?.id, 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: { where: {
userId: user?.id, userId: user?.id,
}, },

View file

@ -1,9 +1,9 @@
import { Lucia } from 'lucia'; import { Lucia } from 'lucia';
import { PrismaAdapter } from '@lucia-auth/adapter-prisma'; import { PrismaAdapter } from '@lucia-auth/adapter-prisma';
import { cookies } from 'next/headers'; 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, { export const lucia = new Lucia(adapter, {
sessionCookie: { sessionCookie: {

View file

@ -1,7 +1,7 @@
import { ActionResponse } from '@/lib/types/actionResponse'; import { ActionResponse } from '@/lib/types/actionResponse';
import { URL_SIGN_IN } from '@/lib/constants'; import { URL_SIGN_IN } from '@/lib/constants';
import { getUser, lucia } from '@/auth'; import { getUser, lucia } from '@/auth';
import { prismaClient } from '@/prisma'; import prisma from '@/prisma';
import { cookies } from 'next/headers'; import { cookies } from 'next/headers';
export default async function accountDelete(): Promise<ActionResponse> { export default async function accountDelete(): Promise<ActionResponse> {
@ -17,31 +17,31 @@ export default async function accountDelete(): Promise<ActionResponse> {
}; };
} }
await prismaClient.payment.deleteMany({ await prisma.payment.deleteMany({
where: { where: {
userId: user.id, userId: user.id,
}, },
}); });
await prismaClient.entity.deleteMany({ await prisma.entity.deleteMany({
where: { where: {
userId: user.id, userId: user.id,
}, },
}); });
await prismaClient.category.deleteMany({ await prisma.category.deleteMany({
where: { where: {
userId: user.id, userId: user.id,
}, },
}); });
await prismaClient.session.deleteMany({ await prisma.session.deleteMany({
where: { where: {
userId: user.id, userId: user.id,
}, },
}); });
await prismaClient.user.delete({ await prisma.user.delete({
where: { where: {
id: user.id, id: user.id,
}, },

View file

@ -1,6 +1,6 @@
import { z } from 'zod'; import { z } from 'zod';
import { ActionResponse } from '@/lib/types/actionResponse'; import { ActionResponse } from '@/lib/types/actionResponse';
import { prismaClient } from '@/prisma'; import prisma from '@/prisma';
import { getUser } from '@/auth'; import { getUser } from '@/auth';
import { URL_SIGN_IN } from '@/lib/constants'; import { URL_SIGN_IN } from '@/lib/constants';
import { categoryFormSchema } from '@/lib/form-schemas/categoryFormSchema'; import { categoryFormSchema } from '@/lib/form-schemas/categoryFormSchema';
@ -25,7 +25,7 @@ export default async function categoryCreateUpdate({
// create/update category // create/update category
try { try {
if (id) { if (id) {
await prismaClient.category.update({ await prisma.category.update({
where: { where: {
id: id, id: id,
}, },
@ -42,7 +42,7 @@ export default async function categoryCreateUpdate({
message: `'${name}' updated`, message: `'${name}' updated`,
}; };
} else { } else {
await prismaClient.category.create({ await prisma.category.create({
data: { data: {
userId: user.id, userId: user.id,
name: name, name: name,

View file

@ -1,5 +1,5 @@
import { ActionResponse } from '@/lib/types/actionResponse'; import { ActionResponse } from '@/lib/types/actionResponse';
import { prismaClient } from '@/prisma'; import prisma from '@/prisma';
import { getUser } from '@/auth'; import { getUser } from '@/auth';
import { URL_SIGN_IN } from '@/lib/constants'; import { URL_SIGN_IN } from '@/lib/constants';
@ -25,7 +25,7 @@ export default async function categoryDelete(id: number): Promise<ActionResponse
} }
// check that category is associated with user // check that category is associated with user
const category = await prismaClient.category.findFirst({ const category = await prisma.category.findFirst({
where: { where: {
id: id, id: id,
userId: user.id, userId: user.id,
@ -40,7 +40,7 @@ export default async function categoryDelete(id: number): Promise<ActionResponse
// delete category // delete category
try { try {
await prismaClient.category.delete({ await prisma.category.delete({
where: { where: {
id: category.id, id: category.id,
userId: user.id, userId: user.id,

View file

@ -1,7 +1,7 @@
import { z } from 'zod'; import { z } from 'zod';
import { ActionResponse } from '@/lib/types/actionResponse'; import { ActionResponse } from '@/lib/types/actionResponse';
import { entityFormSchema } from '@/lib/form-schemas/entityFormSchema'; import { entityFormSchema } from '@/lib/form-schemas/entityFormSchema';
import { prismaClient } from '@/prisma'; import prisma from '@/prisma';
import { getUser } from '@/auth'; import { getUser } from '@/auth';
import { URL_SIGN_IN } from '@/lib/constants'; import { URL_SIGN_IN } from '@/lib/constants';
@ -26,7 +26,7 @@ export default async function entityCreateUpdate({
// create/update entity // create/update entity
try { try {
if (id) { if (id) {
await prismaClient.entity.update({ await prisma.entity.update({
where: { where: {
id: id, id: id,
}, },
@ -44,7 +44,7 @@ export default async function entityCreateUpdate({
message: `${type} '${name}' updated`, message: `${type} '${name}' updated`,
}; };
} else { } else {
await prismaClient.entity.create({ await prisma.entity.create({
data: { data: {
userId: user.id, userId: user.id,
name: name, name: name,

View file

@ -1,5 +1,5 @@
import { ActionResponse } from '@/lib/types/actionResponse'; import { ActionResponse } from '@/lib/types/actionResponse';
import { prismaClient } from '@/prisma'; import prisma from '@/prisma';
import { getUser } from '@/auth'; import { getUser } from '@/auth';
import { URL_SIGN_IN } from '@/lib/constants'; import { URL_SIGN_IN } from '@/lib/constants';
@ -25,7 +25,7 @@ export default async function entityDelete(id: number): Promise<ActionResponse>
} }
// check that entity is associated with user // check that entity is associated with user
const entity = await prismaClient.entity.findFirst({ const entity = await prisma.entity.findFirst({
where: { where: {
id: id, id: id,
userId: user.id, userId: user.id,
@ -40,7 +40,7 @@ export default async function entityDelete(id: number): Promise<ActionResponse>
// delete entity // delete entity
try { try {
await prismaClient.entity.delete({ await prisma.entity.delete({
where: { where: {
id: entity.id, id: entity.id,
userId: user.id, userId: user.id,

View file

@ -1,4 +1,4 @@
import { prismaClient } from '@/prisma'; import prisma from '@/prisma';
import type { Category, Entity } from '@prisma/client'; import type { Category, Entity } from '@prisma/client';
import { EntityType } from '@prisma/client'; import { EntityType } from '@prisma/client';
import { getUser } from '@/auth'; import { getUser } from '@/auth';
@ -19,12 +19,12 @@ export default async function generateSampleData(): Promise<ActionResponse> {
} }
// Categories: create sample data // Categories: create sample data
const categories: Category[] = await prismaClient.category.findMany({where: {userId: user.id}}); const categories: Category[] = await prisma.category.findMany({where: {userId: user.id}});
if (await prismaClient.category.count({where: {userId: user.id}}) == 0) { if (await prisma.category.count({where: {userId: user.id}}) == 0) {
console.log('Creating sample categories...'); console.log('Creating sample categories...');
categories.push(await prismaClient.category.create({ categories.push(await prisma.category.create({
data: { data: {
userId: user.id, userId: user.id,
name: 'Groceries', name: 'Groceries',
@ -32,7 +32,7 @@ export default async function generateSampleData(): Promise<ActionResponse> {
}, },
})); }));
categories.push(await prismaClient.category.create({ categories.push(await prisma.category.create({
data: { data: {
userId: user.id, userId: user.id,
name: 'Drugstore items', name: 'Drugstore items',
@ -40,7 +40,7 @@ export default async function generateSampleData(): Promise<ActionResponse> {
}, },
})); }));
categories.push(await prismaClient.category.create({ categories.push(await prisma.category.create({
data: { data: {
userId: user.id, userId: user.id,
name: 'Going out', name: 'Going out',
@ -48,7 +48,7 @@ export default async function generateSampleData(): Promise<ActionResponse> {
}, },
})); }));
categories.push(await prismaClient.category.create({ categories.push(await prisma.category.create({
data: { data: {
userId: user.id, userId: user.id,
name: 'Random stuff', name: 'Random stuff',
@ -56,7 +56,7 @@ export default async function generateSampleData(): Promise<ActionResponse> {
}, },
})); }));
categories.push(await prismaClient.category.create({ categories.push(await prisma.category.create({
data: { data: {
userId: user.id, userId: user.id,
name: 'Salary', name: 'Salary',
@ -69,12 +69,12 @@ export default async function generateSampleData(): Promise<ActionResponse> {
console.log(categories); console.log(categories);
// Entities: create sample data // Entities: create sample data
const entities: Entity[] = await prismaClient.entity.findMany({where: {userId: user.id}}); const entities: Entity[] = await prisma.entity.findMany({where: {userId: user.id}});
if (await prismaClient.entity.count({where: {userId: user.id}}) == 0) { if (await prisma.entity.count({where: {userId: user.id}}) == 0) {
console.log('Creating sample entities...'); console.log('Creating sample entities...');
entities.push(await prismaClient.entity.create({ entities.push(await prisma.entity.create({
data: { data: {
userId: user.id, userId: user.id,
name: 'Main Account', name: 'Main Account',
@ -82,7 +82,7 @@ export default async function generateSampleData(): Promise<ActionResponse> {
}, },
})); }));
entities.push(await prismaClient.entity.create({ entities.push(await prisma.entity.create({
data: { data: {
userId: user.id, userId: user.id,
name: 'Company', name: 'Company',
@ -90,7 +90,7 @@ export default async function generateSampleData(): Promise<ActionResponse> {
}, },
})); }));
entities.push(await prismaClient.entity.create({ entities.push(await prisma.entity.create({
data: { data: {
userId: user.id, userId: user.id,
name: 'Supermarket 1', name: 'Supermarket 1',
@ -98,7 +98,7 @@ export default async function generateSampleData(): Promise<ActionResponse> {
}, },
})); }));
entities.push(await prismaClient.entity.create({ entities.push(await prisma.entity.create({
data: { data: {
userId: user.id, userId: user.id,
name: 'Supermarket 2', name: 'Supermarket 2',
@ -106,7 +106,7 @@ export default async function generateSampleData(): Promise<ActionResponse> {
}, },
})); }));
entities.push(await prismaClient.entity.create({ entities.push(await prisma.entity.create({
data: { data: {
userId: user.id, userId: user.id,
name: 'Supermarket 3', name: 'Supermarket 3',
@ -114,7 +114,7 @@ export default async function generateSampleData(): Promise<ActionResponse> {
}, },
})); }));
entities.push(await prismaClient.entity.create({ entities.push(await prisma.entity.create({
data: { data: {
userId: user.id, userId: user.id,
name: 'Supermarket 4', name: 'Supermarket 4',
@ -129,14 +129,14 @@ export default async function generateSampleData(): Promise<ActionResponse> {
// Payments: create sample data // Payments: create sample data
console.log('Creating sample payments...'); 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++) { for (let i = 0; i < 4; i++) {
const date = new Date(); const date = new Date();
date.setDate(1); date.setDate(1);
date.setMonth(date.getMonth() - i); date.setMonth(date.getMonth() - i);
await prismaClient.payment.create({ await prisma.payment.create({
data: { data: {
userId: user.id, userId: user.id,
amount: 200000, amount: 200000,
@ -164,7 +164,7 @@ export default async function generateSampleData(): Promise<ActionResponse> {
const date = new Date( const date = new Date(
new Date().getTime() - Math.floor(Math.random() * 10000000000)); new Date().getTime() - Math.floor(Math.random() * 10000000000));
await prismaClient.payment.create({ await prisma.payment.create({
data: { data: {
userId: user.id, userId: user.id,
amount: Math.floor( amount: Math.floor(

View file

@ -1,6 +1,6 @@
import { z } from 'zod'; import { z } from 'zod';
import { ActionResponse } from '@/lib/types/actionResponse'; import { ActionResponse } from '@/lib/types/actionResponse';
import { prismaClient } from '@/prisma'; import prisma from '@/prisma';
import { getUser } from '@/auth'; import { getUser } from '@/auth';
import { URL_SIGN_IN } from '@/lib/constants'; import { URL_SIGN_IN } from '@/lib/constants';
import { paymentFormSchema } from '@/lib/form-schemas/paymentFormSchema'; import { paymentFormSchema } from '@/lib/form-schemas/paymentFormSchema';
@ -29,7 +29,7 @@ export default async function paymentCreateUpdate({
// create/update payment // create/update payment
try { try {
if (id) { if (id) {
await prismaClient.payment.update({ await prisma.payment.update({
where: { where: {
id: id, id: id,
}, },
@ -50,7 +50,7 @@ export default async function paymentCreateUpdate({
message: `Payment updated`, message: `Payment updated`,
}; };
} else { } else {
await prismaClient.payment.create({ await prisma.payment.create({
data: { data: {
userId: user.id, userId: user.id,
amount: amount, amount: amount,

View file

@ -1,5 +1,5 @@
import { ActionResponse } from '@/lib/types/actionResponse'; import { ActionResponse } from '@/lib/types/actionResponse';
import { prismaClient } from '@/prisma'; import prisma from '@/prisma';
import { getUser } from '@/auth'; import { getUser } from '@/auth';
import { URL_SIGN_IN } from '@/lib/constants'; import { URL_SIGN_IN } from '@/lib/constants';
@ -25,7 +25,7 @@ export default async function paymentDelete(id: number): Promise<ActionResponse>
} }
// check that payment is associated with user // check that payment is associated with user
const payment = await prismaClient.payment.findFirst({ const payment = await prisma.payment.findFirst({
where: { where: {
id: id, id: id,
userId: user.id, userId: user.id,
@ -40,7 +40,7 @@ export default async function paymentDelete(id: number): Promise<ActionResponse>
// delete payment // delete payment
try { try {
await prismaClient.payment.delete({ await prisma.payment.delete({
where: { where: {
id: payment.id, id: payment.id,
userId: user.id, userId: user.id,

View file

@ -5,12 +5,12 @@ import { cookies } from 'next/headers';
import { signInFormSchema } from '@/lib/form-schemas/signInFormSchema'; import { signInFormSchema } from '@/lib/form-schemas/signInFormSchema';
import { ActionResponse } from '@/lib/types/actionResponse'; import { ActionResponse } from '@/lib/types/actionResponse';
import { URL_HOME } from '@/lib/constants'; import { URL_HOME } from '@/lib/constants';
import { prismaClient } from '@/prisma'; import prisma from '@/prisma';
export default async function signIn({username, password}: z.infer<typeof signInFormSchema>): Promise<ActionResponse> { export default async function signIn({username, password}: z.infer<typeof signInFormSchema>): Promise<ActionResponse> {
'use server'; 'use server';
const existingUser = await prismaClient.user.findFirst({ const existingUser = await prisma.user.findFirst({
where: { where: {
username: username.toLowerCase(), username: username.toLowerCase(),
}, },

View file

@ -6,7 +6,7 @@ import { cookies } from 'next/headers';
import { signUpFormSchema } from '@/lib/form-schemas/signUpFormSchema'; import { signUpFormSchema } from '@/lib/form-schemas/signUpFormSchema';
import { ActionResponse } from '@/lib/types/actionResponse'; import { ActionResponse } from '@/lib/types/actionResponse';
import { URL_HOME } from '@/lib/constants'; import { URL_HOME } from '@/lib/constants';
import { prismaClient } from '@/prisma'; import prisma from '@/prisma';
export default async function signUp({username, password}: z.infer<typeof signUpFormSchema>): Promise<ActionResponse> { export default async function signUp({username, password}: z.infer<typeof signUpFormSchema>): Promise<ActionResponse> {
'use server'; 'use server';
@ -14,7 +14,7 @@ export default async function signUp({username, password}: z.infer<typeof signUp
const hashedPassword = await new Argon2id().hash(password); const hashedPassword = await new Argon2id().hash(password);
const userId = generateId(15); const userId = generateId(15);
const existingUser = await prismaClient.user.findFirst({ const existingUser = await prisma.user.findFirst({
where: { where: {
username: username.toLowerCase(), username: username.toLowerCase(),
}, },
@ -27,7 +27,7 @@ export default async function signUp({username, password}: z.infer<typeof signUp
}; };
} }
await prismaClient.user.create({ await prisma.user.create({
data: { data: {
id: userId, id: userId,
username: username, username: username,

View file

@ -1,3 +1,18 @@
import { PrismaClient } from '@prisma/client'; import { PrismaClient } from '@prisma/client';
export const prismaClient = new PrismaClient(); const prismaClientSingleton = () => {
return new PrismaClient();
};
declare global {
// noinspection ES6ConvertVarToLetConst
var prismaGlobal: undefined | ReturnType<typeof prismaClientSingleton>;
}
const prisma = globalThis.prismaGlobal ?? prismaClientSingleton();
export default prisma;
if (process.env.NODE_ENV !== 'production') {
globalThis.prismaGlobal = prisma
}