@cyberskill/shared 2.28.0 → 3.0.0

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.
Files changed (91) hide show
  1. package/dist/config/config.util.cjs +1 -1
  2. package/dist/config/config.util.js +24 -33
  3. package/dist/config/eslint/index.cjs +1 -1
  4. package/dist/config/eslint/index.d.ts +4 -0
  5. package/dist/config/eslint/index.js +4 -12
  6. package/dist/config/graphql-codegen/graphql-codegen.util.cjs +1 -1
  7. package/dist/config/graphql-codegen/graphql-codegen.util.js +28 -33
  8. package/dist/config/storybook/storybook.preview.cjs +1 -1
  9. package/dist/config/storybook/storybook.preview.js +10 -13
  10. package/dist/config/vitest/vitest.e2e.cjs +1 -1
  11. package/dist/config/vitest/vitest.e2e.js +1 -1
  12. package/dist/config/vitest/vitest.unit.cjs +1 -1
  13. package/dist/config/vitest/vitest.unit.js +19 -8
  14. package/dist/constant/common.cjs +1 -1
  15. package/dist/constant/common.js +1 -1
  16. package/dist/node/apollo-server/apollo-server.util.cjs +1 -1
  17. package/dist/node/apollo-server/apollo-server.util.js +21 -48
  18. package/dist/node/cli/index.cjs +2 -2
  19. package/dist/node/cli/index.js +109 -165
  20. package/dist/node/command/command.util.cjs +2 -2
  21. package/dist/node/command/command.util.js +135 -170
  22. package/dist/node/express/express.util.cjs +1 -1
  23. package/dist/node/express/express.util.js +39 -72
  24. package/dist/node/fs/fs.util.cjs +3 -3
  25. package/dist/node/fs/fs.util.js +45 -60
  26. package/dist/node/log/log.util.cjs +1 -1
  27. package/dist/node/log/log.util.js +29 -30
  28. package/dist/node/mongo/mongo.controller.cjs +1 -1
  29. package/dist/node/mongo/mongo.controller.js +531 -629
  30. package/dist/node/mongo/mongo.util.cjs +2 -2
  31. package/dist/node/mongo/mongo.util.js +280 -321
  32. package/dist/node/package/package.util.cjs +1 -1
  33. package/dist/node/package/package.util.js +197 -226
  34. package/dist/node/path/index.cjs +1 -1
  35. package/dist/node/path/index.js +26 -25
  36. package/dist/node/path/path.constant.cjs +1 -1
  37. package/dist/node/path/path.constant.d.ts +2 -0
  38. package/dist/node/path/path.constant.js +130 -150
  39. package/dist/node/storage/storage.util.cjs +1 -1
  40. package/dist/node/storage/storage.util.js +110 -158
  41. package/dist/node/upload/upload.util.cjs +1 -1
  42. package/dist/node/upload/upload.util.js +119 -150
  43. package/dist/node/ws/ts.util.cjs +1 -1
  44. package/dist/node/ws/ts.util.js +31 -54
  45. package/dist/react/apollo-client/apollo-client.component.cjs +1 -1
  46. package/dist/react/apollo-client/apollo-client.component.js +4 -4
  47. package/dist/react/apollo-client/apollo-client.util.cjs +1 -1
  48. package/dist/react/apollo-client/apollo-client.util.js +51 -58
  49. package/dist/react/apollo-client/links/upload.cjs +1 -1
  50. package/dist/react/apollo-client/links/upload.js +71 -74
  51. package/dist/react/apollo-client-nextjs/apollo-client-nextjs.util.cjs +1 -1
  52. package/dist/react/apollo-client-nextjs/apollo-client-nextjs.util.js +11 -18
  53. package/dist/react/apollo-error/apollo-error.component.cjs +1 -1
  54. package/dist/react/apollo-error/apollo-error.component.js +61 -23
  55. package/dist/react/apollo-error/apollo-error.module.scss.cjs +1 -1
  56. package/dist/react/apollo-error/apollo-error.module.scss.js +6 -6
  57. package/dist/react/apollo-error/apollo-error.test.unit.d.ts +1 -0
  58. package/dist/react/apollo-error/apollo-error.util.cjs +1 -1
  59. package/dist/react/apollo-error/apollo-error.util.js +6 -6
  60. package/dist/react/loading/loading.component.cjs +2 -2
  61. package/dist/react/loading/loading.component.js +30 -46
  62. package/dist/react/log/log.util.cjs +1 -1
  63. package/dist/react/log/log.util.js +4 -4
  64. package/dist/react/next-intl/next-intl.hoc.cjs +1 -1
  65. package/dist/react/next-intl/next-intl.hoc.js +13 -22
  66. package/dist/react/next-intl/next-intl.provider.cjs +1 -1
  67. package/dist/react/next-intl/next-intl.provider.js +7 -7
  68. package/dist/react/storage/storage.hook.cjs +1 -1
  69. package/dist/react/storage/storage.hook.js +43 -59
  70. package/dist/react/storage/storage.util.cjs +1 -1
  71. package/dist/react/storage/storage.util.js +28 -53
  72. package/dist/react/userback/userback.component.cjs +1 -1
  73. package/dist/react/userback/userback.component.js +18 -42
  74. package/dist/style.css +1 -1
  75. package/dist/util/common/common.util.cjs +1 -1
  76. package/dist/util/common/common.util.d.ts +2 -0
  77. package/dist/util/common/common.util.js +29 -37
  78. package/dist/util/index.cjs +1 -1
  79. package/dist/util/index.js +10 -9
  80. package/dist/util/object/object.util.cjs +1 -1
  81. package/dist/util/object/object.util.js +72 -76
  82. package/dist/util/string/index.cjs +1 -1
  83. package/dist/util/string/index.js +4 -3
  84. package/dist/util/string/string.util.cjs +1 -1
  85. package/dist/util/string/string.util.d.ts +12 -0
  86. package/dist/util/string/string.util.js +63 -44
  87. package/package.json +25 -16
  88. package/public/tsconfig.base.json +7 -3
  89. package/README.md +0 -470
  90. /package/dist/node_modules/.pnpm/{vitest@4.0.18_@types_node@25.3.0_jiti@2.6.1_jsdom@28.1.0_@noble_hashes@1.8.0__sass@1.97.3_tsx@4.21.0_yaml@2.8.2 → vitest@4.0.18_@types_node@25.3.3_jiti@2.6.1_jsdom@28.1.0_@noble_hashes@1.8.0__sass@1.97.3_tsx@4.21.0_yaml@2.8.2}/node_modules/vitest/dist/config.cjs +0 -0
  91. /package/dist/node_modules/.pnpm/{vitest@4.0.18_@types_node@25.3.0_jiti@2.6.1_jsdom@28.1.0_@noble_hashes@1.8.0__sass@1.97.3_tsx@4.21.0_yaml@2.8.2 → vitest@4.0.18_@types_node@25.3.3_jiti@2.6.1_jsdom@28.1.0_@noble_hashes@1.8.0__sass@1.97.3_tsx@4.21.0_yaml@2.8.2}/node_modules/vitest/dist/config.js +0 -0
