--- type: "resource" title: "CompartmentDefinition" resource: "CompartmentDefinition" --- # CompartmentDefinition ## Introduction ## Scope and Usage Each resource may belong to one or more logical compartments. A compartment is a logical grouping of resources which share a common property. Compartments have two principal roles: - Function as an access mechanism for finding a set of related resources quickly - Provide a definitional basis for applying access control to resources quickly Each compartment definition is associated with exactly one FHIR resource type, identified by `CompartmentDefinition.code`. The compartment also identifies all resources that are members of the compartment and what search parameters are used to determine membership. A compartment instance (the set of all resources tied to the 'compartment resource' - a resource instance of the compartment's type) contains all resources found by searching using the associated search parameters with a value of the compartment resource. For example, the compartment for `Patient/123` would include `Account?subject=Patient/123`, `AllergyIntolerance?patient=Patient/123`, `AllergyIntolerance?asserter=Patient/123`, etc. Each CompartmentDefinition instance will list all resources available for that FHIR release ([Additional resources are handled separately](structuredefinition#additional)). Listed resources that have one or more .param elements are considered part of the compartment. Listed resources that do not have a .param element are considered to be outside the compartment. **Notes:** - At present, CompartmentDefinition instances can _only_ be defined by HL7 International in this specification. This is because their existence creates significant impact on the behavior of servers. The CompartmentDefinitions defined in this specification may be hosted on other servers. There is no mechanism to describe a given server's compartment implementation (e.g. resources not supported in the implementation of the compartment or additional resources included in the compartment). - Although compartment definitions can be a useful foundation for access control, they may not be sufficient for this purpose. Resources outside the compartment might still be appropriate to be made available or subject to compartment restrictions even though they are not formally part of the compartment (see below about access rules). - Resources that are not part of a compartment MAY still be returned in searches against the compartment by using the \_include mechanism. Local convention can also include additional resources in compartment-based retrieval operations such as $everything. These resources are not 'in' the compartment, but they are still available at server discretion. ## Boundaries and Relationships Compartment definitions describe how particular compartment instances are named and identified, and how systems know which resources are in the compartment. The following compartments are defined by this specification: <%compartmentlist%> The full definitions of these compartments are published as `CompartmentDefinition` resources. Servers typically do not support the full definition of a compartment, and are not required to. Systems may publish `CompartmentDefinition` resources so that other systems may make use of compartments properly. - CompartmentDefinitions are used by [CapabilityStatement](capabilitystatement) instances for specifying how resources are accessed - [GraphDefinition](http://build.fhir.org/HL7/api-incubator/StructureDefinition-GraphDefinition) also provides a mechanism for grouping resources, though it does not provide an additional search mechanism. Inclusion of resources in a [GraphDefinition](http://build.fhir.org/HL7/api-incubator/StructureDefinition-GraphDefinition) supports more complex criteria for inclusion than that allowed by CompartmentDefinition. ## Notes ### Base Resource A compartment implicitly always includes the resource that is a 'root' of the compartment e.g. the resource `Patient/123` is the root resource of the Patient Compartment for `Patient/123`. Thus an operation that retrieves everything within the compartment will include the base resource; a scope that grants access to everything within the compartment will grant access to the resource; etc. ### Using Compartments As an example of compartment usage, to retrieve a list of a patient's conditions, use the URL: GET \[base\]/Patient/\[id\]/Condition Additional search parameters can be defined, such as this hypothetical search for acute conditions: GET \[base\]/Patient/\[id\]/Condition?code:in=http://hspc.org/ValueSet/acute-concerns Note that as searches, these are syntactic variations on these two search URLs respectively: GET \[base\]/Condition?patient=\[id\] GET \[base\]/Condition?patient=\[id\]&code:in=http://hspc.org/ValueSet/acute-concerns The outcome of a compartment search is the same as the equivalent system or type search and will have full URLs that are not compartment-specific. For example, both these searches return the same outcome if there is no patient 333: GET \[base\]/Patient/333/Condition GET \[base\]/Condition?patient=333 Whether the patient doesn't exist, or the user has no access to the patient, both these searches return an empty bundle with no matches. Some systems will include an operation outcome warning that there is no matching patient. However, there is a key difference in functionality between compartment based searches and direct searches with parameters. Consider this search: GET \[base\]/Patient/\[id\]/Communication Because the definition of the [patient compartment](compartmentdefinition-patient) for [Communication](communication) says that a Communication resource is in the patient compartment if the subject, sender, or recipient is the patient, the compartment search is actually the same as the union of these 3 searches: GET \[base\]/Communication?subject=\[id\] GET \[base\]/Communication?sender=\[id\] GET \[base\]/Communication?recipient=\[id\] There is no way to do this as a single search, except by using the [\_filter](search_filter): GET \[base\]/Communication?\_filter=subject re \[id\] or sender re \[id\] or recipient re \[id\] Further details of searching by compartment are [described under the search operation](http#vsearch). As a search related operation, the assignment of resources to compartments is only based on the current version of any of the resources involved. Note that [contained](references#contained) patient resources cannot create a patient compartment of their own. **Supported Interactions:** The only RESTful interaction defined on a compartment endpoint is `search`. Reads, writes (create, update, patch, delete), history, operations (e.g. `$validate`, `$lastn`) and batch/transaction POSTs are not defined when invoked through a compartment-qualified URL such as `[base]/Patient/[id]/Condition/[rid]` or `[base]/Patient/[id]/Condition/$lastn`. Clients SHOULD NOT expect servers to support these interactions via compartment-qualified paths, however servers MAY choose to implement them. To interact with a specific instance or invoke an operation that targets a resource type or instance, use the native URL for the resource (e.g. `[base]/Condition/[id]`). See the [HTTP Search variants](http#vsearch) and [Search Contexts](search#searchcontexts) for further details. Note that while this specification describes how to use the compartment syntax to find resources that are logically associated with the compartment, the compartment is not part of the identity of the resource. E.g. the response to the following is not defined by this specification: GET \[base\]/Patient/\[patient-id\]/Condition/\[resource-id\] The response for write operations (PUT/DELETE/PATCH) are also not defined by this specification. Nor is the response to a POST defined: POST \[base\]/Patient/\[patient-id\]/Condition There is no expectation for servers to support either read or write to such URLs. ### Logical Meaning of Compartments Compartments may be used explicitly, like this, but can also be used implicitly. For instance, if a FHIR server is providing a patient view of a record, the authorized user associated with use of the FHIR RESTful API may be limited to accessing records from the compartment instance(s) logically associated with their identity. Irrespective of whether compartments are being used explicitly or implicitly, servers will need to make arrangements to make some resources with no direct link to a patient available to the client (medications, substances, binaries, etc.). Note that resources may cross between compartments, or interlink them. Examples of this would be where a [Diagnostic Report](diagnosticreport) identifies a subject, but an [Observation](observation) it references identifies a different subject, or where a [List](list) resource references items that identify different subjects. Such cross-linking may arise for many valid reasons, including: - Cases where subject records are inter-linked - Transplants, Perinatal care, family therapy etc. - Workflow management where action lists link multiple patients and/or practitioners Given the wide variety of use cases and contexts in which FHIR is used, compartments do not define how cross-linking is handled. Systems may reject resources, remove them from both compartments, or place them in both, or act in some other fashion. The [graph definition](http://build.fhir.org/HL7/api-incubator/StructureDefinition-GraphDefinition) resource provides a method by which rules about cross-linking may be made and enforced. It is at the discretion of the server whether to include resources in a compartment when the reference to the resource that establishes the compartment is in an extension. Some resources are not in any compartment, e.g. [Medication](medication), [Substance](substance), [Location](location). These resources are not linked directly to a patient or authored record, and are sometimes called 'master files'. Servers will need to make arrangements to make these resources available to the clients that are limited to particular compartments. For example, a Medication resource describes a medication itself and does not link to a patient; however, a resource such as MedicationAdministration connects the Medication (details of what was administered) to the patient (for whom was it administered), and so is required to interpret the administration. ## Defining New Compartments Compartments are defined and added to the list above when implementer communities identify them as common access points for data. As described below, compartments have both syntactical and logical consequences, and both these aspects of their functionality are evaluated when deciding whether to define compartments. ## StructureDefinition ### Elements (Simplified) - **[CompartmentDefinition](/compartmentdefinition-definitions#CompartmentDefinition)** [0..*]: - Compartment Definition for a resource - **[CompartmentDefinition.url](/compartmentdefinition-definitions#CompartmentDefinition.url)** [1..1]: [uri](/uri) Canonical identifier for this compartment definition, represented as a URI (globally unique) - **[CompartmentDefinition.version](/compartmentdefinition-definitions#CompartmentDefinition.version)** [0..1]: [string](/string) Business version of the compartment definition - **[CompartmentDefinition.versionAlgorithm[x]](/compartmentdefinition-definitions#CompartmentDefinition.versionAlgorithm%5Bx%5D)** [0..1]: [string](/string), [Coding](/Coding) extensible:[version-algorithm](/valueset-version-algorithm) How to compare versions - **[CompartmentDefinition.name](/compartmentdefinition-definitions#CompartmentDefinition.name)** [1..1]: [string](/string) Name for this compartment definition (computer friendly) - **[CompartmentDefinition.title](/compartmentdefinition-definitions#CompartmentDefinition.title)** [0..1]: [string](/string) Name for this compartment definition (human friendly) - **[CompartmentDefinition.status](/compartmentdefinition-definitions#CompartmentDefinition.status)** [1..1]: [code](/code) required:[publication-status](/valueset-publication-status) draft | active | retired | unknown - **[CompartmentDefinition.experimental](/compartmentdefinition-definitions#CompartmentDefinition.experimental)** [0..1]: [boolean](/boolean) For testing only - never for real usage - **[CompartmentDefinition.date](/compartmentdefinition-definitions#CompartmentDefinition.date)** [0..1]: [dateTime](/dateTime) Date last changed - **[CompartmentDefinition.publisher](/compartmentdefinition-definitions#CompartmentDefinition.publisher)** [0..1]: [string](/string) Name of the publisher/steward (organization or individual) - **[CompartmentDefinition.contact](/compartmentdefinition-definitions#CompartmentDefinition.contact)** [0..*]: [ContactDetail](/ContactDetail) Contact details for the publisher - **[CompartmentDefinition.description](/compartmentdefinition-definitions#CompartmentDefinition.description)** [0..1]: [markdown](/markdown) Natural language description of the compartment definition - **[CompartmentDefinition.useContext](/compartmentdefinition-definitions#CompartmentDefinition.useContext)** [0..*]: [UsageContext](/UsageContext) The context that the content is intended to support - **[CompartmentDefinition.purpose](/compartmentdefinition-definitions#CompartmentDefinition.purpose)** [0..1]: [markdown](/markdown) Why this compartment definition is defined - **[CompartmentDefinition.code](/compartmentdefinition-definitions#CompartmentDefinition.code)** [1..1]: [code](/code) required:[compartment-type](/valueset-compartment-type) Patient | Encounter | RelatedPerson | Practitioner | Device | EpisodeOfCare - **[CompartmentDefinition.search](/compartmentdefinition-definitions#CompartmentDefinition.search)** [1..1]: [boolean](/boolean) Whether the search syntax is supported - **[CompartmentDefinition.resource](/compartmentdefinition-definitions#CompartmentDefinition.resource)** [0..*]: [BackboneElement](/BackboneElement) How a resource is related to the compartment - **[CompartmentDefinition.resource.code](/compartmentdefinition-definitions#CompartmentDefinition.resource.code)** [1..1]: [code](/code) required:[resource-types](/valueset-resource-types) Name of resource type - **[CompartmentDefinition.resource.param](/compartmentdefinition-definitions#CompartmentDefinition.resource.param)** [0..*]: [string](/string) Search Parameter Name, or chained parameters - **[CompartmentDefinition.resource.startParam](/compartmentdefinition-definitions#CompartmentDefinition.resource.startParam)** [0..1]: [uri](/uri) Search Param for interpreting $everything.start - **[CompartmentDefinition.resource.endParam](/compartmentdefinition-definitions#CompartmentDefinition.resource.endParam)** [0..1]: [uri](/uri) Search Param for interpreting $everything.end ## Mappings - [CompartmentDefinition Mappings](/compartmentdefinition-mappings) — 11 mapping entries ## Resource Packs ### list-CompartmentDefinition-packs.xml ```xml ``` ## Search Parameters - [code](/compartmentdefinition-search#code) — **token** — Patient | Encounter | RelatedPerson | Practitioner | Device — `CompartmentDefinition.code` - [context](/compartmentdefinition-search#context) — **token** — A use context assigned to the compartment definition — `(CompartmentDefinition.useContext.value.ofType(CodeableConcept))` - [context-quantity](/compartmentdefinition-search#context-quantity) — **quantity** — A quantity- or range-valued use context assigned to the compartment definition — `(CompartmentDefinition.useContext.value.ofType(Quantity)) | (CompartmentDefinition.useContext.value.ofType(Range))` - [context-type](/compartmentdefinition-search#context-type) — **token** — A type of use context assigned to the compartment definition — `CompartmentDefinition.useContext.code` - [context-type-quantity](/compartmentdefinition-search#context-type-quantity) — **composite** — A use context type and quantity- or range-based value assigned to the compartment definition — `CompartmentDefinition.useContext` - [context-type-value](/compartmentdefinition-search#context-type-value) — **composite** — A use context type and value assigned to the compartment definition — `CompartmentDefinition.useContext` - [date](/compartmentdefinition-search#date) — **date** — The compartment definition publication date — `CompartmentDefinition.date` - [description](/compartmentdefinition-search#description) — **string** — The description of the compartment definition — `CompartmentDefinition.description` - [name](/compartmentdefinition-search#name) — **string** — Computationally friendly name of the compartment definition — `CompartmentDefinition.name` - [publisher](/compartmentdefinition-search#publisher) — **string** — Name of the publisher of the compartment definition — `CompartmentDefinition.publisher` - [resource](/compartmentdefinition-search#resource) — **token** — Name of resource type — `CompartmentDefinition.resource.code` - [status](/compartmentdefinition-search#status) — **token** — The current status of the compartment definition — `CompartmentDefinition.status` - [url](/compartmentdefinition-search#url) — **uri** — The uri that identifies the compartment definition — `CompartmentDefinition.url` - [version](/compartmentdefinition-search#version) — **token** — The business version of the compartment definition — `CompartmentDefinition.version` [Full Search Parameters](/compartmentdefinition-search) ## Examples - [compartmentdefinition-example](/compartmentdefinition-example-compartmentdefinition-example) — compartmentdefinition-example - [compartmentdefinition-examples-header](/compartmentdefinition-example-compartmentdefinition-examples-header) — compartmentdefinition-examples-header - [device](/compartmentdefinition-example-device) — compartmentdefinition-device — Full Device Compartment - [encounter](/compartmentdefinition-example-encounter) — compartmentdefinition-encounter — Full Encounter Compartment - [example](/compartmentdefinition-example-example) — compartmentdefinition-example — Example Compartment - [patient](/compartmentdefinition-example-patient) — compartmentdefinition-patient — Full Patient Compartment - [practitioner](/compartmentdefinition-example-practitioner) — compartmentdefinition-practitioner — Full Practitioner Compartment - [relatedperson](/compartmentdefinition-example-relatedperson) — compartmentdefinition-relatedperson — Full RelatedPerson Compartment [Full Examples](/compartmentdefinition-examples) ## Mapping Exceptions ### compartmentdefinition-fivews-mapping-exceptions.xml ### Unmapped Elements - **FiveWs.what** — Not relevant for this resource - **FiveWs.author** — Not relevant for this resource - **FiveWs.actor** — Not relevant for this resource - **FiveWs.cause** — Not relevant for this resource - **FiveWs.where** — Not relevant for this resource - **FiveWs.context** — Unknown - **FiveWs.init** — Not relevant for this resource - **FiveWs.source** — Not relevant for this resource - **FiveWs.who** — Not relevant for this resource - **FiveWs.grade** — Not relevant for this resource - **FiveWs.planned** — Not relevant for this resource - **FiveWs.done** — Not relevant for this resource - **FiveWs.subject** — Not relevant for this resource