Skip to main content

dhis2@6.0.1

attr(attribute, value)
create(resourceType, data, [options], [callback])
destroy(resourceType, path, [data], [options], [callback])
discover(httpMethod, endpoint)
dv(dataElement, value)
findAttributeValue(trackedEntity, attributeDisplayName)
get(resourceType, query, [options], [callback])
patch(resourceType, path, data, [options], [callback])
post(resourceType, data, [options], [callback])
update(resourceType, path, data, [options], [callback])
upsert(resourceType, query, data, [options], [callback])

This adaptor exports the following from common:

alterState()
cursor()
dataPath()
dataValue()
dateFns
each()
field()
fields()
fn()
fnIf()
group()
http
lastReferenceValue()
merge()
sourceValue()

Functions

attr

attr(attribute, value) ⇒ object

Converts an attribute ID and value into a DHIS2 attribute object

ParamTypeDescription
attributestringA tracked entity instance (TEI) attribute ID.
valuestringThe value for that attribute.

Example

attr('w75KJ2mc4zz', 'Elias')

create

create(resourceType, data, [options], [callback]) ⇒ Operation

Create a record

ParamTypeDescription
resourceTypestringType of resource to create. E.g. trackedEntities, programs, events, ...
dataDhis2DataObject which defines data that will be used to create a given instance of resource. To create a single instance of a resource, data must be a javascript object, and to create multiple instances of a resources, data must be an array of javascript objects.
[options]ObjectOptional options to define URL parameters via params (E.g. filter, dimension and other import parameters), request config (E.g. auth) and the DHIS2 apiVersion.
[callback]functionOptional callback to handle the response

Example: Create a program

create('programs', {
name: 'name 20',
shortName: 'n20',
programType: 'WITHOUT_REGISTRATION',
});

Example: Create a single event

create('events', {
program: 'eBAyeGv0exc',
orgUnit: 'DiszpKrYNg8',
status: 'COMPLETED',
});

Example: Create a single tracker entity. See Create tracker docs

create('trackedEntities', {
orgUnit: 'TSyzvBiovKh',
trackedEntityType: 'nEenWmSyUEp',
attributes: [
{
attribute: 'w75KJ2mc4zz',
value: 'Gigiwe',
},
]
});

Example: Create a dataSet

create('dataSets', { name: 'OpenFn Data Set', periodType: 'Monthly' });

Example: a dataSetNotification

create('dataSetNotificationTemplates', {
dataSetNotificationTrigger: 'DATA_SET_COMPLETION',
notificationRecipient: 'ORGANISATION_UNIT_CONTACT',
name: 'Notification',
messageTemplate: 'Hello',
deliveryChannels: ['SMS'],
dataSets: [],
});

Example: Create a dataElement

create('dataElements', {
aggregationType: 'SUM',
domainType: 'AGGREGATE',
valueType: 'NUMBER',
name: 'Paracetamol',
shortName: 'Para',
});

Example: Create a dataElementGroup

create('dataElementGroups', {
name: 'Data Element Group 1',
dataElements: [],
});

Example: Create a dataElementGroupSet

create('dataElementGroupSets', {
name: 'Data Element Group Set 4',
dataDimension: true,
shortName: 'DEGS4',
dataElementGroups: [],
});

Example: Create a dataValueSet

create('dataValueSets', {
dataElement: 'f7n9E0hX8qk',
period: '201401',
orgUnit: 'DiszpKrYNg8',
value: '12',
});

Example: Create a dataValueSet with related dataValues

create('dataValueSets', {
dataSet: 'pBOMPrpg1QX',
completeDate: '2014-02-03',
period: '201401',
orgUnit: 'DiszpKrYNg8',
dataValues: [
{
dataElement: 'f7n9E0hX8qk',
value: '1',
},
{
dataElement: 'Ix2HsbDMLea',
value: '2',
},
{
dataElement: 'eY5ehpbEsB7',
value: '3',
},
],
});

Example: Create an enrollment

