registry

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 22, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

README

Registry

Shared FHIR definition catalog for the monorepo.

Scope

  • Bundled HL7 FHIR R4 base StructureDefinition and SearchParameter resources
  • Persistent catalog storage via pkg/store
  • SQLite and Postgres backends (pkg/sqlite, pkg/postgres)
  • In-memory compiled snapshot for fast lookups
  • Resource enablement overlay
  • Capability snapshot for future HTTP consumers

Usage

manager := registry.NewManager(registry.Config{
    Definitions: db.DefinitionStore(),
    Installs:    db.RegistryInstallStore(),
})
_ = manager.SeedBundled(ctx)
_ = manager.InstallDefinitionsFromDir(ctx, "/path/to/custom/definitions", registry.InstallProvenance{
    PackageName: "example.custom",
})
_ = manager.EnableResource(ctx, "Patient")
snapshot, _ := manager.RebuildSnapshot(ctx)

SeedBundled only loads the embedded HL7 bundle. Use InstallDefinitionsFromDir or InstallDefinitionsFromFS for custom definition JSON trees on disk.

Postgres uses a hybrid model: the base catalog is global on postgres.DB; enablement lives on postgres.TenantDB.

Bundled catalog

Base definitions are vendored under internal/bundles/r4/ from HL7 definitions.json.zip:

make generate-r4-bundle

This writes 148 base resource StructureDefinition files and 1375 base SearchParameter files.

Snapshot

Snapshot implements validate.ResourceTypeRegistry and exposes:

  • enabled resource checks
  • base structure definitions
  • search parameter metadata and expressions
  • canonical lookup
  • empty profile and operation buckets reserved for future compilation

Unknown future definition kinds can be stored through InstallDefinition and are ignored by the MVP compiler.

Documentation

Overview

Package registry provides the shared FHIR definition catalog for the monorepo.

It seeds bundled R4 StructureDefinition and SearchParameter resources, persists catalog and install overlay state through pkg/store contracts, and compiles an immutable in-memory snapshot for fast runtime lookups.

Index

Constants

View Source
const BundledCorePackageName = "hl7.fhir.r4.core"

BundledCorePackageName is the package id for embedded R4 base definitions. SeedBundled intentionally does not enqueue ValueSet pre-expand for this pack.

View Source
const DefaultFHIRVersion = "4.0.1"
View Source
const ModulesPackageName = "haistack-modules"

ModulesPackageName is the prefix for haistack module package ids.

Variables

View Source
var (
	ErrInvalidDefinition  = errors.New("invalid definition resource")
	ErrMissingDefinition  = errors.New("missing required definition")
	ErrSnapshotCompile    = errors.New("snapshot compilation failed")
	ErrResourceNotEnabled = errors.New("resource type not enabled")
	ErrDefinitionNotFound = errors.New("definition not found")
	ErrDefinitionConflict = errors.New("definition ownership conflict")
)

Functions

func ContextWithJobTerminologyInstalls

func ContextWithJobTerminologyInstalls(ctx context.Context, job store.JobRecord, factory store.TerminologyInstallStoreFactory, defaultTenantID string) (context.Context, error)

ContextWithJobTerminologyInstalls opts the job owner's tenant into terminology installs. When the job has no tenant owner, defaultTenantID is used.

func ContextWithTerminologyInstalls

func ContextWithTerminologyInstalls(ctx context.Context, installs store.TerminologyInstallStore) context.Context

ContextWithTerminologyInstalls overrides the terminology opt-in store for one call tree.

func DefinitionStoreWithEmbeddedBase

func DefinitionStoreWithEmbeddedBase(primary store.DefinitionStore) store.DefinitionStore

DefinitionStoreWithEmbeddedBase returns a store that resolves from primary first and falls back to bundled HL7 R4 StructureDefinitions.

func EmbeddedDefinitionStore

func EmbeddedDefinitionStore() store.DefinitionStore

EmbeddedDefinitionStore resolves HL7 R4 base StructureDefinitions from the embedded bundle.

