Skip to content

feat: referential integrity on write and query-time search narrowing - #53

Merged
degoke merged 7 commits into
mainfrom
degoke/referential-integrity-86c6
Sep 21, 2026
Merged

degoke merged 7 commits into
mainfrom
degoke/referential-integrity-86c6

Conversation

@degoke

@degoke degoke commented Sep 21, 2026

Copy link
Copy Markdown
Owner

HAPI-style write-time referential integrity and search narrowing that rewrites the query instead of fetching then hiding primary matches.

Merged main after #52 (hook SPI). Persist prep is pre-storage → referential integrity → version meta. The placeholder writeHooks SPI is gone; prepareWrite calls the real hooks.Hooks pre-storage point, then Exists.

Referential integrity

On create/update (including transaction-bundle entries), pkg/core walks local typed relative references with types.GetReferences and requires ResourceStore.Exists before persist.

Skipped:

  • contained fragments (#id)
  • absolute http(s) URLs
  • untyped ids
  • self-references
  • history suffixes (Type/id/_history/vid checks Type/id)

EnforceReferentialIntegrity defaults on; set the pointer to false to disable.

Same-bundle entries (including Observation-before-Patient and urn:uuid: fullUrls that map to an entry) satisfy Exists.

Missing local targets return ErrorKindInvalid.

Search narrowing

Patient compartment and SMART 2.2 scope filters inject query parameters (subject=Patient/{id}, scope category=…). Bundle.total stays the query-time count. Primary match rows are still compartment/SMART-checked as a safety net if a backend ignores the rewrite. _include / _revinclude and empty-mode ($everything) entries remain filtered.

History filtering is unchanged.

Review follow-ups

  • gofmt pkg/core/doc.go
  • Restore match post-filter without changing Total
  • Integrity disable flag
  • Unordered transactions and urn:uuid intra-bundle refs
  • After feat: four-point hook SPI (incoming, pre-storage, post-commit, outgoing) #52: prepareWrite uses real pre-storage hooks, then integrity, then version meta
  • Delete uses the pre-storage replacement for persist/history/events/post-commit; Previous and terminology cleanup keep the stored clone
  • Postgres search tests create Encounter/enc-1 before Observation so default-on integrity no longer fails on the dangling encounter ref
Open in Web Open in Cursor 

cursoragent and others added 5 commits September 21, 2026 09:22
Check local typed references with GetReferences + Exists before persist.
Rewrite patient and SMART search params instead of fetch-then-hide of
primary matches; keep compartment checks on include/revinclude entries.

Co-authored-by: Adegoke Adewoye <[email protected]>
Search narrowing skips post-filter only for mode=match. Empty-mode
operation bundles still hide out-of-scope entries. Transaction tests
create the referenced Patient with POST so Exists sees it.

Co-authored-by: Adegoke Adewoye <[email protected]>
Keep query rewrite while post-filtering out-of-scope match rows without
recomputing Bundle.total. Add EnforceReferentialIntegrity (default on)
and treat intra-bundle Type/id plus matching urn:uuid fullUrl values as
present so unordered transaction creates succeed.

Co-authored-by: Adegoke Adewoye <[email protected]>
Rewrite intra-bundle urn:uuid references to Type/id before persist so stored JSON matches assigned identities. Route writes through prepareWrite so optional pre-storage always runs before referential integrity and version meta.

Co-authored-by: Adegoke Adewoye <[email protected]>
@degoke
degoke marked this pull request as ready for review September 21, 2026 11:55
cursoragent and others added 2 commits September 21, 2026 11:59
Keep both Hooks and EnforceReferentialIntegrity. prepareWrite now runs
the real pre-storage SPI, then Exists checks, then version meta.

Co-authored-by: Adegoke Adewoye <[email protected]>
Use the envelope returned by pre-storage for delete persist, history,
events, and post-commit. Keep a clone of the stored resource for
terminology cleanup and Previous. Create Encounter/enc-1 before
Observation in postgres search tests so default-on integrity succeeds.

Co-authored-by: Adegoke Adewoye <[email protected]>
@degoke
degoke merged commit 22f08d8 into main Sep 21, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants