Last active
December 9, 2022 10:24
-
-
Save hagemann/30cfee724d047007a031eb12b3a95a23 to your computer and use it in GitHub Desktop.
Promisified MySQL middleware for Node.js
This file contains 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
const util = require('util') | |
const mysql = require('mysql') | |
const pool = mysql.createPool({ | |
connectionLimit: 10, | |
host: 'localhost', | |
user: 'root', | |
password: 'password', | |
database: 'my_database' | |
}) | |
// Ping database to check for common exception errors. | |
pool.getConnection((err, connection) => { | |
if (err) { | |
if (err.code === 'PROTOCOL_CONNECTION_LOST') { | |
console.error('Database connection was closed.') | |
} | |
if (err.code === 'ER_CON_COUNT_ERROR') { | |
console.error('Database has too many connections.') | |
} | |
if (err.code === 'ECONNREFUSED') { | |
console.error('Database connection was refused.') | |
} | |
} | |
if (connection) connection.release() | |
return | |
}) | |
// Promisify for Node.js async/await. | |
pool.query = util.promisify(pool.query) | |
module.exports = pool |
Hi there,
I am using this database connection configuration but somehow it is throwing an error while fire the query.
import { pool } from '../../config/mysql-config';
public async insertRecords() {
try {
const SQL = `INSERT INTO ${tableName} (description, title)
VALUES ("description", "title")`
const result = await pool.query(SQL)
return result
} catch (error) {
throw new Error(error);
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi, how I add queryFormat to pool connections?