How to model the union-type in MongoDB

Viewed 113

What would be the proper structure for the following?

@dataclass
class _AbstractNamespace:
    id: str # GLOBAL_SCOPE | CUSTOM_SCOPE::UserID+Name | SCOPE_TYPE::ID
    created_at: datetime
    scope: ENUM_SCOPE
    oneof: CustomNamespace | WorkbookNamespace | WorkbookNamespace | UserNamespace | SourceNamespace | GlobalScope

@dataclass
class CustomNamespace(_AbstractNamespace):
    name: str
    description: str
    created_by_id: int

@dataclass
class WorkbookNamespace(_AbstractNamespace):
    workbook_id: int

@dataclass
class WorksheetNamespace(_AbstractNamespace):
    worksheet_id: int

@dataclass
class UserNamespace(_AbstractNamespace):
    user_id: int

@dataclass
class SourceNamespace(_AbstractNamespace):
    source_id: int

@dataclass
class GlobalScope(_AbstractNamespace):
    pass

In other words, I would like to have a schema that validates the following:

Obj:

  • id
  • created_at
  • scope
  • oneOf(object1 | object2 | ...)

What would be the proper way to do that?

0 Answers
Related