|
| 1 | +This document serves as some special instructions when working with Convex. |
| 2 | + |
| 3 | +# Schemas |
| 4 | + |
| 5 | +When designing the schema please see this page on built in System fields and data types available: https://docs.convex.dev/database/types |
| 6 | + |
| 7 | +Here are some specifics that are often mishandled: |
| 8 | + |
| 9 | +## v (https://docs.convex.dev/api/modules/values#v) |
| 10 | + |
| 11 | +The validator builder. |
| 12 | + |
| 13 | +This builder allows you to build validators for Convex values. |
| 14 | + |
| 15 | +Validators can be used in schema definitions and as input validators for Convex functions. |
| 16 | + |
| 17 | +Type declaration |
| 18 | +Name Type |
| 19 | +id <TableName>(tableName: TableName) => VId<GenericId<TableName>, "required"> |
| 20 | +null () => VNull<null, "required"> |
| 21 | +number () => VFloat64<number, "required"> |
| 22 | +float64 () => VFloat64<number, "required"> |
| 23 | +bigint () => VInt64<bigint, "required"> |
| 24 | +int64 () => VInt64<bigint, "required"> |
| 25 | +boolean () => VBoolean<boolean, "required"> |
| 26 | +string () => VString<string, "required"> |
| 27 | +bytes () => VBytes<ArrayBuffer, "required"> |
| 28 | +literal <T>(literal: T) => VLiteral<T, "required"> |
| 29 | +array <T>(element: T) => VArray<T["type"][], T, "required"> |
| 30 | +object <T>(fields: T) => VObject<Expand<{ [Property in string | number | symbol]?: Exclude<Infer<T[Property]>, undefined> } & { [Property in string | number | symbol]: Infer<T[Property]> }>, T, "required", { [Property in string | number | symbol]: Property | `${Property & string}.${T[Property]["fieldPaths"]}` }[keyof T] & string> |
| 31 | +record <Key, Value>(keys: Key, values: Value) => VRecord<Record<Infer<Key>, Value["type"]>, Key, Value, "required", string> |
| 32 | +union <T>(...members: T) => VUnion<T[number]["type"], T, "required", T[number]["fieldPaths"]> |
| 33 | +any () => VAny<any, "required", string> |
| 34 | +optional <T>(value: T) => VOptional<T> |
| 35 | + |
| 36 | +## System fields (https://docs.convex.dev/database/types#system-fields) |
| 37 | + |
| 38 | +Every document in Convex has two automatically-generated system fields: |
| 39 | + |
| 40 | +_id: The document ID of the document. |
| 41 | +_creationTime: The time this document was created, in milliseconds since the Unix epoch. |
| 42 | + |
| 43 | +You do not need to add indices as these are added automatically. |
| 44 | + |
| 45 | +## Example Schema |
| 46 | + |
| 47 | +This is an example of a well crafted schema. |
| 48 | + |
| 49 | +```ts |
| 50 | +import { defineSchema, defineTable } from "convex/server"; |
| 51 | +import { v } from "convex/values"; |
| 52 | + |
| 53 | +export default defineSchema( |
| 54 | + { |
| 55 | + users: defineTable({ |
| 56 | + name: v.string(), |
| 57 | + }), |
| 58 | + |
| 59 | + sessions: defineTable({ |
| 60 | + userId: v.id("users"), |
| 61 | + sessionId: v.string(), |
| 62 | + }).index("sessionId", ["sessionId"]), |
| 63 | + |
| 64 | + threads: defineTable({ |
| 65 | + uuid: v.string(), |
| 66 | + summary: v.optional(v.string()), |
| 67 | + summarizer: v.optional(v.id("_scheduled_functions")), |
| 68 | + }).index("uuid", ["uuid"]), |
| 69 | + |
| 70 | + messages: defineTable({ |
| 71 | + message: v.string(), |
| 72 | + threadId: v.id("threads"), |
| 73 | + author: v.union( |
| 74 | + v.object({ |
| 75 | + role: v.literal("system"), |
| 76 | + }), |
| 77 | + v.object({ |
| 78 | + role: v.literal("assistant"), |
| 79 | + context: v.array(v.id("messages")), |
| 80 | + model: v.optional(v.string()), |
| 81 | + }), |
| 82 | + v.object({ |
| 83 | + role: v.literal("user"), |
| 84 | + userId: v.id("users"), |
| 85 | + }), |
| 86 | + ), |
| 87 | + }) |
| 88 | + .index("threadId", ["threadId"]), |
| 89 | + }, |
| 90 | +); |
| 91 | +``` |
| 92 | + |
| 93 | +Sourced from: https://github.com/PatrickJS/awesome-cursorrules/blob/main/rules/convex-cursorrules-prompt-file/.cursorrules |
0 commit comments