Skip to content

Commit 9d8ea42

Browse files
Tiny fix in PuddySqlQuery.
1 parent b763715 commit 9d8ea42

File tree

3 files changed

+22
-23
lines changed

3 files changed

+22
-23
lines changed

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"pg": "^8.14.1",
2222
"sqlite": "^5.1.1",
2323
"sqlite3": "^5.1.7",
24-
"tiny-essentials": "^1.12.1"
24+
"tiny-essentials": "^1.12.2"
2525
},
2626
"keywords": [],
2727
"scripts": {

src/PuddySqlQuery.mjs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,9 +1038,7 @@ class PuddySqlQuery {
10381038
const selectValue =
10391039
typeof settings.select !== 'undefined'
10401040
? this.selectGenerator(settings.select)
1041-
: typeof this.#settings.select === 'string'
1042-
? this.#settings.select
1043-
: '*';
1041+
: this.#settings?.select || '*';
10441042

10451043
/** @type {Settings} */
10461044
const newSettings = {
@@ -1108,15 +1106,16 @@ class PuddySqlQuery {
11081106
async has(id, subId) {
11091107
if (typeof id !== 'string' && typeof id !== 'number')
11101108
throw new Error(`Expected 'id' to be string or number, got ${typeof id}`);
1111-
if (typeof subId !== 'string' && typeof subId !== 'number')
1109+
if (typeof subId !== 'undefined' && typeof subId !== 'string' && typeof subId !== 'number')
11121110
throw new Error(`Expected 'subId' to be string or number, got ${typeof subId}`);
11131111
if (!this.#settings?.name || !this.#settings?.id)
11141112
throw new Error('Invalid table settings: name and id must be defined.');
11151113

11161114
const db = this.getDb();
1117-
const useSub = this.#settings.subId && subId ? true : false;
1115+
const useSub = this.#settings.subId && (typeof subId === 'string' || typeof subId === 'number') ? true : false;
11181116
const params = [id];
11191117
const query = `SELECT COUNT(*) FROM ${this.#settings.name} WHERE ${this.#settings.id} = $1${useSub ? ` AND ${this.#settings.subId} = $2` : ''} LIMIT 1`;
1118+
// @ts-ignore
11201119
if (useSub) params.push(subId);
11211120

11221121
const result = await db.get(query, params, 'has');
@@ -1215,14 +1214,12 @@ class PuddySqlQuery {
12151214

12161215
const setClause = columns.map((col, index) => `${col} = $${index + 1}`).join(', ');
12171216

1218-
const useSub =
1219-
typeof this.#settings.subId === 'string' &&
1220-
typeof valueObj[this.#settings.subId] !== 'undefined';
1217+
const useSub = this.#settings.subId && typeof valueObj[this.#settings.subId] !== 'undefined';
12211218
const query = `UPDATE ${this.#settings.name} SET ${setClause} WHERE ${this.#settings.id} = $${columns.length + 1}${useSub ? ` AND ${this.#settings.subId} = $${columns.length + 2}` : ''}`;
12221219

12231220
const params = [...values, id];
1224-
if (typeof this.#settings.subId === 'string' && useSub)
1225-
params.push(valueObj[this.#settings.subId]);
1221+
// @ts-ignore
1222+
if (useSub) params.push(valueObj[this.#settings.subId]);
12261223

12271224
const result = await db.run(query, params, 'update');
12281225
return this.getResultCount(result);
@@ -1325,14 +1322,15 @@ class PuddySqlQuery {
13251322
async get(id, subId) {
13261323
if (typeof id !== 'string' && typeof id !== 'number')
13271324
throw new Error(`Expected 'id' to be string or number, got ${typeof id}`);
1328-
if (typeof subId !== 'string' && typeof subId !== 'number')
1325+
if (typeof subId !== 'undefined' && typeof subId !== 'string' && typeof subId !== 'number')
13291326
throw new Error(`Expected 'subId' to be string or number, got ${typeof subId}`);
13301327

13311328
const db = this.getDb();
1332-
const useSub = this.#settings.subId && subId ? true : false;
1329+
const useSub = this.#settings.subId && (typeof subId === 'string' || typeof subId === 'number') ? true : false;
13331330
const params = [id];
13341331
const query = `SELECT ${this.#settings.select} FROM ${this.#settings.name} t
13351332
${this.insertJoin()} WHERE t.${this.#settings.id} = $1${useSub ? ` AND t.${this.#settings.subId} = $2` : ''}`;
1333+
// @ts-ignore
13361334
if (useSub) params.push(subId);
13371335
const result = this.resultChecker(await db.get(query, params, 'get'));
13381336
if (!result) return null;
@@ -1372,13 +1370,14 @@ class PuddySqlQuery {
13721370
async delete(id, subId) {
13731371
if (typeof id !== 'string' && typeof id !== 'number')
13741372
throw new Error(`Expected 'id' to be string or number, got ${typeof id}`);
1375-
if (typeof subId !== 'string' && typeof subId !== 'number')
1373+
if (typeof subId !== 'undefined' && typeof subId !== 'string' && typeof subId !== 'number')
13761374
throw new Error(`Expected 'subId' to be string or number, got ${typeof subId}`);
13771375

13781376
const db = this.getDb();
1379-
const useSub = this.#settings.subId && subId ? true : false;
1377+
const useSub = this.#settings.subId && (typeof subId === 'string' || typeof subId === 'number') ? true : false;
13801378
const query = `DELETE FROM ${this.#settings.name} WHERE ${this.#settings.id} = $1${useSub ? ` AND ${this.#settings.subId} = $2` : ''}`;
13811379
const params = [id];
1380+
// @ts-ignore
13821381
if (useSub) params.push(subId);
13831382

13841383
const result = await db.run(query, params, 'delete');
@@ -1749,7 +1748,7 @@ class PuddySqlQuery {
17491748
const order = searchData.order || this.#settings.order;
17501749
const joinConfig = searchData.join || null;
17511750

1752-
if (!criteria || !isJsonObject(criteria)) return null;
1751+
if (!isJsonObject(criteria)) return null;
17531752
if (typeof perPage !== 'number' || perPage < 1) throw new Error('Invalid perPage value');
17541753

17551754
/** @type {Pcache} */
@@ -1766,7 +1765,7 @@ class PuddySqlQuery {
17661765
tagCriteria.forEach((group, i) => {
17671766
const column = typeof group.column === 'string' ? group.column : 'tags';
17681767
const tag = this.getTagEditor(column);
1769-
if (!tag) return;
1768+
if (!(tag instanceof PuddySqlTags)) return;
17701769

17711770
const clause = tag.parseWhere(group, pCache);
17721771
if (!clause) return;
@@ -1914,7 +1913,7 @@ class PuddySqlQuery {
19141913
tagCriteria.forEach((group, i) => {
19151914
const column = typeof group.column === 'string' ? group.column : 'tags'; // default name if not set
19161915
const tag = this.getTagEditor(column);
1917-
if (!tag) return;
1916+
if (!(tag instanceof PuddySqlTags)) return;
19181917

19191918
const clause = tag.parseWhere(group, pCache);
19201919
if (!clause) return;
@@ -1926,7 +1925,7 @@ class PuddySqlQuery {
19261925
} else if (isJsonObject(tagCriteria)) {
19271926
const column = typeof tagCriteria.column === 'string' ? tagCriteria.column : 'tags';
19281927
const tag = this.getTagEditor(column);
1929-
if (tag) {
1928+
if (tag instanceof PuddySqlTags) {
19301929
const clause = tag.parseWhere(tagCriteria, pCache);
19311930
if (clause) whereParts.push(clause);
19321931
}

0 commit comments

Comments
 (0)