Skip to content

Commit 8aa482e

Browse files
committed
データベースにsectionテーブル追加
1 parent 32dc358 commit 8aa482e

File tree

6 files changed

+538
-9
lines changed

6 files changed

+538
-9
lines changed

app/actions/chatActions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ export async function askAI(params: ChatParams): Promise<ChatResult> {
170170
targetSectionId = introSectionId(path);
171171
}
172172
const responseMessage = text.split(/-{3,}/)[1].trim();
173-
const newChat = await addChat(targetSectionId, [
173+
const newChat = await addChat(path, targetSectionId, [
174174
{ role: "user", content: userQuestion },
175175
{ role: "ai", content: responseMessage },
176176
]);

app/lib/chatHistory.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import { headers } from "next/headers";
44
import { getAuthServer } from "./auth";
55
import { getDrizzle } from "./drizzle";
6-
import { chat, message } from "@/schema/chat";
7-
import { and, asc, eq } from "drizzle-orm";
6+
import { chat, message, section } from "@/schema/chat";
7+
import { and, asc, eq, exists } from "drizzle-orm";
88
import { Auth } from "better-auth";
99
import { revalidateTag, unstable_cacheLife } from "next/cache";
1010
import { isCloudflare } from "./detectCloudflare";
@@ -54,6 +54,7 @@ export async function initContext(ctx?: Partial<Context>): Promise<Context> {
5454
}
5555

5656
export async function addChat(
57+
path: PagePath,
5758
sectionId: SectionId,
5859
messages: CreateChatMessage[],
5960
context?: Partial<Context>
@@ -81,17 +82,21 @@ export async function addChat(
8182
)
8283
.returning();
8384

84-
revalidateTag(cacheKeyForPage({}, userId));
85+
revalidateTag(cacheKeyForPage(path, userId));
8586
if (isCloudflare()) {
8687
const cache = await caches.open("chatHistory");
8788
console.log(
88-
`deleting cache for chatHistory/getChat for user ${userId} and docs ${lang}/${page}`
89+
`deleting cache for chatHistory/getChat for user ${userId} and docs ${path.lang}/${path.page}`
8990
);
90-
await cache.delete(cacheKeyForPage({}, userId));
91+
await cache.delete(cacheKeyForPage(path, userId));
9192
}
9293

9394
return {
9495
...newChat,
96+
section: {
97+
sectionId,
98+
pagePath: `${path.lang}/${path.page}`,
99+
},
95100
messages: chatMessages,
96101
};
97102
}
@@ -108,8 +113,17 @@ export async function getChat(
108113
}
109114

110115
const chats = await drizzle.query.chat.findMany({
111-
where: and(eq(chat.userId, userId), eq(chat.docsId, docsId)),
116+
where: and(
117+
eq(chat.userId, userId),
118+
exists(
119+
drizzle
120+
.select()
121+
.from(section)
122+
.where(eq(section.pagePath, `${path.lang}/${path.page}`))
123+
)
124+
),
112125
with: {
126+
section: true,
113127
messages: {
114128
orderBy: [asc(message.createdAt)],
115129
},

app/schema/chat.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@ import { pgTable, text, timestamp, uuid } from "drizzle-orm/pg-core";
44
export const chat = pgTable("chat", {
55
chatId: uuid("chatId").primaryKey().defaultRandom(),
66
userId: text("userId").notNull(),
7-
docsId: text("docsId").notNull(),
87
sectionId: text("sectionId").notNull(),
98
createdAt: timestamp("createdAt").notNull().defaultNow(),
109
});
1110

11+
export const section = pgTable("section", {
12+
sectionId: text("sectionId").primaryKey().notNull(),
13+
pagePath: text("pagePath").notNull(),
14+
});
15+
1216
export const message = pgTable("message", {
1317
id: uuid("id").primaryKey().defaultRandom(),
1418
chatId: uuid("chatId").notNull(),
@@ -17,8 +21,16 @@ export const message = pgTable("message", {
1721
createdAt: timestamp("createdAt").notNull().defaultNow(),
1822
});
1923

20-
export const chatRelations = relations(chat, ({ many }) => ({
24+
export const chatRelations = relations(chat, ({ many, one }) => ({
2125
messages: many(message),
26+
section: one(section, {
27+
fields: [chat.sectionId],
28+
references: [section.sectionId],
29+
}),
30+
}));
31+
32+
export const sectionRelations = relations(chat, ({ many }) => ({
33+
chat: many(chat),
2234
}));
2335

2436
export const messageRelations = relations(message, ({ one }) => ({

drizzle/0003_thin_ben_grimm.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE TABLE "section" (
2+
"sectionId" text PRIMARY KEY NOT NULL,
3+
"pagePath" text NOT NULL
4+
);
5+
--> statement-breakpoint
6+
ALTER TABLE "chat" DROP COLUMN "docsId";

0 commit comments

Comments
 (0)