forked from typeorm/typeorm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Aurora Data API - Postgres Support (typeorm#5651)
* Data API Postgres WIP * Refactored the code to be more supportable
- Loading branch information
1 parent
79f54e0
commit 354986c
Showing
8 changed files
with
298 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/driver/aurora-data-api-pg/AuroraDataApiPostgresConnectionOptions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import {BaseConnectionOptions} from "../../connection/BaseConnectionOptions"; | ||
|
||
/** | ||
* Postgres-specific connection options. | ||
*/ | ||
export interface AuroraDataApiPostgresConnectionOptions extends BaseConnectionOptions { | ||
|
||
/** | ||
* Database type. | ||
*/ | ||
readonly type: "aurora-data-api-pg"; | ||
|
||
readonly region: string; | ||
|
||
readonly secretArn: string; | ||
|
||
readonly resourceArn: string; | ||
|
||
readonly database: string; | ||
|
||
/** | ||
* The Postgres extension to use to generate UUID columns. Defaults to uuid-ossp. | ||
* If pgcrypto is selected, TypeORM will use the gen_random_uuid() function from this extension. | ||
* If uuid-ossp is selected, TypeORM will use the uuid_generate_v4() function from this extension. | ||
*/ | ||
readonly uuidExtension?: "pgcrypto" | "uuid-ossp"; | ||
|
||
|
||
/* | ||
* Function handling errors thrown by drivers pool. | ||
* Defaults to logging error with `warn` level. | ||
*/ | ||
readonly poolErrorHandler?: (err: any) => any; | ||
} |
138 changes: 138 additions & 0 deletions
138
src/driver/aurora-data-api-pg/AuroraDataApiPostgresQueryRunner.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import {QueryRunnerAlreadyReleasedError} from "../../error/QueryRunnerAlreadyReleasedError"; | ||
import {TransactionAlreadyStartedError} from "../../error/TransactionAlreadyStartedError"; | ||
import {TransactionNotStartedError} from "../../error/TransactionNotStartedError"; | ||
import {QueryRunner} from "../../query-runner/QueryRunner"; | ||
import {IsolationLevel} from "../types/IsolationLevel"; | ||
import {AuroraDataApiPostgresDriver} from "../postgres/PostgresDriver"; | ||
import {PostgresQueryRunner} from "../postgres/PostgresQueryRunner"; | ||
|
||
class PostgresQueryRunnerWrapper extends PostgresQueryRunner { | ||
driver: any; | ||
|
||
constructor(driver: any, mode: "master"|"slave") { | ||
super(driver, mode); | ||
} | ||
} | ||
|
||
/** | ||
* Runs queries on a single postgres database connection. | ||
*/ | ||
export class AuroraDataApiPostgresQueryRunner extends PostgresQueryRunnerWrapper implements QueryRunner { | ||
|
||
// ------------------------------------------------------------------------- | ||
// Public Implemented Properties | ||
// ------------------------------------------------------------------------- | ||
|
||
/** | ||
* Database driver used by connection. | ||
*/ | ||
driver: AuroraDataApiPostgresDriver; | ||
|
||
// ------------------------------------------------------------------------- | ||
// Protected Properties | ||
// ------------------------------------------------------------------------- | ||
|
||
/** | ||
* Promise used to obtain a database connection for a first time. | ||
*/ | ||
protected databaseConnectionPromise: Promise<any>; | ||
|
||
/** | ||
* Special callback provided by a driver used to release a created connection. | ||
*/ | ||
protected releaseCallback: Function; | ||
|
||
// ------------------------------------------------------------------------- | ||
// Constructor | ||
// ------------------------------------------------------------------------- | ||
|
||
constructor(driver: AuroraDataApiPostgresDriver, mode: "master"|"slave" = "master") { | ||
super(driver, mode); | ||
} | ||
|
||
// ------------------------------------------------------------------------- | ||
// Public Methods | ||
// ------------------------------------------------------------------------- | ||
|
||
/** | ||
* Creates/uses database connection from the connection pool to perform further operations. | ||
* Returns obtained database connection. | ||
*/ | ||
connect(): Promise<any> { | ||
if (this.databaseConnection) | ||
return Promise.resolve(this.databaseConnection); | ||
|
||
if (this.databaseConnectionPromise) | ||
return this.databaseConnectionPromise; | ||
|
||
if (this.mode === "slave" && this.driver.isReplicated) { | ||
this.databaseConnectionPromise = this.driver.obtainSlaveConnection().then(([ connection, release]: any[]) => { | ||
this.driver.connectedQueryRunners.push(this); | ||
this.databaseConnection = connection; | ||
this.releaseCallback = release; | ||
return this.databaseConnection; | ||
}); | ||
|
||
} else { // master | ||
this.databaseConnectionPromise = this.driver.obtainMasterConnection().then(([connection, release]: any[]) => { | ||
this.driver.connectedQueryRunners.push(this); | ||
this.databaseConnection = connection; | ||
this.releaseCallback = release; | ||
return this.databaseConnection; | ||
}); | ||
} | ||
|
||
return this.databaseConnectionPromise; | ||
} | ||
|
||
/** | ||
* Starts transaction on the current connection. | ||
*/ | ||
async startTransaction(isolationLevel?: IsolationLevel): Promise<void> { | ||
if (this.isTransactionActive) | ||
throw new TransactionAlreadyStartedError(); | ||
|
||
this.isTransactionActive = true; | ||
await this.driver.client.startTransaction(); | ||
} | ||
|
||
/** | ||
* Commits transaction. | ||
* Error will be thrown if transaction was not started. | ||
*/ | ||
async commitTransaction(): Promise<void> { | ||
if (!this.isTransactionActive) | ||
throw new TransactionNotStartedError(); | ||
|
||
await this.driver.client.commitTransaction(); | ||
this.isTransactionActive = false; | ||
} | ||
|
||
/** | ||
* Rollbacks transaction. | ||
* Error will be thrown if transaction was not started. | ||
*/ | ||
async rollbackTransaction(): Promise<void> { | ||
if (!this.isTransactionActive) | ||
throw new TransactionNotStartedError(); | ||
|
||
await this.driver.client.rollbackTransaction(); | ||
this.isTransactionActive = false; | ||
} | ||
|
||
/** | ||
* Executes a given SQL query. | ||
*/ | ||
async query(query: string, parameters?: any[]): Promise<any> { | ||
if (this.isReleased) | ||
throw new QueryRunnerAlreadyReleasedError(); | ||
|
||
const result = await this.driver.client.query(query, parameters); | ||
|
||
if (result.records) { | ||
return result.records; | ||
} | ||
|
||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,4 +16,5 @@ export type DatabaseType = | |
"mssql"| | ||
"mongodb"| | ||
"aurora-data-api"| | ||
"aurora-data-api-pg"| | ||
"expo"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -135,4 +135,4 @@ describe("query builder > order-by", () => { | |
expect(loadedPost2!.num2).to.be.equal(2); | ||
}))); | ||
|
||
}); | ||
}); |