@@ -1,24 +1,23 @@
1
- import c from "chalk";
1
+ import l from "chalk";
2
2
  import r from "consola";
3
- import { GraphQLError as u } from "graphql";
4
- import { getEnv as h } from "../../config/env/env.util.js";
5
- import { RESPONSE_STATUS as s } from "../../constant/response-status.js";
6
- const R = h();
7
- function w({
8
- message: o,
9
- status: e = s.INTERNAL_SERVER_ERROR,
10
- type: t = "graphql"
3
+ import { GraphQLError as h } from "graphql";
4
+ import { getEnv as u } from "../../config/env/env.util.js";
5
+ import { RESPONSE_STATUS as f } from "../../constant/response-status.js";
6
+ const R = u();
7
+ function S({
8
+ message: e,
9
+ status: o = f.INTERNAL_SERVER_ERROR,
10
+ type: n = "graphql"
11
11
  }) {
12
- var l;
13
- const n = (l = o != null ? o : e.MESSAGE) != null ? l : "Internal server error";
14
- throw t === "graphql" ? new u(n, {
15
- extensions: { code: e.CODE }
16
- }) : new Error(n);
12
+ const t = e ?? o.MESSAGE ?? "Internal server error";
13
+ throw n === "graphql" ? new h(t, {
14
+ extensions: { code: o.CODE }
15
+ }) : new Error(t);
17
16
  }
18
17
  R.DEBUG || (r.level = 4);
19
- function f(o) {
20
- const e = c[o];
21
- return typeof e == "function" ? e : c.green;
18
+ function c(e) {
19
+ const o = l[e];
20
+ return typeof o == "function" ? o : l.green;
22
21
  }
23
22
  const m = {
24
23
  silent: r.silent,
@@ -47,27 +46,27 @@ const m = {
47
46
  * @param issues - An array of issue entries to display.
48
47
  * @param color - The color to use for highlighting (defaults to 'red').
49
48
  */
50
- printBoxedLog(o, e, t = "red") {
51
- if (!(e != null && e.length)) {
52
- r.box(c.green(o));
49
+ printBoxedLog(e, o, n = "red") {
50
+ if (!o?.length) {
51
+ r.box(l.green(e));
53
52
  return;
54
53
  }
55
- e.forEach(({ file: l, position: n, rule: a, message: g }) => {
56
- const i = n ? `:${n}` : "", E = `${l}${i}`;
57
- r.log(`${c.gray("File:")} ${c.blue(E)}`), a && r.log(` ${f(t)("Rule:")} ${a}`), r.log(` ${f(t)("Message:")} ${g}`);
58
- }), r.box(f(t)(`${o} : ${e.length}`)), r.log(c.gray("─".repeat(40)));
54
+ o.forEach(({ file: t, position: s, rule: a, message: g }) => {
55
+ const i = s ? `:${s}` : "", E = `${t}${i}`;
56
+ r.log(`${l.gray("File:")} ${l.blue(E)}`), a && r.log(` ${c(n)("Rule:")} ${a}`), r.log(` ${c(n)("Message:")} ${g}`);
57
+ }), r.box(c(n)(`${e} : ${o.length}`)), r.log(l.gray("─".repeat(40)));
59
58
  }
60
59
  };
61
- function S(o, e) {
62
- const { shouldLog: t = !0, returnValue: l, callback: n } = e != null ? e : {}, a = o instanceof Error ? o : new Error(typeof o == "string" ? o : "Unknown error");
63
- return t && m.error(a.message), n && typeof n == "function" && n(a), l || {
60
+ function v(e, o) {
61
+ const { shouldLog: n = !0, returnValue: t, callback: s } = o ?? {}, a = e instanceof Error ? e : new Error(typeof e == "string" ? e : "Unknown error");
62
+ return n && m.error(a.message), s && typeof s == "function" && s(a), t || {
64
63
  success: !1,
65
64
  message: a.message,
66
- code: s.INTERNAL_SERVER_ERROR.CODE
65
+ code: f.INTERNAL_SERVER_ERROR.CODE
67
66
  };
68
67
  }
69
68
  export {
70
- S as catchError,
69
+ v as catchError,
71
70
  m as log,
72
- w as throwError
71
+ S as throwError
73
72
  };
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const L=require("./mongo.constant.cjs"),$=require("./mongo.util.cjs"),M=require("../../util/object/object.util.cjs"),x=require("../../util/string/string.util.cjs"),N=require("../../constant/response-status.cjs"),m=require("../log/log.util.cjs");var W=Object.defineProperty,B=Object.defineProperties,G=Object.getOwnPropertyDescriptors,U=Object.getOwnPropertySymbols,K=Object.prototype.hasOwnProperty,Q=Object.prototype.propertyIsEnumerable,w=(l,r,e)=>r in l?W(l,r,{enumerable:!0,configurable:!0,writable:!0,value:e}):l[r]=e,V=(l,r)=>{for(var e in r||(r={}))K.call(r,e)&&w(l,e,r[e]);if(U)for(var e of U(r))Q.call(r,e)&&w(l,e,r[e]);return l},A=(l,r)=>B(l,G(r)),k=(l,r,e)=>w(l,typeof r!="symbol"?r+"":r,e),f=(l,r,e)=>new Promise((t,s)=>{var n=a=>{try{o(e.next(a))}catch(u){s(u)}},c=a=>{try{o(e.throw(a))}catch(u){s(u)}},o=a=>a.done?t(a.value):Promise.resolve(a.value).then(n,c);o((e=e.apply(l,r)).next())});function z(l){return l!=null&&typeof l=="object"}function C(l,r){if(!l||!r||r.length===0)return l;const e=new Set(r.map(t=>t.name));if(Array.isArray(l)){const t=l.filter(s=>{if(typeof s=="string")return!Array.from(e).some(n=>s===n||s.startsWith(`${n}.`));if(typeof s=="object"&&s!==null){const n=s,c=n.path||n.populate||"";return!Array.from(e).some(o=>c===o||c.startsWith(`${o}.`))}return!0});return t.length>0?t:void 0}if(typeof l=="string")return Array.from(e).some(t=>l===t||l.startsWith(`${t}.`))?void 0:l;if(typeof l=="object"&&l!==null){const t=l,s=t.path||t.populate||"";return Array.from(e).some(n=>s===n||s.startsWith(`${n}.`))?void 0:l}return l}function X(l,r,e){if(!l.length||!r||!(e!=null&&e.ref))return[];const t=new Map;return l.forEach(s=>{try{const n=e.ref(s);if(n==null)return;const c=typeof n=="string"?n:String(n);if(c&&c.trim()!==""){const o=$.convertEnumToModelName(c);t.has(o)||t.set(o,[]),t.get(o).push(s)}}catch(n){m.catchError(new Error(`Dynamic ref function failed for virtual "${r}": ${n instanceof Error?n.message:String(n)}`))}}),Array.from(t.entries()).map(([s,n])=>({model:s,docs:n}))}function J(l){return l!==null&&typeof l=="object"&&"toObject"in l&&typeof l.toObject=="function"}function q(l,r,e,t,s,n){return f(this,null,function*(){if(!r.length||!e.length||!t)return r;const c=e.filter(u=>{if(Array.isArray(t))return t.length>0&&t.some(d=>{if(typeof d=="string")return d===u.name||d.startsWith(`${u.name}.`);if(d&&typeof d=="object"){const i=d,p=i.path||i.populate||"";return p===u.name||p.startsWith(`${u.name}.`)}return!1});if(typeof t=="string")return t===u.name||t.startsWith(`${u.name}.`);if(typeof t=="object"&&t!==null){const d=t,i=d.path||d.populate||"";return i===u.name||i.startsWith(`${u.name}.`)}return!1});if(c.length===0)return r;const o=M.deepClone(r.map(u=>J(u)?u.toObject():u));o.forEach(u=>{c.forEach(({name:d,options:i})=>{d in u||(u[d]=i.count?0:i.justOne?null:[])})});const a=new Map;for(const u of c){const{name:d,options:i}=u,p=X(o,d,i);for(const y of p){a.has(y.model)||a.set(y.model,{virtuals:[],localValueSets:new Map,docsByLocalValue:new Map});const E=a.get(y.model);E.virtuals.find(S=>S.name===d)||(E.virtuals.push(u),E.localValueSets.set(d,new Set));const j=E.localValueSets.get(d);y.docs.forEach(S=>{const O=S[i.localField];if(O!=null){const g=String(O);j.add(g);let h=-1;const D=S;D.id!==void 0?h=o.findIndex(P=>P.id===D.id):D._id!==void 0&&(h=o.findIndex(P=>{var b,_,R,v;return((_=(b=P._id)==null?void 0:b.toString)==null?void 0:_.call(b))===((v=(R=D._id)==null?void 0:R.toString)==null?void 0:v.call(R))})),h!==-1&&(E.docsByLocalValue.has(g)||E.docsByLocalValue.set(g,[]),E.docsByLocalValue.get(g).push(h))}})}}if(yield Promise.all(Array.from(a.entries()).map(u=>f(null,[u],function*([d,i]){const p=l.models[d];if(!p)return;const y=new Set;if(i.localValueSets.forEach(g=>{g.forEach(h=>y.add(h))}),y.size===0)return;const E=[...new Set(i.virtuals.map(g=>g.options.foreignField))],j=Array.from(y);let S;E.length===1?S={[String(E[0])]:{$in:j}}:S={$or:E.map(g=>({[g]:{$in:j}}))};const O=yield p.find(S,s).lean();for(const g of i.virtuals){const{name:h,options:D}=g,P=O.filter(b=>{const _=b[D.foreignField];return _!=null&&y.has(String(_))});if(D.count){const b=new Map;P.forEach(_=>{var R;const v=(R=_[D.foreignField])==null?void 0:R.toString();v&&b.set(v,(b.get(v)||0)+1)}),i.localValueSets.get(h).forEach(_=>{const R=i.docsByLocalValue.get(_)||[],v=b.get(_)||0;R.forEach(T=>{const F=o[T];F[h]===void 0&&(F[h]=v)})})}else{const b=new Map;P.forEach(_=>{var R;const v=(R=_[D.foreignField])==null?void 0:R.toString();v&&(b.has(v)||b.set(v,[]),b.get(v).push(_))}),i.localValueSets.get(h).forEach(_=>{const R=i.docsByLocalValue.get(_)||[],v=b.get(_)||[],T=D.justOne?v[0]||null:v;R.forEach(F=>{const I=o[F];I[h]=T})})}}}))),t){const d=(i=>{const p=Array.isArray(i)?i:[i],y=new Map,E=[];for(const S of p)if(typeof S=="string")if(S.includes(".")){const O=S.split("."),g=O[0]||"",h=O.slice(1).join(".");g&&(y.has(g)||y.set(g,[]),h&&y.get(g).push(h))}else E.push(S);else if(S&&typeof S=="object"){const O=S;if(O.path&&O.path.includes(".")){const g=O.path.split("."),h=g[0]||"",D=g.slice(1).join(".");h&&(y.has(h)||y.set(h,[]),D&&y.get(h).push(D),O.populate&&y.get(h).push(O.populate))}else E.push(S)}const j=[...E];return y.forEach((S,O)=>{const g=[];for(const h of S)(typeof h=="string"||h&&typeof h=="object")&&g.push(h);g.length>0?j.push({path:O,populate:g}):j.push(O)}),j})(t);yield $.applyNestedPopulate(l,o,d,e,n)}return o})}class Y{constructor(r,e){k(this,"collection"),this.collection=r.collection(e)}createOne(r){return f(this,null,function*(){try{const e=V(V({},$.mongo.createGenericFields()),r);return(yield this.collection.insertOne(e)).acknowledged?{success:!0,message:"Document created successfully",result:e}:{success:!1,message:"Document creation failed",code:N.RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE}}catch(e){return m.catchError(e)}})}createMany(r){return f(this,null,function*(){try{const e=r.map(s=>V(V({},$.mongo.createGenericFields()),s)),t=yield this.collection.insertMany(e);return t.insertedCount===0?{success:!1,message:"No documents were inserted",code:N.RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE}:{success:!0,message:`${t.insertedCount} documents created successfully`,result:e}}catch(e){return m.catchError(e)}})}findOne(r){return f(this,null,function*(){try{const e=yield this.collection.findOne(r);return e?{success:!0,message:"Document found",result:e}:{success:!1,message:"Document not found",code:N.RESPONSE_STATUS.NOT_FOUND.CODE}}catch(e){return m.catchError(e)}})}findAll(){return f(this,arguments,function*(r={}){try{return{success:!0,message:"Documents retrieved successfully",result:yield this.collection.find(r).toArray()}}catch(e){return m.catchError(e)}})}count(){return f(this,arguments,function*(r={}){try{const e=yield this.collection.countDocuments(r);return{success:!0,message:`${e} documents counted successfully`,result:e}}catch(e){return m.catchError(e)}})}updateOne(r,e){return f(this,null,function*(){try{const t=yield this.collection.updateOne(r,{$set:e});return t.matchedCount===0?{success:!1,message:"No documents matched the filter",code:N.RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE}:{success:!0,message:"Document updated successfully",result:t}}catch(t){return m.catchError(t)}})}updateMany(r,e){return f(this,null,function*(){try{const t=yield this.collection.updateMany(r,{$set:e});return t.matchedCount===0?{success:!1,message:"No documents matched the filter",code:N.RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE}:{success:!0,message:"Documents updated successfully",result:t}}catch(t){return m.catchError(t)}})}deleteOne(r){return f(this,null,function*(){try{const e=yield this.collection.deleteOne(r);return e.deletedCount===0?{success:!1,message:"No documents matched the filter",code:N.RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE}:{success:!0,message:"Document deleted successfully",result:e}}catch(e){return m.catchError(e)}})}deleteMany(r){return f(this,null,function*(){try{const e=yield this.collection.deleteMany(r);return e.deletedCount===0?{success:!1,message:"No documents matched the filter",code:N.RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE}:{success:!0,message:"Documents deleted successfully",result:e}}catch(e){return m.catchError(e)}})}}class Z{constructor(r){this.model=r}getModelName(){return this.model.modelName}getDynamicVirtuals(){if(this.model._virtualConfigs){const t=this.model._virtualConfigs.filter(s=>{var n;return typeof((n=s.options)==null?void 0:n.ref)=="function"});if(t.length>0)return t}return this.model.schema.statics._dynamicVirtuals}populateDynamicVirtualsForDocument(r,e){return f(this,null,function*(){const t=this.getDynamicVirtuals();if(t&&t.length>0){const s=yield q(this.model.base,[r],t,e,void 0,this.model);return s&&s[0]?s[0]:r}return r})}populateDynamicVirtualsForDocuments(r,e){return f(this,null,function*(){const t=this.getDynamicVirtuals();return t&&t.length>0&&r.length>0?yield q(this.model.base,r,t,e,void 0,this.model):r})}findOne(){return f(this,arguments,function*(r={},e={},t={},s){var n,c;try{const o=M.normalizeMongoFilter(r),a=this.model.findOne(o,e,t),u=this.getDynamicVirtuals(),d=C(s,u);d&&a.populate(d);const i=yield a.exec();if(!i)return{success:!1,message:`No ${this.getModelName()} found.`,code:N.RESPONSE_STATUS.NOT_FOUND.CODE};const p=yield this.populateDynamicVirtualsForDocument(i,s);return{success:!0,result:(c=(n=p==null?void 0:p.toObject)==null?void 0:n.call(p))!=null?c:p}}catch(o){return m.catchError(o)}})}findAll(){return f(this,arguments,function*(r={},e={},t={},s){try{const n=M.normalizeMongoFilter(r),c=this.model.find(n,e,t),o=this.getDynamicVirtuals(),a=C(s,o);a&&c.populate(a);const u=yield c.exec();return{success:!0,result:(yield this.populateDynamicVirtualsForDocuments(u,s)).map(i=>{var p,y;return(y=(p=i==null?void 0:i.toObject)==null?void 0:p.call(i))!=null?y:i})}}catch(n){return m.catchError(n)}})}findPaging(){return f(this,arguments,function*(r={},e={}){try{const t=M.normalizeMongoFilter(r),s=this.getDynamicVirtuals(),n=V({},e);e.populate&&(n.populate=C(e.populate,s));const c=yield this.model.paginate(t,n);if(s&&s.length>0){const o=yield this.populateDynamicVirtualsForDocuments(c.docs,e.populate);return{success:!0,result:A(V({},c),{docs:o.map(a=>{var u,d;return(d=(u=a==null?void 0:a.toObject)==null?void 0:u.call(a))!=null?d:a})})}}return{success:!0,result:A(V({},c),{docs:c.docs.map(o=>{var a,u;return(u=(a=o==null?void 0:o.toObject)==null?void 0:a.call(o))!=null?u:o})})}}catch(t){return m.catchError(t)}})}findPagingAggregate(r){return f(this,arguments,function*(e,t={}){try{const s=this.getDynamicVirtuals(),n=V({},t);t.populate&&(n.populate=C(t.populate,s));const c=yield this.model.aggregatePaginate(this.model.aggregate(e),n),o=yield this.populateDynamicVirtualsForDocuments(c.docs,t.populate);return{success:!0,result:A(V({},c),{docs:o.map(a=>{var u,d;return(d=(u=a==null?void 0:a.toObject)==null?void 0:u.call(a))!=null?d:a})})}}catch(s){return m.catchError(s)}})}count(){return f(this,arguments,function*(r={}){try{const e=M.normalizeMongoFilter(r);return{success:!0,result:yield this.model.countDocuments(e)}}catch(e){return m.catchError(e)}})}createOne(r){return f(this,null,function*(){var e,t;try{const s=yield this.model.create(r);return{success:!0,result:(t=(e=s==null?void 0:s.toObject)==null?void 0:e.call(s))!=null?t:s}}catch(s){return m.catchError(s)}})}createMany(r){return f(this,arguments,function*(e,t={}){try{return{success:!0,result:(yield this.model.insertMany(e,t)).map(n=>{var c,o;return(o=(c=n==null?void 0:n.toObject)==null?void 0:c.call(n))!=null?o:n})}}catch(s){return m.catchError(s)}})}updateOne(){return f(this,arguments,function*(r={},e={},t={}){var s,n;try{const c=M.normalizeMongoFilter(r),o=yield this.model.findOneAndUpdate(c,e,V({new:!0},t)).exec();return o?{success:!0,result:(n=(s=o==null?void 0:o.toObject)==null?void 0:s.call(o))!=null?n:o}:{success:!1,message:`Failed to update ${this.getModelName()}.`,code:N.RESPONSE_STATUS.NOT_FOUND.CODE}}catch(c){return m.catchError(c)}})}updateMany(){return f(this,arguments,function*(r={},e={},t={}){try{const s=M.normalizeMongoFilter(r);return{success:!0,result:yield this.model.updateMany(s,e,t).exec()}}catch(s){return m.catchError(s)}})}deleteOne(){return f(this,arguments,function*(r={},e={}){var t,s;try{const n=M.normalizeMongoFilter(r),c=yield this.model.findOneAndDelete(n,e).exec();return c?{success:!0,result:(s=(t=c==null?void 0:c.toObject)==null?void 0:t.call(c))!=null?s:c}:{success:!1,message:`No ${this.getModelName()} found to delete.`,code:N.RESPONSE_STATUS.NOT_FOUND.CODE}}catch(n){return m.catchError(n)}})}deleteMany(){return f(this,arguments,function*(r={},e={}){try{const t=M.normalizeMongoFilter(r),s=yield this.model.deleteMany(t,e).exec();return s.deletedCount===0?{success:!1,message:"No documents found to delete.",code:N.RESPONSE_STATUS.NOT_FOUND.CODE}:{success:!0,result:s}}catch(t){return m.catchError(t)}})}createShortId(r,e=4){return f(this,null,function*(){try{const s=Array.from({length:10},(o,a)=>x.generateShortId(r,a+e)),c=(yield Promise.all(s.map(o=>this.model.exists({shortId:o})))).findIndex(o=>!o);if(c!==-1){const o=s[c];if(o)return{success:!0,result:o}}return{success:!1,message:"Failed to create a unique shortId",code:N.RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE}}catch(t){return m.catchError(t)}})}createSlugQuery({slug:r,field:e,isObject:t,haveHistory:s=!1,filter:n}){const c=V({},n!=null?n:{});return t?A(V({},c),{$or:[{[`slug.${e}`]:r},...s?[{slugHistory:{$elemMatch:{[`slug.${e}`]:r}}}]:[]]}):A(V({},c),{$or:[{slug:r},...s?[{slugHistory:r}]:[]]})}createUniqueSlug(r){return f(this,arguments,function*({slug:e,field:t,isObject:s,haveHistory:n,filter:c}){if(!e||typeof e!="string")throw new Error("Invalid slug provided: must be a non-empty string");const o=x.generateSlug(e);if(!(yield this.model.exists(this.createSlugQuery({slug:o,field:t,isObject:s,haveHistory:n,filter:c}))))return o;for(let i=1;i<=L.MONGO_SLUG_MAX_ATTEMPTS;i++){const p=`${o}-${i}`;if(!(yield this.model.exists(this.createSlugQuery({slug:p,field:t,isObject:s,haveHistory:n,filter:c}))))return p}const u=Date.now(),d=Math.random().toString(36).substring(2,8);return`${o}-${u}-${d}`})}createSlug(r){return f(this,arguments,function*({field:e,from:t,filter:s,haveHistory:n}){try{const c=t[e];return z(c)?{success:!0,result:Object.fromEntries(yield Promise.all(Object.entries(c).map(d=>f(this,[d],function*([i,p]){const y=yield this.createUniqueSlug({slug:p,field:i,isObject:!0,haveHistory:n,filter:s});return[i,y]}))))}:{success:!0,result:yield this.createUniqueSlug({slug:c,field:e,isObject:!1,haveHistory:n,filter:s})}}catch(c){return m.catchError(c)}})}checkSlug(r){return f(this,arguments,function*({slug:e,field:t,from:s,filter:n,haveHistory:c}){try{const o=s[t];if(z(o)){const p=Object.values(o).map(E=>x.generateSlug(E));return(yield Promise.all(p.map(E=>this.model.exists(this.createSlugQuery({slug:E,field:t,isObject:!0,haveHistory:c,filter:n}))))).some(E=>E)?{success:!0,result:!0}:{success:!0,result:!1}}const u=x.generateSlug(e);return{success:!0,result:(yield this.model.exists(this.createSlugQuery({slug:u,field:t,isObject:!1,filter:n})))!==null}}catch(o){return m.catchError(o)}})}aggregate(r){return f(this,null,function*(){try{return{success:!0,result:yield this.model.aggregate(r)}}catch(e){return m.catchError(e)}})}distinct(r){return f(this,arguments,function*(e,t={},s={}){try{return{success:!0,result:yield this.model.distinct(e,t,s)}}catch(n){return m.catchError(n)}})}}exports.MongoController=Y;exports.MongooseController=Z;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const $=require("./mongo.constant.cjs"),T=require("./mongo.util.cjs"),b=require("../../util/object/object.util.cjs"),M=require("../../util/string/string.util.cjs"),w=require("../../constant/response-status.cjs"),d=require("../log/log.util.cjs");function P(i){return i!=null&&typeof i=="object"}function j(i,r){if(!i||!r||r.length===0)return i;const e=new Set(r.map(t=>t.name));if(Array.isArray(i)){const t=i.filter(s=>{if(typeof s=="string")return!Array.from(e).some(c=>s===c||s.startsWith(`${c}.`));if(typeof s=="object"&&s!==null){const c=s,n=c.path||c.populate||"";return!Array.from(e).some(o=>n===o||n.startsWith(`${o}.`))}return!0});return t.length>0?t:void 0}if(typeof i=="string")return Array.from(e).some(t=>i===t||i.startsWith(`${t}.`))?void 0:i;if(typeof i=="object"&&i!==null){const t=i,s=t.path||t.populate||"";return Array.from(e).some(c=>s===c||s.startsWith(`${c}.`))?void 0:i}return i}function U(i,r,e){if(!i.length||!r||!e?.ref)return[];const t=new Map;return i.forEach(s=>{try{const c=e.ref(s);if(c==null)return;const n=typeof c=="string"?c:String(c);if(n&&n.trim()!==""){const o=T.convertEnumToModelName(n);t.has(o)||t.set(o,[]),t.get(o).push(s)}}catch(c){d.catchError(new Error(`Dynamic ref function failed for virtual "${r}": ${c instanceof Error?c.message:String(c)}`))}}),Array.from(t.entries()).map(([s,c])=>({model:s,docs:c}))}function z(i){return i!==null&&typeof i=="object"&&"toObject"in i&&typeof i.toObject=="function"}async function x(i,r,e,t,s,c){if(!r.length||!e.length||!t)return r;const n=e.filter(u=>{if(Array.isArray(t))return t.length>0&&t.some(a=>{if(typeof a=="string")return a===u.name||a.startsWith(`${u.name}.`);if(a&&typeof a=="object"){const l=a,D=l.path||l.populate||"";return D===u.name||D.startsWith(`${u.name}.`)}return!1});if(typeof t=="string")return t===u.name||t.startsWith(`${u.name}.`);if(typeof t=="object"&&t!==null){const a=t,l=a.path||a.populate||"";return l===u.name||l.startsWith(`${u.name}.`)}return!1});if(n.length===0)return r;const o=b.deepClone(r.map(u=>z(u)?u.toObject():u));o.forEach(u=>{n.forEach(({name:a,options:l})=>{a in u||(u[a]=l.count?0:l.justOne?null:[])})});const g=new Map;for(const u of n){const{name:a,options:l}=u,D=U(o,a,l);for(const m of D){g.has(m.model)||g.set(m.model,{virtuals:[],localValueSets:new Map,docsByLocalValue:new Map});const p=g.get(m.model);p.virtuals.find(E=>E.name===a)||(p.virtuals.push(u),p.localValueSets.set(a,new Set));const N=p.localValueSets.get(a);m.docs.forEach(E=>{const y=E[l.localField];if(y!=null){const f=String(y);N.add(f);let h=-1;const V=E;V.id!==void 0?h=o.findIndex(O=>O.id===V.id):V._id!==void 0&&(h=o.findIndex(O=>O._id?.toString?.()===V._id?.toString?.())),h!==-1&&(p.docsByLocalValue.has(f)||p.docsByLocalValue.set(f,[]),p.docsByLocalValue.get(f).push(h))}})}}if(await Promise.all(Array.from(g.entries()).map(async([u,a])=>{const l=i.models[u];if(!l)return;const D=new Set;if(a.localValueSets.forEach(y=>{y.forEach(f=>D.add(f))}),D.size===0)return;const m=[...new Set(a.virtuals.map(y=>y.options.foreignField))],p=Array.from(D);let N;m.length===1?N={[String(m[0])]:{$in:p}}:N={$or:m.map(y=>({[y]:{$in:p}}))};const E=await l.find(N,s).lean();for(const y of a.virtuals){const{name:f,options:h}=y,V=E.filter(O=>{const S=O[h.foreignField];return S!=null&&D.has(String(S))});if(h.count){const O=new Map;V.forEach(S=>{const R=S[h.foreignField]?.toString();R&&O.set(R,(O.get(R)||0)+1)}),a.localValueSets.get(f).forEach(S=>{const R=a.docsByLocalValue.get(S)||[],_=O.get(S)||0;R.forEach(F=>{const A=o[F];A[f]===void 0&&(A[f]=_)})})}else{const O=new Map;V.forEach(S=>{const R=S[h.foreignField]?.toString();R&&(O.has(R)||O.set(R,[]),O.get(R).push(S))}),a.localValueSets.get(f).forEach(S=>{const R=a.docsByLocalValue.get(S)||[],_=O.get(S)||[],F=h.justOne?_[0]||null:_;R.forEach(A=>{const C=o[A];C[f]=F})})}}})),t){const a=(l=>{const D=Array.isArray(l)?l:[l],m=new Map,p=[];for(const E of D)if(typeof E=="string")if(E.includes(".")){const y=E.split("."),f=y[0]||"",h=y.slice(1).join(".");f&&(m.has(f)||m.set(f,[]),h&&m.get(f).push(h))}else p.push(E);else if(E&&typeof E=="object"){const y=E;if(y.path&&y.path.includes(".")){const f=y.path.split("."),h=f[0]||"",V=f.slice(1).join(".");h&&(m.has(h)||m.set(h,[]),V&&m.get(h).push(V),y.populate&&m.get(h).push(y.populate))}else p.push(E)}const N=[...p];return m.forEach((E,y)=>{const f=[];for(const h of E)(typeof h=="string"||h&&typeof h=="object")&&f.push(h);f.length>0?N.push({path:y,populate:f}):N.push(y)}),N})(t);await T.applyNestedPopulate(i,o,a,e,c)}return o}class v{collection;constructor(r,e){this.collection=r.collection(e)}async createOne(r){try{const e={...T.mongo.createGenericFields(),...r};return(await this.collection.insertOne(e)).acknowledged?{success:!0,message:"Document created successfully",result:e}:{success:!1,message:"Document creation failed",code:w.RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE}}catch(e){return d.catchError(e)}}async createMany(r){try{const e=r.map(s=>({...T.mongo.createGenericFields(),...s})),t=await this.collection.insertMany(e);return t.insertedCount===0?{success:!1,message:"No documents were inserted",code:w.RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE}:{success:!0,message:`${t.insertedCount} documents created successfully`,result:e}}catch(e){return d.catchError(e)}}async findOne(r){try{const e=await this.collection.findOne(r);return e?{success:!0,message:"Document found",result:e}:{success:!1,message:"Document not found",code:w.RESPONSE_STATUS.NOT_FOUND.CODE}}catch(e){return d.catchError(e)}}async findAll(r={}){try{return{success:!0,message:"Documents retrieved successfully",result:await this.collection.find(r).toArray()}}catch(e){return d.catchError(e)}}async count(r={}){try{const e=await this.collection.countDocuments(r);return{success:!0,message:`${e} documents counted successfully`,result:e}}catch(e){return d.catchError(e)}}async updateOne(r,e){try{const t=await this.collection.updateOne(r,{$set:e});return t.matchedCount===0?{success:!1,message:"No documents matched the filter",code:w.RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE}:{success:!0,message:"Document updated successfully",result:t}}catch(t){return d.catchError(t)}}async updateMany(r,e){try{const t=await this.collection.updateMany(r,{$set:e});return t.matchedCount===0?{success:!1,message:"No documents matched the filter",code:w.RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE}:{success:!0,message:"Documents updated successfully",result:t}}catch(t){return d.catchError(t)}}async deleteOne(r){try{const e=await this.collection.deleteOne(r);return e.deletedCount===0?{success:!1,message:"No documents matched the filter",code:w.RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE}:{success:!0,message:"Document deleted successfully",result:e}}catch(e){return d.catchError(e)}}async deleteMany(r){try{const e=await this.collection.deleteMany(r);return e.deletedCount===0?{success:!1,message:"No documents matched the filter",code:w.RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE}:{success:!0,message:"Documents deleted successfully",result:e}}catch(e){return d.catchError(e)}}}class q{constructor(r){this.model=r}getModelName(){return this.model.modelName}getDynamicVirtuals(){if(this.model._virtualConfigs){const t=this.model._virtualConfigs.filter(s=>typeof s.options?.ref=="function");if(t.length>0)return t}return this.model.schema.statics._dynamicVirtuals}async populateDynamicVirtualsForDocument(r,e){const t=this.getDynamicVirtuals();if(t&&t.length>0){const s=await x(this.model.base,[r],t,e,void 0,this.model);return s&&s[0]?s[0]:r}return r}async populateDynamicVirtualsForDocuments(r,e){const t=this.getDynamicVirtuals();return t&&t.length>0&&r.length>0?await x(this.model.base,r,t,e,void 0,this.model):r}async findOne(r={},e={},t={},s){try{const c=b.normalizeMongoFilter(r),n=this.model.findOne(c,e,t),o=this.getDynamicVirtuals(),g=j(s,o);g&&n.populate(g);const u=await n.exec();if(!u)return{success:!1,message:`No ${this.getModelName()} found.`,code:w.RESPONSE_STATUS.NOT_FOUND.CODE};const a=await this.populateDynamicVirtualsForDocument(u,s);return{success:!0,result:a?.toObject?.()??a}}catch(c){return d.catchError(c)}}async findAll(r={},e={},t={},s){try{const c=b.normalizeMongoFilter(r),n=this.model.find(c,e,t),o=this.getDynamicVirtuals(),g=j(s,o);g&&n.populate(g);const u=await n.exec();return{success:!0,result:(await this.populateDynamicVirtualsForDocuments(u,s)).map(l=>l?.toObject?.()??l)}}catch(c){return d.catchError(c)}}async findPaging(r={},e={}){try{const t=b.normalizeMongoFilter(r),s=this.getDynamicVirtuals(),c={...e};e.populate&&(c.populate=j(e.populate,s));const n=await this.model.paginate(t,c);if(s&&s.length>0){const o=await this.populateDynamicVirtualsForDocuments(n.docs,e.populate);return{success:!0,result:{...n,docs:o.map(g=>g?.toObject?.()??g)}}}return{success:!0,result:{...n,docs:n.docs.map(o=>o?.toObject?.()??o)}}}catch(t){return d.catchError(t)}}async findPagingAggregate(r,e={}){try{const t=this.getDynamicVirtuals(),s={...e};e.populate&&(s.populate=j(e.populate,t));const c=await this.model.aggregatePaginate(this.model.aggregate(r),s),n=await this.populateDynamicVirtualsForDocuments(c.docs,e.populate);return{success:!0,result:{...c,docs:n.map(o=>o?.toObject?.()??o)}}}catch(t){return d.catchError(t)}}async count(r={}){try{const e=b.normalizeMongoFilter(r);return{success:!0,result:await this.model.countDocuments(e)}}catch(e){return d.catchError(e)}}async createOne(r){try{const e=await this.model.create(r);return{success:!0,result:e?.toObject?.()??e}}catch(e){return d.catchError(e)}}async createMany(r,e={}){try{return{success:!0,result:(await this.model.insertMany(r,e)).map(s=>s?.toObject?.()??s)}}catch(t){return d.catchError(t)}}async updateOne(r={},e={},t={}){try{const s=b.normalizeMongoFilter(r),c=await this.model.findOneAndUpdate(s,e,{new:!0,...t}).exec();return c?{success:!0,result:c?.toObject?.()??c}:{success:!1,message:`Failed to update ${this.getModelName()}.`,code:w.RESPONSE_STATUS.NOT_FOUND.CODE}}catch(s){return d.catchError(s)}}async updateMany(r={},e={},t={}){try{const s=b.normalizeMongoFilter(r);return{success:!0,result:await this.model.updateMany(s,e,t).exec()}}catch(s){return d.catchError(s)}}async deleteOne(r={},e={}){try{const t=b.normalizeMongoFilter(r),s=await this.model.findOneAndDelete(t,e).exec();return s?{success:!0,result:s?.toObject?.()??s}:{success:!1,message:`No ${this.getModelName()} found to delete.`,code:w.RESPONSE_STATUS.NOT_FOUND.CODE}}catch(t){return d.catchError(t)}}async deleteMany(r={},e={}){try{const t=b.normalizeMongoFilter(r),s=await this.model.deleteMany(t,e).exec();return s.deletedCount===0?{success:!1,message:"No documents found to delete.",code:w.RESPONSE_STATUS.NOT_FOUND.CODE}:{success:!0,result:s}}catch(t){return d.catchError(t)}}async createShortId(r,e=4){try{const s=Array.from({length:10},(o,g)=>M.generateShortId(r,g+e)),n=(await Promise.all(s.map(o=>this.model.exists({shortId:o})))).findIndex(o=>!o);if(n!==-1){const o=s[n];if(o)return{success:!0,result:o}}return{success:!1,message:"Failed to create a unique shortId",code:w.RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE}}catch(t){return d.catchError(t)}}createSlugQuery({slug:r,field:e,isObject:t,haveHistory:s=!1,filter:c}){const n={...c??{}};return t?{...n,$or:[{[`slug.${e}`]:r},...s?[{slugHistory:{$elemMatch:{[`slug.${e}`]:r}}}]:[]]}:{...n,$or:[{slug:r},...s?[{slugHistory:r}]:[]]}}async createUniqueSlug({slug:r,field:e,isObject:t,haveHistory:s,filter:c}){if(!r||typeof r!="string")throw new Error("Invalid slug provided: must be a non-empty string");const n=M.generateSlug(r);if(!await this.model.exists(this.createSlugQuery({slug:n,field:e,isObject:t,haveHistory:s,filter:c})))return n;for(let a=1;a<=$.MONGO_SLUG_MAX_ATTEMPTS;a++){const l=`${n}-${a}`;if(!await this.model.exists(this.createSlugQuery({slug:l,field:e,isObject:t,haveHistory:s,filter:c})))return l}const g=Date.now(),u=M.generateRandomString(6);return`${n}-${g}-${u}`}async createSlug({field:r,from:e,filter:t,haveHistory:s}){try{const c=e[r];return P(c)?{success:!0,result:Object.fromEntries(await Promise.all(Object.entries(c).map(async([u,a])=>{const l=await this.createUniqueSlug({slug:a,field:u,isObject:!0,haveHistory:s,filter:t});return[u,l]})))}:{success:!0,result:await this.createUniqueSlug({slug:c,field:r,isObject:!1,haveHistory:s,filter:t})}}catch(c){return d.catchError(c)}}async checkSlug({slug:r,field:e,from:t,filter:s,haveHistory:c}){try{const n=t[e];if(P(n)){const l=Object.values(n).map(m=>M.generateSlug(m));return(await Promise.all(l.map(m=>this.model.exists(this.createSlugQuery({slug:m,field:e,isObject:!0,haveHistory:c,filter:s}))))).some(m=>m)?{success:!0,result:!0}:{success:!0,result:!1}}const g=M.generateSlug(r);return{success:!0,result:await this.model.exists(this.createSlugQuery({slug:g,field:e,isObject:!1,filter:s}))!==null}}catch(n){return d.catchError(n)}}async aggregate(r){try{return{success:!0,result:await this.model.aggregate(r)}}catch(e){return d.catchError(e)}}async distinct(r,e={},t={}){try{return{success:!0,result:await this.model.distinct(r,e,t)}}catch(s){return d.catchError(s)}}}exports.MongoController=v;exports.MongooseController=q;