av6-core 1.5.15 → 1.5.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -375,6 +375,73 @@ function smartAndMerge(a, b) {
375
375
  if (merged !== void 0) return merged;
376
376
  return andWrap(a, b);
377
377
  }
378
+ var SCALAR_OPS = /* @__PURE__ */ new Set([
379
+ "equals",
380
+ "in",
381
+ "notIn",
382
+ "lt",
383
+ "lte",
384
+ "gt",
385
+ "gte",
386
+ "contains",
387
+ "startsWith",
388
+ "endsWith",
389
+ "mode",
390
+ "search",
391
+ "not"
392
+ ]);
393
+ var RELATION_OPS = /* @__PURE__ */ new Set(["some", "every", "none", "is", "isNot"]);
394
+ function isScalarFilterObject(obj) {
395
+ if (!isPlainObject(obj)) return false;
396
+ return Object.keys(obj).some((k) => SCALAR_OPS.has(k));
397
+ }
398
+ function isRelationFilterObject(obj) {
399
+ if (!isPlainObject(obj)) return false;
400
+ return Object.keys(obj).some((k) => RELATION_OPS.has(k));
401
+ }
402
+ function normalizeWhere(where) {
403
+ if (Array.isArray(where)) return where.map(normalizeWhere);
404
+ if (!isPlainObject(where)) return where;
405
+ for (const k of Object.keys(where)) {
406
+ where[k] = normalizeWhere(where[k]);
407
+ }
408
+ if (Array.isArray(where.OR) && where.OR.length > 0) {
409
+ const orItems = where.OR;
410
+ const keysArr = orItems.map((it) => isPlainObject(it) ? Object.keys(it) : []);
411
+ const allSingleKey = keysArr.every((ks) => ks.length === 1);
412
+ if (allSingleKey) {
413
+ const commonKey = keysArr[0][0];
414
+ const allSameKey = keysArr.every((ks) => ks[0] === commonKey);
415
+ const isLogicalKey = commonKey === "AND" || commonKey === "OR" || commonKey === "NOT";
416
+ if (allSameKey && !isLogicalKey) {
417
+ const innerList = orItems.map((it) => it[commonKey]).filter((v) => isPlainObject(v));
418
+ const anyScalar = innerList.some(isScalarFilterObject);
419
+ if (!anyScalar) {
420
+ const shouldFold = innerList.some(isRelationFilterObject) || innerList.some(
421
+ (o) => Object.keys(o).some((k) => !SCALAR_OPS.has(k) && k !== "AND" && k !== "OR" && k !== "NOT")
422
+ );
423
+ if (shouldFold) {
424
+ const foldedRel = { OR: innerList };
425
+ if (isPlainObject(where[commonKey])) {
426
+ if (Array.isArray(where[commonKey].OR)) {
427
+ where[commonKey].OR = [...where[commonKey].OR, ...innerList];
428
+ } else {
429
+ where[commonKey] = { ...where[commonKey], ...foldedRel };
430
+ }
431
+ } else {
432
+ where[commonKey] = foldedRel;
433
+ }
434
+ delete where.OR;
435
+ }
436
+ }
437
+ }
438
+ }
439
+ }
440
+ if (Array.isArray(where.AND)) {
441
+ where.AND = where.AND.map(normalizeWhere);
442
+ }
443
+ return where;
444
+ }
378
445
 
379
446
  // src/repository/common.repository.ts
380
447
  var import_util2 = require("util");
