UNPKG

@adonisjs/lucid

Version:

SQL ORM built on top of Active Record pattern

195 lines (194 loc) 6.31 kB
"use strict"; /* * @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. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.MysqlDialect = void 0; /// <reference path="../../adonis-typings/index.ts" /> const Raw_1 = require("../Database/StaticBuilder/Raw"); class MysqlDialect { constructor(client, config) { Object.defineProperty(this, "client", { enumerable: true, configurable: true, writable: true, value: client }); Object.defineProperty(this, "config", { enumerable: true, configurable: true, writable: true, value: config }); Object.defineProperty(this, "name", { enumerable: true, configurable: true, writable: true, value: 'mysql' }); Object.defineProperty(this, "supportsAdvisoryLocks", { enumerable: true, configurable: true, writable: true, value: true }); Object.defineProperty(this, "supportsViews", { enumerable: true, configurable: true, writable: true, value: true }); Object.defineProperty(this, "supportsTypes", { enumerable: true, configurable: true, writable: true, value: false }); Object.defineProperty(this, "supportsReturningStatement", { enumerable: true, configurable: true, writable: true, value: false }); /** * Reference to the database version. Knex.js fetches the version after * the first database query, so it will be set to undefined initially */ Object.defineProperty(this, "version", { enumerable: true, configurable: true, writable: true, value: this.client.getReadClient()['context']['client'].version }); /** * The default format for datetime column. The date formats is * valid for luxon date parsing library */ Object.defineProperty(this, "dateTimeFormat", { enumerable: true, configurable: true, writable: true, value: 'yyyy-MM-dd HH:mm:ss' }); } /** * Truncate mysql table with option to cascade */ async truncate(table, cascade = false) { if (!cascade) { return this.client.knexQuery().table(table).truncate(); } /** * Cascade and truncate */ const trx = await this.client.transaction(); try { await trx.rawQuery('SET FOREIGN_KEY_CHECKS=0;'); await trx.knexQuery().table(table).truncate(); await trx.rawQuery('SET FOREIGN_KEY_CHECKS=1;'); await trx.commit(); } catch (error) { await trx.rollback(); throw error; } } /** * Returns an array of table names */ async getAllTables() { const tables = await this.client .query() .from('information_schema.tables') .select('table_name as table_name') .where('TABLE_TYPE', 'BASE TABLE') .where('table_schema', new Raw_1.RawBuilder('database()')) .orderBy('table_name', 'asc'); return tables.map(({ table_name }) => table_name); } /** * Returns an array of all views names */ async getAllViews() { const tables = await this.client .query() .from('information_schema.tables') .select('table_name as table_name') .where('TABLE_TYPE', 'VIEW') .where('table_schema', new Raw_1.RawBuilder('database()')) .orderBy('table_name', 'asc'); return tables.map(({ table_name }) => table_name); } /** * Returns an array of all types names */ async getAllTypes() { throw new Error("MySQL doesn't support types"); } /** * Drop all tables inside the database */ async dropAllTables() { let tables = await this.getAllTables(); /** * Filter out tables that are not allowed to be dropped */ tables = tables.filter((table) => !(this.config.wipe?.ignoreTables || []).includes(table)); /** * Add backquote around table names to avoid syntax errors * in case of a table name with a reserved keyword */ tables = tables.map((table) => '`' + table + '`'); if (!tables.length) { return; } /** * Cascade and truncate */ const trx = await this.client.transaction(); try { await trx.rawQuery('SET FOREIGN_KEY_CHECKS=0;'); await trx.rawQuery(`DROP TABLE ${tables.join(',')};`); await trx.rawQuery('SET FOREIGN_KEY_CHECKS=1;'); await trx.commit(); } catch (error) { await trx.rollback(); throw error; } } /** * Drop all views inside the database */ async dropAllViews() { const views = await this.getAllViews(); return this.client.rawQuery(`DROP VIEW ${views.join(',')};`); } /** * Drop all custom types inside the database */ async dropAllTypes() { throw new Error("MySQL doesn't support types"); } /** * Attempts to add advisory lock to the database and * returns it's status. */ async getAdvisoryLock(key, timeout = 0) { const response = await this.client.rawQuery(`SELECT GET_LOCK('${key}', ${timeout}) as lock_status;`); return response[0] && response[0][0] && response[0][0].lock_status === 1; } /** * Releases the advisory lock */ async releaseAdvisoryLock(key) { const response = await this.client.rawQuery(`SELECT RELEASE_LOCK('${key}') as lock_status;`); return response[0] && response[0][0] && response[0][0].lock_status === 1; } } exports.MysqlDialect = MysqlDialect;