adam
adam8mo ago

File based routing: How to organize files?

Hi, I'm looking for the best way to organise files within the convex directory. The single file such as messages.ts is becoming very large with many different mutations, queries, actions etc. I tried to create a ./messages/index.ts and then use export * from './create' from within the ./messages/index.ts file, though this approach doesn't work very well with the auto generated Convex functions. I worked out that I can use the _ prefix, eg ./_messages/create.ts for the folder (also works with files too), so they will be excluded from Convex function auto generation but the TS types are still created for these _* files and directories. My question is, am I on the right path with using _* prefix to exclude some files and folders from file based routing, or is there a better approach to organizing files?
4 Replies
Michal Srb
Michal Srb8mo ago
I recommend the pattern showcased here (ignore the use of convex-ents): https://github.com/xixixao/saas-starter So I group my functions by the logical entities they are related to, and I model the folders based on the approximate relationship between those entities. My advice is to keep code together which is related, and to not split code based on "type" (like having read/ and write/ folder, or query/ mutation/ action/, since these are technical details). Another "metric" you can use when organizing files is "how easy is it to delete a feature" - is it a matter of deleting a single folder, or digging through all sorts of different files? In Convex these folders also determine the "api" shape, so you can think of this as the problem of designing a clean API.
GitHub
GitHub - xixixao/saas-starter: Convex, Clerk, Next.js, Convex Ents
Convex, Clerk, Next.js, Convex Ents. Contribute to xixixao/saas-starter development by creating an account on GitHub.
adam
adamOP8mo ago
Ok, thanks. It's a helpful reference.
kstulgys
kstulgys6mo ago
I have some internal functions/actions that has to "use node" then I'm forced to put these in separate files or folders. How should I name them? I.e when I'm dealing with stripe. I have to interact with my convex orders, payments and users tables. This becomes out of hand quickly.
adam
adamOP6mo ago
I tend to suffix these “use node” files with _node. For example I have stripe_node.ts You can then call queries, or internal queries from your action with await ctx.runQuery(..) approach to access your data from other tables.