agentleFS
grezxunegrezxune/ai-tools/skills/convex/SKILL.md

convex

Skill0 starsChanged 56 days agoMITView on GitHub

Convex patterns for schema, queries, mutations, and auth. Use before writing or reviewing Convex code in a repo with no `docs/convex.md`.

11 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use convex-migration-helper, convex-performance-audit and 1 more.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • elpiartheraSkill

    convex-migration-helper

    Plans and executes safe Convex schema and data migrations using the widen-migrate-narrow workflow and the @convex-dev/migrations component. Use this skill when a deployment fails schema validation, existing documents need backfilling, fields need adding or removing or changing type, tables need splitting or merging, or a zero-downtime migration strategy is needed. Also use when the user mentions breaking schema changes, multi-deploy rollouts, or data transformations on existing Convex tables.

    Also in 3 projects that use this

  • elpiartheraSkill

    convex-performance-audit

    Audits and optimizes Convex application performance across hot-path reads, write contention, subscription cost, and function limits. Use this skill when a Convex feature is slow or expensive, npx convex insights shows high bytes or documents read, OCC conflict errors or mutation retries appear, subscriptions or UI updates are costly, functions hit execution or transaction limits, or the user mentions performance, latency, read amplification, or invalidation problems in a Convex app.

    Also in 3 projects that use this

  • +1 more from elpiarthera

The file

Show its rules and text

We split this skill into the rules it gives an agent

16 rules. 0 are common, 3 are distinctive, and 13 are very specific to this project.

  • 0

    Common

    Most of the 11 edited versions we read keep these.

  • 3

    Distinctive

    This author's own choices. Worth a look.

  • 13

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Read this before writing or reviewing Convex schema, queries, mutations, actions, or auth.

    line 8
  2. Very specific:

    Use userId: v.id("users") as the ownership field on every user-owned table.

    line 12
  3. Very specific:

    Never use email as a foreign key.

    line 13
  4. Very specific:

    Never join or filter across tables by email.

    line 14
  5. Very specific:

    Every table with a userId field needs a by_userId index.

    line 15
  6. Very specific:

    Use compound indexes for common filters, e.g.

    line 16
  7. Very specific:

    Never trust client-supplied userId, email, role, or platformRole for authorization.

    line 21
  8. Very specific:

    Every public Convex query/mutation that accepts userId must:

    line 23
  9. Very specific:

    Use ctx.auth.getUserIdentity() as canonical actor identity and reject mismatches.

    line 26
  10. Very specific:

    Do not use raw query/mutation for protected operations.

    line 32
  11. Very specific:

    Prefer internalQuery/internalMutation for server-to-server work.

    line 35
  12. Distinctive:

    Every list the UI shows comes from a query that already applied the view's filters.

    line 84
  13. Very specific:

    Put each filter the view uses (status, date range, parent id, search text) in the query's args and apply it in the handler.

    line 86
  14. Very specific:

    Run text search through a search index with .withSearchIndex().

    line 88
  15. Very specific:

    Use .paginate() with usePaginatedQuery for any list that grows with usage.

    line 89
  16. Distinctive:

    When a view needs a different subset, write a query for that subset rather than reusing a broader one.

    line 91

“Common” is measured: kept by at least 6 in 10 of the 11 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Convex Patterns

Read this before writing or reviewing Convex schema, queries, mutations, actions, or auth.

User IDs & Data Ownership

  • Use userId: v.id("users") as the ownership field on every user-owned table.
  • Never use email as a foreign key. Email lives only on the users table.
  • Never join or filter across tables by email. Use userId.
  • Every table with a userId field needs a by_userId index.
  • Use compound indexes for common filters, e.g. by_userId_status.
  • The by_email index on users exists only for Auth.js session lookup in getAuthUser(). After that, use user._id.

Authorization (non-negotiable)

Never trust client-supplied userId, email, role, or platformRole for authorization.

Every public Convex query/mutation that accepts userId must:

  1. Receive the canonical value derived from the authenticated NextAuth session.
  2. Enforce ownership and role checks in Convex against stored records.
  3. Use ctx.auth.getUserIdentity() as canonical actor identity and reject mismatches.

Deny by default: missing auth, missing ownership, or identity mismatch fails immediately. No deterministic or default secret/key fallbacks in production. Development-only auth providers must be disabled in production builds.

