Skip to content

Commit cb0bec0

Browse files
isFlatTags added.
1 parent cd217e1 commit cb0bec0

File tree

2 files changed

+40
-25
lines changed

2 files changed

+40
-25
lines changed

src/PuddySqlQuery.mjs

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,7 +1301,7 @@ class PuddySqlQuery {
13011301
for (const item in result) {
13021302
const column = this.#table?.[item];
13031303
if (!column || result[item] == null) continue;
1304-
const type = column.type || '';
1304+
const type = column.type ?? '';
13051305
const raw = result[item];
13061306
if (typeof this.#jsonEscapeAlias[type] === 'function')
13071307
result[item] = this.#jsonEscapeAlias[type](raw);
@@ -1466,7 +1466,7 @@ class PuddySqlQuery {
14661466
*/
14671467
escapeValuesFix(v, name) {
14681468
const column = this.#table?.[name];
1469-
const type = column.type || '';
1469+
const type = column.type ?? '';
14701470
const func = this.#jsonEscapeFix[type];
14711471
if (typeof func !== 'function') return v;
14721472
else return func(v);
@@ -1978,7 +1978,7 @@ class PuddySqlQuery {
19781978
*/
19791979
insertJoin() {
19801980
return typeof this.#settings.join === 'string'
1981-
? `LEFT JOIN ${this.#settings.join} j ON ${this.#settings.joinCompare || ''}`
1981+
? `LEFT JOIN ${this.#settings.join} j ON ${this.#settings.joinCompare ?? ''}`
19821982
: '';
19831983
}
19841984

@@ -2106,6 +2106,7 @@ class PuddySqlQuery {
21062106
* @param {QueryGroup} [searchData.q={}] - Nested criteria object.
21072107
* @param {TagCriteria[]|TagCriteria|null} [searchData.tagCriteria] - One or multiple tag criteria groups.
21082108
* @param {string[]} [searchData.tagCriteriaOps] - Optional logical operators between tag groups (e.g., ['AND', 'OR']).
2109+
* @param {boolean} [searchData.isFlatTags=false] - Use the parseWhereFlat mode to tags.
21092110
* @param {number} [searchData.perPage] - Number of items per page.
21102111
* @param {SelectQuery} [searchData.select='*'] - Which columns to select. Set to null to skip item data.
21112112
* @param {string} [searchData.order] - SQL ORDER BY clause. Defaults to configured order.
@@ -2116,16 +2117,17 @@ class PuddySqlQuery {
21162117
findQuery(searchData = {}) {
21172118
// --- Validate searchData types ---
21182119
if (!isJsonObject(searchData)) throw new TypeError(`'searchData' must be a object`);
2119-
const criteria = searchData.q || {};
2120-
const tagCriteria = searchData.tagCriteria || null;
2120+
const criteria = searchData.q ?? {};
2121+
const tagCriteria = searchData.tagCriteria ?? null;
2122+
const isFlatTags = searchData.isFlatTags ?? false;
21212123
const tagCriteriaOps = Array.isArray(searchData.tagCriteriaOps)
21222124
? searchData.tagCriteriaOps
21232125
: [];
21242126

21252127
const selectValue = searchData.select ?? '*';
2126-
const perPage = searchData.perPage || null;
2127-
const order = searchData.order || this.#settings.order;
2128-
const joinConfig = searchData.join || null;
2128+
const perPage = searchData.perPage ?? null;
2129+
const order = searchData.order ?? this.#settings.order;
2130+
const joinConfig = searchData.join ?? null;
21292131

21302132
if (!isJsonObject(criteria))
21312133
throw new TypeError(`'searchData.q' must be a plain object or nested QueryGroup`);
@@ -2170,7 +2172,9 @@ class PuddySqlQuery {
21702172
throw new TypeError(`'group.column' must be a string if defined`);
21712173

21722174
const tag = this.getTagEditor(group.column);
2173-
const clause = tag.parseWhere(group, pCache);
2175+
const clause = !isFlatTags
2176+
? tag.parseWhere(group, pCache)
2177+
: tag.parseWhereFlat(group, pCache);
21742178
if (!clause) return;
21752179

21762180
const op = i > 0 ? tagCriteriaOps[i - 1] || 'AND' : null;
@@ -2182,7 +2186,9 @@ class PuddySqlQuery {
21822186
throw new TypeError(`'tagCriteria.column' must be a string if defined`);
21832187

21842188
const tag = this.getTagEditor(tagCriteria.column);
2185-
const clause = tag.parseWhere(tagCriteria, pCache);
2189+
const clause = !isFlatTags
2190+
? tag.parseWhere(tagCriteria, pCache)
2191+
: tag.parseWhereFlat(tagCriteria, pCache);
21862192
if (clause) whereParts.push(clause);
21872193
}
21882194

@@ -2269,6 +2275,7 @@ class PuddySqlQuery {
22692275
* @param {string[]} [searchData.tagsOpsQ] - Optional logical operators between tag groups (e.g., ['AND', 'OR']).
22702276
* @param {SelectQuery} [searchData.select='*'] - Defines which columns or expressions should be selected in the query.
22712277
* @param {number|null} [searchData.perPage=null] - Number of results per page. If set, pagination is applied.
2278+
* @param {boolean} [searchData.isFlatTags=false] - Use the parseWhereFlat mode to tags.
22722279
* @param {number} [searchData.page=1] - Page number to retrieve when `perPage` is used.
22732280
* @param {string} [searchData.order] - Custom `ORDER BY` clause (e.g. `'created_at DESC'`).
22742281
* @param {string|JoinObj|JoinObj[]} [searchData.join] - A string for single join or array of objects for multiple joins.
@@ -2313,15 +2320,16 @@ class PuddySqlQuery {
23132320
*/
23142321
searchQuery(searchData = {}) {
23152322
if (!isJsonObject(searchData)) throw new TypeError(`'searchData' must be a object`);
2316-
const order = searchData.order || this.#settings.order;
2317-
const join = searchData.join || this.#settings.join;
2318-
const limit = searchData.limit || null;
2319-
const selectValue = searchData.select || '*';
2320-
const perPage = searchData.perPage || null;
2321-
const page = searchData.page || 1;
2322-
2323-
const criteria = searchData.q || {};
2324-
const tagCriteria = searchData.tagsQ || {};
2323+
const order = searchData.order ?? this.#settings.order;
2324+
const join = searchData.join ?? this.#settings.join;
2325+
const limit = searchData.limit ?? null;
2326+
const selectValue = searchData.select ?? '*';
2327+
const perPage = searchData.perPage ?? null;
2328+
const page = searchData.page ?? 1;
2329+
2330+
const criteria = searchData.q ?? {};
2331+
const tagCriteria = searchData.tagsQ ?? {};
2332+
const isFlatTags = searchData.isFlatTags ?? false;
23252333
const tagCriteriaOps = searchData.tagsOpsQ;
23262334

23272335
// --- Validate searchData types ---
@@ -2383,7 +2391,9 @@ class PuddySqlQuery {
23832391
throw new TypeError(`Each item in 'tagsQ' must be a valid object`);
23842392

23852393
const tag = this.getTagEditor(group.column);
2386-
const clause = tag.parseWhere(group, pCache);
2394+
const clause = !isFlatTags
2395+
? tag.parseWhere(group, pCache)
2396+
: tag.parseWhereFlat(group, pCache);
23872397
if (!clause) return;
23882398

23892399
const op = i > 0 ? operators[i - 1] || 'AND' : null;
@@ -2392,7 +2402,9 @@ class PuddySqlQuery {
23922402
});
23932403
} else if (isJsonObject(tagCriteria) && typeof tagCriteria.column === 'string') {
23942404
const tag = this.getTagEditor(tagCriteria.column);
2395-
const clause = tag.parseWhere(tagCriteria, pCache);
2405+
const clause = !isFlatTags
2406+
? tag.parseWhere(tagCriteria, pCache)
2407+
: tag.parseWhereFlat(tagCriteria, pCache);
23962408
if (clause) whereParts.push(clause);
23972409
}
23982410

test/index.mjs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,10 +257,13 @@ const db = new PuddySql.Instance();
257257
);
258258

259259
const tagManager = tagTable.getTagEditor('tags');
260-
tagManager.addSpecialQuery({ title: 'rating', parser: (value) => {
261-
console.log(`\n🔖 \x1b[34mRating Tag Detected: ${value}\x1b[0m\n`);
262-
return value;
263-
}});
260+
tagManager.addSpecialQuery({
261+
title: 'rating',
262+
parser: (value) => {
263+
console.log(`\n🔖 \x1b[34mRating Tag Detected: ${value}\x1b[0m\n`);
264+
return value;
265+
},
266+
});
264267

265268
const tagsList = `(pinkie pie OR rarity) AND (applejack OR rarity) AND (farm OR boutique) AND (!party OR balloons^2) AND rating:safe AND order:random AND NOT order:random2`;
266269

0 commit comments

Comments
 (0)