Fhirbase连接器
fhirbase的Python项目详细描述
fhirbase.py
用于python的firbase连接器。 这个包提供了psycopg2连接的包装器 为fhirbase中的资源提供crud。
安装
pip install fhirbase
用法
导入fhirbase
和psycopg2
库:
import fhirbase
import psycopg2
使用psycopg2.connect
;创建连接
connection = psycopg2.connect(
dbname='postgres', user='postgres',
host='localhost', port='5432')
创建FHIRBase
:
fb = fhribase.FHIRBase(connection)
现在您可以使用FHIRBase
实例的以下方法:
.execute(sql, params=None, commit=False)
.execute_without_result(sql, params=None, commit=False)
.row_to_resource(row)
crud方法与FHIR resources一起工作。
用指定的resourceType
键作为所需键表示为dict的资源。
以下方法用于资源并返回资源。
.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)
方法
。执行
使用参数执行SQL。
语法:.execute(sql, params=None, commit=False)
返回:将光标作为上下文的上下文管理器
示例:
with fb.execute('SELECT * FROM patient WHERE id=%s', ['id']) as cursor:
print(cursor.fetchall())
在没有结果的情况下执行
使用参数执行SQL。
语法:.execute_without_result(sql, params=None, commit=False)
返回:无
示例:
fb.execute_without_result('INSERT INTO transaction (resource) VALUES (%s)', ['{}'])
.row_to_resource
将原始行从数据库转换为资源。
语法:.row_to_resource(row)
返回:资源表示(dict)
示例:
fb.row_to_resource({
'resource': {'name': []},
'ts': 'ts',
'txid': 'txid',
'resource_type': 'Patient',
'meta': {'tag': 'created'},
'id': 'id',
}))
将返回资源表示:
{
'id': 'id',
'meta': {'lastUpdated': 'ts', 'versionId': 'txid'},
'name': [],
'resourceType': 'Patient',
}
.create
创建资源。 如果未指定TxID,则将生成新的唯一逻辑事务ID。
语法:.create(resource, txid=None, commit=True)
返回:资源表示(dict)
示例:
fb.create({
'resourceType': 'Patient',
'name': [{'text': 'John'}],
})
返回
{
'resourceType': 'Patient',
'id': 'UNIQUE ID',
'name': [{'text': 'John'}],
'meta': {'lastUpdated': 'timestamp', 'versionId': 'txid'},
}
.update
更新资源。 如果未指定TxID,则将生成新的唯一逻辑事务ID。
参数resource
中需要键id
。
语法:.update(resource, txid=None, commit=True)
返回:资源表示(dict)
示例:
fb.update({
'resourceType': 'Patient',
'id': 'id',
'name': [{'text': 'John'}],
})
返回
{
'resourceType': 'Patient',
'id': 'UNIQUE ID',
'name': [{'text': 'John'}],
'meta': {'lastUpdated': 'timestamp', 'versionId': 'txid'},
}
.delete
删除资源。
如果未指定TxID,则将生成新的唯一逻辑事务ID。
在第一个用法变体中,resource
参数中需要键id
和resourceType
。
语法:.delete(resource, txid=None, commit=True)
或.delete(resource_type, id, txid=None, commit=True)
返回:无
示例:
fb.delete({
'resourceType': 'Patient',
'id': 'id',
})
或
fb.delete(resource_type='Patient', id='id')
.read
读取资源。
在第一个用法变体中,resource
参数中需要键id
和resourceType
。
语法:.read(resource)
或.read(resource_type, id)
返回:资源表示(dict)
示例:
fb.read({
'resourceType': 'Patient',
'id': 'id',
})
或
fb.read(resource_type='Patient', id='id')
.list
执行SQL并返回资源的迭代器。 注意:SQL查询必须返回资源表的所有字段。
语法:.list(sql, params)
返回:资源的迭代器
示例:
for patient in fb.list('SELECT * FROM patient'):
print(patient)
或
patients = list(fb.list('SELECT * FROM patient'))
示例应用程序
要运行示例,请执行以下操作:
docker-compose build
docker-compose up -d
等待数据库启动过程完成,然后运行:
docker-compose run --rm fhirbase fhirbase init 3.0.1
docker-compose run --rm fhirbasepy python examples/example.py