I have an sqlite database (Chinook, iTunes-like data), for which I have setup a simple Phoenix project.
The birds eye view:
I setup a JsonApi for
Music.Artist. The path
api/json/music/artists works, but
api/json/music/artists/:id errors (
KeyError at GET /api/json/music/artists/5 key :arguments not found in: nil).
And so here are the questions…
- how to set this up (api to get by primary key) correctly?
- how to get by other attributes? (e.g.,
- where to specify the load of calculations/relationships?
- are there public examples of AshJsonApi that I can read/learn from?
Truncated code follows:
defmodule Music do
defmodule TutorialWeb.Music.Router do
# Artist resource
defmodule Music.Artist do
# post :create
attribute :ArtistId, :integer do # the unusual upcase attribute name comes from source db
attribute :Name, :string
defaults [:create, :read, :update, :destroy]
I think you should be able to use
get :read to use the default
:read action and then request an artist by the primary key with
I’m not sure about your other questions but the tests have some pretty good examples for loading related stuff and filtering.
Also, if you would like to abstract away the funky database column naming, you can specify the
source key DSL: Ash.Resource — ash v2.15.19
attribute :id, :integer do
- there error is bad here, but
get :by_id is not pointing at an action on the resource. You want
:by_id is the name of your code interface function that also points at
:read. @dblack is correct.
- you can pass a fiiter statement
- You don’t specify loads in the DSL/actions, you allow the client to specify them using the
include and the
fields parameters. You do need to configure in the resource what relationships can be included. For example:
Would allow using
?include=friends.comments,comments. See the JSON:API spec for more information on how fields and includes are meant to work. https://jsonapi.org/
- I don’t know of any open source implementations of
Thank you @dblack and @zachdaniel. I started with
:read but didn’t realize the error was
api/json/music/artist/5 looking specifically for
:id (and not the primary key). Using
source to map
:id and this is working.
And thank you for pointing to the tests — these are terse and to the point, super clear and easy to learn from.
(4) public example: I now have ducks-in-a-row for a 2nd Primer tutorial. This starts from an sqlite c/ Livebook, passes through identities, validation, pagination, telemetry, bulk create, mermaid diagrams, and end with a minimal Phoenix app showing a json api. The Mix/Phoenix part will probably be a Github repo with sequential commits.
(This would set the stage for the last Primer, that uses AirTable-sync’ed postgres, and have auth / policies / pubsub. The trilogy should give someone new to Ash a three-hour straight path through the most important features, and let y’all answer me once instead of answering the same questions repeatedly )