🎓
Custom Resources
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:
Request
Response
1
POST /Entity
2
3
id: UserSetting
4
type: resource
5
isOpen: true
Copied!
1
resourceType: Entity
2
id: UserSetting
3
meta:
4
lastUpdated: '2018-10-16T12:19:51.672Z'
5
versionId: '2'
6
type: resource
7
isOpen: true
Copied!
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} :
Request
Response
1
GET /UserSetting
Copied!
1
resourceType: Bundle
2
type: searchset
3
params: []
4
query-sql: ['SELECT "usersetting".* FROM "usersetting" LIMIT ? OFFSET ?', 100, 0]
5
query-time: 2
6
entry: []
7
total: _undefined
8
link: []
Copied!
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:
1
SELECT * FROM "usersetting";
Copied!
Cool! Now, let's create first UserSetting resource using the REST Console:
Request
Response
1
POST /UserSetting
2
3
id: user-1
4
theme: dark
Copied!
1
resourceType: UserSetting
2
meta:
3
lastUpdated: '2018-10-16T12:33:21.225Z'
4
versionId: '3'
5
id: user-1
6
theme: dark
Copied!
Try to get all user settings now:
Request
Response
1
GET /UserSetting
Copied!
1
resourceType: Bundle
2
type: searchset
3
params: []
4
query-sql: ['SELECT "usersetting".* FROM "usersetting" LIMIT ? OFFSET ?', 100, 0]
5
query-time: 15
6
entry:
7
- resource:
8
theme: dark
9
id: user-1
10
resourceType: UserSetting
11
meta:
12
lastUpdated: '2018-10-16T12:33:21.225Z'
13
versionId: '3'
14
total: 1
15
link: []
Copied!
Or execute the SQL query in the Aidbox.Cloud DB Console:
1
SELECT id, resource->>'theme' as theme FROM "usersetting";
Copied!
id
theme
user-1
dark

CRUD Operations with a Custom Resource

