UNPKG

tspace-mysql

Version:

Tspace MySQL is a promise-based ORM for Node.js, designed with modern TypeScript and providing type safety for schema databases.

150 lines 5.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MariadbDriver = void 0; const __1 = require(".."); const MariadbQueryBuilder_1 = require("./MariadbQueryBuilder"); class MariadbDriver extends __1.BaseDriver { constructor(options) { super(); this.options = options; } connect() { const options = this.options; const mariadb = this.import("mariadb"); this.pool = mariadb.createPool({ host: options.host, port: options.port, database: options.database, user: options.user || options.username, password: options.password, connectionLimit: options.connectionLimit ?? 20, connectTimeout: options.connectTimeout ?? 1000 * 60, minimumIdle: Math.max(2, Math.floor((options.connectionLimit ?? 20) / 3)), acquireTimeout: 1000 * 20, idleTimeout: 1000 * 60, queryTimeout: 1000 * 60, pipelining: true, bigIntAsNumber: true, insertIdAsNumber: true, }); this.pool.getConnection().catch((err) => { const message = this._messageError.bind(this); process.nextTick(() => { console.log(message(err?.message)); return process.exit(); }); }); return { database: () => options.database, on: (event, data) => this.on(event, data), queryBuilder: MariadbQueryBuilder_1.MariadbQueryBuilder, query: (sql) => this._query(sql), connection: () => this._connection(), end: () => this._end() }; } disconnect(pool) { if (pool == null) return; pool?.end(() => { pool = undefined; }); } async _query(sql) { const start = Date.now(); const results = await this.pool.query(sql); this._detectEventQuery({ start, sql }); this.meta(results, sql); return this.returning(results); } async _connection() { let closeTransaction = false; const connection = await this.pool.getConnection(); const query = async (sql) => { if (closeTransaction) throw new Error(this.MESSAGE_TRX_CLOSED); const start = Date.now(); const results = await connection.query(sql); this._detectEventQuery({ start, sql }); this.meta(results, sql); return this.returning(results); }; const startTransaction = async () => { if (closeTransaction) throw new Error(this.MESSAGE_TRX_CLOSED); await connection.beginTransaction(); }; const commit = async () => { if (closeTransaction) throw new Error(this.MESSAGE_TRX_CLOSED); await connection.commit(); await end(); }; const rollback = async () => { if (closeTransaction) throw new Error(this.MESSAGE_TRX_CLOSED); await connection.rollback(); await end(); }; const end = async () => { await new Promise((resolve) => setTimeout(() => { if (!closeTransaction) { closeTransaction = true; // After commit the transaction, you can't perform any actions with this transaction. connection.destroy(); // After destroying the connection, it will be removed from the connection this.pool. this.pool.end(); } return resolve(); }, 500)); return; }; return { on: (event, data) => this.on(event, data), query, queryBuilder: MariadbQueryBuilder_1.MariadbQueryBuilder, startTransaction, commit, rollback, end, }; } async _end() { await this.pool.end(); this.pool = undefined; } meta(results, sql) { if (Array.isArray(results)) return; if (results.$meta == null) results.$meta = {}; const command = this._detectQueryType(sql); results.$meta = { command }; if (command === 'INSERT') { const insertIds = results.affectedRows <= 1 ? [results.insertId] : [...Array(results.affectedRows)].map((_, i) => results.insertId + i); results.$meta = { ...results.$meta, insertIds, affected: true }; } if (command === 'UPDATE' || command === 'DELETE') { results.$meta = { ...results.$meta, insertIds: [], affected: Boolean(results.affectedRows) }; } } returning(results) { if (Array.isArray(results)) return results; return results; } } exports.MariadbDriver = MariadbDriver; //# sourceMappingURL=MariadbDriver.js.map