How to build a Book Store api with a data handler in 5 mins

Taleh Ibrahimli
2 min readApr 17

--

We want to create Author and Book APIs.

With help of Data Handler, we can simply do it.

schema.yml

type: resource
name: author
properties:
- name: name # name property of author
type: STRING
length: 255
unique: true
required: true
- name: description # description property of author
type: STRING
length: 255
unique: true
required: true
---
type: resource
name: book
properties:
- name: name # name property of book
type: STRING
length: 255
unique: true
required: true
- name: description # description property of book
type: STRING
length: 255
unique: true
required: true
- name: author # author property of book, it is referenced to author
type: REFERENCE
reference:
referencedResource: author # reference to author
- name: isbn # isbn property of book
type: STRING
length: 255
unique: true
required: true
dhctl apply -f schema.yml

And we have CRUD APIs for the book and author, it is also documented by Swagger(openapi 3.0).

Let’s create an author with our brand new Crud API

curl -X 'POST' \
'http://localhost:9009/author' \
-H 'accept: application/json' \
-H 'Authorization: Bearer <token>' \
-H 'Content-Type: application/json' \
-d '{
"name": "Author name",
"description": "Author description"
}'

## Result will be
{
"description": "Author description",
"id": "831fecd2-dd0d-11ed-b194-b29c4ac91271",
"name": "Author name",
"version": 1
}

Now, Let’s create a book with reference to author

curl -X 'POST' \
'http://localhost:9009/book' \
-H 'accept: application/json' \
-H 'Authorization: Bearer <token>' \
-H 'Content-Type: application/json' \
-d '{
"author": {
"id": "831fecd2-dd0d-11ed-b194-b29c4ac91271"
},
"description": "Book-1",
"name": "Book-1",
"isbn": "isbn-1"
}'

## It will create author for you

{
"author": {
"id": "831fecd2-dd0d-11ed-b194-b29c4ac91271"
},
"description": "Book-1",
"id": "f2a07b9e-dd0d-11ed-b194-b29c4ac91271",
"isbn": "isbn-1",
"name": "Book-1",
"version": 1
}

See data handler documentation here

Installation docs for Data handler and dhctl client

Repository: link

--

--