-
-
Notifications
You must be signed in to change notification settings - Fork 540
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): use knex to generate sql + enable connection pooling [BC]
QueryBuilder now internally uses knex to run all queries. As knex already supports connection pooling, thbis feature comes without any effort. New configuration for pooling is now availableAs knex already supports connection pooling, this feature comes without any effort. BREAKING CHANGES: Transactions now require using EM.transactional() method, previous helpers `beginTransaction/commit/rollback` are now removed. All transaction management has been removed from IDatabaseDriver interface, now EM handles this, passing the transaction context (carried by EM, and created by Connection) to all driver methods. New methods on EM exists: `isInTransaction` and `getTransactionContext`. Because of knex being used as a query runner, there are some minor differences in results of plain sql calls (made by calling connection.execute() with string sql parameter instead of using QueryBuilder). Another difference is in postgre driver, that used to require passing parameters as indexed dollar sign ($1, $2, ...), while now knex requires the placeholder to be simple question mark (?), like in other dialects, so this is now unified with other drivers. Closes #64
- Loading branch information
Martin Adamek
committed
Jul 15, 2019
1 parent
fa24c8e
commit a1ae86d
Showing
43 changed files
with
1,387 additions
and
1,669 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import * as Knex from 'knex'; | ||
import { Config, QueryBuilder, Raw, Transaction } from 'knex'; | ||
import { readFile } from 'fs-extra'; | ||
|
||
import { Connection, QueryResult } from './Connection'; | ||
import { Utils } from '../utils'; | ||
import { EntityData, IEntity } from '../decorators'; | ||
|
||
export abstract class AbstractSqlConnection extends Connection { | ||
|
||
protected client: Knex; | ||
|
||
getKnex(): Knex { | ||
return this.client; | ||
} | ||
|
||
async close(force?: boolean): Promise<void> { | ||
await this.client.destroy(); | ||
} | ||
|
||
async isConnected(): Promise<boolean> { | ||
try { | ||
await this.client.raw('select 1'); | ||
return true; | ||
} catch { | ||
return false; | ||
} | ||
} | ||
|
||
async transactional(cb: (trx: Transaction) => Promise<any>, ctx?: Transaction): Promise<any> { | ||
await (ctx || this.client).transaction(async trx => { | ||
try { | ||
const ret = await cb(trx); | ||
await trx.commit(); | ||
|
||
return ret; | ||
} catch (e) { | ||
await trx.rollback(e); | ||
throw e; | ||
} | ||
}); | ||
} | ||
|
||
async execute(queryOrKnex: string | QueryBuilder | Raw, params: any[] = [], method: 'all' | 'get' | 'run' = 'all'): Promise<QueryResult | any | any[]> { | ||
if (Utils.isObject<QueryBuilder | Raw>(queryOrKnex)) { | ||
return await this.executeKnex(queryOrKnex, method); | ||
} | ||
|
||
const res = await this.executeQuery<any>(queryOrKnex, params, () => this.client.raw(queryOrKnex, params)); | ||
return this.transformRawResult(res, method); | ||
} | ||
|
||
async loadFile(path: string): Promise<void> { | ||
const buf = await readFile(path); | ||
await this.client.raw(buf.toString()); | ||
} | ||
|
||
protected createKnexClient(type: string): Knex { | ||
return Knex(this.getKnexOptions(type)) | ||
.on('query', data => { | ||
if (!data.__knexQueryUid) { | ||
this.logQuery(data.sql.toLowerCase().replace(/;$/, '')); | ||
} | ||
}); | ||
} | ||
|
||
protected getKnexOptions(type: string): Config { | ||
return { | ||
client: type, | ||
connection: this.getConnectionOptions(), | ||
pool: this.config.get('pool'), | ||
}; | ||
} | ||
|
||
protected async executeKnex(qb: QueryBuilder | Raw, method: 'all' | 'get' | 'run'): Promise<QueryResult | any | any[]> { | ||
const q = qb.toSQL(); | ||
const query = q.toNative ? q.toNative() : q; | ||
const res = await this.executeQuery(query.sql, query.bindings, () => qb); | ||
|
||
return this.transformKnexResult(res, method); | ||
} | ||
|
||
protected transformKnexResult(res: any, method: 'all' | 'get' | 'run'): QueryResult | any | any[] { | ||
if (method === 'all') { | ||
return res; | ||
} | ||
|
||
if (method === 'get') { | ||
return res[0]; | ||
} | ||
|
||
const affectedRows = typeof res === 'number' ? res : 0; | ||
const insertId = typeof res[0] === 'number' ? res[0] : 0; | ||
|
||
return { insertId, affectedRows, row: res[0] }; | ||
} | ||
|
||
protected abstract transformRawResult(res: any, method: 'all' | 'get' | 'run'): QueryResult | EntityData<IEntity> | EntityData<IEntity>[]; | ||
|
||
} |
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.