@@ -702,6 +769,7 @@ var commonRepository = (serviceDeps) => {
702
769
  if (searchOR?.length) {
703
770
  whereClause = smartAndMerge(whereClause, { OR: searchOR });
704
771
  }
772
+ whereClause = normalizeWhere(whereClause);
705
773
  const orderByClause = {};
706
774
  if (sortBy && sortDir) {
707
775
  orderByClause[sortBy] = sortDir.toLowerCase();
package/dist/index.mjs CHANGED
@@ -325,6 +325,73 @@ function smartAndMerge(a, b) {
325
325
  if (merged !== void 0) return merged;
326
326
  return andWrap(a, b);
327
327
  }
328
+ var SCALAR_OPS = /* @__PURE__ */ new Set([
329
+ "equals",
330
+ "in",
331
+ "notIn",
332
+ "lt",
333
+ "lte",
334
+ "gt",
335
+ "gte",
336
+ "contains",
337
+ "startsWith",
338
+ "endsWith",
339
+ "mode",
340
+ "search",
341
+ "not"
342
+ ]);
343
+ var RELATION_OPS = /* @__PURE__ */ new Set(["some", "every", "none", "is", "isNot"]);
344
+ function isScalarFilterObject(obj) {
345
+ if (!isPlainObject(obj)) return false;
346
+ return Object.keys(obj).some((k) => SCALAR_OPS.has(k));
347
+ }
348
+ function isRelationFilterObject(obj) {
349
+ if (!isPlainObject(obj)) return false;
350
+ return Object.keys(obj).some((k) => RELATION_OPS.has(k));
351
+ }
352
+ function normalizeWhere(where) {
353
+ if (Array.isArray(where)) return where.map(normalizeWhere);
354
+ if (!isPlainObject(where)) return where;
355
+ for (const k of Object.keys(where)) {
356
+ where[k] = normalizeWhere(where[k]);
357
+ }
358
+ if (Array.isArray(where.OR) && where.OR.length > 0) {
359
+ const orItems = where.OR;
360
+ const keysArr = orItems.map((it) => isPlainObject(it) ? Object.keys(it) : []);
361
+ const allSingleKey = keysArr.every((ks) => ks.length === 1);
362
+ if (allSingleKey) {
363
+ const commonKey = keysArr[0][0];
364
+ const allSameKey = keysArr.every((ks) => ks[0] === commonKey);
365
+ const isLogicalKey = commonKey === "AND" || commonKey === "OR" || commonKey === "NOT";
366
+ if (allSameKey && !isLogicalKey) {
367
+ const innerList = orItems.map((it) => it[commonKey]).filter((v) => isPlainObject(v));
368
+ const anyScalar = innerList.some(isScalarFilterObject);
369
+ if (!anyScalar) {
370
+ const shouldFold = innerList.some(isRelationFilterObject) || innerList.some(
371
+ (o) => Object.keys(o).some((k) => !SCALAR_OPS.has(k) && k !== "AND" && k !== "OR" && k !== "NOT")
372
+ );
373
+ if (shouldFold) {
374
+ const foldedRel = { OR: innerList };
375
+ if (isPlainObject(where[commonKey])) {
376
+ if (Array.isArray(where[commonKey].OR)) {
377
+ where[commonKey].OR = [...where[commonKey].OR, ...innerList];
378
+ } else {
379
+ where[commonKey] = { ...where[commonKey], ...foldedRel };
380
+ }
381
+ } else {
382
+ where[commonKey] = foldedRel;
383
+ }
384
+ delete where.OR;
385
+ }
386
+ }
387
+ }
388
+ }
389
+ }
390
+ if (Array.isArray(where.AND)) {
391
+ where.AND = where.AND.map(normalizeWhere);
392
+ }
393
+ return where;
394
+ }
328
395
 
329
396
  // src/repository/common.repository.ts
330
397
  import { isDeepStrictEqual as isDeepStrictEqual2 } from "util";
@@ -652,6 +719,7 @@ var commonRepository = (serviceDeps) => {
652
719
  if (searchOR?.length) {
653
720
  whereClause = smartAndMerge(whereClause, { OR: searchOR });
654
721
  }
722
+ whereClause = normalizeWhere(whereClause);
655
723
  const orderByClause = {};
656
724
  if (sortBy && sortDir) {
657
725
  orderByClause[sortBy] = sortDir.toLowerCase();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "av6-core",
3
- "version": "1.5.15",
3
+ "version": "1.5.16",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.mjs",
6
6
  "types": "dist/index.d.ts",