Skip to content

Commit 57951fb

Browse files
imnotjamesSvetlozar
authored and
Svetlozar
committed
refactor: create a ReplicationMode type and update function defs (typeorm#6747)
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 18dfbc2 commit 57951fb

38 files changed

+93
-64
lines changed

src/cache/DbQueryResultCache.ts

+1-1
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

+1-1
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

+4-3
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

+2-1
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

+3-2
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

+2-1
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

+2-1
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

+2-1
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

+2-1
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

+2-1
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;

src/driver/cordova/CordovaDriver.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {QueryRunner} from "../../query-runner/QueryRunner";
55
import {Connection} from "../../connection/Connection";
66
import {DriverOptionNotSetError} from "../../error/DriverOptionNotSetError";
77
import {DriverPackageNotInstalledError} from "../../error/DriverPackageNotInstalledError";
8+
import {ReplicationMode} from "../types/ReplicationMode";
89

910
// needed for typescript compiler
1011
interface Window {
@@ -15,7 +16,7 @@ declare var window: Window;
1516

1617
export class CordovaDriver extends AbstractSqliteDriver {
1718
options: CordovaConnectionOptions;
18-
19+
1920
// -------------------------------------------------------------------------
2021
// Constructor
2122
// -------------------------------------------------------------------------
@@ -37,7 +38,7 @@ export class CordovaDriver extends AbstractSqliteDriver {
3738
// load sqlite package
3839
this.loadDependencies();
3940
}
40-
41+
4142

4243
// -------------------------------------------------------------------------
4344
// Public Methods
@@ -52,17 +53,17 @@ export class CordovaDriver extends AbstractSqliteDriver {
5253
this.databaseConnection.close(ok, fail);
5354
});
5455
}
55-
56+
5657
/**
5758
* Creates a query runner used to execute database queries.
5859
*/
59-
createQueryRunner(mode: "master"|"slave" = "master"): QueryRunner {
60+
createQueryRunner(mode: ReplicationMode): QueryRunner {
6061
if (!this.queryRunner)
6162
this.queryRunner = new CordovaQueryRunner(this);
6263

6364
return this.queryRunner;
6465
}
65-
66+
6667
// -------------------------------------------------------------------------
6768
// Protected Methods
6869
// -------------------------------------------------------------------------
@@ -104,4 +105,4 @@ export class CordovaDriver extends AbstractSqliteDriver {
104105
throw new DriverPackageNotInstalledError("Cordova-SQLite", "cordova-sqlite-storage");
105106
}
106107
}
107-
}
108+
}

src/driver/expo/ExpoDriver.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import {ExpoQueryRunner} from "./ExpoQueryRunner";
44
import {QueryRunner} from "../../query-runner/QueryRunner";
55
import {Connection} from "../../connection/Connection";
66
import {DriverOptionNotSetError} from "../../error/DriverOptionNotSetError";
7+
import {ReplicationMode} from "../types/ReplicationMode";
78