func LoadDefinitionJSONs

func LoadDefinitionJSONs(fsys fs.FS, root string) ([][]byte, error)

LoadDefinitionJSONs reads every .json file under root from fsys in walk order.

func ModulesPackageID

func ModulesPackageID(moduleName string) string

ModulesPackageID returns the package id for one module (haistack-modules/<name>). Completion is tracked as ModulesPackageID(name)@manifestVersion.

func TerminologyInstallsFromContext

func TerminologyInstallsFromContext(ctx context.Context) store.TerminologyInstallStore

TerminologyInstallsFromContext returns a per-request terminology opt-in store when set.

func WarmBundledDefinitionCache

func WarmBundledDefinitionCache() error

WarmBundledDefinitionCache preloads embedded HL7 definition bytes used by SeedBundled.

Types

type CapabilitySnapshot

type CapabilitySnapshot struct {
	FHIRVersion string               `json:"fhirVersion"`
	Resources   []ResourceCapability `json:"resources"`
	CompiledAt  time.Time            `json:"compiledAt"`
}

CapabilitySnapshot is a lightweight capability view for HTTP and runtime consumers.

type CompositeComponent

type CompositeComponent struct {
	Definition string `json:"definition"`
	Expression string `json:"expression"`
}

CompositeComponent describes one component of a composite SearchParameter.

type Config

type Config struct {
	Definitions         store.DefinitionStore
	Installs            store.RegistryInstallStore
	FHIRVersion         string
	Now                 func() time.Time
	SearchReindex       SearchReindexNotifier
	Terminology         store.TerminologyStore
	TerminologyScope    string
	GlobalTerminology   store.TerminologyStore
	TerminologyInstalls store.TerminologyInstallStore
	TerminologyCache    terminology.Invalidator
	JobStore            store.JobStore
	PackageInstalls     store.PackageInstallStore
	PreExpandValueSets  bool
}

Config configures a registry Manager.

type DefinitionRef

type DefinitionRef struct {
	CanonicalURL string `json:"canonicalUrl"`
	Version      string `json:"version"`
}

DefinitionRef identifies one installed definition by canonical URL and version.

type InstallProvenance

type InstallProvenance struct {
	PackageName    string
	PackageVersion string
	ModuleName     string
	SourceModule   string
}

InstallProvenance captures where an installed definition came from.

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager seeds, installs, enables, and compiles the FHIR definition catalog.

func NewManager

func NewManager(cfg Config) *Manager

NewManager constructs a registry manager from persistence stores.

func (*Manager) CompletePackageInstall

func (m *Manager) CompletePackageInstall(ctx context.Context, provenance InstallProvenance) error

CompletePackageInstall enqueues one batched ValueSet pre-expand job for a package after callers finish a loop of InstallDefinition calls and records completion.

func (*Manager) DeleteDefinition

func (m *Manager) DeleteDefinition(ctx context.Context, canonicalURL, version string) error

DeleteDefinition removes a catalog entry and its terminology projection.

func (*Manager) DisableResource

func (m *Manager) DisableResource(ctx context.Context, resourceType string) error

DisableResource marks a resource type as disabled in the install overlay.

func (*Manager) EnableResource

func (m *Manager) EnableResource(ctx context.Context, resourceType string) error

EnableResource marks a resource type as enabled when its base StructureDefinition exists.

func (*Manager) EnsureTerminologyPackEnabled

func (m *Manager) EnsureTerminologyPackEnabled(ctx context.Context, packName, packVersion, sourceModule string) error

EnsureTerminologyPackEnabled opts the current tenant into all CodeSystems and ValueSets from an already-installed package version. Safe to call repeatedly; explicit tenant opt-out is preserved.

func (*Manager) InstallDefinition

func (m *Manager) InstallDefinition(ctx context.Context, jsonData []byte, provenance InstallProvenance) error

InstallDefinition ingests one additional definition resource with provenance.

func (*Manager) InstallDefinitionsFromDir

