Convex Integration Skill
This skill guides the implementation of the Convex backend platform.
Core Concepts
- •Database: Defined in
convex/schema.ts. - •Functions:
- •
query: Read-only, reactive. - •
mutation: Write operations. - •
action: 3rd party API calls (non-deterministic).
- •
Directory Structure
- •
convex/- •
schema.ts: Database schema definition. - •
myFunctions.ts: API endpoints. - •
auth.config.ts: Authentication setup (Clerk/Auth0).
- •
Implementation Rules
- •Define Schema First: Always define tables in
schema.tsusingdefineSchemaanddefineTable. - •Type Safety: Use
vfromconvex/valuesto validate arguments. - •Real-time UI: Use the
useQueryhook in React components for automatic updates.
Example Code
Schema (convex/schema.ts)
typescript
import { defineSchema, defineTable } from "convex/server";
import { v } from "convex/values";
export default defineSchema({
tasks: defineTable({
text: v.string(),
isCompleted: v.boolean(),
}),
});
Mutation (convex/todos.ts)
typescript
import { mutation } from "./_generated/server";
import { v } from "convex/values";
export const createTask = mutation({
args: { text: v.string() },
handler: async (ctx, args) => {
await ctx.db.insert("tasks", { text: args.text, isCompleted: false });
},
});