DCsan
DCsan2y ago

helper queries?

I find myself writing a lot of boilerplate helper queries for every class like this. are there any higher level methods for doing this? eg I don't know the internal _id but i have my own IDs for tracking relations across tables. I would like to have my own baseClass for the data tables and just extend to add these common repetitive methods but not sure where/if I can do that. convex is nice that it's all JS, but it doesn't have the convenience of an actual ORM
async function getCardById(db: any, id: string) {
const baseCard = await db
.query("baseCards")
.filter((bc: any) => bc.eq(bc.field("id"), id))
.first();
return baseCard;
}
async function getCardById(db: any, id: string) {
const baseCard = await db
.query("baseCards")
.filter((bc: any) => bc.eq(bc.field("id"), id))
.first();
return baseCard;
}
2 Replies
ballingt
ballingt2y ago
For this query in particular, you can db.get() any ID to get the object; use db.normalize() if you're not confident the id is of the table you expect or it's a string https://docs.convex.dev/api/interfaces/server.DatabaseWriter#normalizeid EDIT: I didn't read well, you said these are your own IDs (not ._id for which you can use db.get()) We haven't suggested an ORM to fit on top of our database but you can very much imagine one. I could see something on a replacement db object, which could be supplied by a helper or by middleware. For
I would like to have my own baseClass for the data tables and just extend to add these common repetitive methods but not sure where/if I can do that.
check out _generated/dataModel.d.ts which has the types useful for get started on helpers. I'd love to hear if there are ORMs you've especially liked. While there'd be some legwork, adapting existing ORMs or ORM patterns to Convex is absolutely worth thinking about.
jamwt
jamwt2y ago
This post has a number of generic accessor functions that could help you learn how to create something orm-like in convex today: https://stack.convex.dev/functional-relationships-helpers
Functional Relationships: Helpers
In this post, we’ll look at some helper functions to help write code to traverse relationships in a readable, predictable, and debuggable way.

Did you find this page helpful?