|
| 1 | +# 🗃️ `PuddySqlInstance` Class |
| 2 | + |
| 3 | +Extends [`PuddySqlEngine`](./PuddySqlEngine.mjs) to provide high-level SQL operations with table management, debug logging, and support for both **SQLite3** and **PostgreSQL**. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## 🧠 Constructor |
| 8 | + |
| 9 | +```js |
| 10 | +new PuddySqlInstance() |
| 11 | +``` |
| 12 | + |
| 13 | +Initializes the instance and sets up internal properties. |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | +## 🔧 Internal Properties |
| 18 | + |
| 19 | +* `#db`: The internal database instance (`sqlite` or `pg.Pool`). |
| 20 | +* `#tables`: A record of initialized tables using `PuddySqlQuery`. |
| 21 | +* `#debug`: Whether debug logging is enabled. |
| 22 | +* `#debugCount`: Number of debug calls (optional use). |
| 23 | +* `#consoleColors`: Whether debug console output uses colors. |
| 24 | + |
| 25 | +--- |
| 26 | + |
| 27 | +## 🎨 Debug Configuration |
| 28 | + |
| 29 | +### 🎛️ `setConsoleColors(state: boolean): void` |
| 30 | + |
| 31 | +Enable or disable ANSI colors in debug output. |
| 32 | + |
| 33 | +### ✅ `getConsoleColors(): boolean` |
| 34 | + |
| 35 | +Returns whether color output is enabled. |
| 36 | + |
| 37 | +--- |
| 38 | + |
| 39 | +## 🖌️ SQL Formatting (for Debug Output) |
| 40 | + |
| 41 | +### 📜 `debugSql(value: string): string` |
| 42 | + |
| 43 | +Formats a SQL string with indentation and ANSI color codes for better console readability. |
| 44 | + |
| 45 | +* Adds line breaks and highlights clauses like `SELECT`, `WHERE`, `JOIN`, `LIMIT`, etc. |
| 46 | +* Wraps in decorative box with ANSI colors. |
| 47 | + |
| 48 | +### 🧾 `debugConsoleText(id: string | number, debugName?: string, status?: string): string` |
| 49 | + |
| 50 | +Formats a debug console message with styled tags like: |
| 51 | + |
| 52 | +```txt |
| 53 | +[SQL][123] [DEBUG] [MyQuery] [OK] |
| 54 | +``` |
| 55 | + |
| 56 | +--- |
| 57 | + |
| 58 | +## 🐞 Debug Mode |
| 59 | + |
| 60 | +### 🔛 `setIsDebug(isDebug: boolean): void` |
| 61 | + |
| 62 | +Turns debug logging on or off. |
| 63 | + |
| 64 | +### 🔍 `isDebug(): boolean` |
| 65 | + |
| 66 | +Returns `true` if debug mode is enabled. |
| 67 | + |
| 68 | +--- |
| 69 | + |
| 70 | +## 📦 Table Management |
| 71 | + |
| 72 | +### 🆕 `initTable(settings?: TableSettings, tableData?: SqlTableConfig): Promise<PuddySqlQuery>` |
| 73 | + |
| 74 | +Initializes a new table if it hasn't already been created. |
| 75 | + |
| 76 | +* Throws if the table already exists. |
| 77 | +* Internally creates a `PuddySqlQuery` instance and assigns settings/data. |
| 78 | + |
| 79 | +### 🔍 `getTable(tableName: string): PuddySqlQuery` |
| 80 | + |
| 81 | +Returns the existing table instance for a given name. |
| 82 | + |
| 83 | +* Throws if table does not exist. |
| 84 | + |
| 85 | +### ❓ `hasTable(tableName: string): boolean` |
| 86 | + |
| 87 | +Checks if a table has been initialized. |
| 88 | + |
| 89 | +### 🗑️ `removeTable(tableName: string): void` |
| 90 | + |
| 91 | +Removes a table from the internal map without affecting the actual DB. |
| 92 | + |
| 93 | +* Throws if the table does not exist. |
| 94 | + |
| 95 | +### 💣 `dropTable(tableName: string): Promise<void>` |
| 96 | + |
| 97 | +Drops the table from the database schema, then removes it internally. |
| 98 | + |
| 99 | +* Uses the `dropTable()` method of the `PuddySqlQuery` class. |
| 100 | + |
| 101 | +--- |
| 102 | + |
| 103 | +## 🔍 Database Access |
| 104 | + |
| 105 | +### 💾 `getDb(): SqliteDb | PgPool` |
| 106 | + |
| 107 | +Returns the internal database connection instance. |
| 108 | + |
| 109 | +* Useful for advanced custom queries. |
| 110 | +* Throws if the DB has not been initialized. |
| 111 | + |
| 112 | +--- |
| 113 | + |
| 114 | +## 🔌 Connection & Engine Initialization |
| 115 | + |
| 116 | +### ✅ `isDbOpen(): Promise<boolean>` |
| 117 | + |
| 118 | +Checks if the database connection is open by running a test query (`SELECT 1`). |
| 119 | + |
| 120 | +--- |
| 121 | + |
| 122 | +### 🐿️ `initSqlite3(filePath?: string = ':memory:'): Promise<void>` |
| 123 | + |
| 124 | +Initializes a SQLite3 database connection (requires SQLite ≥ 3.35.0). |
| 125 | + |
| 126 | +* Automatically sets the SQL engine to `"sqlite3"`. |
| 127 | +* Adds graceful shutdown logic (e.g., on `SIGINT`). |
| 128 | + |
| 129 | +--- |
| 130 | + |
| 131 | +### 🧩 `setSqlite3(db: SqliteDb): void` |
| 132 | + |
| 133 | +Sets SQLite3-specific methods (`all`, `get`, `run`) using the provided DB instance. |
| 134 | + |
| 135 | +Each method includes: |
| 136 | + |
| 137 | +* SQL validation and parameter checking; |
| 138 | +* Colorized debug output (if enabled); |
| 139 | +* Safe error catching and optional `connection-error` event emission. |
| 140 | + |
| 141 | +--- |
| 142 | + |
| 143 | +### 🐘 `initPostgre(config: Pg.PoolConfig): Promise<void>` |
| 144 | + |
| 145 | +Initializes a PostgreSQL client using the provided configuration. |
| 146 | + |
| 147 | +* Automatically connects the pool. |
| 148 | +* Sets the SQL engine to `"postgre"`. |
| 149 | + |
| 150 | +--- |
| 151 | + |
| 152 | +### 🔄 `setPostgre(db: Pg.Pool): void` |
| 153 | + |
| 154 | +Sets PostgreSQL-specific methods (`all`, `get`, `run`) using a PostgreSQL Pool. |
| 155 | + |
| 156 | +* Includes debug output; |
| 157 | +* Connection error handling via event emitter; |
| 158 | +* Uses `pg`'s async `query()` method for operations. |
| 159 | + |
| 160 | +--- |
| 161 | + |
| 162 | +## 🔧 SQL Operation Overrides |
| 163 | + |
| 164 | +All three methods below are overridden dynamically depending on the SQL engine used: |
| 165 | + |
| 166 | +### 📚 `all(query: string, params?: any[], debugName?: string): Promise<Object[] | null>` |
| 167 | + |
| 168 | +Executes a query expected to return multiple rows. |
| 169 | + |
| 170 | +--- |
| 171 | + |
| 172 | +### 🧍 `get(query: string, params?: any[], debugName?: string): Promise<Object | null>` |
| 173 | + |
| 174 | +Executes a query expected to return a single row. |
| 175 | + |
| 176 | +--- |
| 177 | + |
| 178 | +### ✍️ `run(query: string, params: any[], debugName?: string): Promise<Object | null>` |
| 179 | + |
| 180 | +Executes a query that modifies data (e.g., `INSERT`, `UPDATE`, `DELETE`). |
| 181 | + |
| 182 | +--- |
| 183 | + |
| 184 | +## 🧹 Cleanup |
| 185 | + |
| 186 | +### ❌ `destroy(): Promise<void>` |
| 187 | + |
| 188 | +Gracefully shuts down the current instance: |
| 189 | + |
| 190 | +* Removes all event listeners; |
| 191 | +* Properly closes the DB connection depending on the SQL engine (`sqlite3` or `postgre`); |
| 192 | +* Errors on disconnection are caught and logged. |
0 commit comments