sbennett33

sbennett33

Need help adding relationship support to Sanity DataLayer

I’m working on creating a DataLayer for the Sanity CMS. So far I have support for reading from the API and basic attributes on a resource.

AshSanity

Right now I’m trying to figure out how to support relationships on resources. When I call Post |> Ash.Query.load(:comments) |> Api.read!() I was expecting something to be called in the DataLayer module to either run another query or load the related resources but nothing is happening that I can tell. It looks like the query is run to load the Post resource but that’s it.

What should I expect to see happen at the DataLayer level when loading a related resource?

Testing Loading Relationships

First Post!

zachdaniel

zachdaniel

Creator of Ash

:wave: For loading related data, data layers are not made aware that is loading a relationship, but rather a filter is passed down to the relationship based on the source data. So if there are no posts, no query is made to fetch the comments. You’d get a filter like post_id in [....] to your data layer for the comments query.

Most Liked

sbennett33

sbennett33

Thanks Zach! That makes sense. In my test I wasn’t returning any posts in the response which is why I wasn’t seeing the subsequent request for the comments.

So, this way of handling relationships makes total sense for a relational database model where the resource on the “belongs to” side of a “has many” relationship has a foreign key pointing back at the parent record. Unfortunately Sanity stores their data in a NoSQL data lake where the parent resource keeps a reference to all the related resources so when you query for a list of posts you get something back like the following:

[
  {
    _id: "7d10669d-7e9f-4c8c-a7db-e1ea369e7055",
    _type: "post",
    title: "My First Post",
    body: "A very thought provoking post",
    comments: [
      {
        _key: "13ad3eba8563",
        _type: "reference",
        _ref: "1ccb9a37-c0f2-4aa7-82cb-3e16e6ecab99"
      },
      {
        _key: "d8c1e6a53174",
        _type: "reference",
        _ref: "fff10398-7cb1-4d9f-8dfd-42d0f8aca798"
      },
      {
        _key: "6faf4ddeef52",
        _type: "reference",
        _ref: "a7e06b12-561e-4ad8-9756-cd1979e754be"
      },
    ]
  },
  … (more posts)
]

If you want to load/dereference the comments you would do so when you query for the Posts or you’d have to look at the IDs in the comments array then do a query like id in [...] on the comment resource. I’ve been trying to figure out what the best way would be to model something like that using Ash resources and at this point I have two thoughts:

  1. Create a custom ref type for the comments attribute. Something like:
attributes do
  attribute :comments, {:array, ref: Comment}
end

I’d then need to handle that attribute type when casting attributes in my DataLayer which probably isn’t ideal.

  1. Create a new references block on resources that use AshSanity which could look something like:
references do
  has_many :comments, Comment
end

This approach feels like the more idiomatic way to do it but may involve touching a lot of plumbing and figuring out how to add support to Ash.Query so it knows how to load those references.

It may also just be the case that the Ash DataLayer model isn’t a good fit for modeling a NoSQL data store and I’m just totally barking up the wrong tree.

Would love to get your thoughts.

zachdaniel

zachdaniel

Creator of Ash

Interesting. So you could do this today with calculations or manual relationships (where your tool provides the implementatiton). However, it would be good to generalize this. The goal in Ash is to act as a superset of multiple kinds of storage layers. I think we should consider adding new ttype sof relationships. Specifically references_many, where the list of ids are stored somewhere on the source record. Thoughts?

sbennett33

sbennett33

Nice. I like the idea of adding something like references_many to handle cases like this. I wouldn’t want the implementation to be tied to the shape of the data coming back from Sanity though. We’d probably want to pass the responsibility of managing the relationship down to the DataLayer somehow. For example Sanity returns what is basically an array of reference objects which contain the ID of the referenced document under the _ref key whereas other databases may return just a list of IDs or have the ID under a different key.

I’d be more than happy to help contribute to adding support for this. In the meantime I’ll take a look at how this might look using calculations or manual relationships as a stopgap until there’s builtin support at the framework level.

Last Post!

zachdaniel

zachdaniel

Creator of Ash

Well, technically belongs_to is references_one. We could potentially make it part of the data layer callbacks to provide a default implementation of references_many, or something along those lines. But we’d also want to allow it to be overridden.

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

Other popular topics Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42716 114
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement