Skip to content

Commit 1ecc019

Browse files
committed
fix: enforce polar data uniqueness on insert
1 parent e2d80d1 commit 1ecc019

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/component/lib.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ export const insertCustomer = mutation({
2626
args: schema.tables.customers.validator,
2727
returns: v.id("customers"),
2828
handler: async (ctx, args) => {
29+
const existingCustomer = await ctx.db
30+
.query("customers")
31+
.withIndex("userId", (q) => q.eq("userId", args.userId))
32+
.unique();
33+
if (existingCustomer) {
34+
throw new Error(`Customer already exists for user: ${args.userId}`);
35+
}
2936
return ctx.db.insert("customers", {
3037
id: args.id,
3138
userId: args.userId,
@@ -203,6 +210,13 @@ export const createSubscription = mutation({
203210
subscription: schema.tables.subscriptions.validator,
204211
},
205212
handler: async (ctx, args) => {
213+
const existingSubscription = await ctx.db
214+
.query("subscriptions")
215+
.withIndex("id", (q) => q.eq("id", args.subscription.id))
216+
.unique();
217+
if (existingSubscription) {
218+
throw new Error(`Subscription already exists: ${args.subscription.id}`);
219+
}
206220
await ctx.db.insert("subscriptions", {
207221
...args.subscription,
208222
metadata: args.subscription.metadata,
@@ -234,6 +248,13 @@ export const createProduct = mutation({
234248
product: schema.tables.products.validator,
235249
},
236250
handler: async (ctx, args) => {
251+
const existingProduct = await ctx.db
252+
.query("products")
253+
.withIndex("id", (q) => q.eq("id", args.product.id))
254+
.unique();
255+
if (existingProduct) {
256+
throw new Error(`Product already exists: ${args.product.id}`);
257+
}
237258
await ctx.db.insert("products", {
238259
...args.product,
239260
metadata: args.product.metadata,

0 commit comments

Comments
 (0)