Created
June 2, 2025 15:37
-
-
Save shqld/ccf6071f82ee37d556eac99f91154ecd to your computer and use it in GitHub Desktop.
simple mysql-cli (only JavaScript)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import readline from 'node:readline/promises'; | |
import { createConnection } from 'mysql2/promise'; | |
let stdin = ''; | |
await new Promise((resolve) => { | |
readline | |
.createInterface({ input: process.stdin }) | |
.on('line', (line) => { | |
stdin += line + '\n'; | |
}) | |
.once('close', () => { | |
stdin = stdin.trim(); | |
resolve(); | |
}); | |
}); | |
const [dbUri] = process.argv.slice(2); | |
const connection = await createConnection({ | |
uri: dbUri, | |
}); | |
try { | |
const [rows] = await connection.query(stdin); | |
console.log('Rows:', rows); | |
} catch (error) { | |
console.error('Error executing query:', error); | |
process.exit(1); | |
} finally { | |
if (connection && connection.end) { | |
await connection.end(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment