@churchapps/apihelper
Version:
Library of helper functions not specific to any one ChurchApps project or framework.
56 lines • 2.04 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Pool = void 0;
const dotenv_1 = __importDefault(require("dotenv"));
const mysql2_1 = __importDefault(require("mysql2"));
const _1 = require(".");
dotenv_1.default.config();
class Pool {
static initPool() {
const config = this.getConfig(_1.EnvironmentBase.connectionString);
Pool.current = mysql2_1.default.createPool({
connectionLimit: 3,
host: config.host,
port: config.port,
database: config.database,
user: config.userName,
password: config.password,
multipleStatements: true,
waitForConnections: true,
queueLimit: 9999,
charset: 'utf8mb4',
typeCast: function castField(field, useDefaultTypeCasting) {
// convert bit(1) to bool
if ((field.type === "BIT") && (field.length === 1)) {
try {
const bytes = field.buffer();
return (bytes[0] === 1);
}
catch {
return false;
}
}
return useDefaultTypeCasting();
}
});
}
}
exports.Pool = Pool;
// a bit of a hack
Pool.getConfig = (connectionString) => {
// mysql://user:password@host:port/dbName
const firstSplit = connectionString.replace("mysql://", "").split("@");
const userPass = firstSplit[0].split(":");
const userName = userPass[0];
const password = userPass[1];
const hostDb = firstSplit[1].split("/");
const database = hostDb[1];
const hostPort = hostDb[0].split(':');
const host = hostPort[0];
const port = parseInt(hostPort[1], 0);
return { host, port, database, userName, password };
};
//# sourceMappingURL=Pool.js.map