Skip to content

Add non-destructive db.upsertItems() API #905

@SorraTheOrc

Description

@SorraTheOrc

Summary

Add a upsertItems(items, dependencyEdges?) method to the Database class that saves items via INSERT OR REPLACE without calling clearWorkItems(), eliminating the destructive clear-and-replace pattern.

User Story

As a developer working on GitHub integration flows, I want a safe method to update a subset of work items in the database without risking deletion of unrelated items, so that partial-set updates (delegate, push, import) cannot cause data loss.

Acceptance Criteria

  • db.upsertItems(items) saves each item using saveWorkItem() (INSERT OR REPLACE) without clearing existing items.
  • When dependencyEdges is provided, only edges for the affected item IDs are upserted (not a full clear-and-replace).
  • After upserting, exportToJsonl() and triggerAutoSync() are called exactly once.
  • Calling upsertItems([itemA]) when itemB and itemC exist does not delete itemB or itemC.
  • Calling upsertItems([itemA]) when itemA already exists updates itemA in place.
  • Calling upsertItems([]) with an empty array does not delete any items and does not trigger export/sync.
  • The existing db.import() method is not modified.

Minimal Implementation

  • Add upsertItems(items: WorkItem[], dependencyEdges?: DependencyEdge[]): void to src/database.ts.
  • Iterate items calling this.store.saveWorkItem(item). For edges, upsert only edges where fromId or toId is in the provided items.
  • Call exportToJsonl() and triggerAutoSync() once at the end (skip if items array is empty).
  • Add unit tests in tests/unit/database-upsert.test.ts.

Key Files

  • src/database.ts:1668-1686 — location for new method (adjacent to existing import())
  • src/persistent-store.ts:584-586clearWorkItems() (what we are avoiding)
  • src/persistent-store.tssaveWorkItem() uses INSERT OR REPLACE (the safe path)

Dependencies

None (foundational).

Deliverables

  • Source change in src/database.ts
  • Unit test file tests/unit/database-upsert.test.ts

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions