UNPKG

4.14 kBJavaScriptView Raw
1"use strict";
2/*
3 * @adonisjs/lucid
4 *
5 * (c) Harminder Virk <virk@adonisjs.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10Object.defineProperty(exports, "__esModule", { value: true });
11exports.MysqlDialect = void 0;
12/// <reference path="../../adonis-typings/index.ts" />
13const Raw_1 = require("../Database/StaticBuilder/Raw");
14class MysqlDialect {
15 constructor(client) {
16 Object.defineProperty(this, "client", {
17 enumerable: true,
18 configurable: true,
19 writable: true,
20 value: client
21 });
22 Object.defineProperty(this, "name", {
23 enumerable: true,
24 configurable: true,
25 writable: true,
26 value: 'mysql'
27 });
28 Object.defineProperty(this, "supportsAdvisoryLocks", {
29 enumerable: true,
30 configurable: true,
31 writable: true,
32 value: true
33 });
34 /**
35 * Reference to the database version. Knex.js fetches the version after
36 * the first database query, so it will be set to undefined initially
37 */
38 Object.defineProperty(this, "version", {
39 enumerable: true,
40 configurable: true,
41 writable: true,
42 value: this.client.getReadClient()['context']['client'].version
43 });
44 /**
45 * The default format for datetime column. The date formats is
46 * valid for luxon date parsing library
47 */
48 Object.defineProperty(this, "dateTimeFormat", {
49 enumerable: true,
50 configurable: true,
51 writable: true,
52 value: 'yyyy-MM-dd HH:mm:ss'
53 });
54 }
55 /**
56 * Truncate mysql table with option to cascade
57 */
58 async truncate(table, cascade = false) {
59 if (!cascade) {
60 return this.client.knexQuery().table(table).truncate();
61 }
62 /**
63 * Cascade and truncate
64 */
65 const trx = await this.client.transaction();
66 try {
67 await trx.rawQuery('SET FOREIGN_KEY_CHECKS=0;');
68 await trx.knexQuery().table(table).truncate();
69 await trx.rawQuery('SET FOREIGN_KEY_CHECKS=1;');
70 await trx.commit();
71 }
72 catch (error) {
73 await trx.rollback();
74 throw error;
75 }
76 }
77 /**
78 * Returns an array of table names
79 */
80 async getAllTables() {
81 const tables = await this.client
82 .query()
83 .from('information_schema.tables')
84 .select('table_name as table_name')
85 .where('TABLE_TYPE', 'BASE TABLE')
86 .where('table_schema', new Raw_1.RawBuilder('database()'))
87 .orderBy('table_name', 'asc');
88 return tables.map(({ table_name }) => table_name);
89 }
90 /**
91 * Drop all tables inside the database
92 */
93 async dropAllTables() {
94 const tables = await this.getAllTables();
95 /**
96 * Cascade and truncate
97 */
98 const trx = await this.client.transaction();
99 try {
100 await trx.rawQuery('SET FOREIGN_KEY_CHECKS=0;');
101 await trx.rawQuery(`DROP table ${tables.join(',')};`);
102 await trx.rawQuery('SET FOREIGN_KEY_CHECKS=1;');
103 await trx.commit();
104 }
105 catch (error) {
106 await trx.rollback();
107 throw error;
108 }
109 }
110 /**
111 * Attempts to add advisory lock to the database and
112 * returns it's status.
113 */
114 async getAdvisoryLock(key, timeout = 0) {
115 const response = await this.client.rawQuery(`SELECT GET_LOCK('${key}', ${timeout}) as lock_status;`);
116 return response[0] && response[0][0] && response[0][0].lock_status === 1;
117 }
118 /**
119 * Releases the advisory lock
120 */
121 async releaseAdvisoryLock(key) {
122 const response = await this.client.rawQuery(`SELECT RELEASE_LOCK('${key}') as lock_status;`);
123 return response[0] && response[0][0] && response[0][0].lock_status === 1;
124 }
125}
126exports.MysqlDialect = MysqlDialect;