@@ -3,6 +3,7 @@ import { EventEmitter } from 'events';
33import { isJsonObject } from 'tiny-essentials' ;
44
55import { pg , sqlite3 } from './Modules.mjs' ;
6+ import PuddySqlEngine from './SqlEngine.mjs' ;
67import PuddySqlQuery from './TinySqlQuery.mjs' ;
78
89/** @typedef {import('pg').Pool } PgPool */
@@ -12,11 +13,12 @@ import PuddySqlQuery from './TinySqlQuery.mjs';
1213 * PuddySql is a wrapper for basic SQL operations on a local storage abstraction.
1314 * It supports inserting, updating, deleting, querying and joining JSON-based structured data.
1415 */
15- class PuddySqlInstance {
16- /** @typedef {import('./TinySqlQuery.mjs').TableSettings } TableSettings */
16+ class PuddySqlInstance extends PuddySqlEngine {
17+ constructor ( ) {
18+ super ( ) ;
19+ }
1720
18- /** @type {string } */
19- #sqlEngine = '' ;
21+ /** @typedef {import('./TinySqlQuery.mjs').TableSettings } TableSettings */
2022
2123 // @ts -ignore
2224 #db;
@@ -333,47 +335,6 @@ class PuddySqlInstance {
333335 return this . #events. rawListeners ( eventName ) ;
334336 }
335337
336- /**
337- * Checks if the given error message indicates a connection error based on the SQL engine in use.
338- *
339- * This method evaluates if the provided error message contains any known connection error codes
340- * for the current SQL engine (PostgreSQL or SQLite3).
341- *
342- * @param {Error } err - The error message to check.
343- * @returns {boolean } True if the error message matches any known connection error codes; otherwise, false.
344- */
345- isConnectionError ( err ) {
346- if ( typeof err !== 'object' || err === null || Array . isArray ( err ) )
347- throw new Error ( 'err must be a plain object' ) ;
348- const sqlEngine = this . getSqlEngine ( ) ;
349- if ( typeof sqlEngine === 'string' ) {
350- // PostgreSQL
351- if ( sqlEngine === 'postgre' ) {
352- const codes = [
353- 'ECONNREFUSED' ,
354- 'ENOTFOUND' ,
355- 'EHOSTUNREACH' ,
356- 'ETIMEDOUT' ,
357- 'EPIPE' ,
358- '28P01' ,
359- '3D000' ,
360- '08006' ,
361- '08001' ,
362- '08004' ,
363- '53300' ,
364- '57P01' ,
365- ] ;
366- // @ts -ignore
367- for ( const code of codes ) if ( err . code === code ) return true ;
368- }
369-
370- // Sqlite3
371- if ( sqlEngine === 'sqlite3' && typeof err . message === 'string' )
372- return err . message . includes ( 'SQLITE_CANTOPEN' ) ;
373- }
374- return false ;
375- }
376-
377338 /**
378339 * Formats a debug message string with colored ANSI tags for the console.
379340 * Useful for consistent and readable debug logging with identifiers.
@@ -569,20 +530,6 @@ class PuddySqlInstance {
569530 }
570531 }
571532
572- /**
573- * Returns the current SQL engine identifier used by this instance.
574- *
575- * Possible return values:
576- * - `'sqlite3'` if using SQLite3
577- * - `'postgre'` if using PostgreSQL
578- * - `null` if no engine has been initialized yet
579- *
580- * @returns {string } The name of the current SQL engine or `null` if none is set.
581- */
582- getSqlEngine ( ) {
583- return this . #sqlEngine;
584- }
585-
586533 /**
587534 * Initializes an SQLite3 >= 3.35.0 database connection and sets up the SQL engine for this instance.
588535 *
@@ -595,7 +542,7 @@ class PuddySqlInstance {
595542 * @throws {Error } If a SQL engine has already been initialized for this instance.
596543 */
597544 async initSqlite3 ( filePath = ':memory:' ) {
598- if ( ! this . #sqlEngine ) {
545+ if ( this . isSqlEngineEmpty ( ) ) {
599546 /** @type {SqliteDb } */
600547 this . #db = await open ( {
601548 filename : filePath ,
@@ -625,8 +572,8 @@ class PuddySqlInstance {
625572 setSqlite3 ( db ) {
626573 if ( ! ( db instanceof sqlite3 . Database ) )
627574 throw new Error ( 'Invalid type for db. Expected a Sqlite3.' ) ;
628- if ( ! this . #sqlEngine ) {
629- this . #sqlEngine = 'sqlite3' ;
575+ if ( this . isSqlEngineEmpty ( ) ) {
576+ this . setSqlEngine ( 'sqlite3' ) ;
630577
631578 /**
632579 * Checks if the given error is a connection error.
@@ -763,7 +710,7 @@ class PuddySqlInstance {
763710 * @throws {Error } If a SQL engine is already initialized for this instance.
764711 */
765712 async initPostgre ( config ) {
766- if ( ! this . #sqlEngine ) {
713+ if ( this . isSqlEngineEmpty ( ) ) {
767714 /** @type {PgPool } */
768715 this . #db = new pg . Pool ( config ) ;
769716
@@ -791,8 +738,8 @@ class PuddySqlInstance {
791738 */
792739 setPostgre ( db ) {
793740 if ( ! ( db instanceof pg . Pool ) ) throw new Error ( 'Invalid type for db. Expected a PostgreSQL.' ) ;
794- if ( ! this . #sqlEngine ) {
795- this . #sqlEngine = 'postgre' ;
741+ if ( ! this . getSqlEngine ( ) ) {
742+ this . setSqlEngine ( 'postgre' ) ;
796743
797744 /**
798745 * Checks if the given error is a connection error.
@@ -920,45 +867,6 @@ class PuddySqlInstance {
920867 }
921868 }
922869
923- /**
924- * Executes a query to get all rows from a database table.
925- * @function
926- * @async
927- * @param {string } query - The SQL query to execute.
928- * @param {any[*] } [params] - The parameters to bind to the query.
929- * @param {string } [debugName] - Optional label or context name for the debug log.
930- * @returns {Promise<any[*]> } A promise that resolves to an array of rows.
931- * @throws {Error } Throws an error if the query fails.
932- */
933- // @ts -ignore
934- all = ( query , params , debugName = '' ) => new Promise ( ( resolve ) => resolve ( null ) ) ;
935-
936- /**
937- * Executes a query to get a single row from a database table.
938- * @function
939- * @async
940- * @param {string } query - The SQL query to execute.
941- * @param {any[*] } [params] - The parameters to bind to the query.
942- * @param {string } [debugName] - Optional label or context name for the debug log.
943- * @returns {Promise<Record<any, any>|null> } A promise that resolves to a single row object.
944- * @throws {Error } Throws an error if the query fails.
945- */
946- // @ts -ignore
947- get = ( query , params , debugName = '' ) => new Promise ( ( resolve ) => resolve ( null ) ) ;
948-
949- /**
950- * Executes an SQL statement to modify the database (e.g., INSERT, UPDATE).
951- * @function
952- * @async
953- * @param {string } query - The SQL query to execute.
954- * @param {any[*] } params - The parameters to bind to the query.
955- * @param {string } [debugName] - Optional label or context name for the debug log.
956- * @returns {Promise<Record<any, any>|null> } A promise that resolves to the result of the query execution.
957- * @throws {Error } Throws an error if the query fails.
958- */
959- // @ts -ignore
960- run = ( query , params , debugName = '' ) => new Promise ( ( resolve ) => resolve ( null ) ) ;
961-
962870 /**
963871 * Gracefully destroys the current instance by:
964872 * - Removing all internal and system event listeners;
@@ -974,10 +882,8 @@ class PuddySqlInstance {
974882 this . #sysEvents. removeAllListeners ( ) ;
975883
976884 const sqlEngine = this . getSqlEngine ( ) ;
977- if ( typeof sqlEngine === 'string' ) {
978- if ( sqlEngine === 'postgre' ) await this . #db. end ( ) . catch ( console . error ) ;
979- if ( sqlEngine === 'sqlite3' ) await this . #db. close ( ) . catch ( console . error ) ;
980- }
885+ if ( sqlEngine === 'postgre' ) await this . #db. end ( ) . catch ( console . error ) ;
886+ if ( sqlEngine === 'sqlite3' ) await this . #db. close ( ) . catch ( console . error ) ;
981887 }
982888}
983889
0 commit comments