mikeysee
mikeysee•16mo ago

More complex indexes

It would be awesome if at some point Convex could support PostgresJSON-like index's so we could index deeply within objects. Im thinking something like the following:
defineSchema({
users: defineTable({
name: v.string(),
email: v.string(),
status: v.union(
v.object({
kind: v.literal("not_verified"),
createdAt: v.number(),
}),
v.object({
kind: v.literal("verified"),
createdAt: v.number(),
}),
),
})
.index("by_status_kind", o => o.status.kind)
});
defineSchema({
users: defineTable({
name: v.string(),
email: v.string(),
status: v.union(
v.object({
kind: v.literal("not_verified"),
createdAt: v.number(),
}),
v.object({
kind: v.literal("verified"),
createdAt: v.number(),
}),
),
})
.index("by_status_kind", o => o.status.kind)
});
3 Replies
lee
lee•16mo ago
this should work, but your syntax is slightly off. it's .index("by_status_kind", ['status.kind']) . here's an example in the wild https://github.com/get-convex/ai-town/blob/fe26a3ee5c40dd3a838bd4be7831a55eeb4aa210/convex/schema.ts#L278
GitHub
ai-town/convex/schema.ts at fe26a3ee5c40dd3a838bd4be7831a55eeb4aa21...
A MIT-licensed, deployable starter kit for building and customizing your own version of AI town - a virtual town where AI characters live, chat and socialize. - get-convex/ai-town
mikeysee
mikeyseeOP•16mo ago
oh jeeze! I apologise! I didnt realise we could do this already. You guys are great 😄
lee
lee•16mo ago
no worries. not at all obvious, and it's kind of a power-user feature anyway 🙂