Enforcement Pattern

  • Do not use raw query/mutation for protected operations. Use shared wrappers: authenticatedQuery, authenticatedMutation, superAdmin*.
  • Public functions accepting userId must call requireAuthenticatedUserId(ctx, args.userId).
  • Admin operations enforce role checks inside Convex, not only in Next.js pages or server actions.
  • Prefer internalQuery/internalMutation for server-to-server work.

For custom JWT auth with Convex:

  1. convex/auth.config.ts with issuer, JWKS, and audience.
  2. Short-lived server-minted JWTs for server-side Convex clients.
  3. A rotation-compatible JWKS endpoint.

File Organization

convex/
├── lib/auth.ts               # Shared auth utilities
├── {domain}/
│   ├── queries.ts            # Public queries
│   ├── mutations.ts          # Public mutations
│   ├── internalQueries.ts    # Server-to-server only
│   ├── internalMutations.ts
│   ├── actions.ts            # External API calls
│   └── index.ts
├── schema.ts
└── _generated/

Reference internal functions as internal.{domain}.internalQueries.fn, not .queries..

Auth Utilities (convex/lib/auth.ts)

export async function getAuthUser(ctx, requestedUserId?): Promise<Doc<"users"> | null>
export async function getAuthUserId(ctx, requestedUserId?): Promise<Id<"users"> | null>
export async function requireAuth(ctx, requestedUserId?): Promise<Doc<"users">>
export async function requireAuthUserId(ctx, requestedUserId?): Promise<Id<"users">>
export async function requireAuthenticatedUserId(ctx, requestedUserId?): Promise<Id<"users">>
export async function requireOwnership<T extends { userId: Id<"users"> }>(ctx, resource: T | null, name?): Promise<T>
export async function requirePremium(ctx): Promise<Doc<"users">>
export async function hasPremiumAccess(ctx): Promise<boolean>

Frontend Hook (src/hooks/use-auth-user.ts)

export function useAuthUser() {
  // { userId, user, isLoading, isAuthenticated, authArgs }
  // authArgs builds query args with userId, or returns "skip"
}

Server-Side Filtering

Every list the UI shows comes from a query that already applied the view's filters. The component renders what it receives.

  • Put each filter the view uses (status, date range, parent id, search text) in the query's args and apply it in the handler.
  • Apply filters with .withIndex(). A .collect() followed by a JS .filter() still reads every row, even on the server.
  • Run text search through a search index with .withSearchIndex().
  • Use .paginate() with usePaginatedQuery for any list that grows with usage. Keep .collect() for small, bounded sets.
  • Map documents to the fields the view renders before returning. Other users' emails, internal flags, and tokens stay on the server.
  • When a view needs a different subset, write a query for that subset rather than reusing a broader one.

Query & Mutation Shape

// Good: filter, paginate, and select fields inside the query
export const listGoalsByStatus = query({
  args: { userId: v.id("users"), status: goalStatus, paginationOpts: paginationOptsValidator },
  handler: async (ctx, args) => {
    const userId = await requireAuthenticatedUserId(ctx, args.userId);
    const result = await ctx.db
      .query("goals")
      .withIndex("by_userId_status", (q) => q.eq("userId", userId).eq("status", args.status))
      .paginate(args.paginationOpts);
    return { ...result, page: result.page.map(({ _id, title, status }) => ({ _id, title, status })) };
  },
});

// Good: filter by userId via index
export const getGoals = query({
  args: { userId: v.id("users") },
  handler: async (ctx, args) =>
    ctx.db.query("goals").withIndex("by_userId", (q) => q.eq("userId", args.userId)).collect(),
});

// Good: verify ownership before writing
export const updateGoal = mutation({
  args: { goalId: v.id("goals"), title: v.string() },
  handler: async (ctx, args) => {
    const goal = await ctx.db.get(args.goalId);
    await requireOwnership(ctx, goal, "Goal");
  },
});

// Bad: no userId filter, returns every row
export const getAllGoals = query({ handler: async (ctx) => ctx.db.query("goals").collect() });

// Bad: ships every goal to the client, which filters in the component
const goals = useQuery(api.goals.queries.getGoals, authArgs);
const active = goals?.filter((goal) => goal.status === "active");

Required Security Tests

One negative authorization test per sensitive domain:

  1. Mismatched userId is rejected.
  2. Unauthenticated call is rejected.
  3. Non-admin actor cannot reach admin-only paths.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.