|
| 1 | +--- |
| 2 | +name: convex-create-component |
| 3 | +description: Designs and builds Convex components with isolated tables, clear boundaries, and app-facing wrappers. Use this skill when creating a new Convex component, extracting reusable backend logic into a component, building a third-party integration that owns its own tables, packaging Convex functionality for reuse, or when the user mentions defineComponent, app.use, ComponentApi, ctx.runQuery/runMutation across component boundaries, or wants to separate concerns into isolated Convex modules. |
| 4 | +--- |
| 5 | + |
| 6 | +# Convex Create Component |
| 7 | + |
| 8 | +Create reusable Convex components with clear boundaries and a small app-facing API. |
| 9 | + |
| 10 | +## When to Use |
| 11 | + |
| 12 | +- Creating a new Convex component in an existing app |
| 13 | +- Extracting reusable backend logic into a component |
| 14 | +- Building a third-party integration that should own its own tables and workflows |
| 15 | +- Packaging Convex functionality for reuse across multiple apps |
| 16 | + |
| 17 | +## When Not to Use |
| 18 | + |
| 19 | +- One-off business logic that belongs in the main app |
| 20 | +- Thin utilities that do not need Convex tables or functions |
| 21 | +- App-level orchestration that should stay in `convex/` |
| 22 | +- Cases where a normal TypeScript library is enough |
| 23 | + |
| 24 | +## Workflow |
| 25 | + |
| 26 | +1. Ask the user what they are building and what the end goal is. If the repo already makes the answer obvious, say so and confirm before proceeding. |
| 27 | +2. Choose the shape using the decision tree below and read the matching reference file. |
| 28 | +3. Decide whether a component is justified. Prefer normal app code or a regular library if the feature does not need isolated tables, backend functions, or reusable persistent state. |
| 29 | +4. Make a short plan for: |
| 30 | + - what tables the component owns |
| 31 | + - what public functions it exposes |
| 32 | + - what data must be passed in from the app (auth, env vars, parent IDs) |
| 33 | + - what stays in the app as wrappers or HTTP mounts |
| 34 | +5. Create the component structure with `convex.config.ts`, `schema.ts`, and function files. |
| 35 | +6. Implement functions using the component's own `./_generated/server` imports, not the app's generated files. |
| 36 | +7. Wire the component into the app with `app.use(...)`. If the app does not already have `convex/convex.config.ts`, create it. |
| 37 | +8. Call the component from the app through `components.<name>` using `ctx.runQuery`, `ctx.runMutation`, or `ctx.runAction`. |
| 38 | +9. If React clients, HTTP callers, or public APIs need access, create wrapper functions in the app instead of exposing component functions directly. |
| 39 | +10. Run `npx convex dev` and fix codegen, type, or boundary issues before finishing. |
| 40 | + |
| 41 | +## Choose the Shape |
| 42 | + |
| 43 | +Ask the user, then pick one path: |
| 44 | + |
| 45 | +| Goal | Shape | Reference | |
| 46 | +| ------------------------------------------------- | ---------------- | ----------------------------------- | |
| 47 | +| Component for this app only | Local | `references/local-components.md` | |
| 48 | +| Publish or share across apps | Packaged | `references/packaged-components.md` | |
| 49 | +| User explicitly needs local + shared library code | Hybrid | `references/hybrid-components.md` | |
| 50 | +| Not sure | Default to local | `references/local-components.md` | |
| 51 | + |
| 52 | +Read exactly one reference file before proceeding. |
| 53 | + |
| 54 | +## Default Approach |
| 55 | + |
| 56 | +Unless the user explicitly wants an npm package, default to a local component: |
| 57 | + |
| 58 | +- Put it under `convex/components/<componentName>/` |
| 59 | +- Define it with `defineComponent(...)` in its own `convex.config.ts` |
| 60 | +- Install it from the app's `convex/convex.config.ts` with `app.use(...)` |
| 61 | +- Let `npx convex dev` generate the component's own `_generated/` files |
| 62 | + |
| 63 | +## Component Skeleton |
| 64 | + |
| 65 | +A minimal local component with a table and two functions, plus the app wiring. |
| 66 | + |
| 67 | +```ts |
| 68 | +// convex/components/notifications/convex.config.ts |
| 69 | +import { defineComponent } from "convex/server"; |
| 70 | + |
| 71 | +export default defineComponent("notifications"); |
| 72 | +``` |
| 73 | + |
| 74 | +```ts |
| 75 | +// convex/components/notifications/schema.ts |
| 76 | +import { defineSchema, defineTable } from "convex/server"; |
| 77 | +import { v } from "convex/values"; |
| 78 | + |
| 79 | +export default defineSchema({ |
| 80 | + notifications: defineTable({ |
| 81 | + userId: v.string(), |
| 82 | + message: v.string(), |
| 83 | + read: v.boolean(), |
| 84 | + }).index("by_user", ["userId"]), |
| 85 | +}); |
| 86 | +``` |
| 87 | + |
| 88 | +```ts |
| 89 | +// convex/components/notifications/lib.ts |
| 90 | +import { v } from "convex/values"; |
| 91 | +import { mutation, query } from "./_generated/server.js"; |
| 92 | + |
| 93 | +export const send = mutation({ |
| 94 | + args: { userId: v.string(), message: v.string() }, |
| 95 | + returns: v.id("notifications"), |
| 96 | + handler: async (ctx, args) => { |
| 97 | + return await ctx.db.insert("notifications", { |
| 98 | + userId: args.userId, |
| 99 | + message: args.message, |
| 100 | + read: false, |
| 101 | + }); |
| 102 | + }, |
| 103 | +}); |
| 104 | + |
| 105 | +export const listUnread = query({ |
| 106 | + args: { userId: v.string() }, |
| 107 | + returns: v.array( |
| 108 | + v.object({ |
| 109 | + _id: v.id("notifications"), |
| 110 | + _creationTime: v.number(), |
| 111 | + userId: v.string(), |
| 112 | + message: v.string(), |
| 113 | + read: v.boolean(), |
| 114 | + }), |
| 115 | + ), |
| 116 | + handler: async (ctx, args) => { |
| 117 | + return await ctx.db |
| 118 | + .query("notifications") |
| 119 | + .withIndex("by_user", (q) => q.eq("userId", args.userId)) |
| 120 | + .filter((q) => q.eq(q.field("read"), false)) |
| 121 | + .collect(); |
| 122 | + }, |
| 123 | +}); |
| 124 | +``` |
| 125 | + |
| 126 | +```ts |
| 127 | +// convex/convex.config.ts |
| 128 | +import { defineApp } from "convex/server"; |
| 129 | +import notifications from "./components/notifications/convex.config.js"; |
| 130 | + |
| 131 | +const app = defineApp(); |
| 132 | +app.use(notifications); |
| 133 | + |
| 134 | +export default app; |
| 135 | +``` |
| 136 | + |
| 137 | +```ts |
| 138 | +// convex/notifications.ts (app-side wrapper) |
| 139 | +import { v } from "convex/values"; |
| 140 | +import { mutation, query } from "./_generated/server"; |
| 141 | +import { components } from "./_generated/api"; |
| 142 | +import { getAuthUserId } from "@convex-dev/auth/server"; |
| 143 | + |
| 144 | +export const sendNotification = mutation({ |
| 145 | + args: { message: v.string() }, |
| 146 | + returns: v.null(), |
| 147 | + handler: async (ctx, args) => { |
| 148 | + const userId = await getAuthUserId(ctx); |
| 149 | + if (!userId) throw new Error("Not authenticated"); |
| 150 | + |
| 151 | + await ctx.runMutation(components.notifications.lib.send, { |
| 152 | + userId, |
| 153 | + message: args.message, |
| 154 | + }); |
| 155 | + return null; |
| 156 | + }, |
| 157 | +}); |
| 158 | + |
| 159 | +export const myUnread = query({ |
| 160 | + args: {}, |
| 161 | + handler: async (ctx) => { |
| 162 | + const userId = await getAuthUserId(ctx); |
| 163 | + if (!userId) throw new Error("Not authenticated"); |
| 164 | + |
| 165 | + return await ctx.runQuery(components.notifications.lib.listUnread, { |
| 166 | + userId, |
| 167 | + }); |
| 168 | + }, |
| 169 | +}); |
| 170 | +``` |
| 171 | + |
| 172 | +Note the reference path shape: a function in `convex/components/notifications/lib.ts` is called as `components.notifications.lib.send` from the app. |
| 173 | + |
| 174 | +## Critical Rules |
| 175 | + |
| 176 | +- Keep authentication in the app, because `ctx.auth` is not available inside components. |
| 177 | +- Keep environment access in the app, because component functions cannot read `process.env`. |
| 178 | +- Pass parent app IDs across the boundary as strings, because `Id` types become plain strings in the app-facing `ComponentApi`. |
| 179 | +- Do not use `v.id("parentTable")` for app-owned tables inside component args or schema, because the component has no access to the app's table namespace. |
| 180 | +- Import `query`, `mutation`, and `action` from the component's own `./_generated/server`, not the app's generated files. |
| 181 | +- Do not expose component functions directly to clients. Create app wrappers when client access is needed, because components are internal and need auth/env wiring the app provides. |
| 182 | +- If the component defines HTTP handlers, mount the routes in the app's `convex/http.ts`, because components cannot register their own HTTP routes. |
| 183 | +- If the component needs pagination, use `paginator` from `convex-helpers` instead of built-in `.paginate()`, because `.paginate()` does not work across the component boundary. |
| 184 | +- Add `args` and `returns` validators to all public component functions, because the component boundary requires explicit type contracts. |
| 185 | + |
| 186 | +## Patterns |
| 187 | + |
| 188 | +### Authentication and environment access |
| 189 | + |
| 190 | +```ts |
| 191 | +// Bad: component code cannot rely on app auth or env |
| 192 | +const identity = await ctx.auth.getUserIdentity(); |
| 193 | +const apiKey = process.env.OPENAI_API_KEY; |
| 194 | +``` |
| 195 | + |
| 196 | +```ts |
| 197 | +// Good: the app resolves auth and env, then passes explicit values |
| 198 | +const userId = await getAuthUserId(ctx); |
| 199 | +if (!userId) throw new Error("Not authenticated"); |
| 200 | + |
| 201 | +await ctx.runAction(components.translator.translate, { |
| 202 | + userId, |
| 203 | + apiKey: process.env.OPENAI_API_KEY, |
| 204 | + text: args.text, |
| 205 | +}); |
| 206 | +``` |
| 207 | + |
| 208 | +### Client-facing API |
| 209 | + |
| 210 | +```ts |
| 211 | +// Bad: assuming a component function is directly callable by clients |
| 212 | +export const send = components.notifications.send; |
| 213 | +``` |
| 214 | + |
| 215 | +```ts |
| 216 | +// Good: re-export through an app mutation or query |
| 217 | +export const sendNotification = mutation({ |
| 218 | + args: { message: v.string() }, |
| 219 | + returns: v.null(), |
| 220 | + handler: async (ctx, args) => { |
| 221 | + const userId = await getAuthUserId(ctx); |
| 222 | + if (!userId) throw new Error("Not authenticated"); |
| 223 | + |
| 224 | + await ctx.runMutation(components.notifications.lib.send, { |
| 225 | + userId, |
| 226 | + message: args.message, |
| 227 | + }); |
| 228 | + return null; |
| 229 | + }, |
| 230 | +}); |
| 231 | +``` |
| 232 | + |
| 233 | +### IDs across the boundary |
| 234 | + |
| 235 | +```ts |
| 236 | +// Bad: parent app table IDs are not valid component validators |
| 237 | +args: { |
| 238 | + userId: v.id("users"); |
| 239 | +} |
| 240 | +``` |
| 241 | + |
| 242 | +```ts |
| 243 | +// Good: treat parent-owned IDs as strings at the boundary |
| 244 | +args: { |
| 245 | + userId: v.string(); |
| 246 | +} |
| 247 | +``` |
| 248 | + |
| 249 | +### Advanced Patterns |
| 250 | + |
| 251 | +For additional patterns including function handles for callbacks, deriving validators from schema, static configuration with a globals table, and class-based client wrappers, see `references/advanced-patterns.md`. |
| 252 | + |
| 253 | +## Validation |
| 254 | + |
| 255 | +Try validation in this order: |
| 256 | + |
| 257 | +1. `npx convex codegen --component-dir convex/components/<name>` |
| 258 | +2. `npx convex codegen` |
| 259 | +3. `npx convex dev` |
| 260 | + |
| 261 | +Important: |
| 262 | + |
| 263 | +- Fresh repos may fail these commands until `CONVEX_DEPLOYMENT` is configured. |
| 264 | +- Until codegen runs, component-local `./_generated/*` imports and app-side `components.<name>...` references will not typecheck. |
| 265 | +- If validation blocks on Convex login or deployment setup, stop and ask the user for that exact step instead of guessing. |
| 266 | + |
| 267 | +## Reference Files |
| 268 | + |
| 269 | +Read exactly one of these after the user confirms the goal: |
| 270 | + |
| 271 | +- `references/local-components.md` |
| 272 | +- `references/packaged-components.md` |
| 273 | +- `references/hybrid-components.md` |
| 274 | + |
| 275 | +Official docs: [Authoring Components](https://docs.convex.dev/components/authoring) |
| 276 | + |
| 277 | +## Checklist |
| 278 | + |
| 279 | +- [ ] Asked the user what they want to build and confirmed the shape |
| 280 | +- [ ] Read the matching reference file |
| 281 | +- [ ] Confirmed a component is the right abstraction |
| 282 | +- [ ] Planned tables, public API, boundaries, and app wrappers |
| 283 | +- [ ] Component lives under `convex/components/<name>/` (or package layout if publishing) |
| 284 | +- [ ] Component imports from its own `./_generated/server` |
| 285 | +- [ ] Auth, env access, and HTTP routes stay in the app |
| 286 | +- [ ] Parent app IDs cross the boundary as `v.string()` |
| 287 | +- [ ] Public functions have `args` and `returns` validators |
| 288 | +- [ ] Ran `npx convex dev` and fixed codegen or type issues |
0 commit comments