create('enrollments', {
trackedEntity: 'bmshzEacgxa',
orgUnit: 'TSyzvBiovKh',
program: 'gZBxv9Ujxg0',
enrollmentDate: '2013-09-17',
incidentDate: '2013-09-17',
});

Example: Create an multiple objects with the Tracker API

 create("tracker", {
enrollments: [
{
trackedEntity: "bmshzEacgxa",
orgUnit: "TSyzvBiovKh",
program: "gZBxv9Ujxg0",
enrollmentDate: "2013-09-17",
incidentDate: "2013-09-17",
},
],
trackedEntities: [
{
orgUnit: "TSyzvBiovKh",
trackedEntityType: "nEenWmSyUEp",
attributes: [
{
attribute: "w75KJ2mc4zz",
value: "Gigiwe",
},
],
},
],
});

destroy

destroy(resourceType, path, [data], [options], [callback]) ⇒ Operation

Delete a record. A generic helper function to delete an object

ParamTypeDescription
resourceTypestringThe type of resource to be deleted. E.g. trackedEntities, organisationUnits, etc.
pathstringCan be an id of an object or path to the nested object to delete.
[data]ObjectOptional. This is useful when you want to remove multiple objects from a collection in one request. You can send data as, for example, {"identifiableObjects": [{"id": "IDA"}, {"id": "IDB"}, {"id": "IDC"}]}. See more on DHIS2 API docs
[options]ObjectOptional options for del operation including params e.g. {preheatCache: true, strategy: 'UPDATE', mergeMode: 'REPLACE'}. Run discover or see DHIS2 documentation. Defaults to {operationName: 'delete', apiVersion: state.configuration.apiVersion, responseType: 'json'}
[callback]functionOptional callback to handle the response

Example: a tracked entity instance. See Delete tracker docs

destroy('trackedEntities', 'LcRd6Nyaq7T');

discover

discover(httpMethod, endpoint) ⇒ Operation

Discover DHIS2 api endpoint query parameters and allowed operators for a given resource's endpoint.

ParamTypeDescription
httpMethodstringThe HTTP to inspect parameter usage for a given endpoint, e.g., get, post,put,patch,delete
endpointstringThe path for a given endpoint. E.g. /trackedEntities or /dataValueSets

Example: a list of parameters allowed on a given endpoint for specific http method

discover('post', '/trackedEntities')

dv

dv(dataElement, value) ⇒ object

Converts a dataElement and value into a DHIS2 dataValue object

ParamTypeDescription
dataElementstringA data element ID.
valuestringThe value for that data element.

Example

dv('f7n9E0hX8qk', 12)

findAttributeValue

findAttributeValue(trackedEntity, attributeDisplayName) ⇒ string

Gets an attribute value by its case-insensitive display name

ParamTypeDescription
trackedEntityObjectA tracked entity instance (TEI) object
attributeDisplayNamestringThe 'displayName' to search for in the TEI's attributes

Example

findAttributeValue(state.data.trackedEntities[0], 'first name')

get

get(resourceType, query, [options], [callback]) ⇒ Operation

Get data. Generic helper method for getting data of any kind from DHIS2.

  • This can be used to get DataValueSets,events,trackers,etc.

Returns: Operation - state

ParamTypeDescription
resourceTypestringThe type of resource to get(use its plural name). E.g. dataElements, tracker/trackedEntities,organisationUnits, etc.
queryObjectA query object that will limit what resources are retrieved when converted into request params.
[options]ObjectOptional options to define URL parameters via params beyond filters, request configuration (e.g. auth) and DHIS2 api version to use.
[callback]functionOptional callback to handle the response

Example: Get all data values for the 'pBOMPrpg1QX' dataset

get('dataValueSets', {
dataSet: 'pBOMPrpg1QX',
orgUnit: 'DiszpKrYNg8',
period: '201401',
fields: '*',
});

Example: Get all programs for an organization unit

get('programs', { orgUnit: 'TSyzvBiovKh', fields: '*' });

Example: Get a single tracked entity given the provided ID. See TrackedEntities docs

