-
-
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.
feat(drivers): add support for PostgreSQL
- Loading branch information
Showing
13 changed files
with
1,095 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { Client, ClientConfig } from 'pg'; | ||
import { readFileSync } from 'fs'; | ||
import { URL } from 'url'; | ||
import { Connection, QueryResult } from './Connection'; | ||
import { EntityData, IEntity } from '../decorators'; | ||
|
||
export class PostgreSqlConnection extends Connection { | ||
|
||
private connection: Client; | ||
|
||
async connect(): Promise<void> { | ||
this.connection = new Client(this.getConnectionOptions()); | ||
await this.connection.connect(); | ||
} | ||
|
||
async close(force?: boolean): Promise<void> { | ||
await this.connection.end(); | ||
} | ||
|
||
async isConnected(): Promise<boolean> { | ||
try { | ||
await this.connection.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[]> { | ||
try { | ||
const now = Date.now(); | ||
const res = await this.connection.query(query, params); | ||
this.logQuery(query + ` [took ${Date.now() - now} ms]`); | ||
|
||
return this.transformResult(res, method); | ||
} catch (e) { | ||
e.message += `\n in query: ${query}`; | ||
|
||
if (params && params.length) { | ||
e.message += `\n with params: ${JSON.stringify(params)}`; | ||
} | ||
|
||
throw e; | ||
} | ||
} | ||
|
||
getConnectionOptions(): ClientConfig { | ||
const ret = {} as ClientConfig; | ||
const url = new URL(this.config.getClientUrl()); | ||
ret.host = this.config.get('host', url.hostname); | ||
ret.port = this.config.get('port', +url.port); | ||
ret.user = this.config.get('user', url.username); | ||
ret.password = this.config.get('password', url.password); | ||
ret.database = this.config.get('dbName', url.pathname.replace(/^\//, '')); | ||
|
||
return ret; | ||
} | ||
|
||
async loadFile(path: string): Promise<void> { | ||
const file = readFileSync(path); | ||
await this.execute(file.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; | ||
} | ||
|
||
} |
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,28 @@ | ||
import { PostgreSqlConnection } from '../connections/PostgreSqlConnection'; | ||
import { AbstractSqlDriver } from './AbstractSqlDriver'; | ||
import { EntityData, IEntityType } from '../decorators'; | ||
import { QueryType } from '../query'; | ||
import { PostgreSqPlatform } from '../platforms/PostgreSqPlatform'; | ||
|
||
export class PostgreSqlDriver extends AbstractSqlDriver<PostgreSqlConnection> { | ||
|
||
protected readonly connection = new PostgreSqlConnection(this.config); | ||
protected readonly platform = new PostgreSqPlatform(); | ||
|
||
async nativeInsert<T extends IEntityType<T>>(entityName: string, data: EntityData<T>): Promise<number> { | ||
const collections = this.extractManyToMany(entityName, data); | ||
const qb = this.createQueryBuilder(entityName).insert(data); | ||
const params = qb.getParams(); | ||
let sql = qb.getQuery(); | ||
|
||
if (qb.type === QueryType.INSERT && Object.keys(params).length === 0) { | ||
sql = sql.replace('() VALUES ()', '("id") VALUES (DEFAULT)'); | ||
} | ||
|
||
const res = await this.connection.execute(sql, params, 'run'); | ||
await this.processManyToMany(entityName, res.insertId, collections); | ||
|
||
return res.insertId; | ||
} | ||
|
||
} |
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,26 @@ | ||
import { NamingStrategy, UnderscoreNamingStrategy } from '../naming-strategy'; | ||
import { Platform } from './Platform'; | ||
|
||
export class PostgreSqPlatform extends Platform { | ||
|
||
supportsSavePoints(): boolean { | ||
return true; | ||
} | ||
|
||
getNamingStrategy(): { new(): NamingStrategy} { | ||
return UnderscoreNamingStrategy; | ||
} | ||
|
||
getIdentifierQuoteCharacter(): string { | ||
return '"'; | ||
} | ||
|
||
getParameterPlaceholder(index?: number): string { | ||
return '$' + index; | ||
} | ||
|
||
usesReturningStatement(): boolean { | ||
return true; | ||
} | ||
|
||
} |
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
Oops, something went wrong.