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