Files
cloudron-box/src/database.js
Girish Ramakrishnan cbc73f5c9a remove uuid module
built into node.js now
2025-07-28 12:54:09 +02:00

147 lines
5.7 KiB
JavaScript

'use strict';
exports = module.exports = {
initialize,
uninitialize,
query,
transaction,
importFromFile,
exportToFile,
_clear: clear
};
const assert = require('assert'),
BoxError = require('./boxerror.js'),
constants = require('./constants.js'),
debug = require('debug')('box:database'),
execSync = require('child_process').execSync,
mysql = require('mysql2/promise'),
safe = require('safetydance'),
shell = require('./shell.js')('database');
let gConnectionPool = null;
const gDatabase = {
hostname: '127.0.0.1',
username: 'root',
password: 'password',
port: 3306,
name: 'box'
};
async function initialize() {
if (gConnectionPool !== null) return;
if (constants.TEST) {
// see setupTest script how the mysql-server is run
gDatabase.hostname = execSync('docker inspect -f "{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}" mysql-server').toString().trim();
}
// https://github.com/mysqljs/mysql#pool-options
gConnectionPool = mysql.createPool({
connectionLimit: 5,
connectTimeout: 60000,
host: gDatabase.hostname,
user: gDatabase.username,
password: gDatabase.password,
port: gDatabase.port,
database: gDatabase.name,
multipleStatements: false,
waitForConnections: true, // getConnection() will wait until a connection is avaiable
ssl: false,
timezone: 'Z', // mysql follows the SYSTEM timezone. on Cloudron, this is UTC
charset: 'utf8mb4',
jsonStrings: true, // for JSON types, JSONARRAYAGG will return string instead of JSON
});
// run one time setup commands on new connections. connections are reused and so we cannot use 'acquire' event of the pool
gConnectionPool.on('connection', async function (connection) { // https://github.com/sidorares/node-mysql2/issues/565, 567
debug('new connection');
const conn = connection.promise(); // convert PoolConnection to PromisePoolConnection
try {
// await connection.query('SET NAMES utf8mb4 COLLATE utf8mb4_bin');
await conn.query('SET SESSION sql_mode = \'strict_all_tables\''); // disable type coercion etc
// GROUP_CONCAT has only 1024 default. we use it in the groups API which doesn't support pagination yet
// a crypto.randomUUID is 36 in length. so the value below provides for roughly 10k users
await conn.query('SET SESSION group_concat_max_len = 360000');
} catch (error) {
debug(`failed to init new db connection ${connection.threadId}:`, error); // only log. we will let the app handle the exception when it calls query()/transaction()
}
});
}
async function uninitialize() {
if (!gConnectionPool) return;
await safe(gConnectionPool.end(), { debug });
gConnectionPool = null;
debug('pool closed');
}
async function clear() {
const tables = await query('SELECT table_name FROM information_schema.tables WHERE table_schema = ? AND table_name != ?', [ 'box', 'migrations' ]);
const queries = [{ query: 'SET FOREIGN_KEY_CHECKS = 0' }];
for (const t of tables) queries.push({ query: `TRUNCATE TABLE ${t.TABLE_NAME}` });
queries.push({ query: 'SET FOREIGN_KEY_CHECKS = 1' }); // this is a session/connection variable, must be reset back
await transaction(queries);
}
async function query(...args) {
assert.notStrictEqual(gConnectionPool, null, 'Database connection is already closed');
const [error, result] = await safe(gConnectionPool.query(...args)); // this is same as getConnection/query/release
if (error) throw new BoxError(BoxError.DATABASE_ERROR, error, { code: error.code, sqlMessage: error.sqlMessage || null });
return result[0]; // the promise version returns a tuple of [rows, fields]
}
async function transaction(queries) {
assert(Array.isArray(queries));
const [error, connection] = await safe(gConnectionPool.getConnection());
if (error) throw new BoxError(BoxError.DATABASE_ERROR, error, { code: error.code, sqlMessage: error.sqlMessage });
try {
await connection.beginTransaction();
const results = [];
for (const query of queries) {
const [rows /*, fields */] = await connection.query(query.query, query.args);
results.push(rows);
}
await connection.commit();
connection.release(); // no await!
return results;
} catch (error) {
await safe(connection.rollback(), { debug });
connection.release(); // no await!
throw new BoxError(BoxError.DATABASE_ERROR, error, { code: error.code, sqlMessage: error.sqlMessage || null });
}
}
async function importFromFile(file) {
assert.strictEqual(typeof file, 'string');
const cmd = `/usr/bin/mysql -h "${gDatabase.hostname}" -u ${gDatabase.username} -p${gDatabase.password} ${gDatabase.name} < ${file}`;
await query('CREATE DATABASE IF NOT EXISTS box');
const [error] = await safe(shell.bash(cmd, {}));
if (error) throw new BoxError(BoxError.DATABASE_ERROR, error);
}
async function exportToFile(file) {
assert.strictEqual(typeof file, 'string');
// latest mysqldump enables column stats by default which is not present in 5.7 util
const mysqlDumpHelp = await shell.spawn('/usr/bin/mysqldump', ['--help'], { encoding: 'utf8' });
const hasColStats = mysqlDumpHelp.includes('column-statistics');
const colStats = hasColStats ? '--column-statistics=0' : '';
const cmd = `/usr/bin/mysqldump -h "${gDatabase.hostname}" -u root -p${gDatabase.password} ${colStats} --single-transaction --routines --triggers ${gDatabase.name} > "${file}"`;
const [error] = await safe(shell.bash(cmd, {}));
if (error) throw new BoxError(BoxError.DATABASE_ERROR, error);
}