casenC
Convex Community3y ago
40 replies
casen

Code organization patterns in Convex

Hello everyone! After playing around and creating a slack app that's wrapping an OpenAI assitant with a knowledge base, I've gotten to kick the tires a bit on Convex. As the various back and forth interactions have grown in complexity, I've noticed some rather strange architectural patterns emerge. I'd like to discuss some best practices around overall app structure, which I have not noticed in any examples or docs, since they are all relatively trivial.

1. Convex seems to force all code into two buckets: internal, and api (public and private).
2. Convex furthur devides all code into actions, and then all the rest (mutations, queries).
3. All files in the root of the convex folder seem to represent a resource, or a domain, and thus become the tip of the spear for the entire application.

Given the above, I've found it rather challenging to organize the codebase. Convex seems to want a CQRS style, which makes sense given the separation of queries and mutations. That much is nice, when everything lives in convex land with pure mutations and queries, the code really looks good.

Where it gets challenging is with actions and the nodejs runtime.

Has anyone else wrestled with this? Any example codebases or tips for better organizing actions? Often times, things are foced into actions merely because they need the nodejs runtime, or they are calling an external service. Because of this they need to be in a separate file, even if the code is highly coupled or related to mutations and queries.

Overall it creates a bit of spaghetti where code bounces around between files, calling mutations, actions, mutations, queries, actions, etc.

Perhaps I am just inexperienced with this database-first reactive style of programming. I welcome any tips!
Was this page helpful?