FhirEngine

interface FhirEngine

Provides an interface for managing FHIR resources in local storage.

The FHIR Engine allows you to create, read, update, and delete (CRUD) FHIR resources, as well as perform searches and synchronize data with a remote FHIR server. The FHIR resources are represented using HAPI FHIR Structures Resource and ResourceType.

To use a FHIR Engine instance, first call FhirEngineProvider.init with a FhirEngineConfiguration. This must be done only once; we recommend doing this in the onCreate() function of your Application class.

class MyApplication : Application() {
override fun onCreate() {
super.onCreate()

FhirEngineProvider.init(
FhirEngineConfiguration(
enableEncryptionIfSupported = true,
RECREATE_AT_OPEN
)
)
}
}

To get a FhirEngine to interact with, use FhirEngineProvider.getInstance:

val fhirEngine = FhirEngineProvider.getInstance(this)

Functions

Link copied to clipboard
abstract suspend fun clearDatabase()

Clears all database tables without resetting the auto-increment value generated by PrimaryKey.autoGenerate.

Link copied to clipboard
abstract suspend fun count(search: Search): Long

Returns the total count of entities available for the given Search.

Link copied to clipboard
inline suspend fun <R : Resource> FhirEngine.count(init: Search.() -> Unit): Long
Link copied to clipboard
abstract suspend fun create(vararg resource: Resource): List<String>

Creates one or more FHIR Resources in the local storage. FHIR Engine requires all stored resources to have a logical Resource.id. If the id is specified in the resource passed to create, the resource created in FhirEngine will have the same id. If no id is specified, FhirEngine will generate a UUID as that resource's id and include it in the returned list of IDs.

Link copied to clipboard
abstract suspend fun delete(type: ResourceType, id: String)

Removes a FHIR resource given its ResourceType and logical ID.

Link copied to clipboard
inline suspend fun <R : Resource> FhirEngine.delete(id: String)

Deletes a FHIR resource of type R with the given id from the local storage.

Link copied to clipboard
abstract suspend fun get(type: ResourceType, id: String): Resource

Loads a FHIR resource given its ResourceType and logical ID.

Link copied to clipboard
inline suspend fun <R : Resource> FhirEngine.get(id: String): R

Retrieves a FHIR resource of type R with the given id from the local storage.

Link copied to clipboard
abstract suspend fun getLastSyncTimeStamp(): OffsetDateTime?

Returns the timestamp when data was last synchronized, or null if no synchronization has occurred yet.

Link copied to clipboard
abstract suspend fun getLocalChanges(type: ResourceType, id: String): List<LocalChange>

Retrieves a list of LocalChanges for the Resource with the given type and ID. This can be used to select resources to purge from the database.

Link copied to clipboard
abstract suspend fun purge(type: ResourceType, id: String, forcePurge: Boolean = false)

Purges a resource from the database without deleting data from the server.

abstract suspend fun purge(type: ResourceType, ids: Set<String>, forcePurge: Boolean = false)

Purges resources of the specified type from the database identified by their IDs without any deletion of data from the server.

Link copied to clipboard
abstract suspend fun <R : Resource> search(search: Search): List<SearchResult<R>>

Searches the database and returns a list of resources matching the Search specifications.

Link copied to clipboard
inline suspend fun <R : Resource> FhirEngine.search(init: Search.() -> Unit): List<SearchResult<R>>

Searches the database and returns a list of resources matching the Search specifications.

suspend fun FhirEngine.search(xFhirQuery: String): List<SearchResult<Resource>>
suspend fun FhirEngine.search(resourceType: ResourceType, init: Search.() -> Unit): List<SearchResult<Resource>>
Link copied to clipboard
abstract suspend fun syncDownload(conflictResolver: ConflictResolver, download: suspend () -> Flow<List<Resource>>)

Synchronizes the download results with the database.

Link copied to clipboard
abstract suspend fun syncUpload(uploadStrategy: UploadStrategy, upload: suspend (List<LocalChange>, List<LocalChangeResourceReference>) -> Flow<UploadRequestResult>): Flow<SyncUploadProgress>

Synchronizes upload results with the database.

Link copied to clipboard
abstract suspend fun update(vararg resource: Resource)

Updates one or more FHIR Resources in the local storage.

Link copied to clipboard
abstract suspend fun withTransaction(block: suspend FhirEngine.() -> Unit)

Adds support for performing actions on FhirEngine as a single atomic transaction where the entire set of changes succeed or fail as a single entity