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:
Usage
Import fhirbase
and psycopg2
libraries:
Create a connection using psycopg2.connect
:
Create an instance of FHIRBase
:
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:
.execute_without_result
Executes sql with params.
Syntax: .execute_without_result(sql, params=None, commit=False)
Returns: nothing
Example:
.row_to_resource
Transforms a raw row from DB to resource.
Syntax: .row_to_resource(row)
Returns: resource representation (dict)
Example:
will return a resource representation:
.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:
returns:
.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:
returns:
.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:
or:
.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:
or:
.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:
or:
Example
To run example, just do:
Wait until db starting process will be completed, and run:
Last updated