Custom Resources using Entity

All examples from this tutorial you can run in Postman. Here's the web view of these examples in Postman.

Defining a Custom Resource

Sometimes your data does not fit any existing FHIR resources. It is not always obvious that your data cannot be translated to FHIR because of some FHIR generalizations. The right first step is to go to FHIR comunity chat and ask your specific question about mapping to FHIR, or contact Health Samurai modeling team about your concern. If you are still sure that there is no appropriate resource in FHIR or it takes too much time to wait for it, you can define your own Custom Resources in Aidbox.

Custom Resources are defined exactly the same way as core FHIR resources. They can refer to existing resources, have uniform REST API for CRUD and Search, and participate in transactions.

Let's imagine that in our application we want to store user preferences such as UI configuration or personalized Patient List filters. It is expected that you have already created a box in Aidbox.Cloud. First of all, we have to define a new resource type by creating an Entity resource.

Create Definition​

Access the REST console and paste the following request. You should see the response:

POST /Entity

id: UserSetting
type: resource
isOpen: true

This means that the resource of the type Entity was successfully created. When you create Entity resources with type resource, Aidbox will on the fly initialize a storage for new resource type and generate CRUD & Search REST API.

When you set the isOpen: true flag, this means that the resource does not have any specific structure and that you can store arbitrary data. This is useful when you do not know the exact resource structure, for example, while working on a prototype. Later we will make its schema more strict and will constraint it with additional validations.

API of a Custom Resource

Let's check API for our custom resource UserSetting. You can list UserSetting resources by the standard FHIR URI template GET /{resourceType} :

GET /UserSetting

In the query-sql we see what query is executed by Aidbox to get these resources and can see that the table usersetting was created. You can test it with the DB Console using the following query:

SELECT * FROM "usersetting";

Cool! Now, let's create first UserSetting resource using the REST Console:

POST /UserSetting

id: user-1
theme: dark

Try to get all user settings now:

GET /UserSetting

Or execute the SQL query in the Aidbox.Cloud DB Console:

SELECT id, resource->>'theme' as theme FROM "usersetting";





CRUD Operations with a Custom Resource

Also you can read, update, and delete UserSetting resource with:

GET /UserSetting/user-1
PUT /UserSetting/user-1
theme: white
  - location: ICU
GET /UserSetting/user-1/_history
DELETE /UserSetting/user-1

Refining the Structure of a Custom Resource

Awesome! We've got a nice API by providing a couple of lines of metadata. But the schema of our custom resource is currently too open and users can put any data into UserSetting resource. For example, we can do this:

POST /UserSetting
id: user-2
  - name: white
  - name: black

Describe Structure of Custom Resource

Now, let's put some restrictions and define our Custom Resource structure. To describe structure of a resource, we will use Attribute meta-resource. For example, we want to restrict the theme attribute to be a string value from the specific enumeration:

POST /Attribute
id: UserSetting.theme
path: ['theme']
type: {id: string, resourceType: Entity}
enum: ['dark', 'white']
resource: {id: UserSetting, resourceType: Entity}

Validation of Incoming Resources

To validate incoming resources, Aidbox uses json-schema which is generated from Entity & Attribute meta-resources (read more in Validation Section). Using $json-schema operation we can inspect which schema will be applied to UserSetting resources:

GET /$json-schema?path=definitions.UserSetting

As we see on the line 17 in the response above, the theme property has now type string and is restricted by the enumeration [dark, white].

Let's try to create an invalid resource now:

POST /UserSetting
id: user-3
theme: 2

Restriction of Extra Attributes

We constrained only one attribute and because our Entity.isOpen = true, this resource can have any additional attributes without a schema. We can turn this off by setting Entity.isOpen to false:

PATCH /Entity/UserSetting?_type=json-merge-patch
isOpen: false

Now, let's inspect the schema:

GET /$json-schema?path=definitions.UserSetting

And we see the schema keyword additionalProperties: false (line 20 in the response above) which means that now our schema is closed. Let's test it by the request with additional property menu:

POST /UserSetting
theme: dark
menu: collapsed

In this tutorial you learned how to define and use Custom Resources in Aidbox. In future series we will show you how to add more advanced validations on Custom Resources and create custom endpoints to define your business logic. If you have any questions or suggestions, please provide us with your feedback!

Last updated