fhirbuilder
v1.8.0
Published
Another FHIR Tool to build FHIR Resources
Downloads
283
Maintainers
Readme
Available Resources
Resources available in this package
| Resource | Resource | Resource | | ------------------------------------------------------------------------- | ------------------------------------------------------------- | ----------------------------------------------------------------- | | EpisodeOfCare | Organization | Patient | | Observation | Coverage | ServiceRequest | | AllergyIntolerance | Procedure | Bundle |
Install
npm i fhirbuilder
# for fhirtypes
npm i fhirtypes
Use
Using models
In this example we will use and Address model datatype
const address = new Address({
use: 'home',
type: 'postal',
text: 'test',
line: ['line1', 'line2'],
});
console.log(address);
Output
{
"use": "home",
"type": "postal",
"text": "test",
"line": ["line1", "line2"]
}
Using builders
For complex object you can use a builders
const patient = new PatientBuilder()
.setId('123')
.setActive(true)
.addName(
new HumanName({
family: 'Doe',
given: ['John'],
}),
)
.setGender(AdministrativeGenderEnum.MALE)
.addAddress(
new Address({
use: 'home',
line: ['123 Main St'],
city: 'Somewhere',
state: 'CA',
postalCode: '12345',
country: 'USA',
}),
)
.build();
Output
{
"id": "123",
"resourceType": "Patient",
"active": true,
"name": [
{
"family": "Doe",
"given": ["John"]
}
],
"gender": "male",
"address": [
{
"use": "home",
"line": ["123 Main St"],
"city": "Somewhere",
"state": "CA",
"postalCode": "12345",
"country": "USA"
}
]
}
Using Validators
const identifier = new Identifier({
assigner: {
reference: 'Acme Healthcare', // ❌ invalid reference
},
});
const patient = new Patient({
id: '123',
active: true,
identifier: [identifier],
});
const result = patient.validate();
console.log(result);
Output error
{
"isValid": false,
"operationOutcome": {
"issue": [
{
"code": "invalid",
"details": {
"text": "Path: Patient.identifier[0].assigner.reference. Value: Acme Healthcare"
},
"diagnostics": "Invalid reference format. Reference must be in the format 'ResourceType/ResourceId'.",
"severity": "error"
}
]
}
}
Examples
This document provides examples of how to use the Builders
class to create Practitioner
instances with different levels of detail, based on the FHIR specification.
Practitioner
A simple practitioner with basic information, such as name, contact details, and one qualification.
const practitionerExample1 = new PractitionerBuilder()
.addIdentifier({ system: 'https://example.org', value: '12345' })
.setActive(true)
.addName({ use: 'official', family: 'Doe', given: ['John'] })
.addTelecom({ system: 'phone', value: '+123456789', use: 'work' })
.addAddress({ use: 'home', line: ['123 Main St'], city: 'Metropolis', postalCode: '12345' })
.setGender(AdministrativeGenderType.MALE)
.setBirthDate('1980-01-01')
.addPhoto({ contentType: 'image/jpeg', url: 'https://example.org/photos/practitioner123.jpg' })
.addQualification({
identifier: [{ system: 'https://example.org/licenses', value: 'MD12345' }],
code: { text: 'Medical Doctor' },
})
.addCommunication({ text: 'English' })
.build();
console.log(practitionerExample1);
A practitioner with multiple identifiers, names, contact points, and addresses. This practitioner is also bilingual and has multiple qualifications.
// Creating HumanName instances
const officialName = new HumanName({
use: 'official',
family: 'Doe',
given: ['John'],
});
// Creating ContactPoint instances
const workPhone = new ContactPoint({
system: 'phone',
value: '+123456789',
use: 'work',
});
const practitionerExample2 = new PractitionerBuilder()
.addIdentifier({ system: 'https://examplehospital.org', value: '67890' })
.addIdentifier({ system: 'https://nationalprovider.org', value: '123-456-7890' })
.setActive(true)
.addName(officialName) // Using the HumanName instance
.addName({ use: 'nickname', text: 'Dr. Jane' })
.addTelecom(workPhone) // Using the ContactPoint instance
.addTelecom({ system: 'phone', value: '+0987654321', use: 'mobile' })
.addAddress({ use: 'home', line: ['456 Oak Lane'], city: 'Gotham', postalCode: '54321' })
.addAddress({ use: 'work', line: ['789 Elm Street'], city: 'Metropolis', postalCode: '67890' })
.setGender(AdministrativeGenderEnum.FEMALE)
.setBirthDate('1975-05-15')
.addPhoto({ contentType: 'image/png', url: 'https://example.org/photos/practitioner456.png' })
.addQualification({
identifier: [{ system: 'https://example.org/licenses', value: 'PHD56789' }],
code: { text: 'Psychologist' },
period: { start: '2005-06-01' },
})
.addCommunication({ text: 'English' })
.addCommunication({ text: 'Spanish' })
.build();
console.log(practitionerExample2);