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
Markdown
# ๐ `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.