Skip to content

Instantly share code, notes, and snippets.

@shqld
Created June 2, 2025 15:37
Show Gist options
  • Save shqld/ccf6071f82ee37d556eac99f91154ecd to your computer and use it in GitHub Desktop.
Save shqld/ccf6071f82ee37d556eac99f91154ecd to your computer and use it in GitHub Desktop.
simple mysql-cli (only JavaScript)
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