89
export class ExpoDriver extends AbstractSqliteDriver {
910
options: ExpoConnectionOptions;
10-
11+
1112
// -------------------------------------------------------------------------
1213
// Constructor
1314
// -------------------------------------------------------------------------
@@ -20,14 +21,14 @@ export class ExpoDriver extends AbstractSqliteDriver {
2021
// validate options to make sure everything is set
2122
if (!this.options.database)
2223
throw new DriverOptionNotSetError("database");
23-
24+
2425
if (!this.options.driver)
2526
throw new DriverOptionNotSetError("driver");
2627

2728
// load sqlite package
2829
this.sqlite = this.options.driver;
2930
}
30-
31+
3132

3233
// -------------------------------------------------------------------------
3334
// Public Methods
@@ -48,17 +49,17 @@ export class ExpoDriver extends AbstractSqliteDriver {
4849
}
4950
});
5051
}
51-
52+
5253
/**
5354
* Creates a query runner used to execute database queries.
5455
*/
55-
createQueryRunner(mode: "master"|"slave" = "master"): QueryRunner {
56+
createQueryRunner(mode: ReplicationMode): QueryRunner {
5657
if (!this.queryRunner)
5758
this.queryRunner = new ExpoQueryRunner(this);
5859

5960
return this.queryRunner;
6061
}
61-
62+
6263
// -------------------------------------------------------------------------
6364
// Protected Methods
6465
// -------------------------------------------------------------------------

src/driver/mongodb/MongoDriver.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {ConnectionOptions} from "../../connection/ConnectionOptions";
1616
import {EntityMetadata} from "../../metadata/EntityMetadata";
1717
import {ObjectUtils} from "../../util/ObjectUtils";
1818
import {ApplyValueTransformers} from "../../util/ApplyValueTransformers";
19+
import {ReplicationMode} from "../types/ReplicationMode";
1920

2021
/**
2122
* Organizes communication with MongoDB.
@@ -262,7 +263,7 @@ export class MongoDriver implements Driver {
262263
/**
263264
* Creates a query runner used to execute database queries.
264265
*/
265-
createQueryRunner(mode: "master"|"slave" = "master") {
266+
createQueryRunner(mode: ReplicationMode) {
266267
return this.queryRunner!;
267268
}
268269

src/driver/mysql/MysqlDriver.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {MysqlConnectionCredentialsOptions} from "./MysqlConnectionCredentialsOpt
1818
import {EntityMetadata} from "../../metadata/EntityMetadata";
1919
import {OrmUtils} from "../../util/OrmUtils";
2020
import {ApplyValueTransformers} from "../../util/ApplyValueTransformers";
21+
import {ReplicationMode} from "../types/ReplicationMode";
2122

2223
/**
2324
* Organizes communication with MySQL DBMS.
@@ -385,7 +386,7 @@ export class MysqlDriver implements Driver {
385386
/**
386387
* Creates a query runner used to execute database queries.
387388
*/
388-
createQueryRunner(mode: "master"|"slave" = "master") {
389+
createQueryRunner(mode: ReplicationMode) {
389390
return new MysqlQueryRunner(this, mode);
390391
}
391392

src/driver/mysql/MysqlQueryRunner.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {TableCheck} from "../../schema-builder/table/TableCheck";
2222
import {IsolationLevel} from "../types/IsolationLevel";
2323
import {TableExclusion} from "../../schema-builder/table/TableExclusion";
2424
import {VersionUtils} from "../../util/VersionUtils";
25+
import {ReplicationMode} from "../types/ReplicationMode";
2526

2627
/**
2728
* Runs queries on a single mysql database connection.
@@ -50,7 +51,7 @@ export class MysqlQueryRunner extends BaseQueryRunner implements QueryRunner {
5051
// Constructor
5152
// -------------------------------------------------------------------------
5253

53-
constructor(driver: MysqlDriver, mode: "master"|"slave" = "master") {
54+
constructor(driver: MysqlDriver, mode: ReplicationMode) {
5455
super();
5556
this.driver = driver;
5657
this.connection = driver.connection;

src/driver/nativescript/NativescriptDriver.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {Connection} from "../../connection/Connection";
66
import {DriverOptionNotSetError} from "../../error/DriverOptionNotSetError";
77
import {DriverPackageNotInstalledError} from "../../error/DriverPackageNotInstalledError";
88
import {ColumnType} from "../types/ColumnTypes";
9+
import {ReplicationMode} from "../types/ReplicationMode";
910

1011
/**
1112
* Organizes communication with sqlite DBMS within Nativescript.
@@ -66,7 +67,7 @@ export class NativescriptDriver extends AbstractSqliteDriver {
6667
/**
6768
* Creates a query runner used to execute database queries.
6869
*/
69-
createQueryRunner(mode: "master"|"slave" = "master"): QueryRunner {
70+
createQueryRunner(mode: ReplicationMode): QueryRunner {
7071
if (!this.queryRunner) {
7172
this.queryRunner = new NativescriptQueryRunner(this);
7273
}

src/driver/oracle/OracleDriver.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {DriverUtils} from "../DriverUtils";
1818
import {EntityMetadata} from "../../metadata/EntityMetadata";
1919
import {OrmUtils} from "../../util/OrmUtils";
2020
import {ApplyValueTransformers} from "../../util/ApplyValueTransformers";
21+
import {ReplicationMode} from "../types/ReplicationMode";
2122

2223
/**
2324
* Organizes communication with Oracle RDBMS.
@@ -287,7 +288,7 @@ export class OracleDriver implements Driver {
287288
/**
288289
* Creates a query runner used to execute database queries.
289290
*/
290-
createQueryRunner(mode: "master"|"slave" = "master") {
291+
createQueryRunner(mode: ReplicationMode) {
291292
return new OracleQueryRunner(this, mode);
292293
}
293294

src/driver/oracle/OracleQueryRunner.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {TableCheck} from "../../schema-builder/table/TableCheck";
2020
import {ColumnType, PromiseUtils} from "../../index";
2121
import {IsolationLevel} from "../types/IsolationLevel";
2222
import {TableExclusion} from "../../schema-builder/table/TableExclusion";
23+
import {ReplicationMode} from "../types/ReplicationMode";
2324

2425
/**
2526
* Runs queries on a single oracle database connection.
@@ -48,7 +49,7 @@ export class OracleQueryRunner extends BaseQueryRunner implements QueryRunner {
4849
// Constructor
4950
// -------------------------------------------------------------------------
5051

51-
constructor(driver: OracleDriver, mode: "master"|"slave" = "master") {
52+
constructor(driver: OracleDriver, mode: ReplicationMode) {
5253
super();
5354
this.driver = driver;
5455
this.connection = driver.connection;

0 commit comments

Comments
 (0)