Tom RedmanT
Convex Community13mo ago
14 replies
Tom Redman

Is this typically a bad practice? v.union(v.id("thisList"), v.id("thatList"))

I have two different types of lists that a user can select to take a singular action on (in my case, send emails).

I have a list of contacts that is a "segment", or a user can select the entire "list". A list is different from a segment, but the same action can be taken on both for all practical purposes.

I have my schema like this:
audience: v.union(v.id("mailchimpLists"), v.id("mailchimpSegments")),

Butttttt I'm immediately realizing that disambiguating this in a function seems hacky at best.

Is it better to do this, and then simply manage what's what further upstream?

mailchimpSegment: v.optional(v.id("mailchimpSegments")),
mailchimpList: v.optional(v.id("mailchimpLists")),


This is fine as well, however, I find myself wanting to enforce that there must be one but not both.

I could do something like this:
audienceType: v.union(
  v.literal("segment"),
  v.literal("list"),
),
mailchimpSegment: v.optional(v.id("mailchimpSegments")),
mailchimpList: v.optional(v.id("mailchimpLists")),


But this still feels brittle.

I'm leaning toward the audienceType option, but would love to know if there's a better way, or if I'm missing something obvious with the v.union(v.id,v.id) idea.
Was this page helpful?