File size: 805 Bytes
cfb0fa4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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()
			});
		}
	}
});