RobR
Convex Community7mo ago
3 replies
Rob

support for module naming or separate .internal.ts .api.ts files

Just posted this via the feedback form but will post it here in case anyone else wants to comment on it.

I currently organise all my code as follows:

convex
─ domain
─ table
─ queries
─ getFoo.ts
─ getBar.ts
─ mutations
─ insertFoo.ts
─ insertBar.ts
─ actions
─ userflow.ts
─ table.ts
─ queries.ts // barrel exports
─ mutations.ts
─ actions.ts
─ tables.ts
─ actions.ts
─ queriesPublic.ts
─ queriesInternal.ts
─ mutationsInternal.ts
─ mutationsPublic.ts
─ schema.ts

This allows me to keep everything really organised. Each query/mutation function in the table folder looks like:

import { v } from "convex/values";
import { MutationCtx } from "cvx/_generated/server"; // I have cvx path alias set up for convex directory.

type Args = { ... };

export const fooFn = async (ctx: MutationCtx, { ... }: Args) => { await ctx.db.insert("foo", {...}); };

export const fooMutation = { args: {...}, handler: fooFn };


In the top level mutation files, I can then import all the mutation objects and wrap them:

mutationsInternal.ts
import * as domainA from "cvx/domainA/mutations";
import * as domainB from "cv/domainB/mutations";

const foo = internalMutation(domainA.privateFooMutation);
const bar = internalMutation(domainB.privateBarMutation);


mutationsPublic.ts
import * as domainA from "cvx/domainA/mutations";
import * as domainB from "cvx/domainB/mutations";

const baz = mutation(domainA.bazMutation);
const qux = mutation(domainB.quxMutation);


I like this because then I can keep my internal and public mutations and queries separate from each other which feels like better practice.

However, when I want to use them I then have to type out internal.mutationsInternal.foo or api.mutationsPublic.baz which is mildly annoying because of the naming redundancy - see comment as hitting message length limit
Was this page helpful?