func (m *Manager) InstallDefinitionsFromDir(ctx context.Context, dir string, provenance InstallProvenance) error

InstallDefinitionsFromDir ingests every .json definition file under dir on the local filesystem.

func (*Manager) InstallDefinitionsFromFS

func (m *Manager) InstallDefinitionsFromFS(ctx context.Context, fsys fs.FS, root string, provenance InstallProvenance) error

InstallDefinitionsFromFS ingests every .json definition file under root from fsys.

func (*Manager) PackageVersionInstalled

func (m *Manager) PackageVersionInstalled(ctx context.Context, packageName, packageVersion string) (bool, error)

PackageVersionInstalled reports whether packageName@packageVersion finished install successfully (CompletePackageInstall was called). When PackageInstalls is not wired (unit tests only), this always returns false so installs proceed.

func (*Manager) RebuildSnapshot

func (m *Manager) RebuildSnapshot(ctx context.Context) (*Snapshot, error)

RebuildSnapshot reloads catalog and install state into an immutable compiled view.

func (*Manager) SeedBundled

func (m *Manager) SeedBundled(ctx context.Context) error

SeedBundled loads embedded R4 base definitions into the catalog idempotently. Bundled core definitions never enqueue ValueSet pre-expand jobs; see BundledCorePackageName.

func (*Manager) Snapshot

func (m *Manager) Snapshot() *Snapshot

Snapshot returns the last compiled snapshot, if any.

type ParsedDefinition

type ParsedDefinition struct {
	CanonicalURL     string
	Version          string
	FHIRVersion      string
	FHIRResourceType string
	DefinitionKind   store.DefinitionKind
	Name             string
	Status           string
}

ParsedDefinition is normalized metadata extracted from one definition JSON resource.

func ParseDefinition

func ParseDefinition(jsonData []byte) (ParsedDefinition, []store.DefinitionTargetRecord, error)

ParseDefinition extracts catalog metadata and target mappings from raw JSON.

type PatientReferenceResolver

type PatientReferenceResolver struct {
	Snapshot *Snapshot
	Engine   fhirpath.Engine
	// contains filtered or unexported fields
}

PatientReferenceResolver extracts the patient id linked to a FHIR resource using the same SearchParameter selection as patient-scoped search.

func (*PatientReferenceResolver) PatientIDForResource

func (r *PatientReferenceResolver) PatientIDForResource(ctx context.Context, resourceType string, resource *types.ResourceEnvelope) (string, bool, error)

PatientIDForResource returns the patient id when the resource is linked to exactly one patient via the registry patient scope SearchParameter.

func (*PatientReferenceResolver) SetSnapshot

func (r *PatientReferenceResolver) SetSnapshot(snapshot *Snapshot)

SetSnapshot replaces the compiled snapshot used for patient resolution.

type ResourceCapability

type ResourceCapability struct {
	ResourceType        string                `json:"resourceType"`
	StructureDefinition *DefinitionRef        `json:"structureDefinition,omitempty"`
	SearchParameters    []SearchParameterInfo `json:"searchParameters,omitempty"`
}

ResourceCapability describes one enabled resource type in the compiled registry.

type SearchParameterInfo

type SearchParameterInfo struct {
	CanonicalURL string               `json:"canonicalUrl"`
	Version      string               `json:"version"`
	Code         string               `json:"code"`
	Name         string               `json:"name"`
	Type         string               `json:"type"`
	Expression   string               `json:"expression"`
	Target       []string             `json:"target,omitempty"`
	Component    []CompositeComponent `json:"component,omitempty"`
	// BaseCount is the number of resource types in the SearchParameter.base array.
	// Single-base parameters are preferred for patient scoping because they index
	// reliably; cross-resource parameters such as clinical-patient may not.
	BaseCount int `json:"baseCount,omitempty"`
}

SearchParameterInfo is compiled search parameter metadata for one resource type.

type SearchParameterReindexNotifier

