fhir-terminology-runtime 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +478 -0
- package/dist/index.cjs +2213 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +356 -0
- package/dist/index.d.ts +356 -0
- package/dist/index.mjs +2205 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +63 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,478 @@
|
|
|
1
|
+
# FHIR Terminology Runtime
|
|
2
|
+
|
|
3
|
+
> Local FHIR terminology runtime for ValueSet expansion, CodeSystem resolution, and terminology operations
|
|
4
|
+
> Part of the [FUME](https://github.com/Outburn-IL/fume-community) open-source initiative · Apache 2.0 License
|
|
5
|
+
|
|
6
|
+
## Overview
|
|
7
|
+
|
|
8
|
+
`fhir-terminology-runtime` (FTR):
|
|
9
|
+
- Expands ValueSets (compose.include / compose.exclude) using CodeSystems from FHIR packages.
|
|
10
|
+
- Resolves CodeSystems by canonical URL with package-context-aware versioning.
|
|
11
|
+
- Caches expanded ValueSets alongside source packages for performance.
|
|
12
|
+
- Supports implicit code systems (ISO 3166, BCP-47) without external dependencies.
|
|
13
|
+
|
|
14
|
+
## Why?
|
|
15
|
+
|
|
16
|
+
Many ValueSets in FHIR packages can be locally expanded given complete CodeSystems. Local expansion:
|
|
17
|
+
- Avoids round-trips to external terminology servers for static, package-defined terminology
|
|
18
|
+
- Provides deterministic, reproducible expansions
|
|
19
|
+
- Works offline and in constrained environments
|
|
20
|
+
- Enables fast terminology operations in data transformation pipelines
|
|
21
|
+
|
|
22
|
+
FTR supports multiple FHIR versions, package-context-aware resolution, lazy or full-cache modes, and works hand-in-hand with [`fhir-package-explorer`](https://github.com/Outburn-IL/fhir-package-explorer) and [`fhir-package-installer`](https://github.com/Outburn-IL/fhir-package-installer).
|
|
23
|
+
|
|
24
|
+
## Installation
|
|
25
|
+
|
|
26
|
+
```
|
|
27
|
+
npm install fhir-terminology-runtime
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Usage
|
|
31
|
+
|
|
32
|
+
### 1. Create an instance
|
|
33
|
+
|
|
34
|
+
FTR uses dependency injection - you provide a pre-configured `FhirPackageExplorer` instance. This allows sharing a single FPE instance across multiple modules (e.g., FSG and FTR):
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
import { FhirTerminologyRuntime } from 'fhir-terminology-runtime';
|
|
38
|
+
import { FhirPackageExplorer } from 'fhir-package-explorer';
|
|
39
|
+
|
|
40
|
+
// Create a single FPE instance
|
|
41
|
+
const fpe = await FhirPackageExplorer.create({
|
|
42
|
+
context: ['hl7.fhir.us.core@6.1.0'],
|
|
43
|
+
cachePath: './.fhir-cache',
|
|
44
|
+
fhirVersion: '4.0.1',
|
|
45
|
+
skipExamples: true
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
// Create FTR using the shared FPE
|
|
49
|
+
const ftr = await FhirTerminologyRuntime.create({
|
|
50
|
+
fpe,
|
|
51
|
+
cacheMode: 'lazy', // 'lazy' | 'ensure' | 'rebuild' | 'none'
|
|
52
|
+
fhirVersion: '4.0.1'
|
|
53
|
+
});
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Benefits of this approach:
|
|
57
|
+
- ✅ Share a single FPE instance across FSG, FTR, and other modules
|
|
58
|
+
- ✅ Single source of truth for FHIR package configuration
|
|
59
|
+
- ✅ Better resource management and configuration consistency
|
|
60
|
+
- ✅ Explicit dependency management
|
|
61
|
+
|
|
62
|
+
If a base FHIR package is missing from the package context and dependencies, FPE will add it automatically according to `fhirVersion`.
|
|
63
|
+
|
|
64
|
+
### 2. Expand a ValueSet
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
const expansion = await ftr.expandValueSet('administrative-gender'); // id | name | canonical URL
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
The `expandValueSet` method accepts any FSH-style identifier: canonical URL, id or name. It also accepts a resolved metadata object if you already have one.
|
|
71
|
+
|
|
72
|
+
### 3. Get ValueSet Expansion Count
|
|
73
|
+
|
|
74
|
+
Get the count of concepts in a ValueSet expansion without loading the full expansion if possible. Results are cached in memory.
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
const result = await ftr.getValueSetExpansionCount('administrative-gender');
|
|
78
|
+
|
|
79
|
+
if (result.status === 'ok') {
|
|
80
|
+
console.log(`Count: ${result.count}`);
|
|
81
|
+
} else {
|
|
82
|
+
console.log(`Could not count: ${result.reason}`);
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
The result type is:
|
|
87
|
+
```ts
|
|
88
|
+
type CountResult =
|
|
89
|
+
| { status: 'ok'; count: number }
|
|
90
|
+
| { status: 'unknown'; reason: 'unexpandable-valueset' | 'unknown-valueset' | 'duplicate-code' };
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### 4. Check membership (`inValueSet`)
|
|
94
|
+
|
|
95
|
+
For fast validation of whether a code is in a ValueSet, use `inValueSet`.
|
|
96
|
+
|
|
97
|
+
It accepts either:
|
|
98
|
+
- a plain code string (common case; system is implicit), OR
|
|
99
|
+
- a Coding-like object with `{ system, code }`.
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
const r1 = await ftr.inValueSet('A', 'administrative-gender');
|
|
103
|
+
// ^ MembershipResult
|
|
104
|
+
|
|
105
|
+
const r2 = await ftr.inValueSet({ system: 'http://hl7.org/fhir/administrative-gender', code: 'male' }, 'administrative-gender');
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Return type:
|
|
109
|
+
```ts
|
|
110
|
+
type ConceptProps = {
|
|
111
|
+
system: string;
|
|
112
|
+
code: string;
|
|
113
|
+
display?: string;
|
|
114
|
+
version?: string;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
type MembershipResult =
|
|
118
|
+
| { status: 'member'; concept: ConceptProps }
|
|
119
|
+
| { status: 'not-member' }
|
|
120
|
+
| { status: 'unknown'; reason: UnknownReason };
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Unknown reasons:
|
|
124
|
+
- `unknown-valueset`: the ValueSet identifier could not be resolved.
|
|
125
|
+
- `unexpandable-valueset`: the ValueSet could not be expanded locally.
|
|
126
|
+
- `duplicate-code`: returned only for **code-only** lookups when the same code exists under **multiple systems** in the ValueSet (ambiguous without a system).
|
|
127
|
+
|
|
128
|
+
## Membership caching (`membershipCache`)
|
|
129
|
+
|
|
130
|
+
`inValueSet` is optimized for very fast repeated lookups and uses multiple caching layers:
|
|
131
|
+
- An in-memory LRU for **small** ValueSets (≤ 50 unique codes), holding up to 100 ValueSets.
|
|
132
|
+
- An in-memory LRU for **per-code** results for larger ValueSets, holding up to 10,000 code lookups.
|
|
133
|
+
- An optional **external async cache** (injectable) used as a fallback and for persistence/distributed caching.
|
|
134
|
+
|
|
135
|
+
### Configure an external membership cache
|
|
136
|
+
|
|
137
|
+
Provide a `membershipCache` in `FhirTerminologyRuntime.create(...)`:
|
|
138
|
+
|
|
139
|
+
```ts
|
|
140
|
+
const ftr = await FhirTerminologyRuntime.create({
|
|
141
|
+
fpe,
|
|
142
|
+
cacheMode: 'lazy',
|
|
143
|
+
fhirVersion: '4.0.1',
|
|
144
|
+
membershipCache
|
|
145
|
+
});
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
The external cache is keyed deterministically by ValueSet metadata: `(packageId, packageVersion, filename)`.
|
|
149
|
+
|
|
150
|
+
Interface (simplified):
|
|
151
|
+
```ts
|
|
152
|
+
type ValueSetDeterministicKey = {
|
|
153
|
+
packageId: string;
|
|
154
|
+
packageVersion: string;
|
|
155
|
+
filename: string;
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
type MembershipCacheEntry =
|
|
159
|
+
| { status: 'member'; conceptsBySystem: Record<string, ConceptProps> }
|
|
160
|
+
| { status: 'not-member' };
|
|
161
|
+
|
|
162
|
+
interface TerminologyMembershipCache {
|
|
163
|
+
getCode(vs: ValueSetDeterministicKey, code: string): Promise<MembershipCacheEntry | undefined>;
|
|
164
|
+
setCode(vs: ValueSetDeterministicKey, code: string, entry: MembershipCacheEntry): Promise<void>;
|
|
165
|
+
|
|
166
|
+
// Optional: more efficient priming for large ValueSets
|
|
167
|
+
bulkSetCodes?(vs: ValueSetDeterministicKey, entries: Array<[string, MembershipCacheEntry]>): Promise<void>;
|
|
168
|
+
}
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### What does “prime / priming” mean?
|
|
172
|
+
|
|
173
|
+
In this codebase, **priming** refers specifically to the optional *external membership cache* behavior.
|
|
174
|
+
|
|
175
|
+
When FTR says it will **prime** the external cache for a ValueSet, it means:
|
|
176
|
+
- FTR has already built a local membership index for that ValueSet (from its expansion).
|
|
177
|
+
- FTR then **bulk-populates the external cache** with *membership answers for many (often all) codes* in that ValueSet, so future `inValueSet(...)` calls can be answered without re-expanding/re-indexing.
|
|
178
|
+
|
|
179
|
+
This is different from the normal per-lookup caching that happens during `inValueSet`:
|
|
180
|
+
- **Per-lookup cache sync** (always small): after answering a single code lookup, FTR may write a single `(ValueSetKey, code) -> entry` into the external cache.
|
|
181
|
+
- **Priming** (larger write, optional): FTR writes *many* codes for the same ValueSet in one go (ideally via `bulkSetCodes`).
|
|
182
|
+
|
|
183
|
+
#### Why prime at all?
|
|
184
|
+
|
|
185
|
+
Priming is a performance / distribution optimization:
|
|
186
|
+
- Speeds up future lookups for the same ValueSet (especially in fresh processes/containers).
|
|
187
|
+
- Enables sharing membership knowledge across workers if the external cache is shared (Redis, DB, etc.).
|
|
188
|
+
- Reduces repeated “first time” cost where the runtime would otherwise need to expand and index again.
|
|
189
|
+
|
|
190
|
+
#### What exactly gets written during priming?
|
|
191
|
+
|
|
192
|
+
Priming writes entries shaped like `MembershipCacheEntry`:
|
|
193
|
+
|
|
194
|
+
- For each `code` in the ValueSet, FTR writes:
|
|
195
|
+
- `{ status: 'member', conceptsBySystem: { [systemUrl]: { system, code, display?, version? }, ... } }`
|
|
196
|
+
|
|
197
|
+
Notes:
|
|
198
|
+
- The external cache entry can store **multiple systems** for the same `code`. This is important because `inValueSet('X', vs)` can be ambiguous when multiple systems contain the same code.
|
|
199
|
+
- FTR does **not** write "unknown" results to the external cache (for example `duplicate-code`), because unknown results often depend on whether the caller provided a system. Instead, FTR stores the raw “member by system(s)” facts.
|
|
200
|
+
|
|
201
|
+
#### When does priming happen?
|
|
202
|
+
|
|
203
|
+
Priming happens inside `inValueSet(...)` after FTR has built a local index for that ValueSet.
|
|
204
|
+
|
|
205
|
+
Current behavior:
|
|
206
|
+
- **Small ValueSets (≤ 50 unique codes)**:
|
|
207
|
+
- FTR builds an in-memory “small index”.
|
|
208
|
+
- FTR also primes the external cache “for completeness” because the write is cheap.
|
|
209
|
+
- **Large ValueSets (> 50 unique codes)**:
|
|
210
|
+
- FTR uses a per-code LRU for in-memory caching.
|
|
211
|
+
- FTR attempts a one-time priming for that ValueSet so subsequent lookups can hit the external cache.
|
|
212
|
+
|
|
213
|
+
Important: priming is **best-effort**.
|
|
214
|
+
- If the external cache is unavailable or throws, FTR continues and still answers the lookup from local evaluation.
|
|
215
|
+
- Priming failures do not make `inValueSet` fail.
|
|
216
|
+
|
|
217
|
+
#### What does “primed” mean?
|
|
218
|
+
|
|
219
|
+
“Primed” is a state meaning: *“we believe the external cache already contains the bulk membership entries for this ValueSet key.”*
|
|
220
|
+
|
|
221
|
+
---
|
|
222
|
+
|
|
223
|
+
## ConceptMap translation (`translateConceptMap`)
|
|
224
|
+
|
|
225
|
+
FTR can translate codes using `ConceptMap` resources from FHIR packages.
|
|
226
|
+
|
|
227
|
+
API:
|
|
228
|
+
|
|
229
|
+
```ts
|
|
230
|
+
const result = await ftr.translateConceptMap('A', 'some-conceptmap');
|
|
231
|
+
|
|
232
|
+
if (result.status === 'mapped') {
|
|
233
|
+
console.log(result.targets);
|
|
234
|
+
} else {
|
|
235
|
+
console.log(result.reason);
|
|
236
|
+
}
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
Return type:
|
|
240
|
+
|
|
241
|
+
- `mapped`: one or more targets were found
|
|
242
|
+
- `unmapped`: no targets, with a reason
|
|
243
|
+
|
|
244
|
+
Input:
|
|
245
|
+
- A code string (common case; source `system` is implicit), or
|
|
246
|
+
- A Coding-like object `{ system, code }`.
|
|
247
|
+
|
|
248
|
+
Output:
|
|
249
|
+
- Returns a structured result object.
|
|
250
|
+
- When `status === 'mapped'`, `targets` is an array of **full target Codings** (never just a string code).
|
|
251
|
+
- When `status === 'unmapped'`, `reason` explains why no targets were returned.
|
|
252
|
+
|
|
253
|
+
Unmapped reasons:
|
|
254
|
+
- `code-not-in-conceptmap`: the source code is not present in the ConceptMap.
|
|
255
|
+
- `no-translation`: the source code exists, but no translation exists for the requested source system (or the mapping is empty).
|
|
256
|
+
- `unsupported-equivalence`: a mapping exists, but all targets were ignored because their `equivalence` values are unsupported. In this case, `ignoredEquivalences` is included.
|
|
257
|
+
- `duplicate-code`: returned only for **code-only** lookups when the same code exists under **multiple source systems** in the ConceptMap.
|
|
258
|
+
- `invalid-code`: the input code was empty.
|
|
259
|
+
|
|
260
|
+
Note: if the ConceptMap identifier cannot be resolved or the ConceptMap cannot be loaded, `translateConceptMap` throws.
|
|
261
|
+
|
|
262
|
+
### Equivalence handling
|
|
263
|
+
|
|
264
|
+
Only these `ConceptMap.group.element.target[].equivalence` values are used for translation:
|
|
265
|
+
|
|
266
|
+
```txt
|
|
267
|
+
equivalent
|
|
268
|
+
equal
|
|
269
|
+
wider
|
|
270
|
+
subsumes
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
If `equivalence` is missing (allowed in FHIR R3), it is treated as `equivalent`.
|
|
274
|
+
Any other equivalence value is ignored.
|
|
275
|
+
|
|
276
|
+
### Group flattening
|
|
277
|
+
|
|
278
|
+
All `ConceptMap.group[]` entries are treated as a single flattened mapping.
|
|
279
|
+
Group boundaries have no semantic meaning for translation in this runtime.
|
|
280
|
+
|
|
281
|
+
### Server ConceptMaps (optional `fhirClient`)
|
|
282
|
+
|
|
283
|
+
If you provide a `fhirClient` to `FhirTerminologyRuntime.create(...)`, `translateConceptMap` can resolve ConceptMaps from a FHIR server.
|
|
284
|
+
|
|
285
|
+
- When `conceptMap` is a string and `packageFilter` is **not** provided, the runtime tries the server first and then falls back to packages.
|
|
286
|
+
- When `packageFilter` is provided, the runtime skips server resolution and uses packages only.
|
|
287
|
+
|
|
288
|
+
Server resolution attempts (in order):
|
|
289
|
+
- `ConceptMap?url={identifier}`
|
|
290
|
+
- `ConceptMap/{identifier}`
|
|
291
|
+
- `ConceptMap?name={identifier}`
|
|
292
|
+
|
|
293
|
+
If the server ConceptMaps may change at runtime, you can clear cached server ConceptMaps:
|
|
294
|
+
|
|
295
|
+
```ts
|
|
296
|
+
await ftr.clearServerConceptMapsFromCache();
|
|
297
|
+
// or clear for a specific server base URL
|
|
298
|
+
await ftr.clearServerConceptMapsFromCache('https://example.org/fhir');
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
### Translation caching (`conceptMapCache`)
|
|
302
|
+
|
|
303
|
+
`translateConceptMap` uses multiple caching layers similar to `inValueSet`:
|
|
304
|
+
|
|
305
|
+
- In-memory LRU for **small** ConceptMaps (≤ 50 unique source codes), holding up to 20 ConceptMaps.
|
|
306
|
+
- In-memory LRU for **hot per-code** translation results, holding up to 1,000 lookups.
|
|
307
|
+
- Optional **external async cache** (injectable) used for cold-start / distributed caching.
|
|
308
|
+
|
|
309
|
+
Provide a `conceptMapCache` in `FhirTerminologyRuntime.create(...)`:
|
|
310
|
+
|
|
311
|
+
```ts
|
|
312
|
+
const ftr = await FhirTerminologyRuntime.create({
|
|
313
|
+
fpe,
|
|
314
|
+
cacheMode: 'lazy',
|
|
315
|
+
fhirVersion: '4.0.1',
|
|
316
|
+
conceptMapCache
|
|
317
|
+
});
|
|
318
|
+
```
|
|
319
|
+
|
|
320
|
+
The external ConceptMap cache is keyed by a deterministic ConceptMap namespace:
|
|
321
|
+
|
|
322
|
+
- Package ConceptMaps: `(packageId, packageVersion, filename)`
|
|
323
|
+
- Server ConceptMaps: (keyed by server base URL + ConceptMap URL)
|
|
324
|
+
|
|
325
|
+
Interface (simplified):
|
|
326
|
+
|
|
327
|
+
```ts
|
|
328
|
+
type ConceptMapDeterministicKey =
|
|
329
|
+
| { kind: 'package'; packageId: string; packageVersion: string; filename: string }
|
|
330
|
+
| { kind: 'server'; serverBaseUrl: string; url: string };
|
|
331
|
+
|
|
332
|
+
type ConceptMapTranslation = {
|
|
333
|
+
system: string;
|
|
334
|
+
code: string;
|
|
335
|
+
display?: string;
|
|
336
|
+
version?: string;
|
|
337
|
+
equivalence: 'equivalent' | 'equal' | 'wider' | 'subsumes';
|
|
338
|
+
};
|
|
339
|
+
|
|
340
|
+
type ConceptMapCacheEntry =
|
|
341
|
+
| {
|
|
342
|
+
status: 'found';
|
|
343
|
+
bySourceSystem: Record<string, { targets: ConceptMapTranslation[]; ignoredEquivalences?: string[] }>;
|
|
344
|
+
}
|
|
345
|
+
| { status: 'not-found' };
|
|
346
|
+
|
|
347
|
+
interface TerminologyConceptMapCache {
|
|
348
|
+
getCode(cm: ConceptMapDeterministicKey, code: string): Promise<ConceptMapCacheEntry | undefined>;
|
|
349
|
+
setCode(cm: ConceptMapDeterministicKey, code: string, entry: ConceptMapCacheEntry): Promise<void>;
|
|
350
|
+
|
|
351
|
+
bulkSetCodes?(cm: ConceptMapDeterministicKey, entries: Array<[string, ConceptMapCacheEntry]>): Promise<void>;
|
|
352
|
+
|
|
353
|
+
// Required: used for package reinstall / server ConceptMap reload operations
|
|
354
|
+
clearNamespace(namespacePrefix: string): Promise<void>;
|
|
355
|
+
}
|
|
356
|
+
```
|
|
357
|
+
|
|
358
|
+
Implementation note for cache authors:
|
|
359
|
+
- FTR provides the `namespacePrefix` string to `clearNamespace(...)`.
|
|
360
|
+
- Your implementation does **not** need to invent prefixes, but it **must** store keys in a way that preserves the prefix so prefix-clearing is possible (for example, avoid hashing the entire key in a way that prevents enumerating/deleting by prefix).
|
|
361
|
+
|
|
362
|
+
FTR supports two ways to track this state:
|
|
363
|
+
|
|
364
|
+
1) **Automatic external primed-state (default; no extra methods required)**
|
|
365
|
+
|
|
366
|
+
FTR automatically tracks whether a ConceptMap has been primed in the external cache using the **same `getCode/setCode` methods you already implement**.
|
|
367
|
+
|
|
368
|
+
Concretely:
|
|
369
|
+
- After a successful prime, FTR writes a reserved **sentinel entry** under a reserved “code” key.
|
|
370
|
+
- Before priming, FTR checks whether that sentinel entry exists.
|
|
371
|
+
|
|
372
|
+
This means the “standard cache interface” is just `getCode`/`setCode` (and optionally `bulkSetCodes`). You do **not** need to implement any additional primed-state methods.
|
|
373
|
+
|
|
374
|
+
What you might see in your cache:
|
|
375
|
+
- FTR may store an extra entry with `code = '__ftr__primed__'` to represent “this ValueSet has been primed”.
|
|
376
|
+
- This entry is internal bookkeeping and is never used as a real terminology code lookup.
|
|
377
|
+
|
|
378
|
+
Collision note (extremely unlikely): if a ValueSet legitimately contains the code `'__ftr__primed__'`, FTR will avoid using the sentinel mechanism for that ValueSet and will fall back to in-memory priming guards.
|
|
379
|
+
|
|
380
|
+
2) **In-memory primed-state (fallback)**
|
|
381
|
+
|
|
382
|
+
If the external cache is unavailable (or throws), FTR uses an in-memory Set (one per process) to avoid re-priming repeatedly within the same runtime instance.
|
|
383
|
+
|
|
384
|
+
This distinction matters:
|
|
385
|
+
- The in-memory guard prevents repeated priming *only within the current process*.
|
|
386
|
+
- The external primed-state prevents repeated priming across processes and restarts.
|
|
387
|
+
|
|
388
|
+
#### A concrete mental model
|
|
389
|
+
|
|
390
|
+
Consider a large ValueSet `VS` and a code lookup `inValueSet('C10', VS)`:
|
|
391
|
+
|
|
392
|
+
1) FTR tries the in-memory LRUs.
|
|
393
|
+
2) If configured, FTR tries the external cache for `(VS-key, 'C10')`.
|
|
394
|
+
3) If not found, FTR expands/indexes locally, answers the lookup, and then:
|
|
395
|
+
- **syncs** the single code result to the external cache, and
|
|
396
|
+
- may **prime** the external cache for `VS` (bulk write) so future codes (`C11`, `C12`, …) can be served externally.
|
|
397
|
+
|
|
398
|
+
#### Priming is not ValueSet expansion caching
|
|
399
|
+
|
|
400
|
+
FTR has two separate caching concepts:
|
|
401
|
+
- **Expansion caching**: stores the expanded ValueSet JSON on disk under `.ftr/` alongside packages.
|
|
402
|
+
- **Membership caching**: accelerates `inValueSet` lookups (in-memory LRUs + optional external cache).
|
|
403
|
+
|
|
404
|
+
“Priming” only refers to the **external membership cache** behavior.
|
|
405
|
+
|
|
406
|
+
## ValueSet Expansion Details
|
|
407
|
+
|
|
408
|
+
The expansion engine performs a deterministic local expansion when possible:
|
|
409
|
+
- Supports: `compose.include` (system + all codes, or explicit concept list), `compose.exclude`, and `include.valueSet` recursion (with cycle detection) plus JSON-style set semantics (union of includes, subtraction of excludes, intersection when combining explicit concepts with referenced ValueSets for the same system).
|
|
410
|
+
- Not supported yet: `include.filter` (expansion will throw an error). This intentionally surfaces intensional ValueSets so callers can fallback to an external terminology service if possible.
|
|
411
|
+
- Recursion: `include.valueSet` entries are resolved first in the source package; if not found there, a global context fallback is attempted.
|
|
412
|
+
- Fallback: If local generation fails but the original ValueSet resource contains an `expansion.contains`, that original expansion is returned and cached (no attempt is made to validate staleness).
|
|
413
|
+
- Displays: When an `include.concept` list supplies explicit codes with displays, the associated CodeSystem resource is not loaded (performance optimization).
|
|
414
|
+
|
|
415
|
+
### CodeSystem Resolution Rules
|
|
416
|
+
|
|
417
|
+
When expanding ValueSets the runtime resolves referenced CodeSystems by canonical URL (may be a versioned URL):
|
|
418
|
+
1. Attempt resolution within the originating ValueSet's package (exact version context).
|
|
419
|
+
2. If not found, fall back to global [`fhir-package-explorer`](https://github.com/Outburn-IL/fhir-package-explorer) context using semver-aware `resolveMeta` from FPE to pick a single best version (prevents duplicate version conflicts).
|
|
420
|
+
3. Only CodeSystems with `content = 'complete'` are eligible. Any other `content` will throw an expansion error.
|
|
421
|
+
4. CodeSystems themselves are NOT cached by FTR (they live in their package). Only the derived ValueSet expansion result is cached.
|
|
422
|
+
|
|
423
|
+
### Expansion Caching
|
|
424
|
+
|
|
425
|
+
Expanded (or fallback) ValueSets are cached in a dedicated `.ftr` directory alongside source packages. Repeated calls reuse the cached expansion unless `cacheMode` is `none`.
|
|
426
|
+
|
|
427
|
+
## Context
|
|
428
|
+
You must provide an array of FHIR packages in `context`. Any package or its dependencies missing in the local FHIR package cache will be downloaded and installed (by [`fhir-package-installer`](https://github.com/Outburn-IL/fhir-package-installer)).
|
|
429
|
+
|
|
430
|
+
Supports `<id>#<version>`, `<id>@<version>`, `<id>` (latest version) or a package identifier object e.g:
|
|
431
|
+
```
|
|
432
|
+
{
|
|
433
|
+
id: 'hl7.fhir.us.core',
|
|
434
|
+
version: '6.1.0'
|
|
435
|
+
}
|
|
436
|
+
```
|
|
437
|
+
|
|
438
|
+
## Cache Modes
|
|
439
|
+
|
|
440
|
+
| Mode | Behavior |
|
|
441
|
+
|-----------|------------------------------------------------------------------------------------------|
|
|
442
|
+
| `lazy` | *Default*. Generates and caches expansions on demand. |
|
|
443
|
+
| `ensure` | Ensures all ValueSets have cached expansions (missing ones are generated). |
|
|
444
|
+
| `rebuild` | Clears cache and regenerates all expansions from scratch. |
|
|
445
|
+
| `none` | Disables caching completely (expansions computed each call, nothing written). |
|
|
446
|
+
|
|
447
|
+
Cached artifacts are stored under:
|
|
448
|
+
|
|
449
|
+
```
|
|
450
|
+
<cachePath>/<packageId>#<packageVersion>/.ftr/<FTR version>/
|
|
451
|
+
```
|
|
452
|
+
- Filenames mirror originals in `<cachePath>/<packageId>#<packageVersion>/package`.
|
|
453
|
+
- FTR Version directory uses major.minor.x (e.g. `0.1.x`).
|
|
454
|
+
|
|
455
|
+
**DEVELOPER NOTICE** – Any change that affects expansion generation output MUST increment the minor version so previously cached results are not silently reused.
|
|
456
|
+
|
|
457
|
+
## Cache Path
|
|
458
|
+
`cachePath` defines the FHIR package cache directory to be used. This is passed through to [`fhir-package-explorer`](https://github.com/Outburn-IL/fhir-package-explorer) and [`fhir-package-installer`](https://github.com/Outburn-IL/fhir-package-installer).
|
|
459
|
+
If not provided, the default cache location will be used.
|
|
460
|
+
See: [Package Cache Directory section](https://github.com/Outburn-IL/fhir-package-installer/blob/main/README.md#package-cache-directory) in FPI's readme for details.
|
|
461
|
+
|
|
462
|
+
## FHIR Version
|
|
463
|
+
|
|
464
|
+
Specify the default FHIR version with the `fhirVersion` option. This determines which base definitions are used when none are explicitly imported through dependencies.
|
|
465
|
+
If not specified, defaults to `4.0.1` (FHIR R4).
|
|
466
|
+
|
|
467
|
+
## Roadmap
|
|
468
|
+
- External terminology service support (fallback for intensional ValueSets)
|
|
469
|
+
- ConceptMap lookup and translation operations
|
|
470
|
+
- LMDB-based caching for improved performance
|
|
471
|
+
- `include.filter` support for local logical expansion
|
|
472
|
+
- Terminology validation operations
|
|
473
|
+
- Expansion parameterization (e.g. date constraints, designations)
|
|
474
|
+
|
|
475
|
+
## License
|
|
476
|
+
|
|
477
|
+
Apache License 2.0
|
|
478
|
+
© Outburn Ltd. 2022–2025. All Rights Reserved.
|