Skip to content

feat: Add unit-of-work identity map with nested transaction savepoints - #7

Merged
ThomasHartDev merged 1 commit into
mainfrom
thomas/feat/unit-of-work
Sep 16, 2026
Merged

ThomasHartDev merged 1 commit into
mainfrom
thomas/feat/unit-of-work

Conversation

@ThomasHartDev

Copy link
Copy Markdown
Owner

This adds the piece the ORM was missing on the write side. A UnitOfWork tracks entities you attach or register as new, keeps a session-scoped identity map so two loads of the same row by pk return one object, and does dirty checking by diffing the live object against the snapshot it took at load time instead of wrapping it in a Proxy. flush() batches inserts, only-changed-column updates, and deletes into a single transaction and only touches the session if something actually changed.

The transaction side is a TransactionManager that makes BEGIN/COMMIT/ROLLBACK nest correctly. Neither Postgres nor SQLite support a real nested BEGIN, so anything past the outermost call turns into SAVEPOINT/RELEASE SAVEPOINT/ROLLBACK TO SAVEPOINT with depth-based names. UnitOfWork.flush() runs through the same manager, so it composes with an app's own transaction: nest a flush inside a manual tx.begin()/tx.rollback() and the flush's writes get undone too, since its "commit" was really just a savepoint release.

Tested against real node:sqlite (same pattern the relations tests already use): identity map reuse, missing-pk and duplicate-registration errors, insert/update/delete batching, only-changed-columns updates, removing a never-flushed entity being a pure no-op instead of issuing a DELETE, a failing flush rolling back every write in that batch while leaving entities pending for retry, and nested savepoints where an inner failure unwinds only its own block.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@ThomasHartDev
ThomasHartDev merged commit 78ee5f5 into main Sep 16, 2026
1 check 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.

1 participant