Skip to content

Commit 489124e

Browse files
committed
techdebt: create a ReplicationMode type and update function defs
create a type to track ReplicationMode instead of writing out `"master"|"slave"` everywhere. update to drop the default from the QueryRunner constructor as they will always receive the mode from the driver when it's part of the QueryRunner also drop the default from Driver.createQueryRunner in the implementations - the interface mandates the mode to be defined so it will never be omitted anyway also drop the explict "master" from any connection.createQueryRunner calls so we leave it either the default or `slave` when needed all of this makes it easier to eventually migrate to other naming convetions for the replication mode should it be deemed the right path to go down
1 parent c714867 commit 489124e

38 files changed

+93
-64
lines changed

src/cache/DbQueryResultCache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ export class DbQueryResultCache implements QueryResultCache {
229229
if (queryRunner)
230230
return queryRunner;
231231

232-
return this.connection.createQueryRunner("master");
232+
return this.connection.createQueryRunner();
233233
}
234234

235235
}

src/commands/QueryCommand.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class QueryCommand implements yargs.CommandModule {
4848
connection = await createConnection(connectionOptions);
4949

5050
// create a query runner and execute query using it
51-
queryRunner = connection.createQueryRunner("master");
51+
queryRunner = connection.createQueryRunner();
5252
console.log(chalk.green("Running query: ") + PlatformTools.highlightSql(args._[1]));
5353
const queryResult = await queryRunner.query(args._[1]);
5454
console.log(chalk.green("Query has been executed. Result: "));

src/connection/Connection.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {PromiseUtils} from "../";
4040
import {IsolationLevel} from "../driver/types/IsolationLevel";
4141
import {AuroraDataApiDriver} from "../driver/aurora-data-api/AuroraDataApiDriver";
4242
import {DriverUtils} from "../driver/DriverUtils";
43+
import {ReplicationMode} from "../driver/types/ReplicationMode";
4344

4445
/**
4546
* Connection is a single database ORM connection to a specific database.
@@ -259,7 +260,7 @@ export class Connection {
259260
*/
260261
// TODO rename
261262
async dropDatabase(): Promise<void> {
262-
const queryRunner = this.createQueryRunner("master");
263+
const queryRunner = this.createQueryRunner();
263264
try {
264265
if (this.driver instanceof SqlServerDriver || this.driver instanceof MysqlDriver || this.driver instanceof AuroraDataApiDriver) {
265266
const databases: string[] = this.driver.database ? [this.driver.database] : [];
@@ -395,7 +396,7 @@ export class Connection {
395396
if (queryRunner && queryRunner.isReleased)
396397
throw new QueryRunnerProviderAlreadyReleasedError();
397398

398-
const usedQueryRunner = queryRunner || this.createQueryRunner("master");
399+
const usedQueryRunner = queryRunner || this.createQueryRunner();
399400

400401
try {
401402
return await usedQueryRunner.query(query, parameters); // await is needed here because we are using finally
@@ -444,7 +445,7 @@ export class Connection {
444445
* If you perform writes you must use master database,
445446
* if you perform reads you can use slave databases.
446447
*/
447-
createQueryRunner(mode: "master"|"slave" = "master"): QueryRunner {
448+
createQueryRunner(mode: ReplicationMode = "master"): QueryRunner {
448449
const queryRunner = this.driver.createQueryRunner(mode);
449450
const manager = this.createEntityManager(queryRunner);
450451
Object.assign(queryRunner, { manager: manager });

src/driver/Driver.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {DataTypeDefaults} from "./types/DataTypeDefaults";
88
import {BaseConnectionOptions} from "../connection/BaseConnectionOptions";
99
import {TableColumn} from "../schema-builder/table/TableColumn";
1010
import {EntityMetadata} from "../metadata/EntityMetadata";
11+
import {ReplicationMode} from "./types/ReplicationMode";
1112

1213
/**
1314
* Driver organizes TypeORM communication with specific database management system.
@@ -102,7 +103,7 @@ export interface Driver {
102103
/**
103104
* Creates a query runner used for common queries.
104105
*/
105-
createQueryRunner(mode: "master"|"slave"): QueryRunner;
106+
createQueryRunner(mode: ReplicationMode): QueryRunner;
106107

107108
/**
108109
* Replaces parameters in the given sql with special escaping character

src/driver/aurora-data-api-pg/AuroraDataApiPostgresQueryRunner.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import {QueryRunner} from "../../query-runner/QueryRunner";
55
import {IsolationLevel} from "../types/IsolationLevel";
66
import {AuroraDataApiPostgresDriver} from "../postgres/PostgresDriver";
77
import {PostgresQueryRunner} from "../postgres/PostgresQueryRunner";
8+
import {ReplicationMode} from "../types/ReplicationMode";
89

910
class PostgresQueryRunnerWrapper extends PostgresQueryRunner {
1011
driver: any;
1112

12-
constructor(driver: any, mode: "master"|"slave") {
13+
constructor(driver: any, mode: ReplicationMode) {
1314
super(driver, mode);
1415
}
1516
}
@@ -46,7 +47,7 @@ export class AuroraDataApiPostgresQueryRunner extends PostgresQueryRunnerWrapper
4647
// Constructor
4748
// -------------------------------------------------------------------------
4849

49-
constructor(driver: AuroraDataApiPostgresDriver, mode: "master"|"slave" = "master") {
50+
constructor(driver: AuroraDataApiPostgresDriver, mode: ReplicationMode) {
5051
super(driver, mode);
5152
}
5253

src/driver/aurora-data-api/AuroraDataApiConnection.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {AuroraDataApiQueryRunner} from "./AuroraDataApiQueryRunner";
22
import {Connection} from "../../connection/Connection";
33
import {ConnectionOptions, QueryRunner} from "../..";
4+
import {ReplicationMode} from "../types/ReplicationMode";
45

56
/**
67
* Organizes communication with MySQL DBMS.
@@ -13,7 +14,7 @@ export class AuroraDataApiConnection extends Connection {
1314
this.queryRunnter = queryRunner;
1415
}
1516

16-
public createQueryRunner(mode: "master" | "slave" = "master"): QueryRunner {
17+
public createQueryRunner(mode: ReplicationMode): QueryRunner {
1718
return this.queryRunnter;
1819
}
1920

src/driver/aurora-data-api/AuroraDataApiDriver.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {AuroraDataApiConnectionCredentialsOptions} from "./AuroraDataApiConnecti
1616
import {EntityMetadata} from "../../metadata/EntityMetadata";
1717
import {OrmUtils} from "../../util/OrmUtils";
1818
import {ApplyValueTransformers} from "../../util/ApplyValueTransformers";
19+
import {ReplicationMode} from "../types/ReplicationMode";
1920

2021
/**
2122
* Organizes communication with MySQL DBMS.
@@ -355,7 +356,7 @@ export class AuroraDataApiDriver implements Driver {
355356
/**
356357
* Creates a query runner used to execute database queries.
357358
*/
358-
createQueryRunner(mode: "master"|"slave" = "master") {
359+
createQueryRunner(mode: ReplicationMode) {
359360
return new AuroraDataApiQueryRunner(this);
360361
}
361362

src/driver/better-sqlite3/BetterSqlite3Driver.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { QueryRunner } from "../../query-runner/QueryRunner";
99
import { AbstractSqliteDriver } from "../sqlite-abstract/AbstractSqliteDriver";
1010
import { BetterSqlite3ConnectionOptions } from "./BetterSqlite3ConnectionOptions";
1111
import { BetterSqlite3QueryRunner } from "./BetterSqlite3QueryRunner";
12+
import {ReplicationMode} from "../types/ReplicationMode";
1213

1314
/**
1415
* Organizes communication with sqlite DBMS.
@@ -63,7 +64,7 @@ export class BetterSqlite3Driver extends AbstractSqliteDriver {
6364
/**
6465
* Creates a query runner used to execute database queries.
6566
*/
66-
createQueryRunner(mode: "master" | "slave" = "master"): QueryRunner {
67+
createQueryRunner(mode: ReplicationMode): QueryRunner {
6768
if (!this.queryRunner)
6869
this.queryRunner = new BetterSqlite3QueryRunner(this);
6970

src/driver/cockroachdb/CockroachDriver.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {EntityMetadata} from "../../metadata/EntityMetadata";
1919
import {OrmUtils} from "../../util/OrmUtils";
2020
import {CockroachQueryRunner} from "./CockroachQueryRunner";
2121
import {ApplyValueTransformers} from "../../util/ApplyValueTransformers";
22+
import {ReplicationMode} from "../types/ReplicationMode";
2223

2324
/**
2425
* Organizes communication with Cockroach DBMS.
@@ -284,7 +285,7 @@ export class CockroachDriver implements Driver {
284285
/**
285286
* Creates a query runner used to execute database queries.
286287
*/
287-
createQueryRunner(mode: "master"|"slave" = "master") {
288+
createQueryRunner(mode: ReplicationMode) {
288289
return new CockroachQueryRunner(this, mode);
289290
}
290291

src/driver/cockroachdb/CockroachQueryRunner.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {TableCheck} from "../../schema-builder/table/TableCheck";
2222
import {ColumnType} from "../../index";
2323
import {IsolationLevel} from "../types/IsolationLevel";
2424
import {TableExclusion} from "../../schema-builder/table/TableExclusion";
25+
import {ReplicationMode} from "../types/ReplicationMode";
2526

2627
/**
2728
* Runs queries on a single postgres database connection.
@@ -65,7 +66,7 @@ export class CockroachQueryRunner extends BaseQueryRunner implements QueryRunner
6566
// Constructor
6667
// -------------------------------------------------------------------------
6768

68-
constructor(driver: CockroachDriver, mode: "master"|"slave" = "master") {
69+
constructor(driver: CockroachDriver, mode: ReplicationMode) {
6970
super();
7071
this.driver = driver;
7172
this.connection = driver.connection;

0 commit comments

Comments
 (0)