-
-
Notifications
You must be signed in to change notification settings - Fork 496
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
1,172 additions
and
69 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
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
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
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
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,68 @@ | ||
import { Client } from 'pg'; | ||
import { readFileSync } from 'fs'; | ||
import { Connection, QueryResult } from './Connection'; | ||
import { EntityData, IEntity } from '../decorators'; | ||
|
||
export class PostgreSqlConnection extends Connection { | ||
|
||
protected client: Client; | ||
|
||
async connect(): Promise<void> { | ||
this.client = new Client(this.getConnectionOptions()); | ||
await this.client.connect(); | ||
} | ||
|
||
async close(force?: boolean): Promise<void> { | ||
await this.client.end(); | ||
} | ||
|
||
async isConnected(): Promise<boolean> { | ||
try { | ||
await this.client.query('SELECT 1'); | ||
return true; | ||
} catch { | ||
return false; | ||
} | ||
} | ||
|
||
async beginTransaction(savepoint?: string): Promise<void> { | ||
await this.execute(savepoint ? `SAVEPOINT ${savepoint}` : 'START TRANSACTION', [], 'run'); | ||
} | ||
|
||
async commit(savepoint?: string): Promise<void> { | ||
await this.execute(savepoint ? `RELEASE SAVEPOINT ${savepoint}` : 'COMMIT', [], 'run'); | ||
} | ||
|
||
async rollback(savepoint?: string): Promise<void> { | ||
await this.execute(savepoint ? `ROLLBACK TO SAVEPOINT ${savepoint}` : 'ROLLBACK', [], 'run'); | ||
} | ||
|
||
getDefaultClientUrl(): string { | ||
return 'postgre://postgres@127.0.0.1:5432'; | ||
} | ||
|
||
async execute(query: string, params: any[] = [], method: 'all' | 'get' | 'run' = 'all'): Promise<QueryResult | any | any[]> { | ||
const res = await this.executeQuery(query, params, () => this.client.query(query, params)); | ||
return this.transformResult(res, method); | ||
} | ||
|
||
async loadFile(path: string): Promise<void> { | ||
await this.client.query(readFileSync(path).toString()); | ||
} | ||
|
||
private transformResult(res: any, method: 'all' | 'get' | 'run'): QueryResult | EntityData<IEntity> | EntityData<IEntity>[] { | ||
if (method === 'get') { | ||
return res.rows[0]; | ||
} | ||
|
||
if (method === 'run') { | ||
return { | ||
affectedRows: res.rowCount, | ||
insertId: res.rows[0] ? res.rows[0].id : 0, | ||
}; | ||
} | ||
|
||
return res.rows; | ||
} | ||
|
||
} |
Oops, something went wrong.