Repository navigation
feat: Add unit-of-work identity map with nested transaction savepoints - #7
Merged
Merged
Conversation
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.