Skip to content

Commit

Permalink
refactor: move aurora-pg driver to the aurora-pg driver directory (#6785
Browse files Browse the repository at this point in the history
)

for some reason the aurora postgres driver was living with the postgres
driver - instead of in the directory explicitly for the aurora postgres
driver.

this moves the aurora-pg driver into that directory so there's a clearer
delineation between the two drivers
  • Loading branch information
imnotjames committed Sep 26, 2020
1 parent 55fbb69 commit 89d83b3
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 125 deletions.
3 changes: 2 additions & 1 deletion src/driver/DriverFactory.ts
Expand Up @@ -9,9 +9,10 @@ import {ReactNativeDriver} from "./react-native/ReactNativeDriver";
import {NativescriptDriver} from "./nativescript/NativescriptDriver";
import {SqljsDriver} from "./sqljs/SqljsDriver";
import {MysqlDriver} from "./mysql/MysqlDriver";
import {PostgresDriver, AuroraDataApiPostgresDriver} from "./postgres/PostgresDriver";
import {PostgresDriver} from "./postgres/PostgresDriver";
import {ExpoDriver} from "./expo/ExpoDriver";
import {AuroraDataApiDriver} from "./aurora-data-api/AuroraDataApiDriver";
import {AuroraDataApiPostgresDriver} from "./aurora-data-api-pg/AuroraDataApiPostgresDriver";
import {Driver} from "./Driver";
import {Connection} from "../connection/Connection";
import {SapDriver} from "./sap/SapDriver";
Expand Down
128 changes: 128 additions & 0 deletions src/driver/aurora-data-api-pg/AuroraDataApiPostgresDriver.ts
@@ -0,0 +1,128 @@
import {Driver} from "../Driver";
import {PostgresDriver} from "../postgres/PostgresDriver";
import {PlatformTools} from "../../platform/PlatformTools";
import {Connection} from "../../connection/Connection";
import {AuroraDataApiPostgresConnectionOptions} from "../aurora-data-api-pg/AuroraDataApiPostgresConnectionOptions";
import {AuroraDataApiPostgresQueryRunner} from "../aurora-data-api-pg/AuroraDataApiPostgresQueryRunner";
import {ReplicationMode} from "../types/ReplicationMode";

abstract class PostgresWrapper extends PostgresDriver {
options: any;

abstract createQueryRunner(mode: ReplicationMode): any;
}

export class AuroraDataApiPostgresDriver extends PostgresWrapper implements Driver {

// -------------------------------------------------------------------------
// Public Properties
// -------------------------------------------------------------------------

/**
* Connection used by driver.
*/
connection: Connection;

/**
* Aurora Data API underlying library.
*/
DataApiDriver: any;

client: any;

// -------------------------------------------------------------------------
// Public Implemented Properties
// -------------------------------------------------------------------------

/**
* Connection options.
*/
options: AuroraDataApiPostgresConnectionOptions;

/**
* Master database used to perform all write queries.
*/
database?: string;

// -------------------------------------------------------------------------
// Constructor
// -------------------------------------------------------------------------

constructor(connection: Connection) {
super();
this.connection = connection;
this.options = connection.options as AuroraDataApiPostgresConnectionOptions;
this.isReplicated = false;

// load data-api package
this.loadDependencies();

this.client = new this.DataApiDriver(
this.options.region,
this.options.secretArn,
this.options.resourceArn,
this.options.database,
(query: string, parameters?: any[]) => this.connection.logger.logQuery(query, parameters),
this.options.serviceConfigOptions,
this.options.formatOptions,
);
}

// -------------------------------------------------------------------------
// Public Implemented Methods
// -------------------------------------------------------------------------

/**
* Performs connection to the database.
* Based on pooling options, it can either create connection immediately,
* either create a pool and create connection when needed.
*/
async connect(): Promise<void> {
}

/**
* Closes connection with database.
*/
async disconnect(): Promise<void> {
}

/**
* Creates a query runner used to execute database queries.
*/
createQueryRunner(mode: ReplicationMode) {
return new AuroraDataApiPostgresQueryRunner(this, mode);
}

// -------------------------------------------------------------------------
// Protected Methods
// -------------------------------------------------------------------------

/**
* If driver dependency is not given explicitly, then try to load it via "require".
*/
protected loadDependencies(): void {
const { pg } = PlatformTools.load("typeorm-aurora-data-api-driver");

this.DataApiDriver = pg;
}

/**
* Executes given query.
*/
protected executeQuery(connection: any, query: string) {
return this.client.query(query);
}

/**
* Makes any action after connection (e.g. create extensions in Postgres driver).
*/
async afterConnect(): Promise<void> {
const extensionsMetadata = await this.checkMetadataForExtensions();

if (extensionsMetadata.hasExtensions) {
await this.enableExtensions(extensionsMetadata, this.connection);
}

return Promise.resolve();
}
}
Expand Up @@ -3,7 +3,7 @@ import {TransactionAlreadyStartedError} from "../../error/TransactionAlreadyStar
import {TransactionNotStartedError} from "../../error/TransactionNotStartedError";
import {QueryRunner} from "../../query-runner/QueryRunner";
import {IsolationLevel} from "../types/IsolationLevel";
import {AuroraDataApiPostgresDriver} from "../postgres/PostgresDriver";
import {AuroraDataApiPostgresDriver} from "./AuroraDataApiPostgresDriver";
import {PostgresQueryRunner} from "../postgres/PostgresQueryRunner";
import {ReplicationMode} from "../types/ReplicationMode";