type SearchParameterReindexNotifier interface {
	SearchReindexNotifier
	ScheduleSearchParameterReindex(ctx context.Context, canonicalURL, version string, resourceTypes ...string) error
}

SearchParameterReindexNotifier schedules reindex jobs for one SearchParameter definition.

type SearchReindexNotifier

type SearchReindexNotifier interface {
	ScheduleReindex(ctx context.Context, resourceTypes ...string) error
}

SearchReindexNotifier schedules search reindex jobs when search parameters change.

type Snapshot

type Snapshot struct {
	// contains filtered or unexported fields
}

Snapshot is an immutable compiled registry view.

func CompileSnapshot

func CompileSnapshot(
	ctx context.Context,
	definitions store.DefinitionStore,
	installs store.RegistryInstallStore,
	fhirVersion string,
	now func() time.Time,
) (*Snapshot, error)

CompileSnapshot builds an immutable registry view from persisted catalog state.

func (*Snapshot) AnyDefinitionByCanonical

func (s *Snapshot) AnyDefinitionByCanonical(canonicalURL string) ([]byte, bool)

AnyDefinitionByCanonical returns raw JSON for canonicalURL, preferring common FHIR and module versions when multiple are installed.

func (*Snapshot) CapabilitySnapshot

func (s *Snapshot) CapabilitySnapshot() CapabilitySnapshot

CapabilitySnapshot returns a lightweight capability view for runtime consumers.

func (*Snapshot) DefinitionsByCanonical

func (s *Snapshot) DefinitionsByCanonical(canonicalURL, version string) ([]byte, bool)

DefinitionsByCanonical returns raw JSON for canonicalURL and version.

func (*Snapshot) EnabledResourceTypes

func (s *Snapshot) EnabledResourceTypes() []string

EnabledResourceTypes returns installed resource types in sorted order.

func (*Snapshot) IsInstalled

func (s *Snapshot) IsInstalled(resourceType string) bool

IsInstalled implements validate.ResourceTypeRegistry.

func (*Snapshot) IsResourceEnabled

func (s *Snapshot) IsResourceEnabled(resourceType string) bool

IsResourceEnabled reports whether resourceType is enabled in the compiled snapshot.

func (*Snapshot) Operations

func (s *Snapshot) Operations() []DefinitionRef

Operations returns installed operation definition references. Reserved for future operation compilation; empty in the MVP snapshot.

func (*Snapshot) PatientSearchParameter

func (s *Snapshot) PatientSearchParameter(resourceType string) (SearchParameterInfo, bool)

PatientSearchParameter returns the installed SearchParameter used to scope resourceType to one patient.

func (*Snapshot) PatientSearchParameterCode

func (s *Snapshot) PatientSearchParameterCode(resourceType string) (string, bool)

PatientSearchParameterCode returns the FHIR search parameter code that scopes resourceType to one Patient, derived from installed reference SearchParameters. Resource-specific parameters are preferred over cross-resource definitions such as clinical-patient so the chosen code matches indexed search fields.

func (*Snapshot) ProfilesFor

func (s *Snapshot) ProfilesFor(resourceType string) []DefinitionRef

ProfilesFor returns installed profile references for resourceType. Reserved for future profile compilation; empty in the MVP snapshot.

func (*Snapshot) SearchExpression

func (s *Snapshot) SearchExpression(resourceType, code string) (string, bool)

SearchExpression returns the FHIRPath search expression for resourceType and code.

func (*Snapshot) SearchParameter

func (s *Snapshot) SearchParameter(resourceType, code string) (*SearchParameterInfo, bool)

SearchParameter returns one search parameter for resourceType and code.

func (*Snapshot) SearchParametersFor

func (s *Snapshot) SearchParametersFor(resourceType string) []SearchParameterInfo

SearchParametersFor returns installed search parameters for an enabled resource type.

func (*Snapshot) StructureDefinitionFor

func (s *Snapshot) StructureDefinitionFor(resourceType string) ([]byte, bool)

StructureDefinitionFor returns the compiled base StructureDefinition JSON for resourceType.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL