Skip to content

Commit 805e304

Browse files
getCondition, parseColumn, and getTagEditor updated.
1 parent 29d8827 commit 805e304

File tree

2 files changed

+75
-29
lines changed

2 files changed

+75
-29
lines changed

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Remove original repository data
22
src
3+
old
34
test
45
.babelrc
56
.prettierignore

src/PuddySqlQuery.mjs

Lines changed: 74 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,25 @@ import PuddySqlTags from './PuddySqlTags.mjs';
124124
* @property {string|null} join - SQL JOIN clause to apply (e.g., `"LEFT JOIN profiles ON users.id = profiles.user_id"`).
125125
*/
126126

127+
/**
128+
* A function that takes a WhereConditions object and returns a modified WhereConditions object.
129+
* Typically used to append or transform SQL WHERE clauses.
130+
*
131+
* @typedef {(conditions: WhereConditions) => WhereConditions} WhereConditionsFunc
132+
*/
133+
134+
/**
135+
* A map of condition identifiers to their associated transformation functions.
136+
* Each key represents a named SQL condition function.
137+
*
138+
* @typedef {Record<string, WhereConditionsFunc>} SqlConditions
139+
*/
140+
127141
/**
128142
* TinySQLQuery is a queries operating system developed to operate in a specific table.
129143
*/
130144
class PuddySqlQuery {
131-
/** @type {Record<string, function(WhereConditions) : WhereConditions>} */
145+
/** @type {SqlConditions} */
132146
#conditions = {};
133147

134148
/** @type {Record<string, function(string) : string>} */
@@ -241,27 +255,35 @@ class PuddySqlQuery {
241255
}
242256

243257
/**
244-
* Returns the condition key if it exists in the internal condition map.
245-
*
246-
* This method is useful for checking the existence of a condition without exposing its implementation.
258+
* Checks whether a specific SQL condition function is registered.
247259
*
248260
* @param {string} key - The condition identifier to look up.
249-
* @returns {string|null} The key if it exists, otherwise null.
261+
* @returns {boolean} - Returns true if the condition exists, otherwise false.
250262
*/
251-
getCondition(key) {
252-
if (typeof key !== 'string' || !this.#conditions[key]) return null;
253-
return key;
263+
hasCondition(key) {
264+
if (!this.#conditions[key]) return false;
265+
return true;
254266
}
255267

256268
/**
257-
* Retrieves a list of all registered condition keys.
269+
* Retrieves a registered SQL condition function by its identifier.
258270
*
259-
* This method returns only the list of identifiers without exposing the associated condition logic.
271+
* @param {string} key - The condition identifier to retrieve.
272+
* @returns {WhereConditionsFunc} - The associated condition function.
273+
* @throws {Error} If the condition does not exist.
274+
*/
275+
getCondition(key) {
276+
if (!this.hasCondition(key)) throw new Error('Condition not found: ' + key);
277+
return this.#conditions[key];
278+
}
279+
280+
/**
281+
* Returns a shallow copy of all registered SQL condition functions.
260282
*
261-
* @returns {string[]} Array of all registered condition keys.
283+
* @returns {SqlConditions} - An object containing all condition functions mapped by key.
262284
*/
263285
getConditions() {
264-
return Object.keys(this.#conditions);
286+
return { ...this.#conditions };
265287
}
266288

267289
/**
@@ -278,7 +300,7 @@ class PuddySqlQuery {
278300
* This method does not allow overwriting an existing key in either condition or value handlers.
279301
*
280302
* @param {string} key - Unique identifier for the new condition type.
281-
* @param {string|WhereConditions|function(WhereConditions):WhereConditions} conditionHandler - Defines the logic or operator of the condition.
303+
* @param {string|WhereConditions|WhereConditionsFunc} conditionHandler - Defines the logic or operator of the condition.
282304
* @param {(function(string): string)|null} [valueHandler=null] - Optional custom function for value transformation (e.g., for SOUNDEX).
283305
*
284306
* @throws {Error} If the key is not a non-empty string.
@@ -605,14 +627,6 @@ class PuddySqlQuery {
605627
* @returns {string} - A valid SQL expression for SELECT clause.
606628
*/
607629
parseColumn(column, alias) {
608-
// If column is a valid expression (e.g., COUNT(*), MAX(id)), return it as is
609-
if (/^[A-Za-z0-9_\*().,]+$/.test(column)) {
610-
if (alias) {
611-
return `${column} AS ${alias}`;
612-
}
613-
return column;
614-
}
615-
616630
// If column contains an alias
617631
if (alias) {
618632
return `${column} AS ${alias}`;
@@ -800,15 +814,37 @@ class PuddySqlQuery {
800814
}
801815

802816
/**
803-
* Returns the PuddySqlTags instance associated with the given column name,
804-
* if it was defined as a "TAGS" column during table creation.
817+
* Checks whether a column is associated with a tag editor.
818+
* Tag editors are used for managing tag-based columns in SQL.
819+
*
820+
* @param {string} name - The column name to check.
821+
* @returns {boolean} - Returns true if the column has an associated tag editor.
822+
*/
823+
hasTagEditor(name) {
824+
if (this.#tagColumns[name]) return true;
825+
return false;
826+
}
827+
828+
/**
829+
* Retrieves the PuddySqlTags instance associated with a specific column.
830+
* Used when the column was defined as a "TAGS" column in the SQL table definition.
805831
*
806832
* @param {string} name - The column name to retrieve the tag editor for.
807-
* @returns {PuddySqlTags|null} - The PuddySqlTags instance if it exists, otherwise null.
833+
* @returns {PuddySqlTags} - The tag editor instance.
834+
* @throws {Error} If the column is not associated with a tag editor.
808835
*/
809836
getTagEditor(name) {
810-
if (this.#tagColumns[name]) return this.#tagColumns[name];
811-
return null;
837+
if (!this.hasTagEditor(name)) throw new Error('Tag editor not found for column: ' + name);
838+
return this.#tagColumns[name];
839+
}
840+
841+
/**
842+
* Returns a shallow copy of all column-to-tag-editor mappings.
843+
*
844+
* @returns {Record<string, PuddySqlTags>} - All tag editor instances mapped by column name.
845+
*/
846+
getTagEditors() {
847+
return { ...this.#tagColumns };
812848
}
813849

814850
/**
@@ -1112,7 +1148,10 @@ class PuddySqlQuery {
11121148
throw new Error('Invalid table settings: name and id must be defined.');
11131149

11141150
const db = this.getDb();
1115-
const useSub = this.#settings.subId && (typeof subId === 'string' || typeof subId === 'number') ? true : false;
1151+
const useSub =
1152+
this.#settings.subId && (typeof subId === 'string' || typeof subId === 'number')
1153+
? true
1154+
: false;
11161155
const params = [id];
11171156
const query = `SELECT COUNT(*) FROM ${this.#settings.name} WHERE ${this.#settings.id} = $1${useSub ? ` AND ${this.#settings.subId} = $2` : ''} LIMIT 1`;
11181157
// @ts-ignore
@@ -1326,7 +1365,10 @@ class PuddySqlQuery {
13261365
throw new Error(`Expected 'subId' to be string or number, got ${typeof subId}`);
13271366

13281367
const db = this.getDb();
1329-
const useSub = this.#settings.subId && (typeof subId === 'string' || typeof subId === 'number') ? true : false;
1368+
const useSub =
1369+
this.#settings.subId && (typeof subId === 'string' || typeof subId === 'number')
1370+
? true
1371+
: false;
13301372
const params = [id];
13311373
const query = `SELECT ${this.#settings.select} FROM ${this.#settings.name} t
13321374
${this.insertJoin()} WHERE t.${this.#settings.id} = $1${useSub ? ` AND t.${this.#settings.subId} = $2` : ''}`;
@@ -1374,7 +1416,10 @@ class PuddySqlQuery {
13741416
throw new Error(`Expected 'subId' to be string or number, got ${typeof subId}`);
13751417

13761418
const db = this.getDb();
1377-
const useSub = this.#settings.subId && (typeof subId === 'string' || typeof subId === 'number') ? true : false;
1419+
const useSub =
1420+
this.#settings.subId && (typeof subId === 'string' || typeof subId === 'number')
1421+
? true
1422+
: false;
13781423
const query = `DELETE FROM ${this.#settings.name} WHERE ${this.#settings.id} = $1${useSub ? ` AND ${this.#settings.subId} = $2` : ''}`;
13791424
const params = [id];
13801425
// @ts-ignore

0 commit comments

Comments
 (0)