@@ -9,6 +9,7 @@ import PuddySqlQuery from './TinySqlQuery.mjs';
99const { Client } = pg ;
1010
1111/** @typedef {import('pg').Pool } PgPool */
12+ /** @typedef {import('sqlite').Database } SqliteDb */
1213
1314/**
1415 * PuddySql is a wrapper for basic SQL operations on a local storage abstraction.
@@ -20,7 +21,7 @@ class PuddySqlInstance {
2021 /** @type {string } */
2122 #sqlEngine = '' ;
2223
23- /** @type { Database|PgPool|undefined } */
24+ // @ts -ignore
2425 #db;
2526
2627 /** @type {Record<string, PuddySqlQuery> } */
@@ -279,10 +280,8 @@ class PuddySqlInstance {
279280 #debugConsoleText( id , debugName , status ) {
280281 if ( typeof id !== 'string' && typeof id !== 'number' )
281282 throw new Error ( 'id must be a string or number' ) ;
282- if ( debugName !== undefined && typeof debugName !== 'string' )
283- throw new Error ( 'debugName must be a string if provided' ) ;
284- if ( status !== undefined && typeof status !== 'string' )
285- throw new Error ( 'status must be a string if provided' ) ;
283+ if ( typeof debugName !== 'string' ) throw new Error ( 'debugName must be a string if provided' ) ;
284+ if ( typeof status !== 'string' ) throw new Error ( 'status must be a string if provided' ) ;
286285 const useColor = this . #consoleColors !== false ;
287286
288287 const reset = useColor ? '\x1b[0m' : '' ;
@@ -293,11 +292,8 @@ class PuddySqlInstance {
293292
294293 const tagSQL = `${ gray } [${ blue } SQL${ gray } ]${ gray } [${ blue } ${ id } ${ gray } ]` ;
295294 const tagDebug = `${ gray } [${ green } DEBUG${ gray } ]` ;
296- const tagName =
297- typeof debugName === 'string' && debugName . length > 0
298- ? ` ${ gray } [${ cyan } ${ debugName } ${ gray } ]`
299- : '' ;
300- const tagStatus = typeof status === 'string' ? ` ${ gray } [${ status } ${ gray } ]` : '' ;
295+ const tagName = debugName . length > 0 ? ` ${ gray } [${ cyan } ${ debugName } ${ gray } ]` : '' ;
296+ const tagStatus = status . length > 0 ? ` ${ gray } [${ status } ${ gray } ]` : '' ;
301297
302298 return `${ tagSQL } ${ tagDebug } ${ tagName } ${ tagStatus } ${ reset } ` ;
303299 }
@@ -438,7 +434,7 @@ class PuddySqlInstance {
438434 * This gives direct access to the internal database connection (PostgreSQL `Client` or SQLite3 `Database`),
439435 * which can be useful for advanced queries or database-specific operations not covered by this wrapper.
440436 *
441- * @returns {Database |PgPool } The internal database instance, or `null` if not initialized.
437+ * @returns {SqliteDb |PgPool } The internal database instance, or `null` if not initialized.
442438 */
443439 getDb ( ) {
444440 if ( ! this . #db) throw new Error ( 'Database instance is not initialized' ) ;
@@ -491,7 +487,7 @@ class PuddySqlInstance {
491487 */
492488 async initSqlite3 ( filePath = ':memory:' ) {
493489 if ( ! this . #sqlEngine) {
494- // Open SQLite3 database using the provided file path
490+ /** @type { SqliteDb } */
495491 this . #db = await open ( {
496492 filename : filePath ,
497493 driver : Database ,
@@ -514,7 +510,7 @@ class PuddySqlInstance {
514510 * These methods internally wrap around the provided `db` object's asynchronous methods (`all`, `get`, `run`),
515511 * returning promises that resolve with the expected results or `null` on invalid data.
516512 *
517- * @param {Database } db - A SQLite3 database wrapper that exposes `all`, `get`, and `run` methods returning Promises.
513+ * @param {SqliteDb } db - A SQLite3 database wrapper that exposes `all`, `get`, and `run` methods returning Promises.
518514 * @throws {Error } If a SQL engine has already been set for this instance.
519515 */
520516 setSqlite3 ( db ) {
@@ -534,6 +530,7 @@ class PuddySqlInstance {
534530 * Emits a 'connection-error' event if the error is related to connection issues.
535531 *
536532 * @param {any } err - The error object to check and possibly emit.
533+ * @param {function } reject
537534 * @returns {void }
538535 */
539536 const rejectConnection = ( reject , err ) => {
@@ -548,7 +545,7 @@ class PuddySqlInstance {
548545 * Sends SQL debug information to the console, including query and parameters.
549546 *
550547 * @param {number } id - The debug operation ID.
551- * @param {string|null } debugName - An optional label to identify the debug context.
548+ * @param {string } debugName - An optional label to identify the debug context.
552549 * @param {string } query - The SQL query being executed.
553550 * @param {any[] } params - The parameters passed to the query.
554551 * @returns {void }
@@ -564,8 +561,8 @@ class PuddySqlInstance {
564561 * Sends SQL result debug information to the console.
565562 *
566563 * @param {number } id - The debug operation ID.
567- * @param {string|null } debugName - An optional label to identify the debug context.
568- * @param {string|null } type - Optional descriptor of the operation type.
564+ * @param {string } debugName - An optional label to identify the debug context.
565+ * @param {string } type - Optional descriptor of the operation type.
569566 * @param {any } data - The result data to output.
570567 * @returns {void }
571568 */
@@ -588,13 +585,13 @@ class PuddySqlInstance {
588585 * @param {any[*] } [params=[]] - Optional query parameters.
589586 * @returns {Promise<Array<Object>|null> } Resolves with an array of result rows or null if invalid.
590587 */
591- this . all = async function ( query , params = [ ] , debugName = null ) {
588+ this . all = async function ( query , params = [ ] , debugName = '' ) {
592589 return new Promise ( ( resolve , reject ) => {
593590 const id = getId ( ) ;
594591 sendSqlDebug ( id , debugName , query , params ) ;
595592 db . all ( query , params )
596593 . then ( ( result ) => {
597- sendSqlDebugResult ( id , debugName , null , result ) ;
594+ sendSqlDebugResult ( id , debugName , '' , result ) ;
598595 resolve ( Array . isArray ( result ) ? result : null ) ;
599596 } )
600597 . catch ( ( err ) => rejectConnection ( reject , err ) ) ;
@@ -608,13 +605,13 @@ class PuddySqlInstance {
608605 * @param {any[*] } [params=[]] - Optional query parameters.
609606 * @returns {Promise<Record<any, any>|null> } Resolves with the result row or null if not a valid object.
610607 */
611- this . get = async function ( query , params = [ ] , debugName = null ) {
608+ this . get = async function ( query , params = [ ] , debugName = '' ) {
612609 return new Promise ( ( resolve , reject ) => {
613610 const id = getId ( ) ;
614611 sendSqlDebug ( id , debugName , query , params ) ;
615612 db . get ( query , params )
616613 . then ( ( result ) => {
617- sendSqlDebugResult ( id , debugName , null , result ) ;
614+ sendSqlDebugResult ( id , debugName , '' , result ) ;
618615 resolve ( objType ( result , 'object' ) ? result : null ) ;
619616 } )
620617 . catch ( ( err ) => rejectConnection ( reject , err ) ) ;
@@ -628,13 +625,13 @@ class PuddySqlInstance {
628625 * @param {any[*] } params - Query parameters.
629626 * @returns {Promise<Record<any, any>|null> } Resolves with the result object or null if invalid.
630627 */
631- this . run = async function ( query , params , debugName = null ) {
628+ this . run = async function ( query , params , debugName = '' ) {
632629 return new Promise ( ( resolve , reject ) => {
633630 const id = getId ( ) ;
634631 sendSqlDebug ( id , debugName , query , params ) ;
635632 db . run ( query , params )
636633 . then ( ( result ) => {
637- sendSqlDebugResult ( id , debugName , null , result ) ;
634+ sendSqlDebugResult ( id , debugName , '' , result ) ;
638635 resolve ( objType ( result , 'object' ) ? result : null ) ;
639636 } )
640637 . catch ( ( err ) => rejectConnection ( reject , err ) ) ;
@@ -657,7 +654,7 @@ class PuddySqlInstance {
657654 */
658655 async initPostgre ( config ) {
659656 if ( ! this . #sqlEngine) {
660- // Create a new pg Client instance using provided config
657+ /** @type { PgPool } */
661658 this . #db = new Client ( config ) ;
662659
663660 // Set up the SQL methods (all, get, run)
@@ -714,7 +711,7 @@ class PuddySqlInstance {
714711 * Sends SQL debug information to the console, including query and parameters.
715712 *
716713 * @param {number } id - The debug operation ID.
717- * @param {string|null } debugName - An optional label to identify the debug context.
714+ * @param {string } debugName - An optional label to identify the debug context.
718715 * @param {string } query - The SQL query being executed.
719716 * @param {any[] } params - The parameters passed to the query.
720717 * @returns {void }
@@ -730,8 +727,8 @@ class PuddySqlInstance {
730727 * Sends SQL result debug information to the console.
731728 *
732729 * @param {number } id - The debug operation ID.
733- * @param {string|null } debugName - An optional label to identify the debug context.
734- * @param {string|null } type - Optional descriptor of the operation type.
730+ * @param {string } debugName - An optional label to identify the debug context.
731+ * @param {string } type - Optional descriptor of the operation type.
735732 * @param {any } data - The result data to output.
736733 * @returns {void }
737734 */
@@ -754,12 +751,12 @@ class PuddySqlInstance {
754751 * @param {any[*] } [params=[]] - Optional query parameters.
755752 * @returns {Promise<Array<Object>|null> } Resolves with an array of result rows or null if invalid.
756753 */
757- this . all = async function ( query , params = [ ] , debugName = null ) {
754+ this . all = async function ( query , params = [ ] , debugName = '' ) {
758755 try {
759756 const id = getId ( ) ;
760757 sendSqlDebug ( id , debugName , query , params ) ;
761758 const res = await db . query ( query , params ) ;
762- sendSqlDebugResult ( id , debugName , null , res ) ;
759+ sendSqlDebugResult ( id , debugName , '' , res ) ;
763760 return objType ( res , 'object' ) && Array . isArray ( res . rows ) ? res . rows : null ;
764761 } catch ( err ) {
765762 rejectConnection ( err ) ;
@@ -774,12 +771,12 @@ class PuddySqlInstance {
774771 * @param {any[*] } [params=[]] - Optional query parameters.
775772 * @returns {Promise<Record<any, any>|null> } Resolves with the first row of the result or null if not found.
776773 */
777- this . get = async function ( query , params = [ ] , debugName = null ) {
774+ this . get = async function ( query , params = [ ] , debugName = '' ) {
778775 try {
779776 const id = getId ( ) ;
780777 sendSqlDebug ( id , debugName , query , params ) ;
781778 const res = await db . query ( query , params ) ;
782- sendSqlDebugResult ( id , debugName , null , res ) ;
779+ sendSqlDebugResult ( id , debugName , '' , res ) ;
783780 return objType ( res , 'object' ) && Array . isArray ( res . rows ) && objType ( res . rows [ 0 ] , 'object' )
784781 ? res . rows [ 0 ]
785782 : null ;
@@ -796,12 +793,12 @@ class PuddySqlInstance {
796793 * @param {any[*] } params - Query parameters.
797794 * @returns {Promise<Record<any, any>|null> } Resolves with the result object or null if invalid.
798795 */
799- this . run = async function ( query , params , debugName = null ) {
796+ this . run = async function ( query , params , debugName = '' ) {
800797 try {
801798 const id = getId ( ) ;
802799 sendSqlDebug ( id , debugName , query , params ) ;
803800 const res = await db . query ( query , params ) ;
804- sendSqlDebugResult ( id , debugName , null , res ) ;
801+ sendSqlDebugResult ( id , debugName , '' , res ) ;
805802 return objType ( res , 'object' ) ? res : null ;
806803 } catch ( err ) {
807804 rejectConnection ( err ) ;
0 commit comments