Matt Luo
Matt Luo11mo ago

`.last()`

Regarding query functions, I noticed there is not a method like last() that would be the opposite of first() https://docs.convex.dev/api/interfaces/server.Query#first To get the last document, do you typically add an index to that column and add something like this to the query: .order("desc").first()? I ask because there doesn't seem to be native functionality to add a unique constraint on a column. So simply doing a last() method may be a hacky way to quickly get coding with a predictable handling of duplicates.
18 Replies
lee
lee11mo ago
yes you can implement .last() by doing .order("desc").first() in all places where you would otherwise use .first()
Matt Luo
Matt LuoOP11mo ago
ok thanks Lee. Perhaps a last() function would be a nice enhancement to make the code easier to reason about
lee
lee11mo ago
if you want to want to check if a column is unique, you could assert that .last() is the same as .first() or you can use the existing .unique() yeah interesting idea. you would want .last() to invert the ordering (so "desc" -> "asc") before doing .first(). if we borrow some precedent from programming languages, we could even make .take(-2) return the last two items or something
Matt Luo
Matt LuoOP11mo ago
@Lee - Many ways to make the syntax work; i just find last() natural since you already created first(). At this point in time, for a new Convex app today, would you advise me to invest time in coding a replication of unique constraint, or should I just write a hacky approach like this and hope that Convex comes out with native unique constraint enforcement?
lee
lee11mo ago
what is the hacky approach for unique constraint?
Matt Luo
Matt LuoOP11mo ago
Using the last document, and merging data in the unlikely chance a user experiences an old reference. My business use case right now is a table for user settings
lee
lee11mo ago
can you enforce the unique constraint in the mutation that writes it?
Matt Luo
Matt LuoOP11mo ago
Well, that leads me down the path of figuring out what to do upon an exception
lee
lee11mo ago
what kind of exception do you mean?
Matt Luo
Matt LuoOP11mo ago
Well, the unique() method throws an exception if there are duplicates
lee
lee11mo ago
there won't be duplicates if you enforce uniqueness in the mutation that writes it
Matt Luo
Matt LuoOP11mo ago
Ah, yes, so upon insertion mutations, i can code to enforce uniqueness where appropriate. Seems like that would be code I would write/maintain and remove when a native unique constraint in the Convex product comes out
lee
lee11mo ago
yep i would do it in the mutation idk if/when we would add uniqueness constraints natively, since it's easy and more versatile to add them in mutations
Matt Luo
Matt LuoOP11mo ago
I see, I think that answers my question. I think I need to wrap my head around paradigm that mutations are where a lot of the data integrity logic will be, as opposed to the database/ Convex defineSchema function thanks Lee! I suppose I am the old dog learning new tricks. USually, i have built apps starting with DDL
lee
lee11mo ago
putting the logic in the schema does make sense in some scenarios, and we'll probably have something like Convex Ents to support this
Matt Luo
Matt LuoOP11mo ago
I'm thinking if a developer wrote last(), there shouldn't be a first() later. THat would be redundant and difficult to skim
lee
lee11mo ago
i'm thinking .order("desc").last() should effectively do .first()
Matt Luo
Matt LuoOP11mo ago
haha

Did you find this page helpful?