Dima Utkin
Dima Utkin2y ago

indexing a union of object types

but i'm not sure about indexes in this scenario
2 Replies
sujayakar
sujayakar2y ago
it's okay to mix object types with indexes! say you have two documents: {a: {kind: "first", message: "hi"} } and {a: {kind: "second", photo: "<some url>"}}, and you add an index on the a.message field path. then, the index will extract the string "hi" from the first document and undefined from the second one (since the field path a.message isn't present). you can find the second document by passing undefined to q.eq (https://docs.convex.dev/api/interfaces/server.IndexRangeBuilder#eq). this type helper explains a bit more about field paths and undefined handling: https://docs.convex.dev/api/modules/server#fieldtypefromfieldpath note that undefined isn't a valid Convex value (so you can't stick it in the database), but it's a valid value to compare against when accessing a field path that's missing. also cc @Mikael Lirbank ^^
Mikael Lirbank
Sweet!

Did you find this page helpful?