Skip to content

Commit b8fdfc6

Browse files
[Penify]: Documentation for commit - b5926d5
1 parent b5926d5 commit b8fdfc6

1 file changed

Lines changed: 45 additions & 9 deletions

File tree

backend/config/database.js

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,13 @@ pool.on('remove', (client) => {
5555
});
5656

5757
/**
58-
* Initialize database connection and create tables
58+
* Initialize database connection and create necessary tables.
59+
*
60+
* This function establishes a connection to the database using the provided configuration,
61+
* tests the connection by executing a simple query, and logs the connection details.
62+
* If the connection is successful, it proceeds to create the required tables by calling
63+
* the createTables function. In case of any errors during the process, it logs the error
64+
* and rethrows it for further handling.
5965
*/
6066
export async function initializeDatabase() {
6167
try {
@@ -84,7 +90,12 @@ export async function initializeDatabase() {
8490
}
8591

8692
/**
87-
* Create database tables
93+
* Create database tables and initialize the database schema.
94+
*
95+
* This function connects to the database, begins a transaction, and creates several tables including users, wheel_stages, user_progress, user_sessions, user_encrypted_data, analytics_events, and audit_logs. It also enables necessary extensions, creates indexes for performance, and sets up triggers for updating timestamps. Finally, it inserts default wheel stages if they do not already exist. If any error occurs, the transaction is rolled back.
96+
*
97+
* @returns {Promise<void>} A promise that resolves when the tables are created and initialized.
98+
* @throws {Error} If there is an error during the database operations.
8899
*/
89100
async function createTables() {
90101
const client = await pool.connect();
@@ -302,7 +313,12 @@ async function createTables() {
302313
}
303314

304315
/**
305-
* Insert default wheel stages
316+
* Insert default wheel stages into the database if none exist.
317+
*
318+
* This asynchronous function connects to the database and checks if any wheel stages are already present.
319+
* If no stages are found, it inserts a predefined set of default stages, each with attributes such as title,
320+
* symbol, essence, meaning, action, chant, and order_index. The function also logs the insertion process
321+
* and handles any potential errors during the database operations.
306322
*/
307323
async function insertDefaultWheelStages() {
308324
const defaultStages = [
@@ -425,7 +441,14 @@ async function insertDefaultWheelStages() {
425441
}
426442

427443
/**
428-
* Execute a database query with error handling and logging
444+
* Execute a database query with error handling and logging.
445+
*
446+
* This function connects to the database, executes the provided SQL query with optional parameters,
447+
* and logs the duration and result of the query. In case of an error, it logs the error message and
448+
* rethrows the error. The database client is released after the operation, ensuring proper resource management.
449+
*
450+
* @param {string} text - The SQL query to be executed.
451+
* @param {Array} [params=[]] - The parameters for the SQL query.
429452
*/
430453
export async function query(text, params = []) {
431454
const start = Date.now();
@@ -454,7 +477,15 @@ export async function query(text, params = []) {
454477
}
455478

456479
/**
457-
* Execute a transaction
480+
* Execute a transaction.
481+
*
482+
* This function establishes a connection to the database, begins a transaction,
483+
* and executes the provided callback function with the database client. If the
484+
* callback completes successfully, the transaction is committed; if an error
485+
* occurs, the transaction is rolled back. Finally, the database client is released.
486+
*
487+
* @param {Function} callback - A function that takes the database client as an argument
488+
* and performs operations within the transaction.
458489
*/
459490
export async function transaction(callback) {
460491
const client = await pool.connect();
@@ -473,7 +504,7 @@ export async function transaction(callback) {
473504
}
474505

475506
/**
476-
* Store encrypted data for a user
507+
* Store encrypted data for a user in the database.
477508
*/
478509
export async function storeEncryptedData(userId, dataType, data) {
479510
const encryptedData = encryptField(data);
@@ -487,7 +518,7 @@ export async function storeEncryptedData(userId, dataType, data) {
487518
}
488519

489520
/**
490-
* Retrieve encrypted data for a user
521+
* Retrieve and decrypt encrypted data for a user.
491522
*/
492523
export async function getEncryptedData(userId, dataType) {
493524
const result = await query(`
@@ -504,7 +535,7 @@ export async function getEncryptedData(userId, dataType) {
504535
}
505536

506537
/**
507-
* Close database connection
538+
* Closes the database connection.
508539
*/
509540
export async function closeDatabase() {
510541
try {
@@ -516,7 +547,12 @@ export async function closeDatabase() {
516547
}
517548

518549
/**
519-
* Health check
550+
* Performs a health check on the database.
551+
*
552+
* This function executes a simple query to verify the database's availability.
553+
* It checks if the result indicates a healthy state by comparing the returned value
554+
* to 1. In case of an error during the query execution, it logs the error and
555+
* returns false to indicate an unhealthy state.
520556
*/
521557
export async function healthCheck() {
522558
try {

0 commit comments

Comments
 (0)