UNPKG

puddysql

Version:

๐Ÿฎ Powerful SQL toolkit for Node.js, built with flexibility and structure in mind. Easily manage SQLite3/PostgreSQL, advanced queries, smart tag systems, and full JSON-friendly filters.

118 lines (69 loc) โ€ข 2.95 kB
# ๐Ÿ“š `PuddySqlEngine` Class Documentation A lightweight class for managing SQL engine state and basic query methods in a pluggable and validated manner. --- ## ๐Ÿ” Private Properties ### `#sqlEngine: string` Holds the SQL engine identifier used internally by this instance. It's set only once and used for engine-specific behaviors. --- ## ๐Ÿงช Methods ### ๐Ÿ”Ž `isSqlEngineEmpty(): boolean` Checks if the SQL engine is undefined, null, or an empty string. * โœ… **Returns**: `true` if not set or empty, otherwise `false`. --- ### ๐Ÿ“ฅ `setSqlEngine(engine: string): void` Sets the SQL engine to be used by this instance. Can only be set **once**, and only with a valid non-empty string. * ๐Ÿ“Œ **Parameters**: * `engine` *(string)*: Name of the SQL engine, e.g. `'sqlite3'`, `'postgre'`. * โš ๏ธ **Throws**: * If already set. * If provided value is not a valid non-empty string. --- ### ๐Ÿ“ค `getSqlEngine(): string` Returns the currently set SQL engine. * โœ… **Returns**: SQL engine name. * โš ๏ธ **Throws**: If engine is not set. --- ### โŒ `isConnectionError(err: Error): boolean` Detects if a given error matches known SQL engine-specific connection issues. * ๐Ÿ“Œ **Parameters**: * `err` *(Error)*: Error object to check. * ๐Ÿ” **Behavior**: * For **PostgreSQL**, checks for known error codes like: * `ECONNREFUSED`, `ETIMEDOUT`, `28P01`, `08006`, etc. * For **SQLite3**, checks if the error message includes `SQLITE_CANTOPEN`. * โœ… **Returns**: `true` if a known connection error is detected. --- ## โš™๏ธ Query Methods These methods are placeholders but follow the async query signature. ### ๐Ÿ“„ `all(query: string, params?: any[], debugName?: string): Promise<any[]>` Executes an SQL `SELECT` query expected to return **multiple rows**. * ๐Ÿ“Œ **Parameters**: * `query`: SQL string. * `params`: Optional array of parameters. * `debugName`: Optional debug label. * โœ… **Returns**: Array of row results. * โš ๏ธ **Throws**: On query failure. --- ### ๐Ÿ“„ `get(query: string, params?: any[], debugName?: string): Promise<object|null>` Executes an SQL `SELECT` query expected to return **a single row**. * ๐Ÿ“Œ **Parameters**: * `query`: SQL string. * `params`: Optional array of parameters. * `debugName`: Optional debug label. * โœ… **Returns**: Single result object or `null`. * โš ๏ธ **Throws**: On query failure. --- ### โœ๏ธ `run(query: string, params: any[], debugName?: string): Promise<object|null>` Executes an SQL `INSERT`, `UPDATE`, or `DELETE` statement. * ๐Ÿ“Œ **Parameters**: * `query`: SQL string. * `params`: Parameters to bind. * `debugName`: Optional debug label. * โœ… **Returns**: Result object or `null`. * โš ๏ธ **Throws**: On execution failure. --- ## ๐Ÿš€ Export ```js export default PuddySqlEngine; ``` This class is ready to be used as a foundational SQL abstraction for SQLite3, PostgreSQL, and others with minimal setup.