kabb8048
kabb804812mo ago

Custom data types

Does convex (or a third party convex lib) support custom data types in the database? For example, if I wanted to store a calendar date YYYY-MM-DD, I could store it in a string but would love for that to be converted to and from custom type at the edges of my code. I know I can "just use code", but the way the database reader works, it seems tricky to do generically AND with type safety.
3 Replies
erquhart
erquhart12mo ago
Welcome! Convex data types are limited to the basics you see documented, so you can have a string, or string literal, but not a format as you're mentioning. The closest you can get is using Zod for your validators - here's an article on that: https://stack.convex.dev/typescript-zod-function-validation
Using Zod with TypeScript for Server-side Validation and End-to-End...
Use Zod with TypeScript for argument validation on your server functions allows you to both protect against invalid data, and define TypeScript types ...
lee
lee12mo ago
yeah i don't think anyone has built this, but it's a great idea! the closest thing that wraps db and changes document types is Ents which add edges, but i don't think they add dates or allow ways to create new custom types https://stack.convex.dev/ents
Convex Ents: Manage your document relationships
Convex Ents is a library that provides: simpler ways to model and query related documents, ability to easily map and filter documents, enforcing uniqu...
kabb8048
kabb8048OP12mo ago
Ya, I'm kind of looking for something a little richer than just regex validation - something like the sql.Valuer interface for Go. Or fromDatabase(v: string): PlainDate and toDatabase(v: PlainDate): string adapters similar to sequelize v7. https://sequelize.org/docs/v7/other-topics/extending-data-types/ GraphQL has a similar concept with custom scalars: https://www.apollographql.com/docs/apollo-server/schema/custom-scalars/ Maybe I'll try a proof of concept this weekend to see how it goes. For anyone in the future reading this, the first step of doing this is checking out the custom-helpers functions. It seems like the convex team has thought of this, but kind of in a more generic way. Particularly: https://stack.convex.dev/custom-functions This looks to be exactly what I need so far. Going to keep digging.

Did you find this page helpful?