grape
grape copied to clipboard
Support for polymorphic schema
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})}})