11import { headers } from "next/headers" ;
22import { getAuthServer } from "./auth" ;
3+ import { getDrizzle } from "./drizzle" ;
4+ import { chat , message } from "@/schema/chat" ;
5+ import { and , asc , eq } from "drizzle-orm" ;
36
47export interface CreateChatMessage {
58 role : "user" | "ai" | "error" ;
@@ -11,66 +14,66 @@ export async function addChat(
1114 sectionId : string ,
1215 messages : CreateChatMessage [ ]
1316) {
14- const prisma = await getPrismaClient ( ) ;
15- const auth = await getAuthServer ( prisma ) ;
17+ const drizzle = await getDrizzle ( ) ;
18+ const auth = await getAuthServer ( drizzle ) ;
1619 const session = await auth . api . getSession ( { headers : await headers ( ) } ) ;
1720 if ( ! session ) {
1821 throw new Error ( "Not authenticated" ) ;
1922 }
2023
21- return await prisma . chat . create ( {
22- data : {
24+ const [ newChat ] = await drizzle
25+ . insert ( chat )
26+ . values ( {
2327 userId : session . user . id ,
2428 docsId,
2529 sectionId,
26- messages : {
27- createMany : {
28- data : messages ,
29- } ,
30- } ,
31- } ,
32- include : {
33- messages : true ,
34- } ,
35- } ) ;
30+ } )
31+ . returning ( ) ;
32+
33+ const chatMessages = await drizzle
34+ . insert ( message )
35+ . values (
36+ messages . map ( ( msg ) => ( {
37+ chatId : newChat . chatId ,
38+ role : msg . role ,
39+ content : msg . content ,
40+ } ) )
41+ )
42+ . returning ( ) ;
43+
44+ return {
45+ ...newChat ,
46+ messages : chatMessages ,
47+ } ;
3648}
3749
3850export type ChatWithMessages = Awaited < ReturnType < typeof addChat > > ;
3951
4052export async function getChat ( docsId : string ) {
41- const prisma = await getPrismaClient ( ) ;
42- const auth = await getAuthServer ( prisma ) ;
53+ const drizzle = await getDrizzle ( ) ;
54+ const auth = await getAuthServer ( drizzle ) ;
4355 const session = await auth . api . getSession ( { headers : await headers ( ) } ) ;
4456 if ( ! session ) {
4557 return [ ] ;
4658 }
4759
48- return await prisma . chat . findMany ( {
49- where : {
50- userId : session . user . id ,
51- docsId,
52- } ,
53- include : {
60+ const chats = await drizzle . query . chat . findMany ( {
61+ where : and ( eq ( chat . userId , session . user . id ) , eq ( chat . docsId , docsId ) ) ,
62+ with : {
5463 messages : {
55- orderBy : {
56- createdAt : "asc" ,
57- } ,
64+ orderBy : [ asc ( message . createdAt ) ] ,
5865 } ,
5966 } ,
60- orderBy : {
61- createdAt : "asc" ,
62- } ,
67+ orderBy : [ asc ( chat . createdAt ) ] ,
6368 } ) ;
69+
70+ return chats ;
6471}
6572
6673export async function migrateChatUser ( oldUserId : string , newUserId : string ) {
67- const prisma = await getPrismaClient ( ) ;
68- await prisma . chat . updateMany ( {
69- where : {
70- userId : oldUserId ,
71- } ,
72- data : {
73- userId : newUserId ,
74- } ,
75- } ) ;
74+ const drizzle = await getDrizzle ( ) ;
75+ await drizzle
76+ . update ( chat )
77+ . set ( { userId : newUserId } )
78+ . where ( eq ( chat . userId , oldUserId ) ) ;
7679}
0 commit comments