Health Data Store Integration Usage Examples
The ByteEngine client now includes full FHIR support using the fhir-kit-client library and official FHIR types for health data stores.
Setup
import { EngineClient, Patient, Bundle } from '@boolbyte/engine';
const client = new EngineClient({
apiKey: 'your-api-key'
});
// Initialize with a server
await client.dataStore.initializeFhirStoreClient('your-server-id');
// Get the underlying fhir-kit-client instance for direct access to all FHIR operations
const fhirClient = client.dataStore.getFhirStoreClient();
Usage Examples
Reading a Patient
const patient: Patient = await fhirClient.read({
resourceType: 'Patient',
id: 'patient-123'
});
Searching for Patients
const patientBundle: Bundle<Patient> = await fhirClient.search({
resourceType: 'Patient',
searchParams: {
family: 'Smith',
gender: 'female'
}
});
Creating a Patient
const newPatient: Patient = {
resourceType: 'Patient',
name: [{
family: 'Doe',
given: ['John']
}],
gender: 'male',
birthDate: '1990-01-01'
};
const createdPatient: Patient = await fhirClient.create({
resource: newPatient
});
Updating a Patient
const updatedPatient: Patient = await fhirClient.update({
resource: patient
});
Deleting a Patient
await fhirClient.delete({
resourceType: 'Patient',
id: 'patient-123'
});
Getting Capability Statement
const capabilities = await fhirClient.capabilityStatement();
Working with Observations
import { Observation } from '@boolbyte/engine';
const observation: Observation = {
resourceType: 'Observation',
status: 'final',
code: {
coding: [{
system: 'http://loinc.org',
code: '33747-0',
display: 'Systolic blood pressure'
}]
},
subject: {
reference: 'Patient/patient-123'
},
valueQuantity: {
value: 120,
unit: 'mmHg'
}
};
const createdObservation = await fhirClient.create({
resource: observation
});
Advanced Examples
Working with Multiple Resource Types
import {
Patient,
Observation,
Condition,
MedicationRequest,
Bundle
} from '@boolbyte/engine';
// Create a patient
const patient: Patient = {
resourceType: 'Patient',
name: [{
family: 'Johnson',
given: ['Jane']
}],
gender: 'female',
birthDate: '1985-03-15'
};
const createdPatient = await fhirClient.create({
resource: patient
});
// Create a condition for the patient
const condition: Condition = {
resourceType: 'Condition',
clinicalStatus: {
coding: [{
system: 'http://terminology.hl7.org/CodeSystem/condition-clinical',
code: 'active'
}]
},
code: {
coding: [{
system: 'http://snomed.info/sct',
code: '44054006',
display: 'Diabetes mellitus type 2'
}]
},
subject: {
reference: `Patient/${createdPatient.id}`
}
};
const createdCondition = await fhirClient.create({
resource: condition
});
// Create a medication request
const medicationRequest: MedicationRequest = {
resourceType: 'MedicationRequest',
status: 'active',
intent: 'order',
medicationCodeableConcept: {
coding: [{
system: 'http://www.nlm.nih.gov/research/umls/rxnorm',
code: '860975',
display: 'Metformin 500 MG Oral Tablet'
}]
},
subject: {
reference: `Patient/${createdPatient.id}`
},
dosageInstruction: [{
text: 'Take 1 tablet twice daily with meals'
}]
};
const createdMedication = await fhirClient.create({
resource: medicationRequest
});
Batch Operations with Bundles
// Create a transaction bundle for multiple operations
const bundle: Bundle = {
resourceType: 'Bundle',
type: 'transaction',
entry: [
{
resource: patient,
request: {
method: 'POST',
url: 'Patient'
}
},
{
resource: observation,
request: {
method: 'POST',
url: 'Observation'
}
},
{
resource: condition,
request: {
method: 'POST',
url: 'Condition'
}
}
]
};
const resultBundle = await fhirClient.transaction({
bundle: bundle
});
Searching with Complex Parameters
// Search for patients with specific criteria
const searchResults = await fhirClient.search({
resourceType: 'Patient',
searchParams: {
'name:contains': 'John',
'birthdate': 'ge1990-01-01',
'gender': 'male',
'_count': '10',
'_sort': 'birthdate'
}
});
// Search for observations with date range
const observations = await fhirClient.search({
resourceType: 'Observation',
searchParams: {
'patient': 'Patient/patient-123',
'date': 'ge2024-01-01&le2024-12-31',
'category': 'vital-signs',
'_include': 'Observation:patient'
}
});
Error Handling
try {
const patient = await fhirClient.read({
resourceType: 'Patient',
id: 'non-existent-id'
});
} catch (error) {
if (error.status === 404) {
console.log('Patient not found');
} else if (error.status === 401) {
console.log('Authentication required');
} else {
console.error('FHIR operation failed:', error);
}
}
Working with History
// Get the history of a patient
const patientHistory = await fhirClient.history({
resourceType: 'Patient',
id: 'patient-123'
});
// Get the history of all patients
const allPatientHistory = await fhirClient.history({
resourceType: 'Patient'
});
Custom Headers and Authentication
// The fhir-kit-client automatically handles authentication
// when initialized through EngineClient.dataStore
// For custom headers, you can configure the client:
const customClient = new EngineClient({
apiKey: 'your-api-key'
});
await customClient.dataStore.initializeFhirStoreClient('your-server-id');
Best Practices
- Always handle errors - FHIR operations can fail for various reasons
- Use proper TypeScript types - Import and use the official FHIR types
- Batch operations when possible - Use bundles for multiple related operations
- Implement proper search parameters - Use FHIR search parameters effectively
- Handle pagination - For large result sets, implement proper pagination
- Validate resources - Ensure your FHIR resources are valid before sending
Related Documentation
- Health Data Store API Overview - Complete API reference
- Health Data Store Introduction - Getting started with health data stores
- Authentication - API key setup and security