Skip to content

Commit

Permalink
fix: throw error when not connected in drivers (#7995)
Browse files Browse the repository at this point in the history
this prevents a `cannot read property connect of undefined` error
  • Loading branch information
imnotjames committed Jul 31, 2021
1 parent 69fabaf commit cd71f62
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/driver/cockroachdb/CockroachDriver.ts
Expand Up @@ -619,6 +619,10 @@ export class CockroachDriver implements Driver {
*/
obtainMasterConnection(): Promise<any> {
return new Promise((ok, fail) => {
if (!this.master) {
return fail(new TypeORMError("Driver not Connected"));
}

this.master.connect((err: any, connection: any, release: any) => {
err ? fail(err) : ok([connection, release]);
});
Expand Down
5 changes: 5 additions & 0 deletions src/driver/oracle/OracleDriver.ts
Expand Up @@ -22,6 +22,7 @@ import {ReplicationMode} from "../types/ReplicationMode";
import { Table } from "../../schema-builder/table/Table";
import { View } from "../../schema-builder/view/View";
import { TableForeignKey } from "../../schema-builder/table/TableForeignKey";
import { TypeORMError } from "../../error";

/**
* Organizes communication with Oracle RDBMS.
Expand Down Expand Up @@ -623,6 +624,10 @@ export class OracleDriver implements Driver {
*/
obtainMasterConnection(): Promise<any> {
return new Promise<any>((ok, fail) => {
if (!this.master) {
return fail(new TypeORMError("Driver not Connected"));
}

this.master.getConnection((err: any, connection: any, release: Function) => {
if (err) return fail(err);
ok(connection);
Expand Down
10 changes: 8 additions & 2 deletions src/driver/postgres/PostgresDriver.ts
Expand Up @@ -910,6 +910,11 @@ export class PostgresDriver implements Driver {
*/
obtainMasterConnection(): Promise<any> {
return new Promise((ok, fail) => {
if (!this.master) {
fail(new TypeORMError("Driver not Connected"))
return;
}

this.master.connect((err: any, connection: any, release: any) => {
err ? fail(err) : ok([connection, release]);
});
Expand All @@ -921,9 +926,10 @@ export class PostgresDriver implements Driver {
* Used for replication.
* If replication is not setup then returns master (default) connection's database connection.
*/
obtainSlaveConnection(): Promise<any> {
if (!this.slaves.length)
async obtainSlaveConnection(): Promise<any> {
if (!this.slaves.length) {
return this.obtainMasterConnection();
}

return new Promise((ok, fail) => {
const random = Math.floor(Math.random() * this.slaves.length);
Expand Down
15 changes: 14 additions & 1 deletion src/driver/sap/SapDriver.ts
@@ -1,4 +1,13 @@
import { ColumnType, Connection, EntityMetadata, ObjectLiteral, Table, TableColumn, TableForeignKey } from "../..";
import {
ColumnType,
Connection,
EntityMetadata,
ObjectLiteral,
Table,
TableColumn,
TableForeignKey,
TypeORMError,
} from "../..";
import {DriverPackageNotInstalledError} from "../../error/DriverPackageNotInstalledError";
import {ColumnMetadata} from "../../metadata/ColumnMetadata";
import {PlatformTools} from "../../platform/PlatformTools";
Expand Down Expand Up @@ -600,6 +609,10 @@ export class SapDriver implements Driver {
* If replication is not setup then returns default connection's database connection.
*/
obtainMasterConnection(): Promise<any> {
if (!this.master) {
throw new TypeORMError("Driver not Connected");
}

return this.master.getConnection();
}

Expand Down
5 changes: 5 additions & 0 deletions src/driver/sqlserver/SqlServerDriver.ts
Expand Up @@ -23,6 +23,7 @@ import {ReplicationMode} from "../types/ReplicationMode";
import { Table } from "../../schema-builder/table/Table";
import { View } from "../../schema-builder/view/View";
import { TableForeignKey } from "../../schema-builder/table/TableForeignKey";
import { TypeORMError } from "../../error";

/**
* Organizes communication with SQL Server DBMS.
Expand Down Expand Up @@ -640,6 +641,10 @@ export class SqlServerDriver implements Driver {
* If replication is not setup then returns default connection's database connection.
*/
obtainMasterConnection(): Promise<any> {
if (!this.master) {
return Promise.reject(new TypeORMError("Driver not Connected"));
}

return Promise.resolve(this.master);
}

Expand Down

0 comments on commit cd71f62

Please sign in to comment.