Hey guys, what is the best way to set default values for Convex Schemas
I was not able to find any documentation on this haha, also it would be great to have an easy way to create schemas from type script interfaces as I am trying to port a large part of my database from postgres to convex to see how it performs on dev!
18 Replies
Also is there any data structure that works with postGIS data too(to query nearest distance)
Also, is it not possible to run multiple queries in a single function?
This error gets thrown
On the last point: You cannot write into the database from a query.
https://docs.convex.dev/database/writing-data
You can read from the database many times from a query function.
(I recommend skimming the tutorial for a good overview of Convex: https://docs.convex.dev/get-started )
Writing Data | Convex Developer Hub
Mutations can insert, update, and
Welcome to Convex | Convex Developer Hub
Convex is a novel, fun, and extremely productive way to make backends for your
i see, i fixed that issue with a customQuery/mutationHook! Thanks though, maybe it would be nice to have the custom functions as part of the docs too!
For the first part is there any method to create your own types from an existing say prisma database?
Also for mutations, what is the best practice to use them(suppose i want to initialise a user(would just calling the function in a useEffect() be best practice or are there other ways of going about it).
Also can you call mutations without explicitly asking for them(like a button)
Yes, you can use a useEffect, just watch out for handling the state before the mutation succeeds. This is what we suggest here:
https://docs.convex.dev/auth/database-auth#call-a-mutation-from-the-client
Storing Users in the Convex Database | Convex Developer Hub
You might want to store user information directly in your Convex database, for
For default values, you can add them after reading data from the database. So if your document has a field
x: v.optional(v.string())
you can read the document and then set x to doc.x ?? "default value"
.where would you do that, would that be in the client component? as for my initial question i wanted to ask if we can call convex functions outside of react components?
Yes, useEffect must be called from a client component.
Yes, you can call Convex functions from many clients, see https://docs.convex.dev/quickstarts
Quickstarts | Convex Developer Hub
Quickly get up and running with your favorite frontend tooling or language:
oh what i meant to ask was that can it be used in a non-client component(something like zustand)
If you want to call a mutation from the client outside of a component you can use the async
mutation
method on the ConvexReactClient
instance. In a component you can get the client instance via the useConvex()
hook.in state stores wouldnt that lead to recursive hook calls though?
That really depends what you do. It might be more of a Zustand question than Convex question.
trueee hmmm
if i figure it out ill do a short writeup here; it might be helpful for others too
I had the same question as the title of this post, but it seems like it hasn't been answered. For strings, one can use v.literal to set a default value, but I'm not sure how to do the same for other types...
It was answered in this message:
https://discord.com/channels/1019350475847499849/1241908328762703962/1242059642452967446
Thanks @Michal Srb, i think that solution works, i.e. to insert to the db upon getting no result. But that seems like a workaround. You wouldn’t be able to distinguish whether the user actually manually set that value, or whether the system did it. I could see this approach working fully if you add a column about whether the system added that default value
I took the useEffect approach for my Next.js client side rendered settings page. I don’t insert a default value to the DB. I used application code to conditionally set a default value. I’m somewhat happy with it. It functionally works, but there is a re-rendering that appears as a flicker in the drop-down to the end user
crystaltxt.com/settings to see the flickering
I’m considering a different approach to avoid the re-rendering: calling a Convex mutation function instead of a query function. The mutation would return a default value. The tradeoff is that I would need to include default value logic inside the mutation function. Also, the return logic could potentially get complicated if I wanted to return information about whether the mutation had to use the default value or whether the return is indeed the user’s chosen value
@Matt Luo you are misinterpreting what I wrote. I did not suggest to write to the DB if the value is missing (although that is sometimes what's needed).
Most of the time you can just read data and combine it with default values in the query (and this way you can also distinguish default values from real values):
The disadvantage of using a mutation for reading data, besides the ergonomics of our hooks, is that it can never be cached.
Ah I see, that makes sense
Thanks Michal