MCPcopy Create free account
hub / github.com/QodeXcli/QodeX / getConnection

Function getConnection

src/tools/database/db-tools.ts:65–104  ·  view source on GitHub ↗
(parsed: ParsedConn)

Source from the content-addressed store, hash-verified

63}
64
65async function getConnection(parsed: ParsedConn): Promise<{ kind: Dialect; conn: any } | { kind: 'error'; message: string }> {
66 if (parsed.dialect === 'mysql') {
67 try {
68 // @ts-ignore — mysql2 is an optionalDependency
69 const mysql: any = await import('mysql2/promise');
70 const conn = await mysql.createConnection({
71 host: parsed.host, port: parsed.port, user: parsed.user, password: parsed.password, database: parsed.database,
72 });
73 return { kind: 'mysql', conn };
74 } catch (e: any) {
75 return { kind: 'error', message: `MySQL driver not installed or connection failed: ${e?.message}. Install: npm install mysql2 --save-optional` };
76 }
77 }
78 if (parsed.dialect === 'postgres') {
79 try {
80 // @ts-ignore — pg is an optionalDependency; types may not be installed
81 const pg: any = await import('pg');
82 const Client = pg.default?.Client ?? pg.Client;
83 const conn = new Client({
84 host: parsed.host, port: parsed.port, user: parsed.user, password: parsed.password, database: parsed.database,
85 });
86 await conn.connect();
87 return { kind: 'postgres', conn };
88 } catch (e: any) {
89 return { kind: 'error', message: `Postgres driver not installed or connection failed: ${e?.message}. Install: npm install pg --save-optional` };
90 }
91 }
92 if (parsed.dialect === 'sqlite') {
93 try {
94 // @ts-ignore — better-sqlite3 loaded dynamically; keep optional
95 const sqlite: any = await import('better-sqlite3');
96 const Database = sqlite.default ?? sqlite;
97 const conn = new Database(parsed.filepath, { readonly: false });
98 return { kind: 'sqlite', conn };
99 } catch (e: any) {
100 return { kind: 'error', message: `SQLite driver not installed: ${e?.message}. Install: npm install better-sqlite3 --save-optional` };
101 }
102 }
103 return { kind: 'error', message: 'Unsupported dialect' };
104}
105
106async function closeConnection(c: any, dialect: Dialect): Promise<void> {
107 try {

Callers 2

executeMethod · 0.85
executeMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected