-
-
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
34 changed files
with
1,158 additions
and
1,087 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,95 @@ | ||
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; | ||
let res = await this.executeQuery(query.sql, query.bindings, () => qb); | ||
|
||
if (method === 'run') { | ||
const affectedRows = typeof res === 'number' ? res : 0; | ||
const insertId = typeof res[0] === 'number' ? res[0] : 0; | ||
res = { insertId, affectedRows, row: res[0] }; | ||
} | ||
|
||
if (method === 'get') { | ||
return res[0]; | ||
} | ||
|
||
return res; | ||
} | ||
|
||
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
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.