Fhirpath.pg

Why fhirpath?

To search though jsonb resources in database sometimes you need to access deeply nested attributes by specific criteria. For example to search patient by official name you have to do something like this:
1
SELECT id, nm
2
FROM (
3
SELECT id as id, jsonb_array_elements(resource->'name') AS nm
4
FROM patient
5
) _
6
WHERE nm->>'use' = 'official' and nm->>'family' ilike '%Chalmers%'
7
LIMIT 10;
8
Copied!
Here is the version with fhirpath:
1
SELECT id, resource->'name'
2
FROM patient
3
WHERE
4
fhirpath(resource,$name.where(use='official').family$)->>0 ilike 'Chal%';
5
Copied!

Implement FHIR search with fhirpath

Having fhirpath in your database also is very handy to implement FHIR Search API. In FHIR SearchParameters are described by fhirpath expressions. For example for Patient we have following search parameters:
Name
Type
Expression
active
token
Patient.active
address
string
Patient.address
address-city
string
Patient.address.city
address-country
string
Patient.address.country
address-postalcode
string
Patient.address.postalCode
address-state
string
Patient.address.state
address-use
token
Patient.address.use
animal-breed
token
Patient.animal.breed
animal-species
token
Patient.animal.species
birthdate
date
Patient.birthDate
death-date
date
Patient.deceased.as(DateTime)
deceased
token
Patient.deceased.exists()
email
token
Patient.telecom.where(system='email')
family
string
Patient.name.family
gender
token
Patient.gender
general-practitioner
reference
Patient.generalPractitioner (Practitioner, Organization)
given
string
Patient.name.given
identifier
token
Patient.identifier
language
token
Patient.communication.language
link
reference
Patient.link.other (Patient, RelatedPerson)
name
string
Patient.name
organization
reference
Patient.managingOrganization (Organization)
phone
token
Patient.telecom.where(system='phone')
phonetic
string
Patient.name
telecom
token
Patient.telecom
With FHIR search there is one tricky moment - most of expressions return complex types like HumanName or Identifier, which is not easy to search by SQL. So fhirpath provides specialised function to output search friendly results:
1
SELECT fhirsearch_string(resource, $Patient.name.where(user='official')$, 'HumanName')
2
-- '^John$ ^Doe$ ^Johny#x27;
3
4
-- so to search for startWith we can do
5
SELECT * from patient
6
WHERE fhirsearch_string(...) ilike '%^Joh%'
Copied!

Invariants with fhirpath.pg

With fhirpath we can create invariants as PostgreSQL Constraints. Let's add a check that all patients have SSN number:
1
-- clear patient table if you have somthing invalid in
2
truncate patient;
3
4
ALTER TABLE patient
5
ADD CONSTRAINT patient_ssn
6
CHECK (
7
(
8
fhirpath(
9
resource,
10
$identifier.where(system = 'ssn').value.exists()$
11
)->>0
12
)::boolean
13
);
Copied!
Now when you will try to add patient without SSN - you will see an ERROR:
1
INSERT INTO patient (resource)
2
VALUES ($
3
{
4
"resourceType":"Patient",
5
"id":"example",
6
"identifier": [{"value": "777777"}]}
7
$);
8
-- ERROR: new row for relation "patient" violates check constraint "patient_ssn"
9
--DETAIL: Failing row contains ({"id": "example", "identifier": [{"value": "777777"}], "resource...).
10
11
-- but this will work
12
INSERT INTO patient (resource)
13
VALUES ($
14
{
15
"resourceType":"Patient",
16
"id":"example",
17
"identifier": [{"system": "ssn", "value": "777777"}]
18
}
19
$);
20
-- INSERT 0 1
Copied!
Last modified 3yr ago