Also you can read, update, and delete UserSetting resource with:
READ Request
READ Response
1
GET /UserSetting/user-1
Copied!
1
id: user-1
2
theme: white
Copied!
UPDATE Request
UPDATE Response
1
PUT /UserSetting/user-1
Copied!
1
theme: white
2
patientsFilters:
3
- location: ICU
Copied!
1
resourceType: UserSetting
2
id: user-1
3
theme: white
4
patientsFilters:
5
- {location: ICU}
Copied!
READ HISTORY Request
READ HISTORY Response
1
GET /UserSetting/user-1/_history
Copied!
1
resourceType: Bundle
2
type: history
3
total: 2
4
entry:
5
- resource:
6
theme: white
7
patientsFilters:
8
- {location: ICU}
9
id: user-1
10
resourceType: UserSetting
11
meta:
12
lastUpdated: '2018-10-16T12:39:41.030Z'
13
versionId: '4'
14
request: {method: PUT, url: UserSetting}
15
- resource:
16
theme: dark
17
id: user-1
18
resourceType: UserSetting
19
meta:
20
lastUpdated: '2018-10-16T12:33:21.225Z'
21
versionId: '3'
22
request: {method: POST, url: UserSetting}
Copied!
DELETE Request
DELETE Response
READ HISTORY Request
READ HISTORY Response
1
DELETE /UserSetting/user-1
Copied!
1
Status 204
Copied!
1
# And again watch history:
2
GET /UserSetting/user-1/_history
Copied!
1
resourceType: Bundle
2
type: history
3
total: 3
4
entry:
5
- resource:
6
theme: white
7
patientsFilters:
8
- {location: ICU}
9
id: user-1
10
resourceType: UserSetting
11
meta:
12
lastUpdated: '2018-10-16T12:42:58.482Z'
13
versionId: '5'
14
request: {method: DELETE, url: UserSetting}
15
- resource:
16
theme: white
17
patientsFilters:
18
- {location: ICU}
19
id: user-1
20
resourceType: UserSetting
21
meta:
22
lastUpdated: '2018-10-16T12:39:41.030Z'
23
versionId: '4'
24
request: {method: PUT, url: UserSetting}
25
- resource:
26
theme: dark
27
id: user-1
28
resourceType: UserSetting
29
meta:
30
lastUpdated: '2018-10-16T12:33:21.225Z'
31
versionId: '3'
32
request: {method: POST, url: UserSetting}
Copied!

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:
Request
Response
1
POST /UserSetting
Copied!
1
id: user-2
2
theme:
3
- name: white
4
- name: black
Copied!
1
resourceType: UserSetting
2
id: user-2
3
theme:
4
- name: white
5
- name: black
Copied!

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:
Request
Response
1
POST /Attribute
Copied!
1
id: UserSetting.theme
2
path: ['theme']
3
type: {id: string, resourceType: Entity}
4
enum: ['dark', 'white']
5
resource: {id: UserSetting, resourceType: Entity}
Copied!
1
resource:
2
id: UserSetting
3
resourceType: Entity
4
id: UserSetting.theme
5
resourceType: Attribute
6
path:
7
- theme
8
type:
9
id: string
10
resourceType: Entity
11
enum:
12
- dark
13
- white
Copied!

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:
Request
Response
1
GET /$json-schema?path=definitions.UserSetting
Copied!
1
path: [definitions, UserSetting]
2
schema:
3
type: object
4
minProperties: 1
5
patternProperties:
6
^(_.*|fhir_.*): {}
7
properties:
8
id: {type: string}
9
extension:
10
type: array
11
items: {$ref: '#/definitions/Extension'}
12
modifierExtension:
13
type: array
14
items: {$ref: '#/definitions/Extension'}
15
meta: {$ref: '#/definitions/Meta'}
16
resourceType: {type: string, constant: UserSetting}
17
theme:
18
type: string
19
enum: [dark, white]
Copied!
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:
Request
Response
Request 2
Response 2
1
POST /UserSetting
Copied!
1
id: user-3
2
theme: 2
Copied!
1
# Response status: 422 Unprocessable Entity
2
3
resourceType: OperationOutcome
4
errors:
5
- path: [theme]
6
message: expected type of string
7
- path: [theme]
8
message: expeceted one of dark, white
9
warnings: []
Copied!
1
PUT /UserSetting
Copied!
1
id: user-4
2
theme: unexisting
Copied!
1
# Response status: 422 Unprocessable Entity
2
3
resourceType: OperationOutcome
4
errors:
5
- path: [theme]
6
message: expected one of dark, white
7
warnings: []
Copied!

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:
Request
Response
1
PATCH /Entity/UserSetting?_type=json-merge-patch
Copied!
1
isOpen: false
Copied!
1
resourceType: Entity
2
type: resource
3
id: UserSetting
4
isOpen: false
Copied!
Now, let's inspect the schema:
Request
Response
1
GET /$json-schema?path=definitions.UserSetting
Copied!
1
path: [definitions, UserSetting]
2
schema:
3
type: object
4
minProperties: 1
5
patternProperties:
6
^(_.*|fhir_.*): {}
7
properties:
8
id: {type: string}
9
extension:
10
type: array
11
items: {$ref: '#/definitions/Extension'}
12
modifierExtension:
13
type: array
14
items: {$ref: '#/definitions/Extension'}
15
meta: {$ref: '#/definitions/Meta'}
16
resourceType: {type: string, constant: UserSetting}
17
theme:
18
type: string
19
enum: [dark, white]
20
additionalProperties: false
Copied!
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:
Request
Response
1
POST /UserSetting
Copied!
1
theme: dark
2
menu: collapsed
Copied!
1
# Response status: 422 Unprocessable Entity
2
3
resourceType: OperationOutcome
4
errors:
5
- path: [menu]
6
message: extra property
7
warnings: []
Copied!
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 modified 6mo ago