On this page

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<WithId<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<WithId<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 sends a conditional create request with an If-None-Exist header. The server checks for an existing match as part of the create request; the client does not perform a separate search followed by a create.

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.