- Notifications
You must be signed in to change notification settings - Fork4
FHIRBase connector for python
License
fhirbase/fhirbase.py
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
FHIRBase connector for python.This package provides a wrapper over psycopg2 connection whichprovides CRUD operations for resources in fhirbase.
pip install fhirbase
Importfhirbase
andpsycopg2
libraries:
import fhirbaseimport psycopg2
Create a connection usingpsycopg2.connect
:
connection = psycopg2.connect( dbname='postgres', user='postgres', host='localhost', port='5432')
Create an instance ofFHIRBase
:
fb = fhirbase.FHIRBase(connection)
Now you can use the following methods ofFHIRBase
instance:
.execute(sql, params=None, commit=False)
.execute_without_result(sql, params=None, commit=False)
.row_to_resource(row)
CRUD methods work withFHIR resources.A resource represented as a dict with a specifiedresourceType
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)
Executes sql with params.
Syntax:.execute(sql, params=None, commit=False)
Returns: context manager with a cursor as context
Example:
with fb.execute('SELECT * FROM patient WHERE id=%s', ['id']) as cursor: print(cursor.fetchall())
Executes sql with params.
Syntax:.execute_without_result(sql, params=None, commit=False)
Returns: nothing
Example:
fb.execute_without_result('INSERT INTO transaction (resource) VALUES (%s)', ['{}'])
Transforms a raw row from DB to a resource.
Syntax:.row_to_resource(row)
Returns: resource representation (dict)
Example:
fb.row_to_resource({ 'resource': {'name': []}, 'ts': 'ts', 'txid': 'txid', 'resource_type': 'Patient', 'meta': {'tag': 'created'}, 'id': 'id',}))
will return a resource representation:
{ 'id': 'id', 'meta': {'lastUpdated': 'ts', 'versionId': 'txid'}, 'name': [], 'resourceType': 'Patient',}
Creates a resource.Iftxid
is not specified, a new unique logical transaction id will be generated.
Syntax:.create(resource, txid=None, commit=True)
Returns: resource representation (dict)
Example:
fb.create({ 'resourceType': 'Patient', 'name': [{'text': 'John'}],})
returns
{ 'resourceType': 'Patient', 'id': 'UNIQUE ID', 'name': [{'text': 'John'}], 'meta': {'lastUpdated': 'timestamp', 'versionId': 'txid'},}
Updates a resource.If txid is not specified, a new unique logical transaction id will be generated.
Keyid
is required inresource
argument.
Syntax:.update(resource, txid=None, commit=True)
Returns: resource representation (dict)
Example:
fb.update({ 'resourceType': 'Patient', 'id': 'id', 'name': [{'text': 'John'}],})
returns
{ 'resourceType': 'Patient', 'id': 'UNIQUE ID', 'name': [{'text': 'John'}], 'meta': {'lastUpdated': 'timestamp', 'versionId': 'txid'},}
Deletes a resource.If txid is not specified, a new unique logical transaction id will be generated.Keysid
andresourceType
are required inresource
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:
fb.delete({ 'resourceType': 'Patient', 'id': 'id',})
or
fb.delete(resource_type='Patient', id='id')
Reads a resource.Keysid
andresourceType
are required inresource
argument in first variant of usage.
Syntax:.read(resource)
or.read(resource_type, id)
Returns: resource representation (dict)
Example:
fb.read({ 'resourceType': 'Patient', 'id': 'id',})
or
fb.read(resource_type='Patient', id='id')
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:
for patient in fb.list('SELECT * FROM patient'): print(patient)
or
patients = list(fb.list('SELECT * FROM patient'))
To run example, just do:
docker-compose builddocker-compose up -d
Wait until db starting process will be completed, and run:
docker-compose run --rm fhirbase fhirbase init 3.0.1docker-compose run --rm fhirbasepy python examples/example.py