reorder functions for no-use-before-define

This commit is contained in:
Girish Ramakrishnan
2026-02-14 16:34:34 +01:00
parent 36aa641cb9
commit e9f96593c3
31 changed files with 2621 additions and 2648 deletions
+41 -42
View File
@@ -10,9 +10,6 @@ import shellModule from './shell.js';
const debug = debugModule('box:database');
const shell = shellModule('database');
const _clear = clear;
let gConnectionPool = null;
const gDatabase = {
@@ -23,6 +20,45 @@ const gDatabase = {
name: 'box'
};
async function uninitialize() {
if (!gConnectionPool) return;
await safe(gConnectionPool.end(), { debug });
gConnectionPool = null;
debug('pool closed');
}
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, { sqlCode: 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, { sqlCode: 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, { sqlCode: error.code, sqlMessage: error.sqlMessage || null });
}
}
async function initialize() {
if (gConnectionPool !== null) return;
@@ -64,14 +100,6 @@ async function initialize() {
});
}
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' }];
@@ -81,37 +109,6 @@ async function clear() {
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, { sqlCode: 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, { sqlCode: 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, { sqlCode: error.code, sqlMessage: error.sqlMessage || null });
}
}
async function runInTransaction(callback) {
assert.strictEqual(typeof callback, 'function');
@@ -161,6 +158,8 @@ async function exportToFile(file) {
if (error) throw new BoxError(BoxError.DATABASE_ERROR, error);
}
const _clear = clear;
export default {
initialize,
uninitialize,