Skip to content

Commit

Permalink
DB: fix DB connection and query format
Browse files Browse the repository at this point in the history
Ooof, see the second change.

First: The .query().then() was just changed to make use of the new
promise interface instead of promisifying the old callback interface.
It wasn't the problem.

Second: Add support for utf8mb3 to the library (we'll open a pull to add
it upstream). See the code comment.
  • Loading branch information
danielbeardsley committed Sep 3, 2024
1 parent 5028d0b commit b4f26d8
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions lib/db.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
var mysql = require("mysql2"),
config = require("./config-loader"),
Promise = require("bluebird");
var mysql = require("mysql2/promise"),
config = require("./config-loader");

// OMG, this took forever to figure out
// Add support for understanding utf8mb3 charset to the mysql2 library
// https://github.com/sidorares/node-mysql2/issues/1398
// We don't have any utf8mb3 columns, but if you run a query that generates
// no result (DELETE, REPLACE, ...) it will return metadata indicating that the
// empty result is in the "servers" charset, which is utf8mb3 (still)
var EncodingToCharset = require("../node_modules/mysql2/lib/constants/encoding_charset");
EncodingToCharset.utf8mb3 = 192;

const pool = mysql.createPool({
host: config.mysql.host,
Expand All @@ -10,6 +18,9 @@ const pool = mysql.createPool({
charset: "utf8mb4",
});

pool.query = Promise.promisify(pool.query);

module.exports = pool;
module.exports = {
query: function(query, params) {
return pool.query(query, params)
.then((result) => result[0]); // [rows, fields]
}
};

0 comments on commit b4f26d8

Please sign in to comment.