FhirEx.Resources.Bundle (fhir_ex v0.2.0)

Copy Markdown View Source

FHIR R5 Bundle resource.

A container for a collection of resources. Used for transactions, search results, documents, messages, and more.

https://www.hl7.org/fhir/R5/bundle.html

Bundle types

document | message | transaction | transaction-response | batch | batch-response | history | searchset | collection | subscription-notification

Building bundles

Use the builder helpers rather than constructing the struct directly:

alias FhirEx.Resources.Bundle

# Transaction bundle (create/update/delete in one request)
bundle = Bundle.transaction([
  Bundle.entry(patient, request: %{method: "POST", url: "Patient"}),
  Bundle.entry(obs, request: %{method: "PUT", url: "Observation/obs-001"})
])

# Batch bundle
bundle = Bundle.batch([
  Bundle.entry(patient, request: %{method: "GET", url: "Patient/p-001"})
])

# Searchset (list of matching resources)
bundle = Bundle.searchset([Bundle.entry(patient)], total: 1)

# Collection (simple grouping)
bundle = Bundle.collection([Bundle.entry(patient), Bundle.entry(obs)])

json = FhirEx.JSON.encode!(bundle)

Entries

Bundle.entry/2 builds a BundleEntry map. Options:

Bundle.entry(resource,
  full_url: "urn:uuid:abc",
  request: %{method: "POST", url: "Patient"},
  search: %{mode: "match", score: 1.0}
)

Decoding

When decoding a Bundle, each entry's :resource is kept as a raw map() because the resource type is only known at runtime. Decode individual entries with the appropriate resource module:

%Bundle{} = bundle = FhirEx.JSON.decode!(json, Bundle)
patient = FhirEx.Resources.Patient.from_map(bundle.entry |> hd() |> Map.get(:resource))

Summary

Functions

Builds a batch bundle from a list of entries.

Builds a collection bundle from a list of entries.

Builds a document bundle from a list of entries.

Builds a bundle entry map.

Builds the struct from a string-keyed map (e.g., from decoded FHIR JSON).

Builds a new struct from a map or keyword list of field values.

Returns the FHIR resource type name string.

Builds a searchset bundle. Pass total: to set the search total.

Converts the struct to a string-keyed map for use with FhirEx.JSON.

Builds a transaction bundle from a list of entries.

Types

entry()

@type entry() :: %{
  link: [link()] | nil,
  full_url: String.t() | nil,
  resource: struct() | map() | nil,
  search: search_info() | nil,
  request: request_info() | nil,
  response: response_info() | nil
}

link()

@type link() :: %{relation: String.t(), url: String.t()}

request_info()

@type request_info() :: %{
  method: String.t(),
  url: String.t(),
  if_none_match: String.t() | nil,
  if_modified_since: String.t() | nil,
  if_match: String.t() | nil,
  if_none_exist: String.t() | nil
}

response_info()

@type response_info() :: %{
  status: String.t(),
  location: String.t() | nil,
  etag: String.t() | nil,
  last_modified: String.t() | nil,
  outcome: map() | nil
}

search_info()

@type search_info() :: %{mode: String.t() | nil, score: number() | nil}

t()

@type t() :: %FhirEx.Resources.Bundle{
  entry: [entry()] | nil,
  id: String.t() | nil,
  identifier: FhirEx.Types.Identifier.t() | nil,
  link: [link()] | nil,
  meta: FhirEx.Types.Meta.t() | nil,
  timestamp: String.t() | nil,
  total: non_neg_integer() | nil,
  type: String.t()
}

Functions

batch(entries)

@spec batch([entry()]) :: t()

Builds a batch bundle from a list of entries.

collection(entries)

@spec collection([entry()]) :: t()

Builds a collection bundle from a list of entries.

document(entries, opts \\ [])

@spec document(
  [entry()],
  keyword()
) :: t()

Builds a document bundle from a list of entries.

entry(resource, opts \\ [])

@spec entry(
  struct() | map() | nil,
  keyword()
) :: entry()

Builds a bundle entry map.

Options:

  • full_url — the full URL for this entry (e.g. "urn:uuid:abc")
  • request — a map with :method and :url (and optional :if_match etc.)
  • search — a map with optional :mode ("match" | "include" | "outcome") and :score

  • response — a map with :status, optional :location, :etag, :last_modified, :outcome

from_map(map)

@spec from_map(map()) :: t()

Builds the struct from a string-keyed map (e.g., from decoded FHIR JSON).

new(attrs \\ %{})

@spec new(map() | keyword()) :: t()

Builds a new struct from a map or keyword list of field values.

resource_type()

@spec resource_type() :: String.t()

Returns the FHIR resource type name string.

searchset(entries, opts \\ [])

@spec searchset(
  [entry()],
  keyword()
) :: t()

Builds a searchset bundle. Pass total: to set the search total.

to_map(b)

@spec to_map(t()) :: map()

Converts the struct to a string-keyed map for use with FhirEx.JSON.

transaction(entries)

@spec transaction([entry()]) :: t()

Builds a transaction bundle from a list of entries.