In a previous blog post we set our first steps in mu-cl-resources. A microservice that generates a JSONAPI compliant API for your resources based on a simple configuration describing the domain. We defined a model for a book and got the endpoints to create, update, list and delete books for free. In this article we will explain how to add relationships to a model.
The author model
Each book is written by (at least one) an author. An author isn’t a regular property of a book like for example the book’s title. It is a resource on its own. An author has its own properties like a name, a birth date etc. And it is related to a book. Before we can define the relationship between books and authors, we first need to specify the model of an author.
The definition of the model is very similar to that of the book. Add the following lines to your domain.lisp:
(define-resource author () :class (s-prefix "schema:Author") :properties `((:name :string ,(s-prefix "schema:name"))) :resource-base (s-url "http://mu.semte.ch/services/github/madnificent/book-service/authors/") :on-path "authors")
Expose the author endpoints in the dispatcher.ex configuration:
match "/authors/*path" do Proxy.forward conn, path, "http://resource/authors/" end
Defining relationships
Now that the author model is added, we can define the relationship between a book and an author. Let’s suppose a one-to-many relationship. A book has one author and an author may have written multiple books.
First extend the book’s model:
(define-resource book () :class (s-prefix "schema:Book") :properties `((:title :string ,(s-prefix "schema:headline")) (:isbn :string ,(s-prefix "schema:isbn")) (:publication-date :date ,(s-prefix "schema:datePublished")) (:genre :string ,(s-prefix "schema:genre")) (:language :string ,(s-prefix "schema:inLanguage")) (:number-of-pages :integer ,(s-prefix "schema:numberOfPages"))) :has-one `((author :via ,(s-prefix "schema:author") :as "author")) :resource-base (s-url "http://mu.semte.ch/services/github/madnificent/book-service/books/") :on-path "books")
Adding an author to a book will now result in the following triple in the store:
<http://mu.semte.ch/services/github/madnificent/book-service/books/182232da-e07d-438f-8608-f6356624a666> schema:author <http://mu.semte.ch/services/github/madnificent/book-service/authors/e6c446ad-36ee-43b3-a8a0-2349ecfdcb5d> .
The :as “author” portion of the definition specifies the path on which the relationship will be exposed in the JSON representation of a book.
{ "attributes": { "title": "Rock & Roll with Ember" }, "id": "620f7a1c-9d31-4b8a-a627-2eb6904fe1f3", "type": "books", "relationships": { "author": { "links": { "self": "/books/620f7a1c-9d31-4b8a-a627-2eb6904fe1f3/links/author", "related": "/books/620f7a1c-9d31-4b8a-a627-2eb6904fe1f3/author" } } } }
Next, add the inverse relationship to the author’s model:
(define-resource author () :class (s-prefix "schema:Author") :properties `((:name :string ,(s-prefix "schema:name"))) :has-many `((book :via (s-prefix "schema:author") :inverse t :as "books")) :resource-base (s-url "http://mu.semte.ch/services/github/madnificent/book-service/authors/") :on-path "authors")
The ‘:inverse t’ indicates that the relationship from author to books is the inverse relation. As you can see the the relationship’s path “books” is in plural since it’s a has-many relation in this case.
If you want to define a many-to-many relationships between books and authors, just change the :has-one to :has-many and pluralize the “author” path to “authors” in the book’s model. Don’t forget to restart your microservice if you’ve updated the model.
Conclusion
Once the microservice is restarted, you can fetch and update the relationships as specified by jsonapi.org. The generated API also supports the include query parameter to include related resources in the response when fetching one or more resource. That’s a lot you get for just a few lines of code, isn’t it?