core.medplumclient.createresourceifnoneexist | Medplum

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>
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.