ã¯ããã«
ååã Prisma ã§æ¤ç´¢å¦çã®ãµã³ãã«ãæ¸ããã¦ããã ãã¾ããããä»åã¯åé¤å¦çã®ãµã³ãã«ã«ãªãã¾ãã
Â
DBå®ç¾©åæ²
schema.prisma
schema.prisma
generator client { provider = "prisma-client-js" } datasource db { provider = "postgresql" url = env("DATABASE_URL") } model user { id Int @id @default(autoincrement()) nickname String @db.VarChar(255) created_at DateTime @default(now()) habbits habbit[] } model habbit { id Int @id @default(autoincrement()) user_id Int title String @db.VarChar(255) created_at DateTime @default(now()) updated_at DateTime? deleted_at DateTime? user user @relation(fields: [user_id], references: [id]) habbit_activities habbit_activity[] } model habbit_activity { id Int @id @default(autoincrement()) habbit_id Int checked Boolean created_at DateTime @default(now()) habbit habbit @relation(fields: [habbit_id], references: [id]) }
Â
Prismaã§ã¬ã³ã¼ãåé¤å¦ç
æ©éãåé¤å¦çãå®è£ ãã¦ããã¾ãã
Â
ï¼ã¬ã³ã¼ãåé¤
habbit.ts
export async function deleteHabbitActivity(id: number) { return await prisma.habbit_activity.delete({ where: { id, }, }); }
- SQL ã® ãWHERE æ¡ä»¶=Aã ã®ããã«ãåé¤ãããã¬ã³ã¼ãã®å¯¾è±¡ã®ã«ã©ã ã¨å¤ã where ãã©ã¡ã¼ã¿ã¼ã«æå®ããå½¢ã§è¨è¼ã§ãã¾ã
Â
è¤æ°ã¬ã³ã¼ãåé¤
habbit.ts
export async function deleteHabbitActivities(ids: number[]) { return await prisma.habbit_activity.deleteMany({ where: { id: { in: ids, }, }, }); }
- è¤æ°ã¬ã³ã¼ããåé¤ããå ´åã¯ãdeleteMany ã¡ã½ããã使ç¨ãã¾ã
- ã«ã©ã ã®ãã©ã¡ã¼ã¿ã¼ã«ãã¹ãããå½¢ã§ããã«æ¡ä»¶ãæå®ãããã¨ãã§ãã¾ãï¼ä¸è¨ä¾ã§ã¯ IN å¥ã使ç¨ãã¦ãã¾ãï¼
- ä»ã«ã SQL ã® like å¥ã«ããã contains ãªã©æ§ã ãªãã©ã¡ã¼ã¿ã¼ã使ç¨ã§ããã®ã§ãä¸è¨ãåããã¦ãåç §ãã ãã
Â
Â
å ¨ã¬ã³ã¼ãåé¤
habbit.ts
export async function deleteAllHabbitActivity() { return await prisma.habbit_activity.deleteMany({}); }
- å ¨ã¬ã³ã¼ãåé¤ã®å ´åã¯ã·ã³ãã«ã«ãdeleteMany ã¡ã½ããã«å¯¾ãã¦ã{}ãã渡ããã¨ã§ç¡æ¡ä»¶ã«å ¨ã¦ã®ã¬ã³ã¼ããåé¤ããã¾ã
Â
ã«ã¹ã±ã¼ãããªã¼ã
habbit.ts
export async function deleteHabbit(id: number) { const deletedHabbitActivities = prisma.habbit_activity.deleteMany({ where: { habbit_id: id, }, }); const deletedHabbit = prisma.habbit.delete({ where: { id, }, }); return await prisma.$transaction([deletedHabbitActivities, deletedHabbit]); }
- ä»åã®ä¾ã ã¨ãhabbitãã¼ãã«ã¨habbit_activityãã¼ãã«ã1対å¤ã®é¢ä¿ã§ãhabbitã«habbit_activityãç´ã¥ããããªé¢ä¿æ§ã¨ãªã£ã¦ããã
- habbitãã¼ãã«ã®ã¬ã³ã¼ããåé¤ããããç´ã¥ãhabbit_activityãã¼ãã«ã®ã¬ã³ã¼ããã¾ã¨ãã¦åé¤ãã¾ã
- habbit ãã¼ãã«ã«å¯¾ãã¦ãPrisma Client ãã delete ã¡ã½ãããçºè¡ããã ãã§ã¯ã¨ã©ã¼ã«ãªãã®ã§ã以ä¸ã«ç´¹ä»ããã¦ããããã«ã
- habbit_activity, habbit ãã¼ãã«ããããã«å¯¾ãã delete ã¯ã¨ãªã¼å®è¡ãã¾ã¨ã㦠$transaction ã¡ã½ããã«æ¸¡ããã¨ã§ã«ã¹ã±ã¼ãããªã¼ããè¡ããããªå®è£ ã«ãªãã¾ã
Â
åä½ç¢ºèª
ä¸è¨ã®åé¤å¦çãåä½ç¢ºèªãã¾ãã
Â
æ¤è¨¼ã³ã¼ã
ä»åãNextJS ã® App Router ã®æ©è½ã使ã£ã¦ã
src/app/api/habbit_activity/route.ts, src/app/api/habbit/route.ts ã« API ãç¨æããAPI ãªã¯ã¨ã¹ãã§DBæ´æ°ãæ¤è¨¼ãã¾ãã
Â
src/app/api/habbit_activity/route.ts
export async function DELETE(request: NextRequest) { try { const jsonBody = await request.json(); const habbit_activity_ids = jsonBody.ids; if (!habbit_activity_ids) { return NextResponse.json({ message: "No IDs provided" }, { status: 400 }); } switch (habbit_activity_ids.length) { case 0: const deletedHabbitAllActivity = await deleteAllHabbitActivity(); return NextResponse.json( { message: "Success", data: deletedHabbitAllActivity }, { status: 200 } ); case 1: const deletedHabbitActivity = await deleteHabbitActivity( habbit_activity_ids[0] ); return NextResponse.json( { message: "Success", data: deletedHabbitActivity }, { status: 200 } ); default: const deletedHabbitActivities = await deleteHabbitActivities( habbit_activity_ids ); return NextResponse.json( { message: "Success", data: deletedHabbitActivities }, { status: 200 } ); } } catch (error) { console.error(`[Delete HabbitActivity]unexpected error: ${error}`); return NextResponse.json({ message: "Error" }, { status: 500 }); } }
Â
src/app/api/habbit/route.ts
export async function DELETE(request: NextRequest) { try { const jsonBody = await request.json(); // delete habbit const data = await deleteHabbit(jsonBody.id); return NextResponse.json({ message: "Success", data }, { status: 200 }); } catch (error) { console.error(`[Delete Habbit]unexpected error: ${error}`); return NextResponse.json({ message: "Error" }, { status: 500 }); } }
Â
ï¼ã¬ã³ã¼ãåé¤
å®è¡åãã¼ãã«ç¶æ
Â
curl ã³ãã³ãã§åé¤å®è¡
curl --location --request DELETE 'http://localhost:3000/api/habbit_activity' \ --header 'Content-Type: text/plain' \ --data '{ "ids": [2] }'
Â
å®è¡å¾ãã¼ãã«ç¶æ
- id:2 ã®ã¬ã³ã¼ããåé¤ããã¦ãããã¨ã確èªã§ãã¾ãã
Â
è¤æ°ã¬ã³ã¼ãåé¤
å®è¡åãã¼ãã«ç¶æ
Â
curl ã³ãã³ãã§åé¤å®è¡
curl --location --request DELETE 'http://localhost:3000/api/habbit_activity' \ --header 'Content-Type: text/plain' \ --data '{ "ids": [3,4] }'
Â
å®è¡å¾ãã¼ãã«ç¶æ
- ãªã¯ã¨ã¹ãããã£ã¼ã§æå®ãã 3,4 ã®ã¬ã³ã¼ããåé¤ããã¦ãã¾ã
Â
å ¨ã¬ã³ã¼ãåé¤
å®è¡åãã¼ãã«ç¶æ
Â
curl ã³ãã³ãã§åé¤å®è¡
curl --location --request DELETE 'http://localhost:3000/api/habbit_activity' \ --header 'Content-Type: text/plain' \ --data '{ "ids": [] }'
Â
å®è¡å¾ãã¼ãã«ç¶æ
- å ¨ã¦ã®ã¬ã³ã¼ããåé¤ããã¦ãããã¨ã確èªã§ãã¾ãã
Â
ã«ã¹ã±ã¼ãããªã¼ã
æå¾ã«ã«ã¹ã±ã¼ãããªã¼ãã試ãã¦ã¿ã¾ãã
habbit ãã¼ãã«ã®ã¬ã³ã¼ããåé¤ããã¨ãç´ã¥ãhabbit_activity ãã¼ãã«ã®ã¬ã³ã¼ããåé¤ããããã¨ã確èªãã¦ããã¾ãã
Â
å®è¡åãã¼ãã«ç¶æ
Â
curl ã³ãã³ãã§åé¤å®è¡
curl --location --request DELETE 'http://localhost:3000/api/habbit' \ --header 'Content-Type: text/plain' \ --data '{ "id":3 }'
Â
å®è¡å¾ãã¼ãã«ç¶æ
habbit ãã¼ãã«
habbit_activity ãã¼ãã«
- habbit ãã¼ãã«ã® id:3 ã®ã¬ã³ã¼ãã¨ãç´ã¥ã habbit_activity ãã¼ãã«ã®ã¬ã³ã¼ããåé¤ããã¦ãããã¨ã確èªã§ãã¾ãã
Â
Â
çµããã«
åé¤å¦çãæ´æ°å¦çãªã©ã¨åãããã«ãwhere ãªãã·ã§ã³ã¨ãã¦æ¡ä»¶ãå¼ã渡ããã¨ã§ã·ã³ãã«ã«å®è£ ã§ãããã¨ãç¥ãã¾ããã
ããã¾ã§ Prisma ã使ã£ã CRUD ãæ¸ããã¦ããã ãã¾ããããç°¡æãªã³ã¼ãã§DBæä½ãå®ç¾ã§ãããã¨ãç¥ãã¦è¯ãã£ãã§ãã
ããã¾ã§ã®å 容ãä½ããåèã«ãªãã°å¬ããã§ãã
Â