Skip to main content

core.medplumclient.createresourceifnoneexist

Home > @medplum/core > MedplumClient > createResourceIfNoneExist

MedplumClient.createResourceIfNoneExist() method

Conditionally create a new FHIR resource only if some equivalent resource does not already exist on the server.

The return value is the existing resource or the newly created resource, including the ID and meta.

Signature:

createResourceIfNoneExist<T extends Resource>(resource: T, query: string, options?: MedplumRequestOptions): Promise<T>;

Parameters

Parameter

Type

Description

resource

T

The FHIR resource to create.

query

string

The search query for an equivalent resource (should not include resource type or "?").

options

MedplumRequestOptions

(Optional) Optional fetch options.

Returns:

Promise<T>

The result of the create operation.

Example

Example:

const result = await medplum.createResourceIfNoneExist(
{
resourceType: 'Patient',
identifier: [{
system: 'http://example.com/mrn',
value: '123'
}]
name: [{
family: 'Smith',
given: ['John']
}]
},
'identifier=123'
);
console.log(result.id);

This method is syntactic sugar for:

return searchOne(resourceType, query) ?? createResource(resource);

The query parameter only contains the search parameters (what would be in the URL following the "?").

See the FHIR "conditional create" operation for full details: https://www.hl7.org/fhir/http.html\#ccreate