Repository navigation
feat: support typeorm slave option - #694
Open
jamesacres wants to merge 2 commits into
Open
jamesacres wants to merge 2 commits into
jamesacres wants to merge 2 commits into
Conversation
This branch has not been deployed
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.
What
Adds an optional
mode: 'slave'to the TypeORM adapter's transaction options so a whole transaction can be pinned to a read replica. When set, every read in the callback — including custom query-builder reads, which TypeORM would otherwise send to the master — runs on a replica query runner. Default behaviour (mode: 'master') is unchanged, so this is purely additive for existing users.Why
TypeORM's replication only routes plain
find-style reads to a replica; anything built with the query builder still hits the master. We need some custom query-builder reads to go to the replica.Worth a close look
transactional-adapter-typeorm.ts—wrapWithReplicaTransactionis the only new runtime path. It manages the query runner's lifecycle by hand (connect→startTransaction→ commit/rollback →releaseinfinally) becausedataSource.transaction()always uses the master and can't be told to use a replica. Worth confirming the release/rollback ordering is what you'd expect.wrapWithNestedTransactionguard.masterpath) is untouched; the twoif (options?.mode === 'slave')guards are the only insertions into existing methods.DataSourcenow usesreplicationconfig, and there's a newSlave modedescribe block. The existing tests and their setup are unchanged apart from the datasource config.Verified
yarn testin the typeorm adapter package: 12/12 pass, including the 4 new slave-mode tests. Tests run against the existing dockerised Postgres onlocalhost:5446.yarn buildfor core → transactional → typeorm adapter: clean.yarn format/format:check: clean.createQueryRunner('slave')), isolation-level pass-through (startTransaction('SERIALIZABLE')when bothmodeandisolationLevelare given), query-runner release on success and on error, and the nested-transaction rejection.DataSourcepoints both master and slave at the same database (there's a single test instance), so the tests verify that the slave connection/query runner is used and options flow through it, not that reads physically land on a distinct replica. Behaviour against a genuinely separate replica relies on TypeORM's owncreateQueryRunner('slave')routing.Notes
Transactionis now atype-only import. It was imported as a value from aexport typere-export, which fails under the repo's Jest ESM runtime — the suite couldn't load at all before this. Independent of the feature, but the suite can't run without it. (If you'd prefer, I can split this into its own commit/PR.)CHANGES.mdfrom commits, so this should flow from the commit message. Flag if you want it done manually.modeis optional and defaults to the existing master behaviour.