Python
This package provides a wrapper over psycopg2 connection which provides CRUD operations for resources in Fhirbase.

Installation

You can install the package using PyPI:
1
pip install fhirbase
Copied!

Usage

Import fhirbase and psycopg2 libraries:
1
import fhirbase
2
import psycopg2
Copied!
Create a connection using psycopg2.connect:
1
connection = psycopg2.connect(
2
dbname='postgres', user='postgres',
3
host='localhost', port='5432')
Copied!
Create an instance of FHIRBase:
1
fb = fhirbase.FHIRBase(connection)
Copied!
Now you can use the following methods of FHIRBase instance:
    .execute(sql, params=None, commit=False)
    .execute_without_result(sql, params=None, commit=False)
    .row_to_resource(row)
CRUD methods work with FHIR resources. A resource represented as a dict with a specified resourceType key as a required key.The following methods works with a resource and returns resources.
    .create(resource, txid=None, commit=True)
    .update(resource, txid=None, commit=True)
    .delete(resource, txid=None, commit=True) / .delete(resource_type, id, txid=None, commit=True)
    .read(resource)/.read(resource_type, id) .list(sql, params=None)

Methods

.execute

Executes sql with params.
Syntax: .execute(sql, params=None, commit=False)
Returns: context manager with a cursor as context
Example:
1
with fb.execute('SELECT * FROM patient WHERE id=%s', ['id']) as cursor:
2
print(cursor.fetchall())
Copied!

.execute_without_result

Executes sql with params.
Syntax: .execute_without_result(sql, params=None, commit=False)
Returns: nothing
Example:
1
fb.execute_without_result('INSERT INTO transaction (resource) VALUES (%s)', ['{}'])
Copied!

.row_to_resource

Transforms a raw row from DB to resource.
Syntax: .row_to_resource(row)
Returns: resource representation (dict)
Example:
1
fb.row_to_resource({
2
'resource': {'name': []},
3
'ts': 'ts',
4
'txid': 'txid',
5
'resource_type': 'Patient',
6
'meta': {'tag': 'created'},
7
'id': 'id',
8
}))
Copied!
will return a resource representation:
1
{
2
'id': 'id',
3
'meta': {'lastUpdated': 'ts', 'versionId': 'txid'},
4
'name': [],
5
'resourceType': 'Patient',
6
}
Copied!

.create

Creates a resource. If txid is not specified, a new unique logical transaction id will be generated.
Syntax: .create(resource, txid=None, commit=True)
Returns: resource representation (dict)
Example:
1
fb.create({
2
'resourceType': 'Patient',
3
'name': [{'text': 'John'}],
4
})
Copied!
returns:
1
{
2
'resourceType': 'Patient',
3
'id': 'UNIQUE ID',
4
'name': [{'text': 'John'}],
5
'meta': {'lastUpdated': 'timestamp', 'versionId': 'txid'},
6
}
Copied!

.update

Updates a resource. If txid is not specified, a new unique logical transaction id will be generated. Key id is required in resource argument.
Syntax: .update(resource, txid=None, commit=True)
Returns: resource representation (dict)
Example:
1
fb.update({
2
'resourceType': 'Patient',
3
'id': 'id',
4
'name': [{'text': 'John'}],
5
})
Copied!
returns:
1
{
2
'resourceType': 'Patient',
3
'id': 'UNIQUE ID',
4
'name': [{'text': 'John'}],
5
'meta': {'lastUpdated': 'timestamp', 'versionId': 'txid'},
6
}
Copied!

.delete

Deletes a resource. If txid is not specified, a new unique logical transaction id will be generated. Keys id and resourceType are required in resource argument in the first variant of an usage.
Syntax: .delete(resource, txid=None, commit=True) or .delete(resource_type, id, txid=None, commit=True)
Returns: nothing
Example:
1
fb.delete({
2
'resourceType': 'Patient',
3
'id': 'id',
4
})
Copied!
or:
1
fb.delete(resource_type='Patient', id='id')
Copied!

.read

Reads a resource. Keys id and resourceType are required in resource argument in first variant of usage.
Syntax: .read(resource) or .read(resource_type, id)
Returns: resource representation (dict)
Example:
1
fb.read({
2
'resourceType': 'Patient',
3
'id': 'id',
4
})
Copied!
or:
1
fb.read(resource_type='Patient', id='id')
Copied!

.list

Executes SQL and returns an iterator of resources. Note: sql query must return all fields of a resource table.
Syntax: .list(sql, params)
Returns: iterator of resources
Example:
1
for patient in fb.list('SELECT * FROM patient'):
2
print(patient)
Copied!
or:
1
patients = list(fb.list('SELECT * FROM patient'))
Copied!

Example

To run example, just do:
1
git clone https://github.com/fhirbase/fhirbase.py.git
2
cd fhirbase.py
3
4
docker-compose build
5
docker-compose up -d
Copied!
Wait until db starting process will be completed, and run:
1
docker-compose run --rm fhirbase fhirbase init 3.0.1
2
docker-compose run --rm fhirbasepy python examples/example.py
Copied!
Last modified 3yr ago