get('tracker/trackedEntities/F8yKM85NbxW');

Example: Get an enrollment given the provided ID. See Enrollment docs

get('tracker/enrollments/abcd');

Example: Get all events matching given criteria. See Events docs

get('tracker/events');

Example: Get the relationship between two tracker entities. The only required parameters are 'trackedEntity', 'enrollment' or 'event'. See Relationships docs

get('tracker/relationships', {
trackedEntity:['F8yKM85NbxW'],
});

patch

patch(resourceType, path, data, [options], [callback]) ⇒ Operation

Patch a record. A generic helper function to send partial updates on one or more object properties.

  • You are not required to send the full body of object properties.
  • This is useful for cases where you don't want or need to update all properties on a object.
ParamTypeDescription
resourceTypestringThe type of resource to be updated. E.g. dataElements, organisationUnits, etc.
pathstringThe id or path to the object to be updated. E.g. FTRrcoaog83 or FTRrcoaog83/{collection-name}/{object-id}
dataObjectData to update. Include only the fields you want to update. E.g. {name: "New Name"}
[options]ObjectOptional configuration, including params for the update ({preheatCache: true, strategy: 'UPDATE', mergeMode: 'REPLACE'}). Defaults to {operationName: 'patch', apiVersion: state.configuration.apiVersion, responseType: 'json'}
[callback]functionOptional callback to handle the response

Example: a dataElement

patch('dataElements', 'FTRrcoaog83', { name: 'New Name' });

post

post(resourceType, data, [options], [callback]) ⇒ Operation

Post data. Generic helper method for posting data of any kind to DHIS2. This can be used to create DataValueSets,events,trackers,etc.

Returns: Operation - state

ParamTypeDescription
resourceTypestringType of resource to create. E.g. trackedEntities, programs, events, ...
dataDhis2DataObject which defines data that will be used to create a given instance of resource. To create a single instance of a resource, data must be a javascript object, and to create multiple instances of a resources, data must be an array of javascript objects.
[options]ObjectOptional options to define URL parameters via params (E.g. filter, dimension and other import parameters), request config (E.g. auth) and the DHIS2 apiVersion.
[callback]functionOptional callback to handle the response

Example: Create an event

post("tracker", {
events: [
{
program: "eBAyeGv0exc",
orgUnit: "DiszpKrYNg8",
status: "COMPLETED",
},
],
});

update

update(resourceType, path, data, [options], [callback]) ⇒ Operation

Update data. A generic helper function to update a resource object of any type. Updating an object requires to send all required fields or the full body

ParamTypeDescription
resourceTypestringThe type of resource to be updated. E.g. dataElements, organisationUnits, etc.
pathstringThe id or path to the object to be updated. E.g. FTRrcoaog83 or FTRrcoaog83/{collection-name}/{object-id}
dataObjectData to update. It requires to send all required fields or the full body. If you want partial updates, use patch operation.
[options]ObjectOptional options to define URL parameters via params (E.g. filter, dimension and other import parameters), request config (E.g. auth) and the DHIS2 apiVersion.
[callback]functionOptional callback to handle the response

Example: a program

update('programs', 'qAZJCrNJK8H', {
name: '14e1aa02c3f0a31618e096f2c6d03bed',
shortName: '14e1aa02',
programType: 'WITHOUT_REGISTRATION',
});

Example: an event

update('events', 'PVqUD2hvU4E', {
program: 'eBAyeGv0exc',
orgUnit: 'Ngelehun CHC',
status: 'COMPLETED',
storedBy: 'admin',
dataValues: [],
});

Example: Update a tracker entity. See Update tracker docs

