Mutating Data

In this tutorial we will learn how to add new data or modify existing data in the Commure Platform.

React Data Mutation

The simplest way to create or update data in the Commure Platform is to use the <withFhirDataMutate/> higher-order component.

import React, { useState, useEffect } from "react";
import {
FhirMethod,
withFhirDataMutate,
FhirDataMutateConsumer
} from "@commure/components-data";
import { Resource, Bundle } from "@commure/fhir-types/r4/types";
interface AnyResource extends Resource {
[key: string]: any;
}
const MyComponent = (props: FhirDataMutateConsumer) => {
const { mutate } = props;
const [outcome, setBundle] = useState<Bundle | undefined>(undefined);
useEffect(() => {
mutate({
query: "Patient",
body: {
resourceType: "Patient",
name: [{ given: ["john"] }],
gender: "male"
} as AnyResource,
method: FhirMethod.POST
})
.then((response: Response) => response.clone().json())
.then(data => setBundle(data));
}, [mutate]);
return <div>Outcome: {JSON.stringify(outcome)}</div>;
};
export default withFhirDataMutate(MyComponent);

The above example will create a new Patient resource with the following contents:

{
"resourceType": "Patient",
"name": [{ "given": ["john"] }],
"gender": "male"
}

Since we are using FhirMethod.POST, a new resource will be created.

Related Reading

withFhirDataMutate component reference documentation

Non-React Data Mutation

The same data as in the React example above can be created using curl as follows.

curl -X POST 'https://api-TENANT_ID.staging.developer.commure.com/api/v1/r4/Patient' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer ACCESS_TOKEN' \
--data-raw '{
"resourceType": "Patient",
"name": [{ "given": ["john"] }],
"gender": "male"
}'

You'll need to replace TENANT_ID with your actual Commure tenant ID, which you find in the top right menu in the console. The ACCESS_TOKEN should be replaced with the value obtained after authentication.

The above curl command will return something like the following.

{
"resourceType": "Patient",
"id": "e64cf541-0d37-4327-a60b-032f8104ecd3",
"meta": {
"versionId": "6881c0ca-e806-40d5-9695-9878508cb6ce",
"lastUpdated": "2020-02-06T08:22:17.419384+00:00"
},
"name": [
{
"given": ["john"]
}
],
"gender": "male"
}

Related Reading

Working With FHIR
Commure FHIR API