You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* 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.
59
65
*/
60
66
exportasyncfunctioninitializeDatabase(){
61
67
try{
@@ -84,7 +90,12 @@ export async function initializeDatabase() {
84
90
}
85
91
86
92
/**
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.
88
99
*/
89
100
asyncfunctioncreateTables(){
90
101
constclient=awaitpool.connect();
@@ -302,7 +313,12 @@ async function createTables() {
302
313
}
303
314
304
315
/**
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.
306
322
*/
307
323
asyncfunctioninsertDefaultWheelStages(){
308
324
constdefaultStages=[
@@ -425,7 +441,14 @@ async function insertDefaultWheelStages() {
425
441
}
426
442
427
443
/**
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.
* Optional authentication middleware (doesn't fail if no token)
232
+
* Optional authentication middleware that allows requests to proceed without a token.
233
+
*
234
+
* This middleware checks for the presence of an authorization header. If the header is missing or does not start with 'Bearer ', it sets req.user and req.token to null and calls next() to continue the request. If the header is present, it attempts to call the authMiddleware function. If authMiddleware throws an error, it catches the error, sets req.user and req.token to null, and continues the request.
0 commit comments