enforce unique indexes
is there a way to enforce unique indexes that combine multiple columns like in sql. like product id, product size, product color. this way when mutation would try to insert already existing combination of colums convex would throw an error?
5 Replies
Generally the way to do this is to create an index and write the query that efficiently checks for that item. That's the same way that unique constraints are implemented in SQL databases.
You can read the code for convex ents, it's doing the same thing you'd do manually.
Convex functions run inside the database, so the "extra round trip" is free, you're just doing what a SQL query planner would do for you in a SQL database.
Take the query that checks for uniqueness and the mutation that adds something and stick them in a helper function. Now you have code that does what you want: inserts, or throws if it exists
ok so manual check before insert
That's what ents is, taken to the extreme
yeah exactly
ok understand thanks
it's in a transaction so it's safe, and the code runs right next to the data so this is very cheap