Open History to see the last commit
1This document serves as some special instructions when working with Convex.
2
3# Schemas
4
5When designing the schema please see this page on built in System fields and data types available: https://docs.convex.dev/database/types
6
7Here are some specifics that are often mishandled:
8
9## v (https://docs.convex.dev/api/modules/values#v)
10
11The validator builder.
12
13This builder allows you to build validators for Convex values.
14
15Validators can be used in schema definitions and as input validators for Convex functions.
16
17Type declaration
18Name Type
19id <TableName>(tableName: TableName) => VId<GenericId<TableName>, "required">
20null () => VNull<null, "required">
21number () => VFloat64<number, "required">
22float64 () => VFloat64<number, "required">
23bigint () => VInt64<bigint, "required">
24int64 () => VInt64<bigint, "required">
25boolean () => VBoolean<boolean, "required">
26string () => VString<string, "required">
27bytes () => VBytes<ArrayBuffer, "required">
28literal <T>(literal: T) => VLiteral<T, "required">
29array <T>(element: T) => VArray<T["type"][], T, "required">
30object <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>
31record <Key, Value>(keys: Key, values: Value) => VRecord<Record<Infer<Key>, Value["type"]>, Key, Value, "required", string>
32union <T>(...members: T) => VUnion<T[number]["type"], T, "required", T[number]["fieldPaths"]>
33any () => VAny<any, "required", string>
34optional <T>(value: T) => VOptional<T>
35
36## System fields (https://docs.convex.dev/database/types#system-fields)
37
38Every 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
43You do not need to add indices as these are added automatically.
44
45## Example Schema
46
47This is an example of a well crafted schema.
48
49```ts
50import { defineSchema, defineTable } from "convex/server";
51import { v } from "convex/values";
52
53export default defineSchema(
54 {
55 users: defineTable({
56 name: v.string(),
57 }),
59 sessions: defineTable({
60 userId: v.id("users"),
61 sessionId: v.string(),
62 }).index("sessionId", ["sessionId"]),
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
93Sourced from: https://github.com/PatrickJS/awesome-cursorrules/blob/main/rules/convex-cursorrules-prompt-file/.cursorrules