UNPKG

iagate-querykit

Version:

QueryKit: lightweight TypeScript query toolkit with models, views, triggers, events, scheduler and adapters (better-sqlite3).

47 lines (46 loc) 1.29 kB
import { createRequire } from 'node:module'; let mysql; function ensureMysql() { if (mysql) return; const mocked = globalThis.__vitest_mocks__?.mysql2; if (mocked) { mysql = mocked; return; } try { const req = createRequire(import.meta.url); mysql = req('mysql2/promise'); } catch { throw new Error('mysql2 is required: npm install mysql2'); } } function convertPlaceholders(sql) { return sql; } export class MysqlExecutor { config; dialect = 'mysql'; pool; constructor(config) { this.config = config; ensureMysql(); this.pool = mysql.createPool({ host: config.host || '127.0.0.1', port: config.port || 3306, user: config.user, password: config.password, database: config.database, connectionLimit: config.connectionLimit || 10, decimalNumbers: true, namedPlaceholders: false, }); } async executeQuery(sql, bindings = []) { const conv = convertPlaceholders(sql); const [rows, info] = await this.pool.execute(conv, bindings); if (/^\s*select/i.test(conv)) { return { data: rows }; } return [rows, info]; } }