UNPKG

@adonisjs/lucid

Version:

SQL ORM built on top of Active Record pattern

126 lines (125 loc) 2.97 kB
/* * @adonisjs/lucid * * (c) Harminder Virk <virk@adonisjs.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ import { QueryRunner } from '../../query_runner/index.js'; /** * Exposes the API to execute raw queries */ export class RawQueryBuilder { knexQuery; client; /** * Custom data someone want to send to the profiler and the * query event */ customReporterData; /** * Control whether to debug the query or not. The initial * value is inherited from the query client */ debugQueries; constructor(knexQuery, client) { this.knexQuery = knexQuery; this.client = client; this.debugQueries = this.client.debug; } /** * Returns the log data */ getQueryData() { return { connection: this.client.connectionName, inTransaction: this.client.isTransaction, ...this.customReporterData, }; } /** * Define custom reporter data. It will be merged with * the existing data */ reporterData(data) { this.customReporterData = data; return this; } /** * Wrap the query with before/after strings. */ wrap(before, after) { this.knexQuery.wrap(before, after); return this; } /** * Turn on/off debugging for this query */ debug(debug) { this.debugQueries = debug; return this; } /** * Define query timeout */ timeout(time, options) { this.knexQuery['timeout'](time, options); return this; } /** * Returns SQL query as a string */ toQuery() { return this.knexQuery.toQuery(); } /** * @deprecated * Do not use this method. Instead create a query using the * transaction client directly. * * ```ts * trx.rawQuery() * ``` */ useTransaction(transaction) { this.knexQuery.transacting(transaction.knexClient); return this; } /** * Executes the query */ async exec() { return new QueryRunner(this.client, this.debugQueries, this.getQueryData()).run(this.knexQuery); } /** * Get sql representation of the query */ toSQL() { return this.knexQuery.toSQL(); } /** * Implementation of `then` for the promise API */ then(resolve, reject) { return this.exec().then(resolve, reject); } /** * Implementation of `catch` for the promise API */ catch(reject) { return this.exec().catch(reject); } /** * Implementation of `finally` for the promise API */ finally(fullfilled) { return this.exec().finally(fullfilled); } /** * Required when Promises are extended */ get [Symbol.toStringTag]() { return this.constructor.name; } }