denn0x5484
denn0x548411mo ago

Enum

hellow how do I use enum in convex?
1 Reply
lee
lee11mo ago
This is what i would suggest (btw there's a chatbot at the top of https://docs.convex.dev which can help with questions): In Convex, you can write a schema with a discriminated union type for documents usingv.union at the top level. You can separate different types of documents by assigning them distinct kind values. Here is an example:
import { defineSchema, defineTable } from "convex/server";
import { v } from "convex/values";

export default defineSchema({
documents: defineTable(
v.union(
v.object({
kind: v.literal("StringDocument"),
value: v.string(),
}),
v.object({
kind: v.literal("NumberDocument"),
value: v.number(),
})
)
),
});
import { defineSchema, defineTable } from "convex/server";
import { v } from "convex/values";

export default defineSchema({
documents: defineTable(
v.union(
v.object({
kind: v.literal("StringDocument"),
value: v.string(),
}),
v.object({
kind: v.literal("NumberDocument"),
value: v.number(),
})
)
),
});
In this schema, documents either have a kind of "StringDocument" and a string for their value or they have a kind of "NumberDocument" and a number for their value.

Did you find this page helpful?