Skip to content

Support for polymorphic schema #8

@cyppan

Description

@cyppan

I see two use cases:

Polymorphic joins

You may want fields in your resources where you could embed different types of resources ordered.
e.g:

(def Links ...)
(def Images ...)
(def Articles
  {:schema
    {:name Str
     :attachments [(PolymorphicResourceField [:links :images])]})

Article data in the DB would look like

{:name "coucou"
 :attachments [{:_type "images" :_id "..."} {:_type "links" :_id "..."}]}

And with links joined on fetch

GET /articles?query={ "relations": { "attachments": {} } }
{
  :_items: [{
    :name "coucou"
    :attachments [ { :_id "..." :_type "images" :url ".../img.jpg" :width 900 :height 500 }
                   { :_id "..." :_type "links" :url "...other-article.html" } ]}]}

Polymorphic document parts

Suppose you have a media field that could be of type "image", or "video", or even "html", each of this type can have different field names, types and validations. It would be nice to have a single media entry with different schemas enabled depending on the "type" discriminant.
e.g:

(def Articles
  {:schema
    {:name Str
     :media (cond
              #(= (:type %) "image")
              {:type (Enum "image") :width Int height Int :url UrlField}
              #(= (:type %) "html")
              {:type (Enum "html") :url UrlField (? :encoding) Str})}})

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions