correction prisma v2

This commit is contained in:
2026-03-02 11:16:46 +01:00
parent 6016169bfd
commit 90f5431c3d
17 changed files with 213 additions and 61 deletions

View File

@@ -2,7 +2,8 @@ export const dynamic = 'force-dynamic';
import { NextRequest, NextResponse } from 'next/server';
import { auth } from '@/lib/auth';
import getDB from '@/lib/prisma';
// 1. On remplace l'import de getDB par l'objet prisma direct
import { prisma } from '@/lib/prisma';
// PUT /api/chapters/[id] — Update a chapter
export async function PUT(
@@ -17,8 +18,8 @@ export async function PUT(
const { id } = await params;
const body = await request.json();
// Verify ownership via project
const chapter = await getDB().chapter.findUnique({
// 2. On utilise 'prisma' au lieu de 'getDB()'
const chapter = await prisma.chapter.findUnique({
where: { id },
include: { project: { select: { userId: true } } },
});
@@ -26,7 +27,7 @@ export async function PUT(
return NextResponse.json({ error: 'Non trouvé' }, { status: 404 });
}
const updated = await getDB().chapter.update({
const updated = await prisma.chapter.update({
where: { id },
data: {
...(body.title !== undefined && { title: body.title }),
@@ -51,7 +52,8 @@ export async function DELETE(
const { id } = await params;
const chapter = await getDB().chapter.findUnique({
// 3. On utilise 'prisma' au lieu de 'getDB()'
const chapter = await prisma.chapter.findUnique({
where: { id },
include: { project: { select: { userId: true } } },
});
@@ -59,7 +61,7 @@ export async function DELETE(
return NextResponse.json({ error: 'Non trouvé' }, { status: 404 });
}
await getDB().chapter.delete({ where: { id } });
await prisma.chapter.delete({ where: { id } });
return NextResponse.json({ success: true });
}
}

View File

@@ -2,7 +2,8 @@ export const dynamic = 'force-dynamic';
import { NextRequest, NextResponse } from 'next/server';
import { auth } from '@/lib/auth';
import getDB from '@/lib/prisma';
// 1. On importe l'objet prisma au lieu de la fonction getDB
import { prisma } from '@/lib/prisma';
// POST /api/chapters — Create a chapter
export async function POST(request: NextRequest) {
@@ -13,15 +14,17 @@ export async function POST(request: NextRequest) {
const body = await request.json();
// Verify project ownership
const project = await getDB().project.findFirst({
// 2. On utilise 'prisma' directement au lieu de 'getDB()'
const project = await prisma.project.findFirst({
where: { id: body.projectId, userId: session.user.id },
});
if (!project) {
return NextResponse.json({ error: 'Projet non trouvé' }, { status: 404 });
}
const chapter = await getDB().chapter.create({
// 3. Pareil ici pour la création
const chapter = await prisma.chapter.create({
data: {
title: body.title || 'Nouveau Chapitre',
content: body.content || '',
@@ -32,4 +35,4 @@ export async function POST(request: NextRequest) {
});
return NextResponse.json(chapter, { status: 201 });
}
}

View File

@@ -2,7 +2,7 @@ export const dynamic = 'force-dynamic';
import { NextRequest, NextResponse } from 'next/server';
import { auth } from '@/lib/auth';
import getDB from '@/lib/prisma';
import { prisma } from '@/lib/prisma';
// PUT /api/entities/[id]
export async function PUT(
@@ -17,7 +17,7 @@ export async function PUT(
const { id } = await params;
const body = await request.json();
const entity = await getDB().entity.findUnique({
const entity = await prisma.entity.findUnique({
where: { id },
include: { project: { select: { userId: true } } },
});
@@ -25,7 +25,7 @@ export async function PUT(
return NextResponse.json({ error: 'Non trouvé' }, { status: 404 });
}
const updated = await getDB().entity.update({
const updated = await prisma.entity.update({
where: { id },
data: {
...(body.name !== undefined && { name: body.name }),
@@ -53,7 +53,7 @@ export async function DELETE(
const { id } = await params;
const entity = await getDB().entity.findUnique({
const entity = await prisma.entity.findUnique({
where: { id },
include: { project: { select: { userId: true } } },
});
@@ -61,7 +61,7 @@ export async function DELETE(
return NextResponse.json({ error: 'Non trouvé' }, { status: 404 });
}
await getDB().entity.delete({ where: { id } });
await prisma.entity.delete({ where: { id } });
return NextResponse.json({ success: true });
}

View File

@@ -2,7 +2,7 @@ export const dynamic = 'force-dynamic';
import { NextRequest, NextResponse } from 'next/server';
import { auth } from '@/lib/auth';
import getDB from '@/lib/prisma';
import { prisma } from '@/lib/prisma';
// POST /api/entities — Create an entity
export async function POST(request: NextRequest) {
@@ -13,14 +13,14 @@ export async function POST(request: NextRequest) {
const body = await request.json();
const project = await getDB().project.findFirst({
const project = await prisma.project.findFirst({
where: { id: body.projectId, userId: session.user.id },
});
if (!project) {
return NextResponse.json({ error: 'Projet non trouvé' }, { status: 404 });
}
const entity = await getDB().entity.create({
const entity = await prisma.entity.create({
data: {
type: body.type,
name: body.name || 'Nouvelle entité',

View File

@@ -2,7 +2,7 @@ export const dynamic = 'force-dynamic';
import { NextRequest, NextResponse } from 'next/server';
import { auth } from '@/lib/auth';
import getDB from '@/lib/prisma';
import { prisma } from '@/lib/prisma';
// PUT /api/ideas/[id]
export async function PUT(
@@ -17,7 +17,7 @@ export async function PUT(
const { id } = await params;
const body = await request.json();
const idea = await getDB().idea.findUnique({
const idea = await prisma.idea.findUnique({
where: { id },
include: { project: { select: { userId: true } } },
});
@@ -25,7 +25,7 @@ export async function PUT(
return NextResponse.json({ error: 'Non trouvé' }, { status: 404 });
}
const updated = await getDB().idea.update({
const updated = await prisma.idea.update({
where: { id },
data: {
...(body.title !== undefined && { title: body.title }),
@@ -50,7 +50,7 @@ export async function DELETE(
const { id } = await params;
const idea = await getDB().idea.findUnique({
const idea = await prisma.idea.findUnique({
where: { id },
include: { project: { select: { userId: true } } },
});
@@ -58,7 +58,7 @@ export async function DELETE(
return NextResponse.json({ error: 'Non trouvé' }, { status: 404 });
}
await getDB().idea.delete({ where: { id } });
await prisma.idea.delete({ where: { id } });
return NextResponse.json({ success: true });
}

View File

@@ -2,7 +2,7 @@ export const dynamic = 'force-dynamic';
import { NextRequest, NextResponse } from 'next/server';
import { auth } from '@/lib/auth';
import getDB from '@/lib/prisma';
import { prisma } from '@/lib/prisma';
// POST /api/ideas
export async function POST(request: NextRequest) {
@@ -13,14 +13,14 @@ export async function POST(request: NextRequest) {
const body = await request.json();
const project = await getDB().project.findFirst({
const project = await prisma.project.findFirst({
where: { id: body.projectId, userId: session.user.id },
});
if (!project) {
return NextResponse.json({ error: 'Projet non trouvé' }, { status: 404 });
}
const idea = await getDB().idea.create({
const idea = await prisma.idea.create({
data: {
title: body.title || 'Nouvelle idée',
description: body.description || '',

View File

@@ -1,11 +1,11 @@
import { NextResponse } from 'next/server';
import getDB from '@/lib/prisma';
import { prisma } from '@/lib/prisma';
export const dynamic = 'force-dynamic';
export async function GET() {
try {
const prisma = getDB();
//const prisma = getDB();
const plans = await prisma.plan.findMany({
orderBy: { price: 'asc' }
});

View File

@@ -2,7 +2,7 @@ export const dynamic = 'force-dynamic';
import { NextRequest, NextResponse } from 'next/server';
import { auth } from '@/lib/auth';
import getDB from '@/lib/prisma';
import { prisma } from '@/lib/prisma';
// GET /api/projects/[id] — Get project with all related data
export async function GET(
@@ -16,7 +16,7 @@ export async function GET(
const { id } = await params;
const project = await getDB().project.findFirst({
const project = await prisma.project.findFirst({
where: { id, userId: session.user.id },
include: {
chapters: { orderBy: { orderIndex: 'asc' } },
@@ -48,14 +48,14 @@ export async function PUT(
const body = await request.json();
// Verify ownership
const existing = await getDB().project.findFirst({
const existing = await prisma.project.findFirst({
where: { id, userId: session.user.id },
});
if (!existing) {
return NextResponse.json({ error: 'Projet non trouvé' }, { status: 404 });
}
const project = await getDB().project.update({
const project = await prisma.project.update({
where: { id },
data: {
...(body.title !== undefined && { title: body.title }),
@@ -81,14 +81,14 @@ export async function DELETE(
const { id } = await params;
// Verify ownership
const existing = await getDB().project.findFirst({
const existing = await prisma.project.findFirst({
where: { id, userId: session.user.id },
});
if (!existing) {
return NextResponse.json({ error: 'Projet non trouvé' }, { status: 404 });
}
await getDB().project.delete({ where: { id } });
await prisma.project.delete({ where: { id } });
return NextResponse.json({ success: true });
}

View File

@@ -1,8 +1,9 @@
export const dynamic = 'force-dynamic';
import type { Prisma } from '@prisma/client';
import { NextRequest, NextResponse } from 'next/server';
import { auth } from '@/lib/auth';
import getDB from '@/lib/prisma';
import { prisma } from '@/lib/prisma';
// PUT /api/projects/[id]/workflow — Sync workflow (nodes + connections)
export async function PUT(
@@ -15,7 +16,7 @@ export async function PUT(
}
const { id } = await params;
const prisma = getDB();
//const prisma = getDB();
// Verify ownership
const project = await prisma.project.findFirst({
@@ -28,7 +29,7 @@ export async function PUT(
const { nodes, connections } = await request.json();
// Replace all nodes and connections in a transaction
await prisma.$transaction(async (tx) => {
await prisma.$transaction(async (tx: Prisma.TransactionClient) => {
// Delete existing
await tx.plotConnection.deleteMany({ where: { projectId: id } });
await tx.plotNode.deleteMany({ where: { projectId: id } });

View File

@@ -2,7 +2,7 @@ export const dynamic = 'force-dynamic';
import { NextRequest, NextResponse } from 'next/server';
import { auth } from '@/lib/auth';
import getDB from '@/lib/prisma';
import { prisma } from '@/lib/prisma';
// GET /api/projects — List all user's projects
export async function GET() {
@@ -11,7 +11,7 @@ export async function GET() {
return NextResponse.json({ error: 'Non autorisé' }, { status: 401 });
}
const projects = await getDB().project.findMany({
const projects = await prisma.project.findMany({
where: { userId: session.user.id },
orderBy: { updatedAt: 'desc' },
include: {
@@ -32,7 +32,6 @@ export async function POST(request: NextRequest) {
}
// Check plan limits
const prisma = getDB();
const user = await prisma.user.findUnique({
where: { id: session.user.id },
include: { subscriptionPlan: true }

View File

@@ -2,7 +2,7 @@ export const dynamic = 'force-dynamic';
import { NextRequest, NextResponse } from 'next/server';
import { auth } from '@/lib/auth';
import getDB from '@/lib/prisma';
import { prisma } from '@/lib/prisma';
// GET /api/user/profile — Get current user profile with stats
export async function GET() {
@@ -11,7 +11,7 @@ export async function GET() {
return NextResponse.json({ error: 'Non autorisé' }, { status: 401 });
}
const prisma = getDB();
//const prisma = getDB();
const user = await prisma.user.findUnique({
where: { id: session.user.id },
include: { subscriptionPlan: true }
@@ -27,7 +27,7 @@ export async function GET() {
select: { content: true },
});
const totalWords = chapters.reduce((total, chapter) => {
const totalWords = chapters.reduce((total: number, chapter: { content: string | null }) => {
const text = (chapter.content || '').replace(/<[^>]*>/g, ' ').trim();
return total + (text ? text.split(/\s+/).length : 0);
}, 0);
@@ -68,7 +68,7 @@ export async function PUT(request: NextRequest) {
return NextResponse.json({ error: 'Non autorisé' }, { status: 401 });
}
const prisma = getDB();
//const prisma = getDB();
const body = await request.json();
const data: Record<string, unknown> = {};

View File

@@ -11,28 +11,40 @@ const globalForPrisma = globalThis as unknown as {
* Uses @prisma/adapter-pg with a pg Pool for direct PostgreSQL connections.
*//*
export function getDB(): PrismaClient {
if (!globalForPrisma.prisma) {
const connectionString = process.env.DATABASE_URL;
const pool = new Pool({ connectionString });
const adapter = new PrismaPg(pool);
if (!globalForPrisma.prisma) {
const connectionString = process.env.DATABASE_URL;
const pool = new Pool({ connectionString });
const adapter = new PrismaPg(pool);
globalForPrisma.prisma = new PrismaClient({ adapter });
}
return globalForPrisma.prisma;
globalForPrisma.prisma = new PrismaClient({ adapter });
}
return globalForPrisma.prisma;
}
export default getDB;
*/
import type { PrismaClient as PrismaClientType } from '@prisma/client';
const { PrismaClient } = require('@prisma/client');
import { PrismaClient } from '@prisma/client';
const globalForPrisma = globalThis as unknown as {
prisma: PrismaClientType | undefined;
prisma: PrismaClient | undefined;
};
export const prisma = globalForPrisma.prisma ?? new PrismaClient();
if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma;
export function getDB(): PrismaClient {
if (!globalForPrisma.prisma) {
globalForPrisma.prisma = new PrismaClient();
}
return globalForPrisma.prisma;
}
if (process.env.NODE_ENV !== 'production') {
globalForPrisma.prisma = getDB();
}
export const prisma = new Proxy({} as any, {
get(target, prop, receiver) {
return Reflect.get(getDB(), prop, receiver);
}
}) as PrismaClient;
export function getDB() { return prisma; }
export default getDB;