pathom

:pathom: https://github.com/wilkerlucio/pathom/ & https://pathom3.wsscode.com & https://roamresearch.com/#/app/wsscode
lilactown 2020-11-03T21:57:13.246800Z

I have a data modeling question. let's say I have a bunch of entities, pets and owners. a pet has an owner, and owners have pets. I have some pathom resolvers:

(pc/defresolver pet-resolver
  [{:keys [db]} {:keys [:pet/id]}]
  {::pc/input #{:pet/id}
   ::pc/output [{:pet [:pet/id :pet/name :pet/age ,,,]}]
  (entity db [:pet/id id]))

(pc/defresolver owner-resolver
  [{:keys [db]} {:keys [:owner/id]}]
  {::pc/input #{:owner/id}
   ::pc/output [{:owner [:owner/id :owner/name :owner/age ,,,]}]
  (entity d/b [:owner/id id]))
now, my idea was that: wouldn't it be easy if each pet just had an :owner/id on their entity in our system of record? this would allow to easily nest the :owner resolver within a query for a pet:
(parse [{[:pet/id 123] [:pet/id :pet/name {:owner [:owner/id :owner/name]}]}])
;; => {[:pet/id 123] {:pet/id 123 :pet/name "Spot" :owner {:owner/id 9 :owner/name "lilactown"}}}

lilactown 2020-11-03T21:58:47.248200Z

however, this introduces a problem in my system of record: if I'm using something like datomic/datascript, suddenly I have entities that have a key :owner/id, which are not in fact owners. :owner/id no longer specifies a unique identity of an entity

wilkerlucio 2020-11-04T14:14:24.260600Z

that property is fine, as long as the children entities only link to one owner

2020-11-03T22:05:27.256Z

maybe :pet/owner-id and have an alias resolver to :owner/id ?

1
lilactown 2020-11-03T22:05:52.256200Z

this could lead me to changing this in a couple of different ways. I could have a :pet/owner-id key and a special pet-owner-resolver which takes that key and looks up the owner.

(pc/defresolver pet-owner-resolver
  [{:keys [db] {:keys [:pet/owner-id]}]
  {::pc/input #{:pet/owner-id}
   ::pc/output [,,,]}
  (entity db [:owner/id owner-id]))
However this is a lot of repetitive code, and my system actually has many different kinds of entities all of which can potentially have M:N relationships with each other. another option is to nest the reference: I could have a :pet/owner key which contains a map {:owner/id 9} in my system of record. this way, the key isn't on an entity but rather it will be interpreted as a ref by datomic/datascript. This changes the query a bit:
(parse [{[:pet/id 123] [:pet/id :pet/name {:pet/owner [{:owner [:owner/id :owner/name]}]}]}])
;; => {[:pet/id 123] {:pet/id 123 :pet/name "Spot" :pet/owner {:owner {:owner/id 9 :owner/name "lilactown}}}}
This additional nesting... annoying? Tedious? It feels like wasted complexity

lilactown 2020-11-03T22:06:44.256900Z

> maybe `:pet/owner-id`   and have an alias resolver to `:owner/id` ? that probably is the answer. I guess there are utilities for creating these that remove the boilerplate?

2020-11-03T22:08:28.258Z

yes, it’s easy 🙂 you can create an a single direction alias or a bidirectional alias https://blog.wsscode.com/pathom/v2/pathom/2.2.0/connect/resolvers.html