Skip to content

Commit

Permalink
techdebt: create a ReplicationMode type and update function defs
Browse files Browse the repository at this point in the history
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
  • Loading branch information
imnotjames committed Sep 21, 2020
1 parent c714867 commit 489124e
Show file tree
Hide file tree
Showing 38 changed files with 93 additions and 64 deletions.
2 changes: 1 addition & 1 deletion src/cache/DbQueryResultCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ export class DbQueryResultCache implements QueryResultCache {
if (queryRunner)
return queryRunner;

return this.connection.createQueryRunner("master");
return this.connection.createQueryRunner();
}

}
2 changes: 1 addition & 1 deletion src/commands/QueryCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class QueryCommand implements yargs.CommandModule {
connection = await createConnection(connectionOptions);

// create a query runner and execute query using it
queryRunner = connection.createQueryRunner("master");
queryRunner = connection.createQueryRunner();
console.log(chalk.green("Running query: ") + PlatformTools.highlightSql(args._[1]));
const queryResult = await queryRunner.query(args._[1]);
console.log(chalk.green("Query has been executed. Result: "));
Expand Down
7 changes: 4 additions & 3 deletions src/connection/Connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {PromiseUtils} from "../";
import {IsolationLevel} from "../driver/types/IsolationLevel";
import {AuroraDataApiDriver} from "../driver/aurora-data-api/AuroraDataApiDriver";
import {DriverUtils} from "../driver/DriverUtils";
import {ReplicationMode} from "../driver/types/ReplicationMode";

/**
* Connection is a single database ORM connection to a specific database.
Expand Down Expand Up @@ -259,7 +260,7 @@ export class Connection {
*/
// TODO rename
async dropDatabase(): Promise<void> {
const queryRunner = this.createQueryRunner("master");
const queryRunner = this.createQueryRunner();
try {
if (this.driver instanceof SqlServerDriver || this.driver instanceof MysqlDriver || this.driver instanceof AuroraDataApiDriver) {
const databases: string[] = this.driver.database ? [this.driver.database] : [];
Expand Down Expand Up @@ -395,7 +396,7 @@ export class Connection {
if (queryRunner && queryRunner.isReleased)
throw new QueryRunnerProviderAlreadyReleasedError();

const usedQueryRunner = queryRunner || this.createQueryRunner("master");
const usedQueryRunner = queryRunner || this.createQueryRunner();

try {
return await usedQueryRunner.query(query, parameters); // await is needed here because we are using finally
Expand Down Expand Up @@ -444,7 +445,7 @@ export class Connection {
* If you perform writes you must use master database,
* if you perform reads you can use slave databases.
*/
createQueryRunner(mode: "master"|"slave" = "master"): QueryRunner {
createQueryRunner(mode: ReplicationMode = "master"): QueryRunner {
const queryRunner = this.driver.createQueryRunner(mode);
const manager = this.createEntityManager(queryRunner);
Object.assign(queryRunner, { manager: manager });
Expand Down
3 changes: 2 additions & 1 deletion src/driver/Driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {DataTypeDefaults} from "./types/DataTypeDefaults";
import {BaseConnectionOptions} from "../connection/BaseConnectionOptions";
import {TableColumn} from "../schema-builder/table/TableColumn";
import {EntityMetadata} from "../metadata/EntityMetadata";
import {ReplicationMode} from "./types/ReplicationMode";

/**
* Driver organizes TypeORM communication with specific database management system.
Expand Down Expand Up @@ -102,7 +103,7 @@ export interface Driver {
/**
* Creates a query runner used for common queries.
*/
createQueryRunner(mode: "master"|"slave"): QueryRunner;
createQueryRunner(mode: ReplicationMode): QueryRunner;

/**
* Replaces parameters in the given sql with special escaping character
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import {QueryRunner} from "../../query-runner/QueryRunner";
import {IsolationLevel} from "../types/IsolationLevel";
import {AuroraDataApiPostgresDriver} from "../postgres/PostgresDriver";
import {PostgresQueryRunner} from "../postgres/PostgresQueryRunner";
import {ReplicationMode} from "../types/ReplicationMode";

class PostgresQueryRunnerWrapper extends PostgresQueryRunner {
driver: any;

constructor(driver: any, mode: "master"|"slave") {
constructor(driver: any, mode: ReplicationMode) {
super(driver, mode);
}
}
Expand Down Expand Up @@ -46,7 +47,7 @@ export class AuroraDataApiPostgresQueryRunner extends PostgresQueryRunnerWrapper
// Constructor
// -------------------------------------------------------------------------

constructor(driver: AuroraDataApiPostgresDriver, mode: "master"|"slave" = "master") {
constructor(driver: AuroraDataApiPostgresDriver, mode: ReplicationMode) {
super(driver, mode);
}

Expand Down
3 changes: 2 additions & 1 deletion src/driver/aurora-data-api/AuroraDataApiConnection.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {AuroraDataApiQueryRunner} from "./AuroraDataApiQueryRunner";
import {Connection} from "../../connection/Connection";
import {ConnectionOptions, QueryRunner} from "../..";
import {ReplicationMode} from "../types/ReplicationMode";

/**
* Organizes communication with MySQL DBMS.
Expand All @@ -13,7 +14,7 @@ export class AuroraDataApiConnection extends Connection {
this.queryRunnter = queryRunner;
}

public createQueryRunner(mode: "master" | "slave" = "master"): QueryRunner {
public createQueryRunner(mode: ReplicationMode): QueryRunner {
return this.queryRunnter;
}

Expand Down
3 changes: 2 additions & 1 deletion src/driver/aurora-data-api/AuroraDataApiDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {AuroraDataApiConnectionCredentialsOptions} from "./AuroraDataApiConnecti
import {EntityMetadata} from "../../metadata/EntityMetadata";
import {OrmUtils} from "../../util/OrmUtils";
import {ApplyValueTransformers} from "../../util/ApplyValueTransformers";
import {ReplicationMode} from "../types/ReplicationMode";

/**
* Organizes communication with MySQL DBMS.
Expand Down Expand Up @@ -355,7 +356,7 @@ export class AuroraDataApiDriver implements Driver {
/**
* Creates a query runner used to execute database queries.
*/
createQueryRunner(mode: "master"|"slave" = "master") {
createQueryRunner(mode: ReplicationMode) {
return new AuroraDataApiQueryRunner(this);
}

Expand Down
3 changes: 2 additions & 1 deletion src/driver/better-sqlite3/BetterSqlite3Driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { QueryRunner } from "../../query-runner/QueryRunner";
import { AbstractSqliteDriver } from "../sqlite-abstract/AbstractSqliteDriver";
import { BetterSqlite3ConnectionOptions } from "./BetterSqlite3ConnectionOptions";
import { BetterSqlite3QueryRunner } from "./BetterSqlite3QueryRunner";
import {ReplicationMode} from "../types/ReplicationMode";

/**
* Organizes communication with sqlite DBMS.
Expand Down Expand Up @@ -63,7 +64,7 @@ export class BetterSqlite3Driver extends AbstractSqliteDriver {
/**
* Creates a query runner used to execute database queries.
*/
createQueryRunner(mode: "master" | "slave" = "master"): QueryRunner {
createQueryRunner(mode: ReplicationMode): QueryRunner {
if (!this.queryRunner)
this.queryRunner = new BetterSqlite3QueryRunner(this);

Expand Down
3 changes: 2 additions & 1 deletion src/driver/cockroachdb/CockroachDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {EntityMetadata} from "../../metadata/EntityMetadata";
import {OrmUtils} from "../../util/OrmUtils";
import {CockroachQueryRunner} from "./CockroachQueryRunner";
import {ApplyValueTransformers} from "../../util/ApplyValueTransformers";
import {ReplicationMode} from "../types/ReplicationMode";

/**
* Organizes communication with Cockroach DBMS.
Expand Down Expand Up @@ -284,7 +285,7 @@ export class CockroachDriver implements Driver {
/**
* Creates a query runner used to execute database queries.
*/
createQueryRunner(mode: "master"|"slave" = "master") {
createQueryRunner(mode: ReplicationMode) {
return new CockroachQueryRunner(this, mode);
}

Expand Down
3 changes: 2 additions & 1 deletion src/driver/cockroachdb/CockroachQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {TableCheck} from "../../schema-builder/table/TableCheck";
import {ColumnType} from "../../index";
import {IsolationLevel} from "../types/IsolationLevel";
import {TableExclusion} from "../../schema-builder/table/TableExclusion";
import {ReplicationMode} from "../types/ReplicationMode";

/**
* Runs queries on a single postgres database connection.
Expand Down Expand Up @@ -65,7 +66,7 @@ export class CockroachQueryRunner extends BaseQueryRunner implements QueryRunner
// Constructor
// -------------------------------------------------------------------------

constructor(driver: CockroachDriver, mode: "master"|"slave" = "master") {
constructor(driver: CockroachDriver, mode: ReplicationMode) {
super();
this.driver = driver;
this.connection = driver.connection;
Expand Down
13 changes: 7 additions & 6 deletions src/driver/cordova/CordovaDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {QueryRunner} from "../../query-runner/QueryRunner";
import {Connection} from "../../connection/Connection";
import {DriverOptionNotSetError} from "../../error/DriverOptionNotSetError";
import {DriverPackageNotInstalledError} from "../../error/DriverPackageNotInstalledError";
import {ReplicationMode} from "../types/ReplicationMode";

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

export class CordovaDriver extends AbstractSqliteDriver {
options: CordovaConnectionOptions;

// -------------------------------------------------------------------------
// Constructor
// -------------------------------------------------------------------------
Expand All @@ -37,7 +38,7 @@ export class CordovaDriver extends AbstractSqliteDriver {
// load sqlite package
this.loadDependencies();
}


// -------------------------------------------------------------------------
// Public Methods
Expand All @@ -52,17 +53,17 @@ export class CordovaDriver extends AbstractSqliteDriver {
this.databaseConnection.close(ok, fail);
});
}

/**
* Creates a query runner used to execute database queries.
*/
createQueryRunner(mode: "master"|"slave" = "master"): QueryRunner {
createQueryRunner(mode: ReplicationMode): QueryRunner {
if (!this.queryRunner)
this.queryRunner = new CordovaQueryRunner(this);

return this.queryRunner;
}

// -------------------------------------------------------------------------
// Protected Methods
// -------------------------------------------------------------------------
Expand Down Expand Up @@ -104,4 +105,4 @@ export class CordovaDriver extends AbstractSqliteDriver {
throw new DriverPackageNotInstalledError("Cordova-SQLite", "cordova-sqlite-storage");
}
}
}
}
13 changes: 7 additions & 6 deletions src/driver/expo/ExpoDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import {ExpoQueryRunner} from "./ExpoQueryRunner";
import {QueryRunner} from "../../query-runner/QueryRunner";
import {Connection} from "../../connection/Connection";
import {DriverOptionNotSetError} from "../../error/DriverOptionNotSetError";
import {ReplicationMode} from "../types/ReplicationMode";

export class ExpoDriver extends AbstractSqliteDriver {
options: ExpoConnectionOptions;

// -------------------------------------------------------------------------
// Constructor
// -------------------------------------------------------------------------
Expand All @@ -20,14 +21,14 @@ export class ExpoDriver extends AbstractSqliteDriver {
// validate options to make sure everything is set
if (!this.options.database)
throw new DriverOptionNotSetError("database");

if (!this.options.driver)
throw new DriverOptionNotSetError("driver");

// load sqlite package
this.sqlite = this.options.driver;
}


// -------------------------------------------------------------------------
// Public Methods
Expand All @@ -48,17 +49,17 @@ export class ExpoDriver extends AbstractSqliteDriver {
}
});
}

/**
* Creates a query runner used to execute database queries.
*/
createQueryRunner(mode: "master"|"slave" = "master"): QueryRunner {
createQueryRunner(mode: ReplicationMode): QueryRunner {
if (!this.queryRunner)
this.queryRunner = new ExpoQueryRunner(this);

return this.queryRunner;
}

// -------------------------------------------------------------------------
// Protected Methods
// -------------------------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion src/driver/mongodb/MongoDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {ConnectionOptions} from "../../connection/ConnectionOptions";
import {EntityMetadata} from "../../metadata/EntityMetadata";
import {ObjectUtils} from "../../util/ObjectUtils";
import {ApplyValueTransformers} from "../../util/ApplyValueTransformers";
import {ReplicationMode} from "../types/ReplicationMode";

/**
* Organizes communication with MongoDB.
Expand Down Expand Up @@ -262,7 +263,7 @@ export class MongoDriver implements Driver {
/**
* Creates a query runner used to execute database queries.
*/
createQueryRunner(mode: "master"|"slave" = "master") {
createQueryRunner(mode: ReplicationMode) {
return this.queryRunner!;
}

Expand Down
3 changes: 2 additions & 1 deletion src/driver/mysql/MysqlDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {MysqlConnectionCredentialsOptions} from "./MysqlConnectionCredentialsOpt
import {EntityMetadata} from "../../metadata/EntityMetadata";
import {OrmUtils} from "../../util/OrmUtils";
import {ApplyValueTransformers} from "../../util/ApplyValueTransformers";
import {ReplicationMode} from "../types/ReplicationMode";

/**
* Organizes communication with MySQL DBMS.
Expand Down Expand Up @@ -385,7 +386,7 @@ export class MysqlDriver implements Driver {
/**
* Creates a query runner used to execute database queries.
*/
createQueryRunner(mode: "master"|"slave" = "master") {
createQueryRunner(mode: ReplicationMode) {
return new MysqlQueryRunner(this, mode);
}

Expand Down
3 changes: 2 additions & 1 deletion src/driver/mysql/MysqlQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {TableCheck} from "../../schema-builder/table/TableCheck";
import {IsolationLevel} from "../types/IsolationLevel";
import {TableExclusion} from "../../schema-builder/table/TableExclusion";
import {VersionUtils} from "../../util/VersionUtils";
import {ReplicationMode} from "../types/ReplicationMode";

/**
* Runs queries on a single mysql database connection.
Expand Down Expand Up @@ -50,7 +51,7 @@ export class MysqlQueryRunner extends BaseQueryRunner implements QueryRunner {
// Constructor
// -------------------------------------------------------------------------

constructor(driver: MysqlDriver, mode: "master"|"slave" = "master") {
constructor(driver: MysqlDriver, mode: ReplicationMode) {
super();
this.driver = driver;
this.connection = driver.connection;
Expand Down
3 changes: 2 additions & 1 deletion src/driver/nativescript/NativescriptDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {Connection} from "../../connection/Connection";
import {DriverOptionNotSetError} from "../../error/DriverOptionNotSetError";
import {DriverPackageNotInstalledError} from "../../error/DriverPackageNotInstalledError";
import {ColumnType} from "../types/ColumnTypes";
import {ReplicationMode} from "../types/ReplicationMode";

/**
* Organizes communication with sqlite DBMS within Nativescript.
Expand Down Expand Up @@ -66,7 +67,7 @@ export class NativescriptDriver extends AbstractSqliteDriver {
/**
* Creates a query runner used to execute database queries.
*/
createQueryRunner(mode: "master"|"slave" = "master"): QueryRunner {
createQueryRunner(mode: ReplicationMode): QueryRunner {
if (!this.queryRunner) {
this.queryRunner = new NativescriptQueryRunner(this);
}
Expand Down
3 changes: 2 additions & 1 deletion src/driver/oracle/OracleDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {DriverUtils} from "../DriverUtils";
import {EntityMetadata} from "../../metadata/EntityMetadata";
import {OrmUtils} from "../../util/OrmUtils";
import {ApplyValueTransformers} from "../../util/ApplyValueTransformers";
import {ReplicationMode} from "../types/ReplicationMode";

/**
* Organizes communication with Oracle RDBMS.
Expand Down Expand Up @@ -287,7 +288,7 @@ export class OracleDriver implements Driver {
/**
* Creates a query runner used to execute database queries.
*/
createQueryRunner(mode: "master"|"slave" = "master") {
createQueryRunner(mode: ReplicationMode) {
return new OracleQueryRunner(this, mode);
}

Expand Down
3 changes: 2 additions & 1 deletion src/driver/oracle/OracleQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {TableCheck} from "../../schema-builder/table/TableCheck";
import {ColumnType, PromiseUtils} from "../../index";
import {IsolationLevel} from "../types/IsolationLevel";
import {TableExclusion} from "../../schema-builder/table/TableExclusion";
import {ReplicationMode} from "../types/ReplicationMode";

/**
* Runs queries on a single oracle database connection.
Expand Down Expand Up @@ -48,7 +49,7 @@ export class OracleQueryRunner extends BaseQueryRunner implements QueryRunner {
// Constructor
// -------------------------------------------------------------------------

constructor(driver: OracleDriver, mode: "master"|"slave" = "master") {
constructor(driver: OracleDriver, mode: ReplicationMode) {
super();
this.driver = driver;
this.connection = driver.connection;
Expand Down
Loading

0 comments on commit 489124e

Please sign in to comment.