Organization patterns for convex folder
Anyone have recommendations for organizing the convex folder? Do you just do one file for each table? A folder for each table with separate files for mutations, queries, and actions? Started playing around with the second but things got redundant when it came to api vs. internal calls.
4 Replies
I ended up doing something like what you're describing. I have a
convex/db
directory that has a file per table (and some of those are supplemented with table-specific utility files). Each table file in this "db layer" serves as the only entry point for using ctx.db.*
against a given table. So those files have a lot of repetitive bits, get/list/page, enforce data integrity, cascading deletes, etc.
The other files in my convex directory serve as the api layer for my application, and is pretty ad hoc. A single db
object is exported from convex/db/index.ts
and is used by the api layer for all database access.
I've been building on this approach since January and it's worked really well.If you have code for scheduled cron jobs or other internal actions, do you also include those in the same directory as the api layer files?
My db layer is designed to be js functions only, no convex functions. However, I did have to include some internal mutations to facilitate cascading deletes. My actions and cron code are in the db layer.
If you think of the db layer as a custom ORM, the question is whether something should be a part of that ORM or outside of it. There are probably valid use cases for using cron or actions in the db layer.
I see, that makes sense! I think actions and cron code in the db layer probably makes sense for me too