@@ -11,6 +11,7 @@ import {
11
11
ObjectType ,
12
12
Repository ,
13
13
SelectQueryBuilder ,
14
+ ReplicationMode ,
14
15
} from 'typeorm' ;
15
16
16
17
import { RequestContext } from '../api/common/request-context' ;
@@ -70,25 +71,63 @@ export class TransactionalConnection {
70
71
* Returns a TypeORM repository which is bound to any existing transactions. It is recommended to _always_ pass
71
72
* the RequestContext argument when possible, otherwise the queries will be executed outside of any
72
73
* ongoing transactions which have been started by the {@link Transaction} decorator.
74
+ *
75
+ * The `options` parameter allows specifying additional configurations, such as the `replicationMode`,
76
+ * which determines whether the repository should interact with the master or replica database.
77
+ *
78
+ * @param ctx - The RequestContext, which ensures the repository is aware of any existing transactions.
79
+ * @param target - The entity type or schema for which the repository is returned.
80
+ * @param options - Additional options for configuring the repository, such as the `replicationMode`.
81
+ *
82
+ * @returns A TypeORM repository for the specified entity type.
73
83
*/
74
84
getRepository < Entity extends ObjectLiteral > (
75
85
ctx : RequestContext | undefined ,
76
86
target : ObjectType < Entity > | EntitySchema < Entity > | string ,
87
+ options ?: {
88
+ replicationMode ?: ReplicationMode ;
89
+ } ,
77
90
) : Repository < Entity > ;
91
+ /**
92
+ * @description
93
+ * Returns a TypeORM repository. Depending on the parameters passed, it will either be transaction-aware
94
+ * or not. If `RequestContext` is provided, the repository is bound to any ongoing transactions. The
95
+ * `options` parameter allows further customization, such as selecting the replication mode (e.g., 'master').
96
+ *
97
+ * @param ctxOrTarget - Either the RequestContext, which binds the repository to ongoing transactions, or the entity type/schema.
98
+ * @param maybeTarget - The entity type or schema for which the repository is returned (if `ctxOrTarget` is a RequestContext).
99
+ * @param options - Additional options for configuring the repository, such as the `replicationMode`.
100
+ *
101
+ * @returns A TypeORM repository for the specified entity type.
102
+ */
78
103
getRepository < Entity extends ObjectLiteral > (
79
104
ctxOrTarget : RequestContext | ObjectType < Entity > | EntitySchema < Entity > | string | undefined ,
80
105
maybeTarget ?: ObjectType < Entity > | EntitySchema < Entity > | string ,
106
+ options ?: {
107
+ replicationMode ?: ReplicationMode ;
108
+ } ,
81
109
) : Repository < Entity > {
82
110
if ( ctxOrTarget instanceof RequestContext ) {
83
111
const transactionManager = this . getTransactionManager ( ctxOrTarget ) ;
84
112
if ( transactionManager ) {
85
113
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
86
114
return transactionManager . getRepository ( maybeTarget ! ) ;
87
- } else {
115
+ }
116
+
117
+ if ( ctxOrTarget . replicationMode === 'master' || options ?. replicationMode === 'master' ) {
88
118
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
89
- return this . rawConnection . getRepository ( maybeTarget ! ) ;
119
+ return this . dataSource . createQueryRunner ( 'master' ) . manager . getRepository ( maybeTarget ! ) ;
90
120
}
121
+
122
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
123
+ return this . rawConnection . getRepository ( maybeTarget ! ) ;
91
124
} else {
125
+ if ( options ?. replicationMode === 'master' ) {
126
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
127
+ return this . dataSource
128
+ . createQueryRunner ( options . replicationMode )
129
+ . manager . getRepository ( maybeTarget ! ) ;
130
+ }
92
131
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
93
132
return this . rawConnection . getRepository ( ctxOrTarget ?? maybeTarget ! ) ;
94
133
}
0 commit comments