Skip to content

Commit

Permalink
N-FIN-61: fix prisma client instantiation (#62)
Browse files Browse the repository at this point in the history
Fixes #61
  • Loading branch information
MarkusThielker authored Mar 24, 2024
2 parents 7731d01 + 00b4e51 commit 51902d7
Show file tree
Hide file tree
Showing 17 changed files with 82 additions and 66 deletions.
8 changes: 4 additions & 4 deletions src/app/account/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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,
},
Expand Down
4 changes: 2 additions & 2 deletions src/app/categories/page.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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,
},
Expand Down
6 changes: 3 additions & 3 deletions src/app/entities/page.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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,
},
Expand All @@ -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,
},
Expand Down
5 changes: 3 additions & 2 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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: {
Expand Down Expand Up @@ -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(),
},
Expand Down
8 changes: 4 additions & 4 deletions src/app/payments/page.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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,
},
Expand All @@ -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,
},
Expand All @@ -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,
},
Expand Down
4 changes: 2 additions & 2 deletions src/auth.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down
12 changes: 6 additions & 6 deletions src/lib/actions/accountDelete.ts
Original file line number Diff line number Diff line change
@@ -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<ActionResponse> {
Expand All @@ -17,31 +17,31 @@ export default async function accountDelete(): Promise<ActionResponse> {
};
}

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,
},
Expand Down
6 changes: 3 additions & 3 deletions src/lib/actions/categoryCreateUpdate.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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,
},
Expand All @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions src/lib/actions/categoryDelete.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -25,7 +25,7 @@ export default async function categoryDelete(id: number): Promise<ActionResponse
}

// check that category is associated with user
const category = await prismaClient.category.findFirst({
const category = await prisma.category.findFirst({
where: {
id: id,
userId: user.id,
Expand All @@ -40,7 +40,7 @@ export default async function categoryDelete(id: number): Promise<ActionResponse

// delete category
try {
await prismaClient.category.delete({
await prisma.category.delete({
where: {
id: category.id,
userId: user.id,
Expand Down
6 changes: 3 additions & 3 deletions src/lib/actions/entityCreateUpdate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { z } from 'zod';
import { ActionResponse } from '@/lib/types/actionResponse';
import { entityFormSchema } from '@/lib/form-schemas/entityFormSchema';
import { prismaClient } from '@/prisma';
import prisma from '@/prisma';
import { getUser } from '@/auth';
import { URL_SIGN_IN } from '@/lib/constants';

Expand All @@ -26,7 +26,7 @@ export default async function entityCreateUpdate({
// create/update entity
try {
if (id) {
await prismaClient.entity.update({
await prisma.entity.update({
where: {
id: id,
},
Expand All @@ -44,7 +44,7 @@ export default async function entityCreateUpdate({
message: `${type} '${name}' updated`,
};
} else {
await prismaClient.entity.create({
await prisma.entity.create({
data: {
userId: user.id,
name: name,
Expand Down
6 changes: 3 additions & 3 deletions src/lib/actions/entityDelete.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -25,7 +25,7 @@ export default async function entityDelete(id: number): Promise<ActionResponse>
}

// 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,
Expand All @@ -40,7 +40,7 @@ export default async function entityDelete(id: number): Promise<ActionResponse>

// delete entity
try {
await prismaClient.entity.delete({
await prisma.entity.delete({
where: {
id: entity.id,
userId: user.id,
Expand Down
Loading

0 comments on commit 51902d7

Please sign in to comment.