| import { v } from 'convex/values'; | |
| import { mutation, query } from './_generated/server'; | |
| export const list = query({ | |
| args: { userId: v.string() }, | |
| handler: async (ctx, { userId }) => { | |
| return await ctx.db | |
| .query('conversations') | |
| .withIndex('by_userId', (q) => q.eq('userId', userId)) | |
| .collect(); | |
| } | |
| }); | |
| export const get = query({ | |
| args: { chatId: v.string() }, | |
| handler: async (ctx, { chatId }) => { | |
| return await ctx.db | |
| .query('conversations') | |
| .withIndex('by_chatId', (q) => q.eq('chatId', chatId)) | |
| .first(); | |
| } | |
| }); | |
| export const save = mutation({ | |
| args: { | |
| chatId: v.string(), | |
| userId: v.string(), | |
| title: v.string(), | |
| data: v.any() | |
| }, | |
| handler: async (ctx, { chatId, userId, title, data }) => { | |
| const existing = await ctx.db | |
| .query('conversations') | |
| .withIndex('by_chatId', (q) => q.eq('chatId', chatId)) | |
| .first(); | |
| if (existing) { | |
| await ctx.db.patch(existing._id, { title, data, updatedAt: Date.now() }); | |
| } else { | |
| await ctx.db.insert('conversations', { | |
| chatId, | |
| userId, | |
| title, | |
| data, | |
| updatedAt: Date.now() | |
| }); | |
| } | |
| } | |
| }); | |
| export const remove = mutation({ | |
| args: { chatId: v.string() }, | |
| handler: async (ctx, { chatId }) => { | |
| const existing = await ctx.db | |
| .query('conversations') | |
| .withIndex('by_chatId', (q) => q.eq('chatId', chatId)) | |
| .first(); | |
| if (existing) { | |
| await ctx.db.delete(existing._id); | |
| } | |
| } | |
| }); | |