update('trackedEntities', '', {
createdAt: '2015-08-06T21:12:37.256',
orgUnit: 'TSyzvBiovKh',
createdAtClient: '2015-08-06T21:12:37.256',
trackedEntity: 'IeQfgUtGPq2',
trackedEntityType: 'nEenWmSyUEp',
inactive: false,
deleted: false,
featureType: 'NONE',
programOwners: [
{
ownerOrgUnit: 'TSyzvBiovKh',
program: 'IpHINAT79UW',
trackedEntity: 'IeQfgUtGPq2',
},
],
attributes: [
{
lastUpdated: '2016-01-12T00:00:00.000',
displayName: 'Last name',
created: '2016-01-12T00:00:00.000',
valueType: 'TEXT',
attribute: 'zDhUuAYrxNC',
value: 'Russell',
},
{
lastUpdated: '2016-01-12T00:00:00.000',
code: 'MMD_PER_NAM',
displayName: 'First name',
created: '2016-01-12T00:00:00.000',
valueType: 'TEXT',
attribute: 'w75KJ2mc4zz',
value: 'Catherine',
},
],
});

Example: Update a dataSet

update('dataSets', 'lyLU2wR22tC', { name: 'OpenFN Data Set', periodType: 'Weekly' });

Example: a dataSetNotification

update('dataSetNotificationTemplates', 'VbQBwdm1wVP', {
dataSetNotificationTrigger: 'DATA_SET_COMPLETION',
notificationRecipient: 'ORGANISATION_UNIT_CONTACT',
name: 'Notification',
messageTemplate: 'Hello Updated',
deliveryChannels: ['SMS'],
dataSets: [],
});

Example: Update a dataElement

update('dataElements', 'FTRrcoaog83', {
aggregationType: 'SUM',
domainType: 'AGGREGATE',
valueType: 'NUMBER',
name: 'Paracetamol',
shortName: 'Para',
});

Example: Update a dataElementGroup

update('dataElementGroups', 'QrprHT61XFk', {
name: 'Data Element Group 1',
dataElements: [],
});

Example: Update a dataElementGroupSet

update('dataElementGroupSets', 'VxWloRvAze8', {
name: 'Data Element Group Set 4',
dataDimension: true,
shortName: 'DEGS4',
dataElementGroups: [],
});

Example: Update a dataValueSet

update('dataValueSets', 'AsQj6cDsUq4', {
dataElement: 'f7n9E0hX8qk',
period: '201401',
orgUnit: 'DiszpKrYNg8',
value: '12',
});

Example: Update a dataValueSet with related dataValues

update('dataValueSets', 'Ix2HsbDMLea', {
dataSet: 'pBOMPrpg1QX',
completeDate: '2014-02-03',
period: '201401',
orgUnit: 'DiszpKrYNg8',
dataValues: [
{
dataElement: 'f7n9E0hX8qk',
value: '1',
},
{
dataElement: 'Ix2HsbDMLea',
value: '2',
},
{
dataElement: 'eY5ehpbEsB7',
value: '3',
},
],
});

Example: Update an enrollment given the provided ID

update('enrollments', 'CmsHzercTBa' {
trackedEntity: 'bmshzEacgxa',
orgUnit: 'TSyzvBiovKh',
program: 'gZBxv9Ujxg0',
enrollmentDate: '2013-10-17',
incidentDate: '2013-10-17',
});

upsert

upsert(resourceType, query, data, [options], [callback]) ⇒ Operation

Upsert a record. A generic helper function used to atomically either insert a row, or on the basis of the row already existing, UPDATE that existing row instead.

Throws:

  • RangeError - Throws range error
ParamTypeDescription
resourceTypestringThe type of a resource to upsert. E.g. trackedEntities
queryObjectA query object that allows to uniquely identify the resource to update. If no matches found, then the resource will be created.
dataObjectThe data to use for update or create depending on the result of the query.
[options]ObjectOptional configuration that will be applied to both the get and the create or update operations.
[callback]functionOptional callback to handle the response

Example: Upsert a trackedEntity

upsert('trackedEntities', {
orgUnit: 'TSyzvBiovKh',
filter: ['w75KJ2mc4zz:Eq:Qassim'],
}, {
orgUnit: 'TSyzvBiovKh',
trackedEntityType: 'nEenWmSyUEp',
attributes: [
{
attribute: 'w75KJ2mc4zz',
value: 'Qassim',
},
],
});