Expand Down
123 changes: 0 additions & 123 deletions src/driver/postgres/PostgresDriver.ts
Expand Up @@ -19,8 +19,6 @@ import {PostgresConnectionCredentialsOptions} from "./PostgresConnectionCredenti
import {EntityMetadata} from "../../metadata/EntityMetadata";
import {OrmUtils} from "../../util/OrmUtils";
import {ApplyValueTransformers} from "../../util/ApplyValueTransformers";
import {AuroraDataApiPostgresConnectionOptions} from "../aurora-data-api-pg/AuroraDataApiPostgresConnectionOptions";
import {AuroraDataApiPostgresQueryRunner} from "../aurora-data-api-pg/AuroraDataApiPostgresQueryRunner";
import {ReplicationMode} from "../types/ReplicationMode";

/**
Expand Down Expand Up @@ -1040,124 +1038,3 @@ export class PostgresDriver implements Driver {
}

}

abstract class PostgresWrapper extends PostgresDriver {
options: any;

abstract createQueryRunner(mode: ReplicationMode): any;
}

export class AuroraDataApiPostgresDriver extends PostgresWrapper {

// -------------------------------------------------------------------------
// Public Properties
// -------------------------------------------------------------------------

/**
* Connection used by driver.
*/
connection: Connection;

/**
* Aurora Data API underlying library.
*/
DataApiDriver: any;

client: any;

// -------------------------------------------------------------------------
// Public Implemented Properties
// -------------------------------------------------------------------------

/**
* Connection options.
*/
options: AuroraDataApiPostgresConnectionOptions;

/**
* Master database used to perform all write queries.
*/
database?: string;

// -------------------------------------------------------------------------
// Constructor
// -------------------------------------------------------------------------

constructor(connection: Connection) {
super();
this.connection = connection;
this.options = connection.options as AuroraDataApiPostgresConnectionOptions;
this.isReplicated = false;

// load data-api package
this.loadDependencies();

this.client = new this.DataApiDriver(
this.options.region,
this.options.secretArn,
this.options.resourceArn,
this.options.database,
(query: string, parameters?: any[]) => this.connection.logger.logQuery(query, parameters),
this.options.serviceConfigOptions,
this.options.formatOptions,
);
}

// -------------------------------------------------------------------------
// Public Implemented Methods
// -------------------------------------------------------------------------

/**
* Performs connection to the database.
* Based on pooling options, it can either create connection immediately,
* either create a pool and create connection when needed.
*/
async connect(): Promise<void> {
}

/**
* Closes connection with database.
*/
async disconnect(): Promise<void> {
}

/**
* Creates a query runner used to execute database queries.
*/
createQueryRunner(mode: ReplicationMode) {
return new AuroraDataApiPostgresQueryRunner(this, mode);
}

// -------------------------------------------------------------------------
// Protected Methods
// -------------------------------------------------------------------------

/**
* If driver dependency is not given explicitly, then try to load it via "require".
*/
protected loadDependencies(): void {
const { pg } = PlatformTools.load("typeorm-aurora-data-api-driver");

this.DataApiDriver = pg;
}

/**
* Executes given query.
*/
protected executeQuery(connection: any, query: string) {
return this.client.query(query);
}

/**
* Makes any action after connection (e.g. create extensions in Postgres driver).
*/
async afterConnect(): Promise<void> {
const extensionsMetadata = await this.checkMetadataForExtensions();

if (extensionsMetadata.hasExtensions) {
await this.enableExtensions(extensionsMetadata, this.connection);
}

return Promise.resolve();
}
}

0 comments on commit 89d83b3

Please sign in to comment.