Skip to content

feat(catalog): build nemar_catalog from local D1, no legacy nemar.org pull - #546

Merged
neuromechanist merged 2 commits into
devfrom
feature/catalog-from-local-source
May 18, 2026
Merged

neuromechanist merged 2 commits into
devfrom
feature/catalog-from-local-source

Conversation

@neuromechanist

Copy link
Copy Markdown
Contributor

Closes the architectural gap in #544.

Two changes

1. Hot path: syncNemarCatalogFromEnrichment is now UPSERT

Was UPDATE-only and silently logged "no row exists" when a freshly-enriched dataset didn't have a catalog row (see #543 for the historical impact: ~100 datasets stuck in that state on prod). New behavior:

INSERT INTO nemar_catalog (...) VALUES (...)
ON CONFLICT(id) DO UPDATE SET
  name = COALESCE(excluded.name, nemar_catalog.name),
  modalities = COALESCE(excluded.modalities, nemar_catalog.modalities),
  ...
  synced_at = datetime('now')
  • First enrichment of any new dataset auto-creates the catalog row -- no operator action required.
  • Existing rows still get COALESCE-preserve semantics on the UPDATE path (null inputs don't clobber).
  • Requires non-null name because nemar_catalog.name is NOT NULL. Both callers (enrich-dataset.ts, dataset-reindex.ts) thread dataset.name from the d1 row through; LLM-derived title is preferred when present.

2. Bulk path: syncCatalogFromLocal() + admin endpoint

New backend/src/services/catalog-from-local.ts reads only from D1:

  • buildCatalogRecordFromLocal(row, enrichment) -- pure projection. Tested across object/array author shapes, null enrichment, on* mirrors, enrichment.title/description preference, search_text composition.
  • syncCatalogFromLocal(db) -- iterates every active public dataset, builds records, INSERT OR REPLACEs in batches.
  • New POST /admin/catalog/sync-local admin endpoint triggers the sweep.

Use cases:

Out of scope

Tests

10 unit tests for the projector (id/source preservation, fallback chains for name/description, author shape detection, null defaults, search_text composition).

Typecheck clean.

Refs #544.

… pull

Closes the architectural gap that #541 / #543 closed for historical
rows: catalog-sync.ts only knows how to pull from
https://nemar.org/api/dataexplorer/datapipeline, so any dataset
created via the new in-process pipeline never gets a catalog row
unless an operator runs a migration. Two changes make the local
path the source going forward.

1. Hot path: syncNemarCatalogFromEnrichment now UPSERTs.
   Was an UPDATE-only that silently logged 'no row exists' when a
   freshly-enriched dataset didn't have a catalog entry. New behavior:
   INSERT ... ON CONFLICT(id) DO UPDATE so the first enrichment of any
   new dataset auto-creates its catalog row. Requires a non-null
   'name' on the call site (nemar_catalog.name is NOT NULL); both
   callers (enrich-dataset.ts, dataset-reindex.ts) now thread name
   from the d1 row.

2. Bulk path: new backend/src/services/catalog-from-local.ts.
   - buildCatalogRecordFromLocal() pure projector: D1 row + parsed
     enrichment_json -> NemarCatalogRecord shape. Tested for every
     observed shape (object/array authors, null enrichment, on*
     mirrors, name/description fallback chain, search_text).
   - syncCatalogFromLocal() walks every active public dataset and
     INSERT-OR-REPLACEs the catalog row from local data. Batched to
     stay under D1 bound-param limits.
   - New POST /admin/catalog/sync-local endpoint triggers the sweep.

Legacy catalog-sync.ts stays in place; #544 tracks the cron switch
and eventual retirement. Migration 0023 + 0024 already healed the
historical state, and the new hot-path UPSERT + bulk endpoint mean
no future dataset can land in the empty-catalog state.

10 new unit tests for buildCatalogRecordFromLocal cover all the
shape branches plus null defaults. Typecheck clean.

Refs #544.
- syncCatalogFromLocal: switch INSERT OR REPLACE -> UPSERT
  (INSERT ... ON CONFLICT(id) DO UPDATE SET ...). INSERT OR REPLACE
  in SQLite is DELETE + INSERT which silently clobbered columns this
  path doesn't manage (readme, bids_version, sessions_count,
  latest_version, is_processed). Those are populated by the legacy
  catalog-sync.ts pull for ~70 datasets and would have gone NULL on
  every sweep. UPSERT preserves them.
- Add datasets.published_at to the SELECT, prefer it for publish_date
  on the LocalCatalogRecord. Was using created_at, which would push
  freshly-published rows to the top of newest-first ordering only by
  upload timestamp, ignoring the months-long gap between upload and
  publish for legacy datasets.
- New structural test test/catalog-from-local-sql.unit.test.ts pins
  the UPSERT choice and the deliberate column omission set so a
  future refactor can't silently regress to INSERT OR REPLACE or add
  a column without a real source for the value.
- New structural test test/catalog-upsert-sql.unit.test.ts pins
  syncNemarCatalogFromEnrichment's UPSERT + COALESCE-preserve on
  every updatable column. Catches any revert that would re-introduce
  the silent-no-op on missing rows (#544 root cause).
- Test fixture: add published_at + publish_date preference test.

18 unit tests pass.
@neuromechanist

Copy link
Copy Markdown
Contributor Author

pr-review-toolkit review (sonnet)

Two reviewers, all findings addressed in d1f3c66.

code-reviewer (REQUEST CHANGES)

Finding Resolution
INSERT OR REPLACE silently drops 6 columns (readme, bids_version, sessions_count, latest_version, is_processed) populated by legacy catalog-sync.ts Switched to INSERT ... ON CONFLICT(id) DO UPDATE SET .... The 21 columns this path manages get refreshed; the omitted columns survive on the UPDATE branch.
publish_date = created_at ignores the real datasets.published_at Added published_at to the SELECT + LocalDatasetRow interface; publish_date = published_at ?? created_at.

pr-test-analyzer (NEEDS MORE COVERAGE)

Gap Resolution
syncNemarCatalogFromEnrichment UPSERT contract untested New test/catalog-upsert-sql.unit.test.ts pins ON CONFLICT(id) DO UPDATE + COALESCE-preserve on every updatable column
syncCatalogFromLocal column-set divergence from catalog-sync.ts untested New test/catalog-from-local-sql.unit.test.ts pins UPSERT + the deliberate column-omission set (so a future PR can't quietly add readme to the INSERT and re-introduce the data-loss hazard)
published_at preference untested New unit test asserts publish_date prefers published_at and falls back to created_at; created_date always tracks upload time

Dynamic-import question: kept as-is (purely stylistic; matches the lazy-import pattern in admin.ts).

18 unit tests (added 8), typecheck clean, biome clean.

@github-actions

Copy link
Copy Markdown

📦 Package Preview

Your package is ready to test!

bun install -g nemar-cli@PR546

Version: 0.8.24-PR546.d1f3c66
Commit: d1f3c66


🤖 Updated on each push.

@neuromechanist
neuromechanist merged commit 7622313 into dev May 18, 2026
8 of 9 checks passed
@neuromechanist
neuromechanist deleted the feature/catalog-from-local-source branch May 18, 2026 15:44

This branch was previously deployed

1 inactive deployment
npm — d1f3c66e Deployed May 18, 2026 by neuromechanist via publish #1003
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.

1 participant