`.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
yes you can implement
.last()
by doing .order("desc").first()
in all places where you would otherwise use .first()
ok thanks Lee. Perhaps a last() function would be a nice enhancement to make the code easier to reason about
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@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?
what is the hacky approach for unique constraint?
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
can you enforce the unique constraint in the mutation that writes it?
Well, that leads me down the path of figuring out what to do upon an exception
what kind of exception do you mean?
Well, the unique() method throws an exception if there are duplicates
there won't be duplicates if you enforce uniqueness in the mutation that writes it
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
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
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
putting the logic in the schema does make sense in some scenarios, and we'll probably have something like Convex Ents to support this
I'm thinking if a developer wrote last(), there shouldn't be a first() later. THat would be redundant and difficult to skim
i'm thinking
.order("desc").last()
should effectively do .first()
haha